<commit_before>/*global $, Backbone, student, submission_id */

var Grade = Backbone.Model.extend({
    urlRoot: base_url + "/api/grade",
    initialize: function () {
        var elem = $("#" + this.get("name"));
        var glyph = $("#" + this.get("name") + "-saved");
        elem.val(this.get("manual_score"));
        elem.attr("placeholder", this.get("auto_score"));

        var that = this;
        $("#" + this.get("name") + "-full-credit").click(function () {
            elem.val(that.get("max_score"));
            elem.trigger("change");
            elem.select();
        });
        $("#" + this.get("name") + "-no-credit").click(function () {
            elem.val(0);
            elem.trigger("change");
            elem.select();
        });
        elem.on("change", function (evt) {
            if (elem.val() === "") {
                that.set("manual_score", null);
            } else {
                that.set("manual_score", Math.min(Math.max(0, elem.val()), that.get("max_score")));
            }

            elem.val(that.get("manual_score"));
            glyph.removeClass("glyphicon-floppy-saved");
            glyph.addClass("glyphicon-refresh");
            glyph.fadeIn(10);

            that.save("manual_score", that.get("manual_score"), {
                success: function () {
                    glyph.removeClass("glyphicon-refresh");
                    glyph.addClass("glyphicon-floppy-saved");
                    setTimeout(function () {
                        glyph.fadeOut();
                    }, 1000);
                    $(document).trigger("finished_saving");
                }
            });
        });
    }
});

var Grades = Backbone.Collection.extend({
    model: Grade,
    url: base_url + "/api/grades"
});

var Comment = Backbone.Model.extend({
    urlRoot: base_url + "/api/comment",
    initialize: function () {
        var elem = $($(".comment")[this.get("name")]);
        var glyph = $($(".comment-saved")[this.get("name")]);
        elem.val(this.get("comment"));

        var that = this;
        elem.on("change", function (evt) {
            that.set("comment", elem.val());

            glyph.removeClass("glyphicon-floppy-saved");
            glyph.addClass("glyphicon-refresh");
            glyph.fadeIn(10);

            that.save("comment", that.get("comment"), {
                success: function () {
                    glyph.removeClass("glyphicon-refresh");
                    glyph.addClass("glyphicon-floppy-saved");
                    setTimeout(function () {
                        glyph.fadeOut();
                    }, 1000);
                    $(document).trigger("finished_saving");
                }
            });
        });
    }
});

var Comments = Backbone.Collection.extend({
    model: Comment,
    url: base_url + "/api/comments"
});

var getIndex = function (elem) {
    var elems = $("input, textarea");
    return elems.index(elem);
};

var selectNext = function (target, shift) {
    var index, elems;
    elems = $("input, textarea");
    if (shift) {
        index = getIndex(target) - 1;
    } else {
        index = getIndex(target) + 1;
    }
    if (index === elems.length) {
        index = 0;
    } else if (index === -1) {
        index = elems.length - 1;
    }
    $(elems[index]).select();
};

var scrollTo = function (elem) {
    var target = elem.closest(".nbgrader_cell");
    return target.offset().top - $(window).height() * 0.33 + 60;
};

var grades;
var comments;
var last_selected;

var nextAssignment = function () {
    window.location = base_url + '/submissions/' + submission_id + '/next' + "#" + getIndex(last_selected);
};

var nextIncorrectAssignment = function () {
    window.location = base_url + '/submissions/' + submission_id + '/next_incorrect' + "#" + getIndex(last_selected);
};

var prevAssignment = function () {
    window.location = base_url + '/submissions/' + submission_id + '/prev' + "#" + getIndex(last_selected);
};

var prevIncorrectAssignment = function () {
    window.location = base_url + '/submissions/' + submission_id + '/prev_incorrect' + "#" + getIndex(last_selected);
};

var save_and_navigate = function(callback) {
    elem = document.activeElement;
    if (elem.tagName === "INPUT" || elem.tagName === "TEXTAREA") {
        $(document).on("finished_saving", callback);
        $(elem).blur();
        $(elem).trigger("change");
    } else {
        callback();
    }
};

$(window).load(function () {
    grades = new Grades();
    grades.fetch({
        data: {
            submission_id: submission_id
        }
    });

    comments = new Comments();
    comments.fetch({
        data: {
            submission_id: submission_id
        }
    });

    $("li.previous a").tooltip({container: 'body'});
    $("li.next a").tooltip({container: 'body'});
    $("li.live-notebook a").tooltip({container: 'body'});

    // disable link selection on tabs
    $('a').attr('tabindex', '-1');

    $("input, textarea").on('keydown', function(e) {
        var keyCode = e.keyCode || e.which;

        if (keyCode === 9) { // tab
            e.preventDefault();
            e.stopPropagation();
            selectNext(e.currentTarget, e.shiftKey);

        } else if (keyCode === 27) { // escape
            $(e.currentTarget).blur();
        }
    });

    $("body").on('keydown', function(e) {
        var keyCode = e.keyCode || e.which;
        var href;
        var elem;

        if (keyCode === 9) { // tab
            e.preventDefault();
            selectNext(last_selected, e.shiftKey);
        } else if (keyCode === 13) { // enter
            last_selected.select();
        } else if (keyCode == 39 && e.shiftKey && e.ctrlKey) { // shift + control + right arrow
            save_and_navigate(nextIncorrectAssignment);
        } else if (keyCode == 37 && e.shiftKey && e.ctrlKey) { // shift + control + left arrow
            save_and_navigate(prevIncorrectAssignment);
        } else if (keyCode == 39 && e.shiftKey) { // shift + right arrow
            save_and_navigate(nextAssignment);
        } else if (keyCode == 37 && e.shiftKey) { // shift + left arrow
            save_and_navigate(prevAssignment);
        }
    });

    var index = parseInt(document.URL.split('#')[1]) || 0;
    last_selected = $($("input, textarea")[index]);<commit_msg>Allow multiline comments, fixes #90<commit_after>/*global $, Backbone, student, submission_id */

var Grade = Backbone.Model.extend({
    urlRoot: base_url + "/api/grade",
    initialize: function () {
        var elem = $("#" + this.get("name"));
        var glyph = $("#" + this.get("name") + "-saved");
        elem.val(this.get("manual_score"));
        elem.attr("placeholder", this.get("auto_score"));

        var that = this;
        $("#" + this.get("name") + "-full-credit").click(function () {
            elem.val(that.get("max_score"));
            elem.trigger("change");
            elem.select();
        });
        $("#" + this.get("name") + "-no-credit").click(function () {
            elem.val(0);
            elem.trigger("change");
            elem.select();
        });
        elem.on("change", function (evt) {
            if (elem.val() === "") {
                that.set("manual_score", null);
            } else {
                that.set("manual_score", Math.min(Math.max(0, elem.val()), that.get("max_score")));
            }

            elem.val(that.get("manual_score"));
            glyph.removeClass("glyphicon-floppy-saved");
            glyph.addClass("glyphicon-refresh");
            glyph.fadeIn(10);

            that.save("manual_score", that.get("manual_score"), {
                success: function () {
                    glyph.removeClass("glyphicon-refresh");
                    glyph.addClass("glyphicon-floppy-saved");
                    setTimeout(function () {
                        glyph.fadeOut();
                    }, 1000);
                    $(document).trigger("finished_saving");
                }
            });
        });
    }
});

var Grades = Backbone.Collection.extend({
    model: Grade,
    url: base_url + "/api/grades"
});

var Comment = Backbone.Model.extend({
    urlRoot: base_url + "/api/comment",
    initialize: function () {
        var elem = $($(".comment")[this.get("name")]);
        var glyph = $($(".comment-saved")[this.get("name")]);
        elem.val(this.get("comment"));

        var that = this;
        elem.on("change", function (evt) {
            that.set("comment", elem.val());

            glyph.removeClass("glyphicon-floppy-saved");
            glyph.addClass("glyphicon-refresh");
            glyph.fadeIn(10);

            that.save("comment", that.get("comment"), {
                success: function () {
                    glyph.removeClass("glyphicon-refresh");
                    glyph.addClass("glyphicon-floppy-saved");
                    setTimeout(function () {
                        glyph.fadeOut();
                    }, 1000);
                    $(document).trigger("finished_saving");
                }
            });
        });
    }
});

var Comments = Backbone.Collection.extend({
    model: Comment,
    url: base_url + "/api/comments"
});

var getIndex = function (elem) {
    var elems = $("input, textarea");
    return elems.index(elem);
};

var selectNext = function (target, shift) {
    var index, elems;
    elems = $("input, textarea");
    if (shift) {
        index = getIndex(target) - 1;
    } else {
        index = getIndex(target) + 1;
    }
    if (index === elems.length) {
        index = 0;
    } else if (index === -1) {
        index = elems.length - 1;
    }
    $(elems[index]).select();
};

var scrollTo = function (elem) {
    var target = elem.closest(".nbgrader_cell");
    return target.offset().top - $(window).height() * 0.33 + 60;
};

var grades;
var comments;
var last_selected;

var nextAssignment = function () {
    window.location = base_url + '/submissions/' + submission_id + '/next' + "#" + getIndex(last_selected);
};

var nextIncorrectAssignment = function () {
    window.location = base_url + '/submissions/' + submission_id + '/next_incorrect' + "#" + getIndex(last_selected);
};

var prevAssignment = function () {
    window.location = base_url + '/submissions/' + submission_id + '/prev' + "#" + getIndex(last_selected);
};

var prevIncorrectAssignment = function () {
    window.location = base_url + '/submissions/' + submission_id + '/prev_incorrect' + "#" + getIndex(last_selected);
};

var save_and_navigate = function(callback) {
    elem = document.activeElement;
    if (elem.tagName === "INPUT" || elem.tagName === "TEXTAREA") {
        $(document).on("finished_saving", callback);
        $(elem).blur();
        $(elem).trigger("change");
    } else {
        callback();
    }
};

$(window).load(function () {
    grades = new Grades();
    grades.fetch({
        data: {
            submission_id: submission_id
        }
    });

    comments = new Comments();
    comments.fetch({
        data: {
            submission_id: submission_id
        }
    });

    $("li.previous a").tooltip({container: 'body'});
    $("li.next a").tooltip({container: 'body'});
    $("li.live-notebook a").tooltip({container: 'body'});

    // disable link selection on tabs
    $('a').attr('tabindex', '-1');

    $("input, textarea").on('keydown', function(e) {
        var keyCode = e.keyCode || e.which;

        if (keyCode === 9) { // tab
            e.preventDefault();
            e.stopPropagation();
            selectNext(e.currentTarget, e.shiftKey);

        } else if (keyCode === 27) { // escape
            $(e.currentTarget).blur();
        }
    });

    $("body").on('keydown', function(e) {
        var keyCode = e.keyCode || e.which;
        var href;
        var elem;

        if (keyCode === 9) { // tab
            e.preventDefault();
            selectNext(last_selected, e.shiftKey);
        } else if (keyCode === 13) { // enter
            if (last_selected[0] !== document.activeElement) {
                last_selected.select();
            }
        } else if (keyCode == 39 && e.shiftKey && e.ctrlKey) { // shift + control + right arrow
            save_and_navigate(nextIncorrectAssignment);
        } else if (keyCode == 37 && e.shiftKey && e.ctrlKey) { // shift + control + left arrow
            save_and_navigate(prevIncorrectAssignment);
        } else if (keyCode == 39 && e.shiftKey) { // shift + right arrow
            save_and_navigate(nextAssignment);
        } else if (keyCode == 37 && e.shiftKey) { // shift + left arrow
            save_and_navigate(prevAssignment);
        }
    });

    var index = parseInt(document.URL.split('#')[1]) || 0;
    last_selected = $($("input, textarea")[index]);
{'fraction_non_alphanumeric': 0.17254101577831854, 'fraction_numerical': 0.006672423267132428, 'mean_word_length': 2.0551558752997603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4921737', 'n_tokens_mistral': 3872, 'n_tokens_neox': 3689, 'n_words': 702}