<commit_before>var getContributors = function () {
    var contributors = Session.get("contributors");
    if (!contributors)
        return [];

    return contributors;
};
Template.rewardBountyView.contributors = getContributors;

/**
 * The bounty minus the fee
 * @returns {number}
 */
var getTotalBounty = function () {
    var openBounties = Session.get("openBounties");
    if (!openBounties)
        return 0;

    var totalBounty = _.reduce(openBounties, function (memo, bounty) {
        return memo + bounty.amount;
    }, 0);

    var minusFee = totalBounty - CB.Payout.Fee(totalBounty);

    return CB.Tools.Truncate(minusFee, 2);
};
Template.rewardBountyView.totalBounty = getTotalBounty;

Template.rewardBountyView.minimum = CB.Payout.Minimum;

//TODO if the total bounty can pay each person the minimum, check everyone
//otherwise check no one

Template.rewardBountyView.rendered = function () {
    var contributors = getContributors();
    var total = getTotalBounty();
    var minimum = CB.Payout.Minimum();
    var numberContributors = contributors.length;

    //TODO Also check for invalid inputs.
    //Sets a hard limit on max/min values for amount.
    var parseAmount = function (amount) {
        if (!amount) return 0;
        if (amount > total)
            amount = total;
        else if (amount < minimum)
            amount = minimum;
        return amount;
    };

    var setAmount = function (row, amount) {
        amount = parseAmount(amount);
        //Move the current amount to previousAmount
        row.data("previousAmount", row.data("currentAmount"));
        //Set the currentAmount.
        row.data("currentAmount", amount);//CB.Tools.Truncate(amount, 2));

        var percent = (amount / total) * 100;
        row.find(".rewardSlider").slider("value", CB.Tools.Truncate(percent, 2));
        row.find(".rewardInput").val(CB.Tools.Truncate(amount, 2));
        row.find(".rewardPercent").val(CB.Tools.Truncate(percent, 2));
    };

    var updateOtherSliders = function (rowToExclude) {
        var currentAmount = rowToExclude.data("currentAmount");
        var previousAmount = rowToExclude.data("previousAmount");

        //Update all rows aside from the one being modified directly.
        $(".contributorRow").not(rowToExclude).each(function (index, row) {
            row = $(row);

            var changedAmount;
            if (previousAmount !== total) {
                var delta = (total - currentAmount) / (total - previousAmount);
                changedAmount = delta * row.data("currentAmount");
            } else {
                //When max reached.
                changedAmount = (total - currentAmount) / (numberContributors - 1)
            }

            setAmount(row, changedAmount);
        });
    };

    var getEqualSplit = function () {
        var usersEnabled = $(".contributorRow.enabled").length;
        var equalSplit = 0;
        if (usersEnabled !== 0) {
            equalSplit = total / usersEnabled;
        }
        return equalSplit;
    };

    var toggleRow = function (row) {
        row.toggleClass("enabled");
        var disabled = !row.hasClass("enabled");
        row.find(".rewardSlider").slider("option", "disabled", disabled);
        row.find(".rewardInput").prop("disabled", disabled);
        row.find(".rewardPercent").prop("disabled", disabled);
        updateContributors();
    };

    var updateContributors = function () {
        $(".contributorRow").each(function (index, row) {
            row = $(row);
            var amount = 0;
            if (row.hasClass("enabled")) {
                amount = getEqualSplit()
            }
            setAmount(row, amount);
        });
        updateCheckboxes();
    };

    var updateCheckboxes = function () {
        var usersEnabled = $(".contributorRow.enabled").length;
        console.log(usersEnabled);
        console.log(minimum*(usersEnabled+1));
        console.log(total);
        if((minimum*(usersEnabled+1))>total){
            $(".shouldPay").not(":checked").prop("disabled", true);
        }else{
            $(".shouldPay").prop("disabled", false);
        }
    };

    $(".contributorRow").each(function (index, row) {
        row = $(row);
        //Slider slide listener
        //Note: Chrome bug steals drag event from slider.
        row.find(".shouldPay").click(function (event) {
            var checkBox = $(event.toElement);
            var row = checkBox.parent(".contributorRow");
            toggleRow(row);
        });

        row.find(".rewardSlider").slider({
            animate: "fast",
            min: 0,
            max: 100,
            disabled: true,
            slide: function (event, ui) {<commit_msg>Fixed a few bugs with disabled rows and minimum values.<commit_after>var getContributors = function () {
    var contributors = Session.get("contributors");
    if (!contributors)
        return [];

    return contributors;
};
Template.rewardBountyView.contributors = getContributors;

/**
 * The bounty minus the fee
 * @returns {number}
 */
var getTotalBounty = function () {
    var openBounties = Session.get("openBounties");
    if (!openBounties)
        return 0;

    var totalBounty = _.reduce(openBounties, function (memo, bounty) {
        return memo + bounty.amount;
    }, 0);

    var minusFee = totalBounty - CB.Payout.Fee(totalBounty);

    return CB.Tools.Truncate(minusFee, 2);
};
Template.rewardBountyView.totalBounty = getTotalBounty;

Template.rewardBountyView.minimum = CB.Payout.Minimum;

//TODO if the total bounty can pay each person the minimum, check everyone
//otherwise check no one

Template.rewardBountyView.rendered = function () {
    var contributors = getContributors();
    var total = getTotalBounty();
    var minimum = CB.Payout.Minimum();
    var numberContributors = contributors.length;

    var setAmount = function (row, amount) {
        //Move the current amount to previousAmount
        row.data("previousAmount", row.data("currentAmount"));
        //Set the currentAmount.
        row.data("currentAmount", amount);//CB.Tools.Truncate(amount, 2));

        var percent = (amount / total) * 100;
        row.find(".rewardSlider").slider("value", CB.Tools.Truncate(percent, 2));
        row.find(".rewardInput").val(CB.Tools.Truncate(amount, 2));
        row.find(".rewardPercent").val(CB.Tools.Truncate(percent, 2));
    };

    var updateOtherSliders = function (rowToExclude) {
        var currentAmount = rowToExclude.data("currentAmount");
        var previousAmount = rowToExclude.data("previousAmount");
        var enabledContributors = $(".contributorRow.enabled").length;

        //Update all rows aside from the one being modified directly.
        $(".contributorRow.enabled").not(rowToExclude).each(function (index, row) {
            row = $(row);

            var changedAmount;
            if (previousAmount !== total) {
                var delta = (total - currentAmount) / (total - previousAmount);
                changedAmount = delta * row.data("currentAmount");
            } else {
                //When max reached.
                changedAmount = (total - currentAmount) / (enabledContributors - 1)
            }

            setAmount(row, changedAmount);
        });
    };

    var getEqualSplit = function () {
        var usersEnabled = $(".contributorRow.enabled").length;
        var equalSplit = 0;
        if (usersEnabled !== 0) {
            equalSplit = total / usersEnabled;
        }
        return equalSplit;
    };

    var toggleRow = function (row) {
        row.toggleClass("enabled");
        var disabled = !row.hasClass("enabled");
        row.find(".rewardSlider").slider("option", "disabled", disabled);
        row.find(".rewardInput").prop("disabled", disabled);
        row.find(".rewardPercent").prop("disabled", disabled);

        //Redistribute contributor amounts
        $(".contributorRow").each(function (index, row) {
            row = $(row);
            var amount = 0;
            if (row.hasClass("enabled")) {
                amount = getEqualSplit()
            }
            setAmount(row, amount);
        });

        //Update checkboxes
        var usersEnabled = $(".contributorRow.enabled").length;
        if((minimum*(usersEnabled+1))>total){
            $(".shouldPay").not(":checked").prop("disabled", true);
        }else{
            $(".shouldPay").prop("disabled", false);
        }
    };

    $(".contributorRow").each(function (index, row) {
        row = $(row);
        //Slider slide listener
        //Note: Chrome bug steals drag event from slider.
        row.find(".shouldPay").click(function (event) {
            var checkBox = $(event.toElement);
            var row = checkBox.parent(".contributorRow");
            toggleRow(row);
        });

        row.find(".rewardSlider").slider({
            animate: "fast",
            min: 0,
            max: 100,
            disabled: true,
            slide: function (event, ui) {
{'fraction_non_alphanumeric': 0.1346539060761184, 'fraction_numerical': 0.00445136879590474, 'mean_word_length': 2.321138211382114, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 6, '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': '1866220', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2416, 'n_words': 578}