actions.js 6.85 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8 9 10
/*global gettext, interpolate, ngettext*/
(function($) {
    'use strict';
    var lastChecked;

    $.fn.actions = function(opts) {
        var options = $.extend({}, $.fn.actions.defaults, opts);
        var actionCheckboxes = $(this);
        var list_editable_changed = false;
        var showQuestion = function() {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
                $(options.acrossClears).hide();
                $(options.acrossQuestions).show();
                $(options.allContainer).hide();
            },
            showClear = function() {
                $(options.acrossClears).show();
                $(options.acrossQuestions).hide();
                $(options.actionContainer).toggleClass(options.selectedClass);
                $(options.allContainer).show();
                $(options.counterContainer).hide();
            },
            reset = function() {
                $(options.acrossClears).hide();
                $(options.acrossQuestions).hide();
                $(options.allContainer).hide();
                $(options.counterContainer).show();
            },
            clearAcross = function() {
Administrator committed
29
                reset();
30 31 32 33 34
                $(options.acrossInput).val(0);
                $(options.actionContainer).removeClass(options.selectedClass);
            },
            checker = function(checked) {
                if (checked) {
Administrator committed
35 36
                    showQuestion();
                } else {
37
                    reset();
Administrator committed
38
                }
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
                $(actionCheckboxes).prop("checked", checked)
                    .parent()
                    .parent()
                    .toggleClass(options.selectedClass, checked);
            },
            updateCounter = function() {
                var sel = $(actionCheckboxes).filter(":checked").length;
                // data-actions-icnt is defined in the generated HTML
                // and contains the total amount of objects in the queryset
                var actions_icnt = $('.action-counter').data('actionsIcnt');

                $(options.counterContainer).html(interpolate(ngettext('%(sel)s of %(cnt)s selected', '%(sel)s of %(cnt)s selected', sel), {
                    sel: sel,
                    cnt: actions_icnt
                }, true));
                $(options.allToggle).prop("checked", function() {
                    var value;

                    if (sel === actionCheckboxes.length) {
                        value = true;
                        showQuestion();
                    } else {
                        value = false;
                        clearAcross();
                    }

                    return value;
                });
            };
Administrator committed
68
        // Show counter by default
69

Administrator committed
70 71
        $(options.counterContainer).show();
        // Check state of checkboxes and reinit state if needed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        $(this).filter(":checked")
            .each(function(i) {
                $(this).parent()
                    .parent()
                    .toggleClass(options.selectedClass);
                updateCounter();
                if ($(options.acrossInput).val() === 1) {
                    showClear();
                }
            });
        $(options.allToggle).show()
            .click(function() {
                checker($(this).prop("checked"));
                updateCounter();
            });
Administrator committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100
        $("a", options.acrossQuestions).click(function(event) {
            event.preventDefault();
            $(options.acrossInput).val(1);
            showClear();
        });
        $("a", options.acrossClears).click(function(event) {
            event.preventDefault();
            $(options.allToggle).prop("checked", false);
            clearAcross();
            checker(0);
            updateCounter();
        });
        lastChecked = null;
        $(actionCheckboxes).click(function(event) {
101 102 103
            if (!event) {
                event = window.event;
            }
Administrator committed
104
            var target = event.target ? event.target : event.srcElement;
105

Administrator committed
106 107
            if (lastChecked && $.data(lastChecked) !== $.data(target) && event.shiftKey === true) {
                var inrange = false;
108

Administrator committed
109
                $(lastChecked).prop("checked", target.checked)
110 111 112
                    .parent()
                    .parent()
                    .toggleClass(options.selectedClass, target.checked);
Administrator committed
113 114 115 116 117 118
                $(actionCheckboxes).each(function() {
                    if ($.data(this) === $.data(lastChecked) || $.data(this) === $.data(target)) {
                        inrange = (inrange) ? false : true;
                    }
                    if (inrange) {
                        $(this).prop("checked", target.checked)
119 120 121
                            .parent()
                            .parent()
                            .toggleClass(options.selectedClass, target.checked);
Administrator committed
122 123 124
                    }
                });
            }
125 126 127
            $(target).parent()
                .parent()
                .toggleClass(options.selectedClass, target.checked);
Administrator committed
128 129 130 131 132 133 134 135 136 137 138 139 140
            lastChecked = target;
            updateCounter();
        });
        $('form#changelist-form table#result_list tr').on('change', 'td:gt(0) :input', function() {
            list_editable_changed = true;
        });
        $('form#changelist-form button[name="index"]').click(function(event) {
            if (list_editable_changed) {
                return confirm(gettext("You have unsaved changes on individual editable fields. If you run an action, your unsaved changes will be lost."));
            }
        });
        $('form#changelist-form input[name="_save"]').click(function(event) {
            var action_changed = false;
141

Administrator committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
            $('select option:selected', options.actionContainer).each(function() {
                if ($(this).val()) {
                    action_changed = true;
                }
            });
            if (action_changed) {
                if (list_editable_changed) {
                    return confirm(gettext("You have selected an action, but you haven't saved your changes to individual fields yet. Please click OK to save. You'll need to re-run the action."));
                } else {
                    return confirm(gettext("You have selected an action, and you haven't made any changes on individual fields. You're probably looking for the Go button rather than the Save button."));
                }
            }
        });
    };
    /* Setup plugin defaults */
    $.fn.actions.defaults = {
        actionContainer: "div.actions",
        counterContainer: "span.action-counter",
        allContainer: "div.actions span.all",
        acrossInput: "div.actions input.select-across",
        acrossQuestions: "div.actions span.question",
        acrossClears: "div.actions span.clear",
        allToggle: "#action-toggle",
        selectedClass: "selected"
    };
    $(document).ready(function() {
        var $actionsEls = $('tr input.action-select');
169

Administrator committed
170 171 172 173 174
        if ($actionsEls.length > 0) {
            $actionsEls.actions();
        }
    });
})(django.jQuery);