Dotclear

source: admin/js/common.js @ 1525:c30081595436

Revision 1525:c30081595436, 9.6 KB checked in by Nicolas <nikrou77@…>, 12 years ago (diff)

Fix issue #1541 : improve and simplify categories page.

Line 
1/* ChainHandler, py Peter van der Beken
2-------------------------------------------------------- */
3function chainHandler(obj, handlerName, handler) {
4     obj[handlerName] = (function(existingFunction) {
5          return function() {
6               handler.apply(this, arguments);
7               if (existingFunction)
8                    existingFunction.apply(this, arguments);
9          };
10     })(handlerName in obj ? obj[handlerName] : null);
11};
12
13/* jQuery extensions
14-------------------------------------------------------- */
15jQuery.fn.check = function() {
16     return this.each(function() {
17          if (this.checked != undefined) { this.checked = true; }
18     });
19};
20jQuery.fn.unCheck = function() {
21     return this.each(function() {
22          if (this.checked != undefined) { this.checked = false; }
23     });
24};
25jQuery.fn.setChecked = function(status) {
26     return this.each(function() {
27          if (this.checked != undefined) { this.checked = status; }
28     });
29};
30jQuery.fn.toggleCheck = function() {
31     return this.each(function() {
32          if (this.checked != undefined) { this.checked = !this.checked; }
33     });
34};
35
36jQuery.fn.enableShiftClick = function() {
37     this.click(
38     function (event) {
39          if (event.shiftKey) {
40               if (dotclear.lastclicked != '') {
41                    var range;
42                    var trparent = $(this).parents('tr');
43                    if (trparent.nextAll('#'+dotclear.lastclicked).length != 0)
44                         range = trparent.nextUntil('#'+dotclear.lastclicked);
45                    else
46                         range = trparent.prevUntil('#'+dotclear.lastclicked);
47
48                    range.find('input[type=checkbox]').setChecked(dotclear.lastclickedstatus);
49                    this.checked = dotclear.lastclickedstatus;
50               }
51          } else {
52               dotclear.lastclicked = $(this).parents('tr')[0].id;
53               dotclear.lastclickedstatus = this.checked;
54          }
55          return true;
56     });
57}
58
59jQuery.fn.toggleWithLegend = function(target,s) {
60     var defaults = {
61          img_on_src: dotclear.img_plus_src,
62          img_on_alt: dotclear.img_plus_alt,
63          img_off_src: dotclear.img_minus_src,
64          img_off_alt: dotclear.img_minus_alt,
65          hide: true,
66          speed: 0,
67          legend_click: false,
68          fn: false, // A function called on first display,
69          cookie: false,
70          reverse_cookie: false // Reverse cookie behavior
71     };
72     var p = jQuery.extend(defaults,s);
73
74     if (!target) { return this; }
75
76     var set_cookie = p.hide ^ p.reverse_cookie;
77     if (p.cookie && jQuery.cookie(p.cookie)) {
78          p.hide = p.reverse_cookie;
79     }
80
81     var toggle = function(i,speed) {
82          speed = speed || 0;
83          if (p.hide) {
84               $(i).get(0).src = p.img_on_src;
85               $(i).get(0).alt = p.img_on_alt;
86               target.hide(speed);
87          } else {
88               $(i).get(0).src = p.img_off_src;
89               $(i).get(0).alt = p.img_off_alt;
90               target.show(speed);
91               if (p.fn) {
92                    p.fn.apply(target);
93                    p.fn = false;
94               }
95          }
96
97          if (p.cookie && set_cookie) {
98               if (p.hide ^ p.reverse_cookie) {
99                    jQuery.cookie(p.cookie,'',{expires: -1});
100               } else {
101                    jQuery.cookie(p.cookie,1,{expires: 30});
102               }
103          }
104
105          p.hide = !p.hide;
106     };
107
108     return this.each(function() {
109          var i = document.createElement('img');
110          i.src = p.img_off_src;
111          i.alt = p.img_off_alt;
112          var a = document.createElement('a');
113          a.href= '#';
114          $(a).append(i);
115          $(a).css({
116               border: 'none',
117               outline: 'none'
118          });
119
120          var ctarget = p.legend_click ? this : a;
121
122          $(ctarget).css('cursor','pointer');
123          if (p.legend_click) {
124               $(ctarget).find('label').css('cursor','pointer');
125          }
126          $(ctarget).click(function() {
127               toggle(i,p.speed);
128               return false;
129          });
130
131
132          toggle($(i).get(0));
133          $(this).prepend(' ').prepend(a);
134     });
135};
136
137jQuery.fn.helpViewer = function() {
138     if (this.length < 1) {
139          return this;
140     }
141
142     var p = {
143          img_on_src: dotclear.img_plus_src,
144          img_on_alt: dotclear.img_plus_alt,
145          img_off_src: dotclear.img_minus_src,
146          img_off_alt: dotclear.img_minus_alt
147     };
148     var This = this;
149     var toggle = function() {
150          $('#content').toggleClass('with-help');
151          if (document.all) {
152               if ($('#content').hasClass('with-help')) {
153                    select = $('#content select:visible').hide();
154               } else {
155                    select.show();
156               }
157          }
158          $('p#help-button span').text($('#content').hasClass('with-help') ? dotclear.msg.help_hide : dotclear.msg.help);
159          sizeBox();
160          return false;
161     };
162
163     var sizeBox = function() {
164          This.css('height','auto');
165          if ($('#main').height() > This.height()) {
166               This.css('height',$('#main').height() + 'px');
167          }
168     };
169
170     var textToggler = function(o) {
171          var i = $('<img src="'+p.img_on_src+'" alt="'+p.img_on_alt+'" />');
172          o.css('cursor','pointer');
173          var hide = true;
174
175          o.prepend(' ').prepend(i);
176          o.click(function() {
177               $(this).nextAll().each(function() {
178                    if ($(this).is('h3')) {
179                         return false;
180                    }
181                    $(this).toggle();
182                    sizeBox();
183                    return true;
184               });
185               hide = !hide;
186               var img = $(this).find('img');
187               if (!hide) {
188                    img.attr('src',p.img_off_src);
189               } else {
190                    img.attr('src',p.img_on_src);
191               }
192          });
193     };
194
195     this.addClass('help-box');
196     this.find('>hr').remove();
197
198     this.find('h3').each(function() { textToggler($(this)); });
199     this.find('h3:first').nextAll('*:not(h3)').hide();
200     sizeBox();
201
202     var img = $('<p id="help-button"><span>'+dotclear.msg.help+'</span></p>');
203     var select = $();
204     img.click(function() { return toggle(); });
205
206     $('#content').append(img);
207
208     return this;
209};
210
211/* Dotclear common object
212-------------------------------------------------------- */
213var dotclear = {
214     msg: {},
215
216     hideLockable: function() {
217          $('div.lockable').each(function() {
218               var current_lockable_div = this;
219               $(this).find('p.form-note').hide();
220               $(this).find('input').each(function() {
221                    this.disabled = true;
222                    $(this).width(($(this).width()-14) + 'px');
223
224                    var imgE = document.createElement('img');
225                    imgE.src = 'images/locker.png';
226                    imgE.style.position = 'absolute';
227                    imgE.style.top = '1.7em';
228                    imgE.style.left = ($(this).width()+4)+'px';
229                    imgE.alt=dotclear.msg.click_to_unlock;
230                    $(imgE).css('cursor','pointer');
231
232                    $(imgE).click(function() {
233                         $(this).hide();
234                         $(this).prev('input').each(function() {
235                              this.disabled = false;
236                              $(this).width(($(this).width()+14) + 'px');
237                         });
238                         $(current_lockable_div).find('p.form-note').show();
239                    });
240
241                    $(this).parent().css('position','relative');
242                    $(this).after(imgE);
243               });
244          });
245     },
246
247     checkboxesHelpers: function(e) {
248          var a = document.createElement('a');
249          a.href='#';
250          $(a).append(document.createTextNode(dotclear.msg.select_all));
251          a.onclick = function() {
252               $(this).parents('form').find('input[type="checkbox"]').check();
253               return false;
254          };
255          $(e).append(a);
256
257          $(e).append(document.createTextNode(' - '));
258
259          a = document.createElement('a');
260          a.href='#';
261          $(a).append(document.createTextNode(dotclear.msg.no_selection));
262          a.onclick = function() {
263               $(this).parents('form').find('input[type="checkbox"]').unCheck();
264               return false;
265          };
266          $(e).append(a);
267
268          $(e).append(document.createTextNode(' - '));
269
270          a = document.createElement('a');
271          a.href='#';
272          $(a).append(document.createTextNode(dotclear.msg.invert_sel));
273          a.onclick = function() {
274               $(this).parents('form').find('input[type="checkbox"]').toggleCheck();
275               return false;
276          };
277          $(e).append(a);
278     },
279
280     postsActionsHelper: function() {
281          $('#form-entries').submit(function() {
282               var action = $(this).find('select[name="action"]').val();
283               var checked = false;
284
285               $(this).find('input[name="entries[]"]').each(function() {
286                    if (this.checked) {
287                         checked = true;
288                    }
289               });
290
291               if (!checked) { return false; }
292
293               if (action == 'delete') {
294                    return window.confirm(dotclear.msg.confirm_delete_posts.replace('%s',$('input[name="entries[]"]:checked').size()));
295               }
296
297               return true;
298          });
299     },
300
301     categoriesActionsHelper: function() {
302          $('#form-categories').submit(function() {
303               var action = $(this).find('select[name="action"]').val();
304               var checked = false;
305
306               $(this).find('input[name="categories[]"]').each(function() {
307                    if (this.checked) {
308                         checked = true;
309                    }
310               });
311
312               if (!checked) { return false; }
313
314               if (action == 'delete') {
315                    return window.confirm(dotclear.msg.confirm_delete_categories.replace('%s',$('input[name="categories[]"]:checked').size()));
316               }
317
318               return true;
319          });
320     },
321
322     commentsActionsHelper: function() {
323          $('#form-comments').submit(function() {
324               var action = $(this).find('select[name="action"]').val();
325               var checked = false;
326
327               $(this).find('input[name="comments[]"]').each(function() {
328                    if (this.checked) {
329                         checked = true;
330                    }
331               });
332
333               if (!checked) { return false; }
334
335               if (action == 'delete') {
336                    return window.confirm(dotclear.msg.confirm_delete_comments.replace('%s',$('input[name="comments[]"]:checked').size()));
337               }
338
339               return true;
340          });
341     }
342};
343
344/* On document ready
345-------------------------------------------------------- */
346$(function() {
347     // Blog switcher
348     $('#switchblog').change(function() {
349          this.form.submit();
350     });
351
352     var menu_settings = {
353          img_on_src: dotclear.img_menu_off,
354          img_off_src: dotclear.img_menu_on,
355          legend_click: true,
356          speed: 100
357     }
358     $('#blog-menu h3:first').toggleWithLegend($('#blog-menu ul:first'),
359          $.extend({cookie:'dc_blog_menu'},menu_settings)
360     );
361     $('#system-menu h3:first').toggleWithLegend($('#system-menu ul:first'),
362          $.extend({cookie:'dc_system_menu'},menu_settings)
363     );
364     $('#plugins-menu h3:first').toggleWithLegend($('#plugins-menu ul:first'),
365          $.extend({cookie:'dc_plugins_menu'},menu_settings)
366     );
367     $('#favorites-menu h3:first').toggleWithLegend($('#favorites-menu ul:first'),
368          $.extend({cookie:'dc_favorites_menu',hide:false,reverse_cookie:true},menu_settings)
369     );
370
371     $('#help').helpViewer();
372
373     $('.message').backgroundFade({sColor:'#cccccc',eColor:'#666666',steps:20});
374     $('.error').backgroundFade({sColor:'#ffdec8',eColor:'#ffbaba',steps:20});
375
376     $('form:has(input[type=password][name=your_pwd])').submit(function() {
377          var e = this.elements['your_pwd'];
378          if (e.value == '') {
379               e.focus();
380               $(e).backgroundFade({sColor:'#ffffff',eColor:'#ffbaba',steps:50},function() {
381                    $(this).backgroundFade({sColor:'#ffbaba',eColor:'#ffffff'});
382               });
383               return false;
384          }
385          return true;
386     });
387});
388
Note: See TracBrowser for help on using the repository browser.

Sites map