<commit_before>﻿function HomeViewModel() {
    var self = this;
    var userId = 0;
    var sessionId = 0;

    self.options = ko.observableArray();
    self.sessions = ko.observableArray();

    // Submit a vote
    self.doVote = function (data, event) {
        $.ajax({
            type: 'PUT',
            url: '/api/user/'+userId+'/vote',
            contentType: 'application/json',
            data: JSON.stringify({
                OptionId: data.Id,
                SessionId: sessionId
            }),

            success: function (returnData) {
                var currentRow = event.currentTarget.parentElement.parentElement;
                $(currentRow).siblings().removeClass("success");
                $(currentRow).addClass("success");

                setTimeout(function () { window.location = "/Result?session=" + sessionId; }, 500);
            }
        });
    }

    self.keyIsEnter = function (key, callback) {
        if (key && key.keyCode == 13)
        {
            callback();
        }
    }

    // Do login
    self.submitLogin = function () {
        $.ajax({
            type: 'PUT',
            url: '/api/user',
            contentType: 'application/json',
            data: JSON.stringify({
                Name: $("#Name").val()
            }),

            success: function (data) {
                userId = data;
                self.highlightPreviousVote();
                $("#login-box").hide();
                $("#vote-table").show();
            }
        });
    }

    self.submitSession = function () {
        sessionId = $("#session-select").val();
        window.location = "?session=" + sessionId;
    }

    self.createSession = function () {
        $.ajax({
            type: 'POST',
            url: '/api/session',
            contentType: 'application/json',

            data: JSON.stringify({
                Name: $("#session-create").val()
            }),

            success: function (data) {
                sessionId = data;
                window.location = "?session=" + sessionId;
            }
        })
    }

    self.highlightPreviousVote = function () {
        $.ajax({
            type: 'GET',
            url: '/api/user/' + userId + '/session/' + sessionId + '/vote',
            contentType: 'application/json',

            success: function (data) {
                for (var index = 0; index < data.length; index++) {
                    var previousOptionId = data[index].OptionId;
                    var previousOption = self.options().filter(function (d) { return d.Id == previousOptionId }).pop();
                    var previousOptionRowIndex = self.options().indexOf(previousOption);
                    var matchingRow = $("#inner-vote-table > tbody > tr").eq(previousOptionRowIndex);
                    matchingRow.addClass("success");
                }
            }
        });
    }

    self.allSessions = function () {
        $.ajax({
            type: 'GET',
            url: '/api/session',

            success: function (data) {
                self.sessions(data);
            }
        })
    }

    self.allOptions = function () {
        // Get all options
        $.ajax({
            type: 'GET',
            url: "/api/option",

            success: function (data) {
                self.options(data);
            }
        });
    }

    self.addOption = function () {
        //Don't submit without an entry in the name field
        if ($("#newName").val() == "") {
            return;
        }

        var newName = $("#newName").val();
        var newDescription = $("#newDescription").val()
        var newInfo = $("#newInfo").val()

        //Reset before posting, to prevent double posts.
        $("#newName").val("")
        $("#newDescription").val("")
        $("#newInfo").val("")

        $.ajax({
            type: 'POST',
            url: '/api/option',
            contentType: 'application/json',

            data: JSON.stringify({
                Name: newName,
                Description: newDescription,
                Info: newInfo
            }),

            success: function () {
                self.allOptions();
            }
        })
    }

    $(document).ready(function () {
        sessionId = getSessionId();

        if (!sessionId) {
            self.allSessions();
            $("#login-box").hide();
            $("#sessions").show();
        }
        else {
            $("#sessions").hide();
            $("#login-box").show();
        }

        self.allOptions();

        $("#Name-submit").click(self.submitLogin);
        //Submit on pressing return key
        $("#Name").keypress(function (event) { self.keyIsEnter(event, self.submitLogin); });

        //Add option on pressing return key
        $("#newOptionRow").keypress(function(event) { self.keyIsEnter(event, self.addOption); });
    });
}<commit_msg>Add return-key hook for new session<commit_after>﻿function HomeViewModel() {
    var self = this;
    var userId = 0;
    var sessionId = 0;

    self.options = ko.observableArray();
    self.sessions = ko.observableArray();

    // Submit a vote
    self.doVote = function (data, event) {
        $.ajax({
            type: 'PUT',
            url: '/api/user/'+userId+'/vote',
            contentType: 'application/json',
            data: JSON.stringify({
                OptionId: data.Id,
                SessionId: sessionId
            }),

            success: function (returnData) {
                var currentRow = event.currentTarget.parentElement.parentElement;
                $(currentRow).siblings().removeClass("success");
                $(currentRow).addClass("success");

                setTimeout(function () { window.location = "/Result?session=" + sessionId; }, 500);
            }
        });
    }

    self.keyIsEnter = function (key, callback) {
        if (key && key.keyCode == 13)
        {
            callback();
        }
    }

    // Do login
    self.submitLogin = function () {
        $.ajax({
            type: 'PUT',
            url: '/api/user',
            contentType: 'application/json',
            data: JSON.stringify({
                Name: $("#Name").val()
            }),

            success: function (data) {
                userId = data;
                self.highlightPreviousVote();
                $("#login-box").hide();
                $("#vote-table").show();
            }
        });
    }

    self.submitSession = function () {
        sessionId = $("#session-select").val();
        window.location = "?session=" + sessionId;
    }

    self.createSession = function () {
        $.ajax({
            type: 'POST',
            url: '/api/session',
            contentType: 'application/json',

            data: JSON.stringify({
                Name: $("#session-create").val()
            }),

            success: function (data) {
                sessionId = data;
                window.location = "?session=" + sessionId;
            }
        })
    }

    self.highlightPreviousVote = function () {
        $.ajax({
            type: 'GET',
            url: '/api/user/' + userId + '/session/' + sessionId + '/vote',
            contentType: 'application/json',

            success: function (data) {
                for (var index = 0; index < data.length; index++) {
                    var previousOptionId = data[index].OptionId;
                    var previousOption = self.options().filter(function (d) { return d.Id == previousOptionId }).pop();
                    var previousOptionRowIndex = self.options().indexOf(previousOption);
                    var matchingRow = $("#inner-vote-table > tbody > tr").eq(previousOptionRowIndex);
                    matchingRow.addClass("success");
                }
            }
        });
    }

    self.allSessions = function () {
        $.ajax({
            type: 'GET',
            url: '/api/session',

            success: function (data) {
                self.sessions(data);
            }
        })
    }

    self.allOptions = function () {
        // Get all options
        $.ajax({
            type: 'GET',
            url: "/api/option",

            success: function (data) {
                self.options(data);
            }
        });
    }

    self.addOption = function () {
        //Don't submit without an entry in the name field
        if ($("#newName").val() == "") {
            return;
        }

        var newName = $("#newName").val();
        var newDescription = $("#newDescription").val()
        var newInfo = $("#newInfo").val()

        //Reset before posting, to prevent double posts.
        $("#newName").val("")
        $("#newDescription").val("")
        $("#newInfo").val("")

        $.ajax({
            type: 'POST',
            url: '/api/option',
            contentType: 'application/json',

            data: JSON.stringify({
                Name: newName,
                Description: newDescription,
                Info: newInfo
            }),

            success: function () {
                self.allOptions();
            }
        })
    }

    $(document).ready(function () {
        sessionId = getSessionId();

        if (!sessionId) {
            self.allSessions();
            $("#login-box").hide();
            $("#sessions").show();
        }
        else {
            $("#sessions").hide();
            $("#login-box").show();
        }

        self.allOptions();

        $("#Name-submit").click(self.submitLogin);
        //Submit on pressing return key
        $("#Name").keypress(function (event) { self.keyIsEnter(event, self.submitLogin); });

        //Add option on pressing return key
        $("#newOptionRow").keypress(function (event) { self.keyIsEnter(event, self.addOption); });

        //Create session on pressing return key
        $("#session-create").keypress(function (event) { self.keyIsEnter(event, self.createSession); });
    });
}
{'fraction_non_alphanumeric': 0.15226792718495424, 'fraction_numerical': 0.0016091722820074424, 'mean_word_length': 1.4632152588555858, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251374', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2521, 'n_words': 532}