1 /*global gettext, interpolate, ngettext*/
6 $.fn.actions = function(opts) {
7 var options = $.extend({}, $.fn.actions.defaults, opts);
8 var actionCheckboxes = $(this);
9 var list_editable_changed = false;
10 var showQuestion = function() {
11 $(options.acrossClears).hide();
12 $(options.acrossQuestions).show();
13 $(options.allContainer).hide();
15 showClear = function() {
16 $(options.acrossClears).show();
17 $(options.acrossQuestions).hide();
18 $(options.actionContainer).toggleClass(options.selectedClass);
19 $(options.allContainer).show();
20 $(options.counterContainer).hide();
23 $(options.acrossClears).hide();
24 $(options.acrossQuestions).hide();
25 $(options.allContainer).hide();
26 $(options.counterContainer).show();
28 clearAcross = function() {
30 $(options.acrossInput).val(0);
31 $(options.actionContainer).removeClass(options.selectedClass);
33 checker = function(checked) {
39 $(actionCheckboxes).prop("checked", checked)
40 .parent().parent().toggleClass(options.selectedClass, checked);
42 updateCounter = function() {
43 var sel = $(actionCheckboxes).filter(":checked").length;
44 // data-actions-icnt is defined in the generated HTML
45 // and contains the total amount of objects in the queryset
46 var actions_icnt = $('.action-counter').data('actionsIcnt');
47 $(options.counterContainer).html(interpolate(
48 ngettext('%(sel)s of %(cnt)s selected', '%(sel)s of %(cnt)s selected', sel), {
52 $(options.allToggle).prop("checked", function() {
54 if (sel === actionCheckboxes.length) {
64 // Show counter by default
65 $(options.counterContainer).show();
66 // Check state of checkboxes and reinit state if needed
67 $(this).filter(":checked").each(function(i) {
68 $(this).parent().parent().toggleClass(options.selectedClass);
70 if ($(options.acrossInput).val() === 1) {
74 $(options.allToggle).show().click(function() {
75 checker($(this).prop("checked"));
78 $("a", options.acrossQuestions).click(function(event) {
79 event.preventDefault();
80 $(options.acrossInput).val(1);
83 $("a", options.acrossClears).click(function(event) {
84 event.preventDefault();
85 $(options.allToggle).prop("checked", false);
91 $(actionCheckboxes).click(function(event) {
92 if (!event) { event = window.event; }
93 var target = event.target ? event.target : event.srcElement;
94 if (lastChecked && $.data(lastChecked) !== $.data(target) && event.shiftKey === true) {
96 $(lastChecked).prop("checked", target.checked)
97 .parent().parent().toggleClass(options.selectedClass, target.checked);
98 $(actionCheckboxes).each(function() {
99 if ($.data(this) === $.data(lastChecked) || $.data(this) === $.data(target)) {
100 inrange = (inrange) ? false : true;
103 $(this).prop("checked", target.checked)
104 .parent().parent().toggleClass(options.selectedClass, target.checked);
108 $(target).parent().parent().toggleClass(options.selectedClass, target.checked);
109 lastChecked = target;
112 $('form#changelist-form table#result_list tr').find('td:gt(0) :input').change(function() {
113 list_editable_changed = true;
115 $('form#changelist-form button[name="index"]').click(function(event) {
116 if (list_editable_changed) {
117 return confirm(gettext("You have unsaved changes on individual editable fields. If you run an action, your unsaved changes will be lost."));
120 $('form#changelist-form input[name="_save"]').click(function(event) {
121 var action_changed = false;
122 $('select option:selected', options.actionContainer).each(function() {
124 action_changed = true;
127 if (action_changed) {
128 if (list_editable_changed) {
129 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."));
131 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."));
136 /* Setup plugin defaults */
137 $.fn.actions.defaults = {
138 actionContainer: "div.actions",
139 counterContainer: "span.action-counter",
140 allContainer: "div.actions span.all",
141 acrossInput: "div.actions input.select-across",
142 acrossQuestions: "div.actions span.question",
143 acrossClears: "div.actions span.clear",
144 allToggle: "#action-toggle",
145 selectedClass: "selected"
147 $(document).ready(function() {
148 var $actionsEls = $('tr input.action-select');
149 if ($actionsEls.length > 0) {
150 $actionsEls.actions();