0be8b261d8b53c8145cf873e3bba72c6a1bb58cb
[lhc/web/www.git] / www / plugins-dist / forum / prive / javascript / actiongroup.js
1 var actiongroup={
2 checkedItems:{},
3 countchecked:0,
4 countCurrent:0
5 }
6 /**
7 * Vider completement la selection
8 */
9 actiongroup.emptySelection = function(){
10 actiongroup.checkedItems={};
11 actiongroup.countchecked=0;
12 actiongroup.unselectAll();
13 }
14 /**
15 * Selectionner les items de la page
16 */
17 actiongroup.selectAll = function(sel){
18 sel = sel || document;
19 $('input.actiongroup',sel)
20 .attr('checked',true)
21 .each(function(){
22 actiongroup.updateChecklist($(this).attr('value'),true,false);
23 });
24 actiongroup.updateChecklist(0,false);
25 }
26 /**
27 * Deselectionner les items de la page
28 */
29 actiongroup.unselectAll = function(sel){
30 sel = sel || document;
31 $('input.actiongroup',sel)
32 .attr('checked',false)
33 .each(function(){
34 actiongroup.updateChecklist($(this).attr('value'),false,false);
35 });
36 actiongroup.updateChecklist(0,false);
37 }
38 /**
39 * Mettre a jour la listes des donnees et la boite html #actiongroup
40 * consecutivement
41 * @param int value
42 * @param bool checked
43 * @param bool|void update_status
44 */
45 actiongroup.updateChecklist = function(value,checked,update_status){
46 if (checked && !(actiongroup.checkedItems[value] || false))
47 actiongroup.countchecked++;
48 if (!checked && (actiongroup.checkedItems[value] || false))
49 actiongroup.countchecked--;
50 actiongroup.checkedItems[value]=checked;
51 if (update_status || typeof update_status=="undefined") {
52 jQuery.spip.log(actiongroup.countchecked);
53 if (actiongroup.countchecked==0){
54 $('#actiongroup .shortcut.empty,#actiongroup .shortcut.unselectall')
55 .addClass('hidden');
56 $('#actiongroup .shortcut.selectall')
57 .removeClass('hidden');
58 $('#actiongroup .status .zero')
59 .show()
60 .siblings(':visible')
61 .hide();
62 $('#actiongroup .action')
63 .addClass('hidden');
64 if ($(".checkable").length)
65 $('#actiongroup').closest('.box:hidden').show('fast');
66 else
67 $('#actiongroup').closest('.box:visible').hide('fast');
68 }
69 else {
70 $('#actiongroup .action')
71 .removeClass('hidden');
72 if (actiongroup.countchecked==1)
73 $('#actiongroup .status .one')
74 .show()
75 .siblings(':visible')
76 .hide();
77 else{
78 $('#actiongroup .status .many b')
79 .html(actiongroup.countchecked);
80 $('#actiongroup .status .many')
81 .show()
82 .siblings(':visible')
83 .hide();
84 }
85 var checked = $('input.actiongroup:checked').length;
86 if (checked)
87 $('#actiongroup .shortcut.unselectall')
88 .removeClass('hidden');
89 else
90 $('#actiongroup .shortcut.unselectall')
91 .addClass('hidden');
92 if (actiongroup.countchecked>checked)
93 $('#actiongroup .shortcut.empty')
94 .removeClass('hidden');
95 else
96 $('#actiongroup .shortcut.empty')
97 .addClass('hidden');
98 if (actiongroup.countCurrent>checked)
99 $('#actiongroup .shortcut.selectall')
100 .removeClass('hidden');
101 else
102 $('#actiongroup .shortcut.selectall')
103 .addClass('hidden');
104 }
105 $('#actiongroup')
106 .siblings('.success:visible')
107 .hide('slow');
108 }
109 }
110 /**
111 * Remettre a jour les checkbox du html a partir de la liste en memoire
112 * apres un chargement ajax par exemple
113 */
114 actiongroup.updateHtmlFromChecklist = function(){
115 actiongroup.countCurrent = $('input.actiongroup')
116 .each(function(){
117 $(this)
118 .attr('checked',actiongroup.checkedItems[$(this).attr('value')] || false);
119 })
120 .length;
121 // forcer la mise a jour des messages
122 actiongroup.updateChecklist(0,false);
123 }
124 /**
125 * Activer le traitement par lot sur une liste d'items
126 * @param node
127 * @param type
128 */
129 actiongroup.activate = function(sel){
130 $(sel)
131 .not('.checkable')
132 .each(function(){
133 var id = parseInt($(this).attr('data-id'));
134 if (id)
135 $(this)
136 .append("<input type='checkbox' name='actiongroup[]' class='actiongroup checkbox' value='"+id+"'/>");
137 })
138 .click(function(){
139 var check = $('input.actiongroup',this);
140 check
141 .prop('checked',!check.prop('checked'))
142 .trigger('change');
143 })
144 .addClass('checkable')
145 .find('input.actiongroup, form, a')
146 .click(function(e){
147 e.stopPropagation();
148 })
149 .filter('input.actiongroup')
150 .change(function(){
151 actiongroup.updateChecklist($(this).attr('value'),$(this).prop('checked'));
152 });
153
154 $('#actiongroup .action')
155 .unbind('click')
156 .click(function(){
157 var href=$(this).attr('data-href');
158 var ids = [];
159 for(var id in actiongroup.checkedItems)
160 if (actiongroup.checkedItems[id])
161 ids.push(id);
162 jQuery.spip.log(href);
163 jQuery.spip.log(ids);
164 if (href && ids.length) {
165 $('#actiongroup')
166 .siblings('.success')
167 .hide()
168 .parent()
169 .animateLoading();
170 $.ajax({
171 url: href,
172 data: {"ids":ids},
173 onAjaxLoad:false,
174 success: function(){
175 // vider le cache car on a fait une operation en base
176 jQuery.spip.preloaded_urls = {};
177 // recharger le bloc de la selection
178 $(sel).eq(0).ajaxReload();
179 actiongroup.checkedItems={};
180 actiongroup.countchecked=0;
181 actiongroup.countCurrent=0;
182 actiongroup.updateChecklist(0,false);
183 $('#actiongroup')
184 .css('opacity','1.0')
185 .siblings()
186 .css('opacity','1.0')
187 .filter('.success')
188 .show()
189 .parent().find('.image_loading').remove();
190 }
191 });
192 }
193 });
194 actiongroup.updateHtmlFromChecklist();
195 }