Dotclear

source: admin/js/common.js @ 3615:b9ac15edad1c

Revision 3615:b9ac15edad1c, 17.8 KB checked in by franck <carnet.franck.paul@…>, 8 years ago (diff)

Reduce close region of message box as it prevents to copy the content, single error message are not more listed as list but paragraph.

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

Sites map