<commit_before>var Handlebars = require('handlebars');

var utils = require('./utils');
var chooser = require('./chooser');
var people_chooser = require('./people_chooser');

const time_served_string = "Time served";

const SAVE_TIMEOUT = 2000;

function showSomethingInSidebar(url) {
    $("#sidebar").html("<h2>Loading...</h2>");
    $.get(url,
          null,
           function(data, status, jqXHR) {
            // Remove a href links from data to avoid inadvertent clicks
            var patt = /<a nosidebar href="[^"]+"/gi;

            data = data.replace(patt, "<a ");

            $("#sidebar").html(data);
            $("#sidebar").find("table.sortable").each(function (i) {
                sorttable.makeSortable(this);
                sorttable.innerSortFunction.apply($(this).find("th")[1], []);
            });
            utils.limitHeight('.should-limit');
    });
}

function showPersonHistoryInSidebar(person) {
    showSomethingInSidebar("/personHistory/" + person.id);
}

function showRuleHistoryInSidebar(rule_id) {
    showSomethingInSidebar("/ruleHistory/" + rule_id);
}

function Charge(charge_id, el) {
    var self = this;

    var SEVERITIES = {
        "mild": "Mild",
        "moderate": "Moderate",
        "serious": "Serious",
        "severe": "Severe"
    };

    this.checkReferralLabelHighlight = function() {
        if (el.find(".minor-referral-destination").val()) {
            el.find(".minor-referral-label").addClass("highlight");
        } else {
            el.find(".minor-referral-label").removeClass("highlight");
        }
    };

    this.loadData = function(json, referenced_charge) {
        el.find(".resolution_plan").val(json.resolution_plan);

        if (json.plea == "Guilty") {
            el.find(".plea-guilty").prop("checked", true);
        } else if (json.plea == "No Contest") {
            el.find(".plea-no-contest").prop("checked", true);
        } else if (json.plea == "Not Guilty") {
            el.find(".plea-not-guilty").prop("checked", true);
        } else if (json.plea == "N/A") {
            el.find(".plea-na").prop("checked", true);
        }

        // rule, person
        if (json.person) {
            self.people_chooser.addPerson(
                json.person.person_id,
                utils.displayName(json.person));
        }

        if (json.rule) {
            self.rule_chooser.loadData(json.rule);
        }

        if (json.referred_to_sm) {
            el.find(".refer-to-sm").prop("checked", true);
        }

        for (var key in SEVERITIES) {
            if (json.severity == SEVERITIES[key]) {
                el.find(".severity-" + key).prop("checked", true);
            }
        }

        el.find(".minor-referral-destination").val(json.minor_referral_destination);
        self.checkReferralLabelHighlight();

        if (referenced_charge) {
            self.referenced_charge = referenced_charge;
            if (referenced_charge.has_default_rule) {
                el.addClass("breaking-res-plan");
            }
            el.find(".rp-followups").show();
            el.find(".original-res-plan").show();
            el.find(".original-res-plan-text").html(referenced_charge.resolution_plan);

            if (json.resolution_plan === time_served_string) {
                el.find(".rp-followup-time-served").prop("checked", true);
                el.find(".rp-row").hide();
                el.find(".original-res-plan-text").addClass("deleted");
            } else if (json.resolution_plan) {
                el.find(".rp-followup-new-rp").prop("checked", true);
                el.find(".original-res-plan-text").addClass("deleted");
            } else {
                el.find(".rp-row").hide();
            }
        }

        if (json.is_referenced) {
            el.find(".remove-charge").addClass("disabled");
            el.find(".remove-charge-disabled-text").show();
        }
    };

    this.saveIfNeeded = function() {
        window.setTimeout(self.saveIfNeeded, 5000);
        if (!self.is_modified) {
            return;
        }

        var url = "/saveCharge?id=" + charge_id;

        if (self.people_chooser.people.length > 0) {
            url += "&person_id=" + self.people_chooser.people[0].id;
        }

        var resolution_plan = el.find(".resolution_plan").val();
        if (el.find(".rp-followups").is(":visible") && el.find(".rp-followup-time-served").prop("checked")) {
            resolution_plan = time_served_string;
        }
        url += "&resolution_plan=" + encodeURIComponent(resolution_plan);

        for (var key in SEVERITIES) {
            if (el.find(".severity-" + key).prop("checked")) {
                url += "&severity=" + SEVERITIES[key];
            }
        }

        var plea = el.find(".plea-guilty");
        if (plea.prop("checked")) {
            url += "&plea=Guilty";
        }

        plea = el.find(".plea-no-contest");
        if (plea.prop("checked")) {
            url += "&plea=No Contest";
        }

        plea = el.find(".plea-not-guilty");
        if (plea.prop("checked")) {
            url += "&plea=Not Guilty";
        }

        plea = el.find(".plea-na");
        if (plea.prop("checked")) {
            url += "&plea=N/A";
        }

        var refer = el.find(".refer-to-sm");
        url += "&referred_to_sm=" + refer.prop("checked");

        var minor_referral_destination = el.find(".minor-referral-destination");
        if (minor_referral_destination.length > 0) {
            url += "&minor_referral_destination=" +
                encodeURIComponent(minor_referral_destination.val());
        }

        if (self.rule_chooser.results[0]) {
            url += "&rule_id=" + self.rule_chooser.results[0];
        }

        $.post(url, function(data) {
            self.is_modified = false;
        });
    };

    this.removeCharge = function() {
        if (self.referenced_charge) {
            self.el.parents('.case').find('.case-charge-reference-generate[data-id=' + self.referenced_charge.id + ']')
                .show().parent().find('.case-charge-reference-already-generated').hide();
        }
        $.post("/removeCharge?id=" + charge_id, function() {
            self.el.remove();
            // Don't try to save any remaining modifications if
            // this charge has been removed.
            self.is_modified = false;
            // TODO: remove from Case.charges list
        });
    };

    this.markAsModified = function() {
        self.is_modified = true;
        if ((self.people_chooser.people.length === 0) ||
            !self.rule_chooser.results[0]) {
            el.find(".last-rp").html("");
        }

        if (self.people_chooser.people.length == 1 && self.rule_chooser.results[0]) {
            var url = "/getLastRp";
            url += "/" + self.people_chooser.people[0].id;
            url += "/" + self.rule_chooser.results[0];
            $.get(url, function (data) {
                el.find(".last-rp").html(data);
                el.find(".last-rp .more-info").click(function() {
                    showSomethingInSidebar(
                        '/personRuleHistory' +
                        '/' + self.people_chooser.people[0].id +
                        '/' + self.rule_chooser.results[0]);
                });
            });
        }

        self.checkReferralLabelHighlight();
    };

    this.old_rp = null;
    this.checkText = function() {
        if (el.find(".resolution_plan").val() !== self.old_rp) {
            self.markAsModified();
            self.old_rp = el.find(".resolution_plan").val();
        }
    };

    self.el = el;
    self.is_modified = false;
    window.setTimeout(self.saveIfNeeded, 5000);

    self.remove_button = el.find("button");
    self.remove_button.click(self.removeCharge);

    el.find(".resolution_plan").change(self.markAsModified);
    el.find(".resolution_plan").on(utils.TEXT_AREA_EVENTS, self.checkText);
    el.find(".plea-guilty").change(self.markAsModified);
    el.find(".plea-no-contest").change(self.markAsModified);
    el.find(".plea-na").change(self.markAsModified);
    el.find(".plea-not-guilty").change(self.markAsModified);
    el.find(".plea-not-guilty").change(function() {
            self.el.find(".refer-to-sm").prop("checked", true);
    });
    el.find(".refer-to-sm").change(self.markAsModified);
    el.find(".minor-referral-destination").change(self.markAsModified);
    el.find(".minor-referral-destination").on(utils.TEXT_AREA_EVENTS, self.markAsModified);

    el.find(".rp-followup").change(function() {
        if ($(this).hasClass("rp-followup-new-rp")) {
            el.find(".rp-row").show();
        } else {
            el.find(".rp-row").hide();
            el.find(".resolution_plan").val("");
        }
        el.find(".original-res-plan-text").addClass("deleted");
        self.markAsModified();
    });<commit_msg>Show prior charges before user clicks "time served" or "replace sentence"<commit_after>var Handlebars = require('handlebars');

var utils = require('./utils');
var chooser = require('./chooser');
var people_chooser = require('./people_chooser');

const time_served_string = "Time served";

const SAVE_TIMEOUT = 2000;

function showSomethingInSidebar(url) {
    $("#sidebar").html("<h2>Loading...</h2>");
    $.get(url,
          null,
           function(data, status, jqXHR) {
            // Remove a href links from data to avoid inadvertent clicks
            var patt = /<a nosidebar href="[^"]+"/gi;

            data = data.replace(patt, "<a ");

            $("#sidebar").html(data);
            $("#sidebar").find("table.sortable").each(function (i) {
                sorttable.makeSortable(this);
                sorttable.innerSortFunction.apply($(this).find("th")[1], []);
            });
            utils.limitHeight('.should-limit');
    });
}

function showPersonHistoryInSidebar(person) {
    showSomethingInSidebar("/personHistory/" + person.id);
}

function showRuleHistoryInSidebar(rule_id) {
    showSomethingInSidebar("/ruleHistory/" + rule_id);
}

function Charge(charge_id, el) {
    var self = this;

    var SEVERITIES = {
        "mild": "Mild",
        "moderate": "Moderate",
        "serious": "Serious",
        "severe": "Severe"
    };

    this.checkReferralLabelHighlight = function() {
        if (el.find(".minor-referral-destination").val()) {
            el.find(".minor-referral-label").addClass("highlight");
        } else {
            el.find(".minor-referral-label").removeClass("highlight");
        }
    };

    this.loadData = function(json, referenced_charge) {
        el.find(".resolution_plan").val(json.resolution_plan);

        if (json.plea == "Guilty") {
            el.find(".plea-guilty").prop("checked", true);
        } else if (json.plea == "No Contest") {
            el.find(".plea-no-contest").prop("checked", true);
        } else if (json.plea == "Not Guilty") {
            el.find(".plea-not-guilty").prop("checked", true);
        } else if (json.plea == "N/A") {
            el.find(".plea-na").prop("checked", true);
        }

        // rule, person
        if (json.person) {
            self.people_chooser.addPerson(
                json.person.person_id,
                utils.displayName(json.person));
        }

        if (json.rule) {
            self.rule_chooser.loadData(json.rule);
        }

        if (json.referred_to_sm) {
            el.find(".refer-to-sm").prop("checked", true);
        }

        for (var key in SEVERITIES) {
            if (json.severity == SEVERITIES[key]) {
                el.find(".severity-" + key).prop("checked", true);
            }
        }

        el.find(".minor-referral-destination").val(json.minor_referral_destination);
        self.checkReferralLabelHighlight();

        if (referenced_charge) {
            self.referenced_charge = referenced_charge;
            if (referenced_charge.has_default_rule) {
                el.addClass("breaking-res-plan");
            }
            el.find(".rp-followups").show();
            el.find(".original-res-plan").show();
            el.find(".original-res-plan-text").html(referenced_charge.resolution_plan);

            if (json.resolution_plan === time_served_string) {
                el.find(".rp-followup-time-served").prop("checked", true);
                el.find(".resolution_plan").hide();
                el.find(".original-res-plan-text").addClass("deleted");
            } else if (json.resolution_plan) {
                el.find(".rp-followup-new-rp").prop("checked", true);
                el.find(".original-res-plan-text").addClass("deleted");
            } else {
                el.find(".resolution_plan").hide();
            }
        }

        if (json.is_referenced) {
            el.find(".remove-charge").addClass("disabled");
            el.find(".remove-charge-disabled-text").show();
        }
    };

    this.saveIfNeeded = function() {
        window.setTimeout(self.saveIfNeeded, 5000);
        if (!self.is_modified) {
            return;
        }

        var url = "/saveCharge?id=" + charge_id;

        if (self.people_chooser.people.length > 0) {
            url += "&person_id=" + self.people_chooser.people[0].id;
        }

        var resolution_plan = el.find(".resolution_plan").val();
        if (el.find(".rp-followups").is(":visible") && el.find(".rp-followup-time-served").prop("checked")) {
            resolution_plan = time_served_string;
        }
        url += "&resolution_plan=" + encodeURIComponent(resolution_plan);

        for (var key in SEVERITIES) {
            if (el.find(".severity-" + key).prop("checked")) {
                url += "&severity=" + SEVERITIES[key];
            }
        }

        var plea = el.find(".plea-guilty");
        if (plea.prop("checked")) {
            url += "&plea=Guilty";
        }

        plea = el.find(".plea-no-contest");
        if (plea.prop("checked")) {
            url += "&plea=No Contest";
        }

        plea = el.find(".plea-not-guilty");
        if (plea.prop("checked")) {
            url += "&plea=Not Guilty";
        }

        plea = el.find(".plea-na");
        if (plea.prop("checked")) {
            url += "&plea=N/A";
        }

        var refer = el.find(".refer-to-sm");
        url += "&referred_to_sm=" + refer.prop("checked");

        var minor_referral_destination = el.find(".minor-referral-destination");
        if (minor_referral_destination.length > 0) {
            url += "&minor_referral_destination=" +
                encodeURIComponent(minor_referral_destination.val());
        }

        if (self.rule_chooser.results[0]) {
            url += "&rule_id=" + self.rule_chooser.results[0];
        }

        $.post(url, function(data) {
            self.is_modified = false;
        });
    };

    this.removeCharge = function() {
        if (self.referenced_charge) {
            self.el.parents('.case').find('.case-charge-reference-generate[data-id=' + self.referenced_charge.id + ']')
                .show().parent().find('.case-charge-reference-already-generated').hide();
        }
        $.post("/removeCharge?id=" + charge_id, function() {
            self.el.remove();
            // Don't try to save any remaining modifications if
            // this charge has been removed.
            self.is_modified = false;
            // TODO: remove from Case.charges list
        });
    };

    this.markAsModified = function() {
        self.is_modified = true;
        if ((self.people_chooser.people.length === 0) ||
            !self.rule_chooser.results[0]) {
            el.find(".last-rp").html("");
        }

        if (self.people_chooser.people.length == 1 && self.rule_chooser.results[0]) {
            var url = "/getLastRp";
            url += "/" + self.people_chooser.people[0].id;
            url += "/" + self.rule_chooser.results[0];
            $.get(url, function (data) {
                el.find(".last-rp").html(data);
                el.find(".last-rp .more-info").click(function() {
                    showSomethingInSidebar(
                        '/personRuleHistory' +
                        '/' + self.people_chooser.people[0].id +
                        '/' + self.rule_chooser.results[0]);
                });
            });
        }

        self.checkReferralLabelHighlight();
    };

    this.old_rp = null;
    this.checkText = function() {
        if (el.find(".resolution_plan").val() !== self.old_rp) {
            self.markAsModified();
            self.old_rp = el.find(".resolution_plan").val();
        }
    };

    self.el = el;
    self.is_modified = false;
    window.setTimeout(self.saveIfNeeded, 5000);

    self.remove_button = el.find("button");
    self.remove_button.click(self.removeCharge);

    el.find(".resolution_plan").change(self.markAsModified);
    el.find(".resolution_plan").on(utils.TEXT_AREA_EVENTS, self.checkText);
    el.find(".plea-guilty").change(self.markAsModified);
    el.find(".plea-no-contest").change(self.markAsModified);
    el.find(".plea-na").change(self.markAsModified);
    el.find(".plea-not-guilty").change(self.markAsModified);
    el.find(".plea-not-guilty").change(function() {
            self.el.find(".refer-to-sm").prop("checked", true);
    });
    el.find(".refer-to-sm").change(self.markAsModified);
    el.find(".minor-referral-destination").change(self.markAsModified);
    el.find(".minor-referral-destination").on(utils.TEXT_AREA_EVENTS, self.markAsModified);

    el.find(".rp-followup").change(function() {
        if ($(this).hasClass("rp-followup-new-rp")) {
            el.find(".resolution_plan").show();
        } else {
            el.find(".resolution_plan").hide().val("");
        }
        el.find(".original-res-plan-text").addClass("deleted");
        self.markAsModified();
    });
{'fraction_non_alphanumeric': 0.17198810392233882, 'fraction_numerical': 0.0031423601369171203, 'mean_word_length': 2.0371506475800953, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 11, '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': '3000752', 'n_tokens_mistral': 5575, 'n_tokens_neox': 5348, 'n_words': 731}