Dotclear

source: admin/js/common.js @ 2810:0aeae42c09b0

Revision 2810:0aeae42c09b0, 15.5 KB checked in by Nicolas <nikrou77@…>, 11 years ago (diff)

When selecting an entry from popup_posts.php, jump to a defined page did not work as expected.
Fixes #1872

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     };
73     var p = jQuery.extend(defaults,s);
74
75     if (!target) { return this; }
76
77     var set_cookie = p.hide ^ p.reverse_cookie;
78     if (p.cookie && jQuery.cookie(p.cookie)) {
79          p.hide = p.reverse_cookie;
80     }
81
82     var set_user_pref = p.hide ^ p.reverse_user_pref;
83     if (p.user_pref && p.unfolded_sections !== undefined && (p.user_pref in p.unfolded_sections)) {
84          p.hide = p.reverse_user_pref;
85     }
86     var toggle = function(i,speed) {
87          speed = speed || 0;
88          if (p.hide) {
89               $(i).get(0).src = p.img_on_src;
90               $(i).get(0).alt = p.img_on_alt;
91               target.addClass('hide');
92          } else {
93               $(i).get(0).src = p.img_off_src;
94               $(i).get(0).alt = p.img_off_alt;
95               target.removeClass('hide');
96               if (p.fn) {
97                    p.fn.apply(target);
98                    p.fn = false;
99               }
100          }
101
102          if (p.cookie && set_cookie) {
103               if (p.hide ^ p.reverse_cookie) {
104                    jQuery.cookie(p.cookie,'',{expires: -1});
105               } else {
106                    jQuery.cookie(p.cookie,1,{expires: 30});
107               }
108          }
109          p.hide = !p.hide;
110     };
111
112     return this.each(function() {
113          var i = document.createElement('img');
114          i.src = p.img_off_src;
115          i.alt = p.img_off_alt;
116          var a = document.createElement('a');
117          a.href= '#';
118          $(a).append(i);
119          $(a).css({
120               border: 'none',
121               outline: 'none'
122          });
123
124          var ctarget = p.legend_click ? this : a;
125
126          $(ctarget).css('cursor','pointer');
127          if (p.legend_click) {
128               $(ctarget).find('label').css('cursor','pointer');
129          }
130          $(ctarget).click(function() {
131               if (p.user_pref && set_user_pref) {
132                    if (p.hide ^ p.reverse_user_pref) {
133                         jQuery.post('services.php',
134                              {'f':'setSectionFold','section':p.user_pref,'value':1,xd_check: dotclear.nonce},
135                              function(data) {});
136                    } else {
137                         jQuery.post('services.php',
138                              {'f':'setSectionFold','section':p.user_pref,'value':0,xd_check: dotclear.nonce},
139                              function(data) {});
140                    }
141                    jQuery.cookie(p.user_pref,'',{expires: -1});
142               }
143               toggle(i,p.speed);
144               return false;
145          });
146
147
148          toggle($(i).get(0));
149          $(this).prepend(' ').prepend(a);
150     });
151};
152
153(function($) {
154     'use strict';
155
156     $.expandContent = function(opts) {
157          var defaults = {};
158          $.expandContent.options = $.extend({},defaults,opts);
159
160          if (opts==undefined || opts.callback==undefined || !$.isFunction(opts.callback)) {
161               return;
162          }
163          if (opts.line!=undefined) {
164               multipleExpander(opts.line,opts.lines);
165          }
166          opts.lines.each(function() {
167               singleExpander(this);
168          });
169     }
170
171     var singleExpander = function singleExpander(line) {
172          $('<input type="image" src="'+dotclear.img_plus_src+'" alt="'+dotclear.img_plus_alt+'"/>')
173               .click(function(e) {
174                    toggleArrow(this);
175                    $.expandContent.options.callback.call(this,line);
176                    e.preventDefault();
177               })
178               .prependTo($(line).children().get(0)); // first td
179     };
180
181     var multipleExpander = function multipleExpander(line,lines) {
182          $('<input type="image" src="'+dotclear.img_plus_src+'" alt="'+dotclear.img_plus_alt+'"/>')
183               .click(function(e) {
184                    var that = this;
185                    var action = toggleArrow(this);
186                    lines.each(function() {
187                         toggleArrow(this.firstChild.firstChild,action);
188                         $.expandContent.options.callback.call(that,this,action);
189
190                    });
191                    e.preventDefault();
192               })
193               .prependTo($(line).children().get(0)); // first td
194     };
195
196     var toggleArrow = function toggleArrow(button,action) {
197          action = action || '';
198          if (action=='') {
199               if (button.alt==dotclear.img_plus_alt) {
200                    action = 'open';
201               } else {
202                    action = 'close';
203               }
204          }
205
206          if (action=='open') {
207               button.src = dotclear.img_minus_src;
208               button.alt = dotclear.img_minus_alt;
209          } else {
210               button.src = dotclear.img_plus_src;
211               button.alt = dotclear.img_plus_alt;
212          }
213
214          return action;
215     }
216})(jQuery);
217
218jQuery.fn.helpViewer = function() {
219     if (this.length < 1) {
220          return this;
221     }
222
223     var p = {
224          img_on_src: dotclear.img_plus_src,
225          img_on_alt: dotclear.img_plus_alt,
226          img_off_src: dotclear.img_minus_src,
227          img_off_alt: dotclear.img_minus_alt
228     };
229     var This = this;
230     var toggle = function() {
231          $('#content').toggleClass('with-help');
232          if (document.all) {
233               if ($('#content').hasClass('with-help')) {
234                    select = $('#content select:visible').hide();
235               } else {
236                    select.show();
237               }
238          }
239          $('p#help-button span a').text($('#content').hasClass('with-help') ? dotclear.msg.help_hide : dotclear.msg.help);
240          sizeBox();
241          return false;
242     };
243
244     var sizeBox = function() {
245          This.css('height','auto');
246          if ($('#wrapper').height() > This.height()) {
247               This.css('height',$('#wrapper').height() + 'px');
248          }
249     };
250
251     var textToggler = function(o) {
252          var i = $('<img src="'+p.img_on_src+'" alt="'+p.img_on_alt+'" />');
253          o.css('cursor','pointer');
254          var hide = true;
255
256          o.prepend(' ').prepend(i);
257          o.click(function() {
258               $(this).nextAll().each(function() {
259                    if ($(this).is('h4')) {
260                         return false;
261                    }
262                    $(this).toggle();
263                    sizeBox();
264                    return true;
265               });
266               hide = !hide;
267               var img = $(this).find('img');
268               if (!hide) {
269                    img.attr('src',p.img_off_src);
270               } else {
271                    img.attr('src',p.img_on_src);
272               }
273          });
274     };
275
276     this.addClass('help-box');
277     this.find('>hr').remove();
278
279     this.find('h4').each(function() { textToggler($(this)); });
280     this.find('h4:first').nextAll('*:not(h4)').hide();
281     sizeBox();
282
283     var img = $('<p id="help-button"><span><a href="">'+dotclear.msg.help+'</a></span></p>');
284     var select = $();
285     img.click(function() { return toggle(); });
286
287     $('#content').append(img);
288
289     // listen for scroll
290     var peInPage = $('#help-button').offset().top;
291     $('#help-button').addClass("floatable");
292     var peInFloat = $('#help-button').offset().top - $(window).scrollTop();
293     $('#help-button').removeClass("floatable");
294     $(window).scroll(
295          function() {
296               if ($(window).scrollTop() >= peInPage - peInFloat ) {
297                    $('#help-button').addClass("floatable");
298               } else {
299                    $('#help-button').removeClass("floatable");
300               }
301          }
302     );
303
304     return this;
305};
306
307
308/* Dotclear common object
309-------------------------------------------------------- */
310var dotclear = {
311     msg: {},
312
313     hideLockable: function() {
314          $('div.lockable').each(function() {
315               var current_lockable_div = this;
316               $(this).find('p.form-note').hide();
317               $(this).find('input').each(function() {
318                    this.disabled = true;
319                    $(this).width(($(this).width()-14) + 'px');
320
321                    var imgE = document.createElement('img');
322                    imgE.src = 'images/locker.png';
323                    imgE.style.position = 'absolute';
324                    imgE.style.top = '1.7em';
325                    imgE.style.left = ($(this).width()+12)+'px';
326                    imgE.alt=dotclear.msg.click_to_unlock;
327                    $(imgE).css('cursor','pointer');
328
329                    $(imgE).click(function() {
330                         $(this).hide();
331                         $(this).prev('input').each(function() {
332                              this.disabled = false;
333                              $(this).width(($(this).width()+14) + 'px');
334                         });
335                         $(current_lockable_div).find('p.form-note').show();
336                    });
337
338                    $(this).parent().css('position','relative');
339                    $(this).after(imgE);
340               });
341          });
342     },
343
344     checkboxesHelpers: function(e, target) {
345          $(e).append(document.createTextNode(dotclear.msg.to_select));
346          $(e).append(document.createTextNode(' '));
347
348          $('<a href="#">'+dotclear.msg.select_all+'</a>').click(function() {
349               if (target !== undefined) {
350                    target.check();
351               } else {
352                    $(e).parents('form').find('input[type="checkbox"]').check();
353               }
354
355               return false;
356          }).appendTo($(e));
357          $(e).append(document.createTextNode(' | '));
358
359          $('<a href="#">'+dotclear.msg.no_selection+'</a>').click(function() {
360               if (target !== undefined) {
361                    target.unCheck();
362               } else {
363                    $(e).parents('form').find('input[type="checkbox"]').unCheck();
364               }
365
366               return false;
367          }).appendTo($(e));
368          $(e).append(document.createTextNode(' - '));
369
370          $('<a href="#">'+dotclear.msg.invert_sel+'</a>').click(function() {
371               if (target !== undefined) {
372                    target.toggleCheck();
373               } else {
374                    $(e).parents('form').find('input[type="checkbox"]').toggleCheck();
375               }
376
377               return false;
378          }).appendTo($(e));
379     },
380
381     postsActionsHelper: function() {
382          $('#form-entries').submit(function() {
383               var action = $(this).find('select[name="action"]').val();
384               if (action===undefined) {
385                    return;
386               }
387               var checked = false;
388
389               $(this).find('input[name="entries[]"]').each(function() {
390                    if (this.checked) {
391                         checked = true;
392                    }
393               });
394
395               if (!checked) { return false; }
396
397               if (action == 'delete') {
398                    return window.confirm(dotclear.msg.confirm_delete_posts.replace('%s',$('input[name="entries[]"]:checked').size()));
399               }
400
401               return true;
402          });
403     },
404
405     commentsActionsHelper: function() {
406          $('#form-comments').submit(function() {
407               var action = $(this).find('select[name="action"]').val();
408               var checked = false;
409
410               $(this).find('input[name="comments[]"]').each(function() {
411                    if (this.checked) {
412                         checked = true;
413                    }
414               });
415
416               if (!checked) { return false; }
417
418               if (action == 'delete') {
419                    return window.confirm(dotclear.msg.confirm_delete_comments.replace('%s',$('input[name="comments[]"]:checked').size()));
420               }
421
422               return true;
423          });
424     },
425     getCSSColor : function ( clazz) {
426          $('<div class="'+clazz+'" id="dotclear-obj-test-color" style="display:none"></div>').appendTo(document.body);
427          var tag2 = $('#dotclear-obj-test-color');
428          var color = $.trim(tag2.css("color").toLowerCase());
429          tag2.remove();
430          if ( color.charAt(0) === '#') {
431               return color;
432          }
433          var result = /^rgb\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\)$/.exec(color);
434          if ( result === null) {
435               return '';
436          }
437          var ret = '#';
438          for ( var i = 1; i < 4; i++) {
439               var val = parseInt(result[i],10);
440               ret += (val < 16 ? '0'+val.toString(16) : val.toString(16));
441          }
442          return ret;
443     },
444     initFadeColor : function() {
445          dotclear.fadeColor = {
446               beginPassword : dotclear.getCSSColor('colorBeginPassword'),
447               endPassword : dotclear.getCSSColor('colorEndPassword'),
448               beginMessage : dotclear.getCSSColor('colorBeginMessage'),
449               endMessage : dotclear.getCSSColor('colorEndMessage'),
450               beginError : dotclear.getCSSColor('colorBeginError'),
451               endError : dotclear.getCSSColor('colorEndError'),
452               beginSuccess : dotclear.getCSSColor('colorBeginSuccess'),
453               endSuccess : dotclear.getCSSColor('colorEndSuccess'),
454               beginValidatorMsg : dotclear.getCSSColor('colorBeginValidatorMsg'),
455               endValidatorMsg : dotclear.getCSSColor('colorEndValidatorMsg'),
456               beginValidatorErr : dotclear.getCSSColor('colorBeginValidatorErr'),
457               endValidatorErr : dotclear.getCSSColor('colorEndValidatorErr'),
458               beginUserMail : dotclear.getCSSColor('colorBeginUserMail'),
459               endUserMail : dotclear.getCSSColor('colorEndUserMail')
460          };
461     }};
462
463/* On document ready
464-------------------------------------------------------- */
465$(function() {
466     dotclear.initFadeColor();
467     // remove class no-js from html tag; cf style/default.css for examples
468     $('body').removeClass('no-js').addClass('with-js');
469
470     $('body').contents().each(function() {
471          if (this.nodeType==8) {
472               var data = this.data;
473               data = data.replace(/ /g,'&nbsp;').replace(/\n/g,'<br/>');
474               $('<span class="tooltip">'+$('#footer a').prop('title')+data+'</span>').appendTo('#footer a');
475          }
476     });
477
478     // manage outgoing links
479     $('a').filter(function() {
480          return ((this.hostname && this.hostname!=location.hostname && !$(this).hasClass('modal'))
481               || $(this).hasClass('outgoing'));
482     }).each(function() {
483          $(this).prop('title',$(this).prop('title')+' ('+dotclear.msg.new_window+')');
484          if (!$(this).hasClass('outgoing')) {
485               $(this).append('&nbsp;<img src="images/outgoing-blue.png" alt=""/>');
486          }
487     }).click(function(e) {
488          e.preventDefault();
489          window.open($(this).attr('href'));
490     });
491
492     // Blog switcher
493     $('#switchblog').change(function() {
494          this.form.submit();
495     });
496
497     var menu_settings = {
498          img_on_src: dotclear.img_menu_off,
499          img_off_src: dotclear.img_menu_on,
500          legend_click: true,
501          speed: 100
502     }
503     $('#blog-menu h3:first').toggleWithLegend($('#blog-menu ul:first'),
504          $.extend({user_pref:'dc_blog_menu'},menu_settings)
505     );
506     $('#system-menu h3:first').toggleWithLegend($('#system-menu ul:first'),
507          $.extend({user_pref:'dc_system_menu'},menu_settings)
508     );
509     $('#plugins-menu h3:first').toggleWithLegend($('#plugins-menu ul:first'),
510          $.extend({user_pref:'dc_plugins_menu'},menu_settings)
511     );
512     $('#favorites-menu h3:first').toggleWithLegend($('#favorites-menu ul:first'),
513          $.extend({user_pref:'dc_favorites_menu',hide:false,reverse_user_pref:true},menu_settings)
514     );
515
516     $('#help').helpViewer();
517
518     $('.message').backgroundFade({sColor: dotclear.fadeColor.beginMessage, eColor: dotclear.fadeColor.endMessage, steps:20});
519     $('.error').backgroundFade({sColor: dotclear.fadeColor.beginError, eColor: dotclear.fadeColor.endError, steps:20});
520     $('.success').backgroundFade({sColor: dotclear.fadeColor.beginSuccess, eColor: dotclear.fadeColor.endSuccess, steps:20});
521
522     $('form:has(input[type=password][name=your_pwd])').submit(function() {
523          var e = this.elements['your_pwd'];
524          if (e.value == '') {
525               e.focus();
526               $(e).backgroundFade({sColor: dotclear.fadeColor.beginPassword,eColor: dotclear.fadeColor.endPassword,steps:50},function() {
527                    $(this).backgroundFade({sColor: dotclear.fadeColor.endPassword,eColor: dotclear.fadeColor.beginPassword});
528               });
529               return false;
530          }
531          return true;
532     });
533
534     // Main menu collapser
535    var objMain = $('#wrapper');
536    function showSidebar(){
537         // Show sidebar
538        objMain.removeClass('hide-mm');
539        $.cookie('sidebar-pref',null,{expires:30});
540    }
541    function hideSidebar(){
542         // Hide sidebar
543        objMain.addClass('hide-mm');
544        $.cookie('sidebar-pref','hide-mm',{expires:30});
545    }
546    // Sidebar separator
547    var objSeparator = $('#collapser');
548    objSeparator.click(function(e){
549        e.preventDefault();
550        if ( objMain.hasClass('hide-mm') ){
551            showSidebar();
552             $('#main-menu input#qx').focus();
553        }
554        else {
555            hideSidebar();
556            $('#content a.go_home').focus();
557        }
558    });
559     if ( $.cookie('sidebar-pref') == 'hide-mm' ){
560          objMain.addClass('hide-mm');
561     } else {
562          objMain.removeClass('hide-mm');
563     }
564});
Note: See TracBrowser for help on using the repository browser.

Sites map