Dotclear

source: admin/js/common.js @ 3771:ee48b80e8d04

Revision 3771:ee48b80e8d04, 18.4 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Use svg image rather than png for outgoing links

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

Sites map