Dotclear

source: admin/js/common.js @ 1861:19f3c0a14aeb

Revision 1861:19f3c0a14aeb, 11.0 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Add a collapser for the main menu, step 1.

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          unfolded_sections: dotclear.unfolded_sections,
66          hide: true,
67          speed: 0,
68          legend_click: false,
69          fn: false, // A function called on first display,
70          user_pref: false,
71          reverse_user_pref: false, // Reverse cookie behavior
72          user_pref:false,
73          reverse_user_pref: false
74     };
75     var p = jQuery.extend(defaults,s);
76
77     if (!target) { return this; }
78
79     var set_cookie = p.hide ^ p.reverse_cookie;
80     if (p.cookie && jQuery.cookie(p.cookie)) {
81          p.hide = p.reverse_cookie;
82     }
83     
84     var set_user_pref = p.hide ^ p.reverse_user_pref;
85     if (p.user_pref && p.unfolded_sections !== undefined && (p.user_pref in p.unfolded_sections)) {
86          p.hide = p.reverse_user_pref;
87     }
88     var toggle = function(i,speed) {
89          speed = speed || 0;
90          if (p.hide) {
91               $(i).get(0).src = p.img_on_src;
92               $(i).get(0).alt = p.img_on_alt;
93               target.hide(speed);
94          } else {
95               $(i).get(0).src = p.img_off_src;
96               $(i).get(0).alt = p.img_off_alt;
97               target.show(speed);
98               if (p.fn) {
99                    p.fn.apply(target);
100                    p.fn = false;
101               }
102          }
103
104          if (p.cookie && set_cookie) {
105               if (p.hide ^ p.reverse_cookie) {
106                    jQuery.cookie(p.cookie,'',{expires: -1});
107               } else {
108                    jQuery.cookie(p.cookie,1,{expires: 30});
109               }
110          }
111          p.hide = !p.hide;
112     };
113
114     return this.each(function() {
115          var i = document.createElement('img');
116          i.src = p.img_off_src;
117          i.alt = p.img_off_alt;
118          var a = document.createElement('a');
119          a.href= '#';
120          $(a).append(i);
121          $(a).css({
122               border: 'none',
123               outline: 'none'
124          });
125
126          var ctarget = p.legend_click ? this : a;
127
128          $(ctarget).css('cursor','pointer');
129          if (p.legend_click) {
130               $(ctarget).find('label').css('cursor','pointer');
131          }
132          $(ctarget).click(function() {
133               if (p.user_pref && set_user_pref) {
134                    if (p.hide ^ p.reverse_user_pref) {
135                         jQuery.post('services.php', 
136                              {'f':'setSectionFold','section':p.user_pref,'value':1,xd_check: dotclear.nonce},
137                              function(data) {});
138                    } else {
139                         jQuery.post('services.php', 
140                              {'f':'setSectionFold','section':p.user_pref,'value':0,xd_check: dotclear.nonce},
141                              function(data) {});
142                    }
143                    jQuery.cookie(p.user_pref,'',{expires: -1});
144               }
145               toggle(i,p.speed);
146               return false;
147          });
148
149
150          toggle($(i).get(0));
151          $(this).prepend(' ').prepend(a);
152     });
153};
154
155jQuery.fn.helpViewer = function() {
156     if (this.length < 1) {
157          return this;
158     }
159
160     var p = {
161          img_on_src: dotclear.img_plus_src,
162          img_on_alt: dotclear.img_plus_alt,
163          img_off_src: dotclear.img_minus_src,
164          img_off_alt: dotclear.img_minus_alt
165     };
166     var This = this;
167     var toggle = function() {
168          $('#content').toggleClass('with-help');
169          if (document.all) {
170               if ($('#content').hasClass('with-help')) {
171                    select = $('#content select:visible').hide();
172               } else {
173                    select.show();
174               }
175          }
176          $('p#help-button span').text($('#content').hasClass('with-help') ? dotclear.msg.help_hide : dotclear.msg.help);
177          sizeBox();
178          return false;
179     };
180
181     var sizeBox = function() {
182          This.css('height','auto');
183          if ($('#main').height() > This.height()) {
184               This.css('height',$('#main').height() + 'px');
185          }
186     };
187
188     var textToggler = function(o) {
189          var i = $('<img src="'+p.img_on_src+'" alt="'+p.img_on_alt+'" />');
190          o.css('cursor','pointer');
191          var hide = true;
192
193          o.prepend(' ').prepend(i);
194          o.click(function() {
195               $(this).nextAll().each(function() {
196                    if ($(this).is('h4')) {
197                         return false;
198                    }
199                    $(this).toggle();
200                    sizeBox();
201                    return true;
202               });
203               hide = !hide;
204               var img = $(this).find('img');
205               if (!hide) {
206                    img.attr('src',p.img_off_src);
207               } else {
208                    img.attr('src',p.img_on_src);
209               }
210          });
211     };
212
213     this.addClass('help-box');
214     this.find('>hr').remove();
215
216     this.find('h4').each(function() { textToggler($(this)); });
217     this.find('h4:first').nextAll('*:not(h4)').hide();
218     sizeBox();
219
220     var img = $('<p id="help-button"><span>'+dotclear.msg.help+'</span></p>');
221     var select = $();
222     img.click(function() { return toggle(); });
223
224     $('#content').append(img);
225
226     return this;
227};
228
229/* Dotclear common object
230-------------------------------------------------------- */
231var dotclear = {
232     msg: {},
233
234     hideLockable: function() {
235          $('div.lockable').each(function() {
236               var current_lockable_div = this;
237               $(this).find('p.form-note').hide();
238               $(this).find('input').each(function() {
239                    this.disabled = true;
240                    $(this).width(($(this).width()-14) + 'px');
241
242                    var imgE = document.createElement('img');
243                    imgE.src = 'images/locker.png';
244                    imgE.style.position = 'absolute';
245                    imgE.style.top = '1.7em';
246                    imgE.style.left = ($(this).width()+12)+'px';
247                    imgE.alt=dotclear.msg.click_to_unlock;
248                    $(imgE).css('cursor','pointer');
249
250                    $(imgE).click(function() {
251                         $(this).hide();
252                         $(this).prev('input').each(function() {
253                              this.disabled = false;
254                              $(this).width(($(this).width()+14) + 'px');
255                         });
256                         $(current_lockable_div).find('p.form-note').show();
257                    });
258
259                    $(this).parent().css('position','relative');
260                    $(this).after(imgE);
261               });
262          });
263     },
264
265     checkboxesHelpers: function(e, target) {
266          $(e).append(document.createTextNode(dotclear.msg.to_select));
267          $(e).append(document.createTextNode(' '));
268
269          target = target || $(e).parents('form').find('input[type="checkbox"]');
270         
271          var a = document.createElement('a');
272          a.href='#';
273          $(a).append(document.createTextNode(dotclear.msg.select_all));
274          a.onclick = function() {
275               target.check();
276               return false;
277          };
278          $(e).append(a);
279
280          $(e).append(document.createTextNode(' | '));
281
282          a = document.createElement('a');
283          a.href='#';
284          $(a).append(document.createTextNode(dotclear.msg.no_selection));
285          a.onclick = function() {
286               target.unCheck();
287               return false;
288          };
289          $(e).append(a);
290
291          $(e).append(document.createTextNode(' - '));
292
293          a = document.createElement('a');
294          a.href='#';
295          $(a).append(document.createTextNode(dotclear.msg.invert_sel));
296          a.onclick = function() {
297               target.toggleCheck();
298               return false;
299          };
300          $(e).append(a);
301     },
302
303     postsActionsHelper: function() {
304          $('#form-entries').submit(function() {
305               var action = $(this).find('select[name="action"]').val();
306               var checked = false;
307
308               $(this).find('input[name="entries[]"]').each(function() {
309                    if (this.checked) {
310                         checked = true;
311                    }
312               });
313
314               if (!checked) { return false; }
315
316               if (action == 'delete') {
317                    return window.confirm(dotclear.msg.confirm_delete_posts.replace('%s',$('input[name="entries[]"]:checked').size()));
318               }
319
320               return true;
321          });
322     },
323
324     commentsActionsHelper: function() {
325          $('#form-comments').submit(function() {
326               var action = $(this).find('select[name="action"]').val();
327               var checked = false;
328
329               $(this).find('input[name="comments[]"]').each(function() {
330                    if (this.checked) {
331                         checked = true;
332                    }
333               });
334
335               if (!checked) { return false; }
336
337               if (action == 'delete') {
338                    return window.confirm(dotclear.msg.confirm_delete_comments.replace('%s',$('input[name="comments[]"]:checked').size()));
339               }
340
341               return true;
342          });
343     }
344};
345
346/* On document ready
347-------------------------------------------------------- */
348$(function() {
349     // remove class no-js from html tag; cf style/default.css for examples
350     $('body').removeClass('no-js').addClass('with-js');
351     
352     $('#wrapper').contents().each(function() {
353          if (this.nodeType==8) {
354               $('#footer a').attr('title', $('#footer a').attr('title') + this.data );
355          }
356     });
357
358     // Blog switcher
359     $('#switchblog').change(function() {
360          this.form.submit();
361     });
362
363     var menu_settings = {
364          img_on_src: dotclear.img_menu_off,
365          img_off_src: dotclear.img_menu_on,
366          legend_click: true,
367          speed: 100
368     }
369     $('#blog-menu h3:first').toggleWithLegend($('#blog-menu ul:first'),
370          $.extend({user_pref:'dc_blog_menu'},menu_settings)
371     );
372     $('#system-menu h3:first').toggleWithLegend($('#system-menu ul:first'),
373          $.extend({user_pref:'dc_system_menu'},menu_settings)
374     );
375     $('#plugins-menu h3:first').toggleWithLegend($('#plugins-menu ul:first'),
376          $.extend({user_pref:'dc_plugins_menu'},menu_settings)
377     );
378     $('#favorites-menu h3:first').toggleWithLegend($('#favorites-menu ul:first'),
379          $.extend({user_pref:'dc_favorites_menu',hide:false,reverse_user_pref:true},menu_settings)
380     );
381
382     $('#help').helpViewer();
383
384     $('.message').backgroundFade({sColor:'#cccccc',eColor:'#676e78',steps:20});
385     $('.error').backgroundFade({sColor:'#ffdec8',eColor:'#ffbaba',steps:20});
386     $('.success').backgroundFade({sColor:'#9BCA1C',eColor:'#bee74b',steps:20});
387
388     $('form:has(input[type=password][name=your_pwd])').submit(function() {
389          var e = this.elements['your_pwd'];
390          if (e.value == '') {
391               e.focus();
392               $(e).backgroundFade({sColor:'#ffffff',eColor:'#ffbaba',steps:50},function() {
393                    $(this).backgroundFade({sColor:'#ffbaba',eColor:'#ffffff'});
394               });
395               return false;
396          }
397          return true;
398     });
399
400     // Main menu collapser
401    var objMain = $('#wrapper');
402    function showSidebar(){
403         // Show sidebar
404        objMain.removeClass('hide-mm');
405        $.cookie('sidebar-pref',null,{expires:30});
406    }
407    function hideSidebar(){
408         // Hide sidebar
409        objMain.addClass('hide-mm');
410        $.cookie('sidebar-pref','hide-mm',{expires:30});
411    }
412    // Sidebar separator
413    var objSeparator = $('#collapser');
414    objSeparator.click(function(e){
415        e.preventDefault();
416        if ( objMain.hasClass('hide-mm') ){
417            showSidebar();
418        }
419        else {
420            hideSidebar();
421        }
422    }); // .css('height', objSeparator.parent().outerHeight() + 'px');
423     if ( $.cookie('sidebar-pref') == 'hide-mm' ){
424          objMain.addClass('hide-mm');
425     } else {
426          objMain.removeClass('hide-mm');
427     }
428
429});
Note: See TracBrowser for help on using the repository browser.

Sites map