1 var Actions = { |
1 (function($) { |
2 init: function() { |
2 $.fn.actions = function(opts) { |
3 var selectAll = document.getElementById('action-toggle'); |
3 var options = $.extend({}, $.fn.actions.defaults, opts); |
4 if (selectAll) { |
4 var actionCheckboxes = $(this); |
5 selectAll.style.display = 'inline'; |
5 var list_editable_changed = false; |
6 addEvent(selectAll, 'click', function() { |
6 checker = function(checked) { |
7 Actions.checker(selectAll.checked); |
7 if (checked) { |
8 }); |
8 showQuestion(); |
9 } |
9 } else { |
10 var changelistTable = document.getElementsBySelector('#changelist table')[0]; |
10 reset(); |
11 if (changelistTable) { |
11 } |
12 addEvent(changelistTable, 'click', function(e) { |
12 $(actionCheckboxes).attr("checked", checked) |
13 if (!e) { var e = window.event; } |
13 .parent().parent().toggleClass(options.selectedClass, checked); |
14 var target = e.target ? e.target : e.srcElement; |
14 } |
15 if (target.nodeType == 3) { target = target.parentNode; } |
15 updateCounter = function() { |
16 if (target.className == 'action-select') { |
16 var sel = $(actionCheckboxes).filter(":checked").length; |
17 var tr = target.parentNode.parentNode; |
17 $(options.counterContainer).html(interpolate( |
18 Actions.toggleRow(tr, target.checked); |
18 ngettext('%(sel)s of %(cnt)s selected', '%(sel)s of %(cnt)s selected', sel), { |
19 } |
19 sel: sel, |
20 }); |
20 cnt: _actions_icnt |
21 } |
21 }, true)); |
22 }, |
22 $(options.allToggle).attr("checked", function() { |
23 toggleRow: function(tr, checked) { |
23 if (sel == actionCheckboxes.length) { |
24 if (checked && tr.className.indexOf('selected') == -1) { |
24 value = true; |
25 tr.className += ' selected'; |
25 showQuestion(); |
26 } else if (!checked) { |
26 } else { |
27 tr.className = tr.className.replace(' selected', ''); |
27 value = false; |
28 } |
28 clearAcross(); |
29 }, |
29 } |
30 checker: function(checked) { |
30 return value; |
31 var actionCheckboxes = document.getElementsBySelector('tr input.action-select'); |
31 }); |
32 for(var i = 0; i < actionCheckboxes.length; i++) { |
32 } |
33 actionCheckboxes[i].checked = checked; |
33 showQuestion = function() { |
34 Actions.toggleRow(actionCheckboxes[i].parentNode.parentNode, checked); |
34 $(options.acrossClears).hide(); |
35 } |
35 $(options.acrossQuestions).show(); |
36 } |
36 $(options.allContainer).hide(); |
37 }; |
37 } |
38 |
38 showClear = function() { |
39 addEvent(window, 'load', Actions.init); |
39 $(options.acrossClears).show(); |
|
40 $(options.acrossQuestions).hide(); |
|
41 $(options.actionContainer).toggleClass(options.selectedClass); |
|
42 $(options.allContainer).show(); |
|
43 $(options.counterContainer).hide(); |
|
44 } |
|
45 reset = function() { |
|
46 $(options.acrossClears).hide(); |
|
47 $(options.acrossQuestions).hide(); |
|
48 $(options.allContainer).hide(); |
|
49 $(options.counterContainer).show(); |
|
50 } |
|
51 clearAcross = function() { |
|
52 reset(); |
|
53 $(options.acrossInput).val(0); |
|
54 $(options.actionContainer).removeClass(options.selectedClass); |
|
55 } |
|
56 // Show counter by default |
|
57 $(options.counterContainer).show(); |
|
58 // Check state of checkboxes and reinit state if needed |
|
59 $(this).filter(":checked").each(function(i) { |
|
60 $(this).parent().parent().toggleClass(options.selectedClass); |
|
61 updateCounter(); |
|
62 if ($(options.acrossInput).val() == 1) { |
|
63 showClear(); |
|
64 } |
|
65 }); |
|
66 $(options.allToggle).show().click(function() { |
|
67 checker($(this).attr("checked")); |
|
68 updateCounter(); |
|
69 }); |
|
70 $("div.actions span.question a").click(function(event) { |
|
71 event.preventDefault(); |
|
72 $(options.acrossInput).val(1); |
|
73 showClear(); |
|
74 }); |
|
75 $("div.actions span.clear a").click(function(event) { |
|
76 event.preventDefault(); |
|
77 $(options.allToggle).attr("checked", false); |
|
78 clearAcross(); |
|
79 checker(0); |
|
80 updateCounter(); |
|
81 }); |
|
82 lastChecked = null; |
|
83 $(actionCheckboxes).click(function(event) { |
|
84 if (!event) { var event = window.event; } |
|
85 var target = event.target ? event.target : event.srcElement; |
|
86 if (lastChecked && $.data(lastChecked) != $.data(target) && event.shiftKey == true) { |
|
87 var inrange = false; |
|
88 $(lastChecked).attr("checked", target.checked) |
|
89 .parent().parent().toggleClass(options.selectedClass, target.checked); |
|
90 $(actionCheckboxes).each(function() { |
|
91 if ($.data(this) == $.data(lastChecked) || $.data(this) == $.data(target)) { |
|
92 inrange = (inrange) ? false : true; |
|
93 } |
|
94 if (inrange) { |
|
95 $(this).attr("checked", target.checked) |
|
96 .parent().parent().toggleClass(options.selectedClass, target.checked); |
|
97 } |
|
98 }); |
|
99 } |
|
100 $(target).parent().parent().toggleClass(options.selectedClass, target.checked); |
|
101 lastChecked = target; |
|
102 updateCounter(); |
|
103 }); |
|
104 $('form#changelist-form table#result_list tr').find('td:gt(0) :input').change(function() { |
|
105 list_editable_changed = true; |
|
106 }); |
|
107 $('form#changelist-form button[name="index"]').click(function(event) { |
|
108 if (list_editable_changed) { |
|
109 return confirm(gettext("You have unsaved changes on individual editable fields. If you run an action, your unsaved changes will be lost.")); |
|
110 } |
|
111 }); |
|
112 $('form#changelist-form input[name="_save"]').click(function(event) { |
|
113 var action_changed = false; |
|
114 $('div.actions select option:selected').each(function() { |
|
115 if ($(this).val()) { |
|
116 action_changed = true; |
|
117 } |
|
118 }); |
|
119 if (action_changed) { |
|
120 if (list_editable_changed) { |
|
121 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.")); |
|
122 } else { |
|
123 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.")); |
|
124 } |
|
125 } |
|
126 }); |
|
127 } |
|
128 /* Setup plugin defaults */ |
|
129 $.fn.actions.defaults = { |
|
130 actionContainer: "div.actions", |
|
131 counterContainer: "span.action-counter", |
|
132 allContainer: "div.actions span.all", |
|
133 acrossInput: "div.actions input.select-across", |
|
134 acrossQuestions: "div.actions span.question", |
|
135 acrossClears: "div.actions span.clear", |
|
136 allToggle: "#action-toggle", |
|
137 selectedClass: "selected" |
|
138 } |
|
139 })(django.jQuery); |