Dotclear

source: admin/js/common.js @ 3700:6a3097e81a2f

Revision 3700:6a3097e81a2f, 17.7 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Admin popups: dealing with Escape key fired → do nothing and close popup.

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/* ChainHandler, py Peter van der Beken
8-------------------------------------------------------- */
9function chainHandler(obj, handlerName, handler) {
10  obj[handlerName] = (function(existingFunction) {
11    return function() {
12      handler.apply(this, arguments);
13      if (existingFunction) existingFunction.apply(this, arguments);
14    };
15  })(handlerName in obj ? obj[handlerName] : null);
16};
17/* jQuery extensions
18-------------------------------------------------------- */
19jQuery.fn.check = function() {
20  return this.each(function() {
21    if (this.checked != undefined) {
22      this.checked = true;
23    }
24  });
25};
26jQuery.fn.unCheck = function() {
27  return this.each(function() {
28    if (this.checked != undefined) {
29      this.checked = false;
30    }
31  });
32};
33jQuery.fn.setChecked = function(status) {
34  return this.each(function() {
35    if (this.checked != undefined) {
36      this.checked = status;
37    }
38  });
39};
40jQuery.fn.toggleCheck = function() {
41  return this.each(function() {
42    if (this.checked != undefined) {
43      this.checked = !this.checked;
44    }
45  });
46};
47jQuery.fn.enableShiftClick = function() {
48  this.click(function(event) {
49    if (event.shiftKey) {
50      if (dotclear.lastclicked != '') {
51        var range;
52        var trparent = $(this).parents('tr');
53        if (trparent.nextAll('#' + dotclear.lastclicked).length != 0) range = trparent.nextUntil('#' + dotclear.lastclicked);
54        else range = trparent.prevUntil('#' + dotclear.lastclicked);
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}
65jQuery.fn.toggleWithLegend = function(target, s) {
66  var defaults = {
67    img_on_src: dotclear.img_plus_src,
68    img_on_alt: dotclear.img_plus_alt,
69    img_off_src: dotclear.img_minus_src,
70    img_off_alt: dotclear.img_minus_alt,
71    unfolded_sections: dotclear.unfolded_sections,
72    hide: true,
73    speed: 0,
74    legend_click: false,
75    fn: false, // A function called on first display,
76    user_pref: false,
77    reverse_user_pref: false // Reverse cookie behavior
78  };
79  var p = jQuery.extend(defaults, s);
80  if (!target) {
81    return this;
82  }
83  var set_cookie = p.hide ^ p.reverse_cookie;
84  if (p.cookie && jQuery.cookie(p.cookie)) {
85    p.hide = p.reverse_cookie;
86  }
87  var set_user_pref = p.hide ^ p.reverse_user_pref;
88  if (p.user_pref && p.unfolded_sections !== undefined && (p.user_pref in p.unfolded_sections)) {
89    p.hide = p.reverse_user_pref;
90  }
91  var toggle = function(i, speed) {
92    speed = speed || 0;
93    if (p.hide) {
94      $(i).get(0).src = p.img_on_src;
95      $(i).get(0).alt = p.img_on_alt;
96      target.addClass('hide');
97    } else {
98      $(i).get(0).src = p.img_off_src;
99      $(i).get(0).alt = p.img_off_alt;
100      target.removeClass('hide');
101      if (p.fn) {
102        p.fn.apply(target);
103        p.fn = false;
104      }
105    }
106    if (p.cookie && set_cookie) {
107      if (p.hide ^ p.reverse_cookie) {
108        jQuery.cookie(p.cookie, '', {
109          expires: -1
110        });
111      } else {
112        jQuery.cookie(p.cookie, 1, {
113          expires: 30
114        });
115      }
116    }
117    p.hide = !p.hide;
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    var ctarget = p.legend_click ? this : a;
131    $(ctarget).css('cursor', 'pointer');
132    if (p.legend_click) {
133      $(ctarget).find('label').css('cursor', 'pointer');
134    }
135    $(ctarget).click(function() {
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(data) {});
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(data) {});
151        }
152        jQuery.cookie(p.user_pref, '', {
153          expires: -1
154        });
155      }
156      toggle(i, p.speed);
157      return false;
158    });
159    toggle($(i).get(0));
160    $(this).prepend(' ').prepend(a);
161  });
162};
163(function($) {
164  'use strict';
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 type="image" src="' + dotclear.img_plus_src + '" alt="' + 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 type="image" src="' + dotclear.img_plus_src + '" alt="' + 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.alt == dotclear.img_plus_alt) {
200        action = 'open';
201      } else {
202        action = 'close';
203      }
204    }
205    if (action == 'open') {
206      button.src = dotclear.img_minus_src;
207      button.alt = dotclear.img_minus_alt;
208    } else {
209      button.src = dotclear.img_plus_src;
210      button.alt = 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_src: dotclear.img_plus_src,
221    img_on_alt: dotclear.img_plus_alt,
222    img_off_src: dotclear.img_minus_src,
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 i = $('<img src="' + p.img_on_src + '" alt="' + p.img_on_alt + '" />');
247    o.css('cursor', 'pointer');
248    var hide = true;
249    o.prepend(' ').prepend(i);
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('img');
261      if (!hide) {
262        img.attr('src', p.img_off_src);
263      } else {
264        img.attr('src', p.img_on_src);
265      }
266    });
267  };
268  this.addClass('help-box');
269  this.find('>hr').remove();
270  this.find('h4').each(function() {
271    textToggler($(this));
272  });
273  this.find('h4:first').nextAll('*:not(h4)').hide();
274  sizeBox();
275  var img = $('<p id="help-button"><span><a href="">' + dotclear.msg.help + '</a></span></p>');
276  var select = $();
277  img.click(function() {
278    return toggle();
279  });
280  $('#content').append(img);
281  // listen for scroll
282  var peInPage = $('#help-button').offset().top;
283  $('#help-button').addClass("floatable");
284  var peInFloat = $('#help-button').offset().top - $(window).scrollTop();
285  $('#help-button').removeClass("floatable");
286  $(window).scroll(function() {
287    if ($(window).scrollTop() >= peInPage - peInFloat) {
288      $('#help-button').addClass("floatable");
289    } else {
290      $('#help-button').removeClass("floatable");
291    }
292  });
293  return this;
294};
295/* Dotclear common object
296-------------------------------------------------------- */
297var dotclear = {
298  msg: {},
299  condSubmit: function(chkboxes, target) {
300    var checkboxes = $(chkboxes),
301      submitButt = $(target);
302    if (checkboxes === undefined || submitButt === undefined) {
303      return;
304    }
305    // Set initial state
306    submitButt.attr("disabled", !checkboxes.is(":checked"));
307    if (!checkboxes.is(":checked")) {
308      submitButt.addClass('disabled');
309    } else {
310      submitButt.removeClass('disabled');
311    }
312    checkboxes.click(function() {
313      // Update target state
314      submitButt.attr("disabled", !checkboxes.is(":checked"));
315      if (!checkboxes.is(":checked")) {
316        submitButt.addClass('disabled');
317      } else {
318        submitButt.removeClass('disabled');
319      }
320    });
321  },
322  hideLockable: function() {
323    $('div.lockable').each(function() {
324      var current_lockable_div = this;
325      $(this).find('p.form-note').hide();
326      $(this).find('input').each(function() {
327        this.disabled = true;
328        $(this).width(($(this).width() - 14) + 'px');
329        var imgE = document.createElement('img');
330        imgE.src = 'images/locker.png';
331        imgE.style.position = 'absolute';
332        imgE.style.top = '1.8em';
333        imgE.style.left = ($(this).width() + 14) + 'px';
334        imgE.alt = dotclear.msg.click_to_unlock;
335        $(imgE).css('cursor', 'pointer');
336        $(imgE).click(function() {
337          $(this).hide();
338          $(this).prev('input').each(function() {
339            this.disabled = false;
340            $(this).width(($(this).width() + 14) + 'px');
341          });
342          $(current_lockable_div).find('p.form-note').show();
343        });
344        $(this).parent().css('position', 'relative');
345        $(this).after(imgE);
346      });
347    });
348  },
349  checkboxesHelpers: function(e, target, c, s) {
350    $(e).append(document.createTextNode(dotclear.msg.to_select));
351    $(e).append(document.createTextNode(' '));
352    $('<button type="button" class="checkbox-helper select-all">' + dotclear.msg.select_all + '</button>').click(function() {
353      if (target !== undefined) {
354        target.check();
355      } else {
356        $(e).parents('form').find('input[type="checkbox"]').check();
357      }
358      if (c !== undefined && s !== undefined) {
359        dotclear.condSubmit(c, s);
360      }
361      return false;
362    }).appendTo($(e));
363    $(e).append(document.createTextNode(' '));
364    $('<button type="button" class="checkbox-helper select-none">' + dotclear.msg.no_selection + '</button>').click(function() {
365      if (target !== undefined) {
366        target.unCheck();
367      } else {
368        $(e).parents('form').find('input[type="checkbox"]').unCheck();
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-reverse">' + dotclear.msg.invert_sel + '</button>').click(function() {
377      if (target !== undefined) {
378        target.toggleCheck();
379      } else {
380        $(e).parents('form').find('input[type="checkbox"]').toggleCheck();
381      }
382      if (c !== undefined && s !== undefined) {
383        dotclear.condSubmit(c, s);
384      }
385      return false;
386    }).appendTo($(e));
387  },
388  postsActionsHelper: function() {
389    $('#form-entries').submit(function() {
390      var action = $(this).find('select[name="action"]').val();
391      if (action === undefined) {
392        return;
393      }
394      var checked = false;
395      $(this).find('input[name="entries[]"]').each(function() {
396        if (this.checked) {
397          checked = true;
398        }
399      });
400      if (!checked) {
401        return false;
402      }
403      if (action == 'delete') {
404        return window.confirm(dotclear.msg.confirm_delete_posts.replace('%s', $('input[name="entries[]"]:checked').size()));
405      }
406      return true;
407    });
408  },
409  commentsActionsHelper: function() {
410    $('#form-comments').submit(function() {
411      var action = $(this).find('select[name="action"]').val();
412      var checked = false;
413      $(this).find('input[name="comments[]"]').each(function() {
414        if (this.checked) {
415          checked = true;
416        }
417      });
418      if (!checked) {
419        return false;
420      }
421      if (action == 'delete') {
422        return window.confirm(dotclear.msg.confirm_delete_comments.replace('%s', $('input[name="comments[]"]:checked').size()));
423      }
424      return true;
425    });
426  }
427};
428/* On document ready
429-------------------------------------------------------- */
430$(function() {
431  // remove class no-js from html tag; cf style/default.css for examples
432  $('body').removeClass('no-js').addClass('with-js');
433  $('body').contents().each(function() {
434    if (this.nodeType == 8) {
435      var data = this.data;
436      data = data.replace(/ /g, '&nbsp;').replace(/\n/g, '<br/>');
437      $('<span class="tooltip">' + $('#footer a').prop('title') + data + '</span>').appendTo('#footer a');
438    }
439  });
440  // manage outgoing links
441  $('a').filter(function() {
442    return ((this.hostname && this.hostname != location.hostname && !$(this).hasClass('modal') && !$(this).hasClass('modal-image')) || $(this).hasClass('outgoing'));
443  }).each(function() {
444    $(this).prop('title', $(this).prop('title') + ' (' + dotclear.msg.new_window + ')');
445    if (!$(this).hasClass('outgoing')) {
446      $(this).append('&nbsp;<img src="images/outgoing-blue.png" alt=""/>');
447    }
448  }).click(function(e) {
449    e.preventDefault();
450    window.open($(this).attr('href'));
451  });
452  // Popups: dealing with Escape key fired
453  $('#dotclear-admin.popup').keyup(function(e) {
454    if (e.key == 'Escape') {
455      e.preventDefault();
456      window.close();
457      return false;
458    }
459  });
460  // Blog switcher
461  $('#switchblog').change(function() {
462    this.form.submit();
463  });
464  var menu_settings = {
465    img_on_src: dotclear.img_menu_off,
466    img_off_src: dotclear.img_menu_on,
467    legend_click: true,
468    speed: 100
469  }
470  $('#blog-menu h3:first').toggleWithLegend($('#blog-menu ul:first'), $.extend({
471    user_pref: 'dc_blog_menu'
472  }, menu_settings));
473  $('#system-menu h3:first').toggleWithLegend($('#system-menu ul:first'), $.extend({
474    user_pref: 'dc_system_menu'
475  }, menu_settings));
476  $('#plugins-menu h3:first').toggleWithLegend($('#plugins-menu ul:first'), $.extend({
477    user_pref: 'dc_plugins_menu'
478  }, menu_settings));
479  $('#favorites-menu h3:first').toggleWithLegend($('#favorites-menu ul:first'), $.extend({
480    user_pref: 'dc_favorites_menu',
481    hide: false,
482    reverse_user_pref: true
483  }, menu_settings));
484  $('#help').helpViewer();
485  // Notices
486  $('p.success,p.warning,p.error,div.error').each(function() {
487    $(this).addClass('close-notice-parent');
488    $(this).append('<button class="close-notice" type="button"><img src="images/close.png" alt="' + dotclear.msg.close_notice + '" /></button>');
489  });
490  $('button.close-notice').click(function(e) {
491    e.preventDefault();
492    $(this).parent().hide();
493  });
494  // Password
495  $('form:has(input[type=password][name=your_pwd])').submit(function() {
496    var e = this.elements['your_pwd'];
497    if (e.value == '') {
498      $(e).addClass('missing').focusout(function() {
499        $(this).removeClass('missing')
500      });
501      e.focus();
502      return false;
503    }
504    return true;
505  });
506  // Cope with ellipsis'ed cells
507  $('table .maximal').each(function() {
508    if (this.offsetWidth < this.scrollWidth) {
509      if (this.title == '') {
510        this.title = this.innerText;
511        $(this).addClass('ellipsis');
512      }
513    }
514  });
515  $('table .maximal.ellipsis a').each(function() {
516    if (this.title == '') {
517      this.title = this.innerText;
518    }
519  })
520  // Advanced users
521  if (dotclear.hideMoreInfo) {
522    $('.more-info,.form-note:not(.warn,.warning,.info)').addClass('no-more-info');
523  }
524  // Ajax loader activity indicator
525  if (dotclear.showAjaxLoader) {
526    $(document).ajaxStart(function() {
527      $('body').addClass('ajax-loader');
528      $('div.ajax-loader').show();
529    });
530    $(document).ajaxStop(function() {
531      $('body').removeClass('ajax-loader');
532      $('div.ajax-loader').hide();
533    });
534  }
535  // Main menu collapser
536  var objMain = $('#wrapper');
537
538  function showSidebar() {
539    // Show sidebar
540    objMain.removeClass('hide-mm');
541    $.cookie('sidebar-pref', null, {
542      expires: 30
543    });
544  }
545
546  function hideSidebar() {
547    // Hide sidebar
548    objMain.addClass('hide-mm');
549    $.cookie('sidebar-pref', 'hide-mm', {
550      expires: 30
551    });
552  }
553  // Sidebar separator
554  var objSeparator = $('#collapser');
555  objSeparator.click(function(e) {
556    e.preventDefault();
557    if (objMain.hasClass('hide-mm')) {
558      showSidebar();
559      $('#main-menu input#qx').focus();
560    } else {
561      hideSidebar();
562      $('#content a.go_home').focus();
563    }
564  });
565  if ($.cookie('sidebar-pref') == 'hide-mm') {
566    objMain.addClass('hide-mm');
567  } else {
568    objMain.removeClass('hide-mm');
569  }
570  // totop scroll
571  $(window).scroll(function() {
572    if ($(this).scrollTop() != 0) {
573      $('#gototop').fadeIn();
574    } else {
575      $('#gototop').fadeOut();
576    }
577  });
578  $('#gototop').click(function(e) {
579    $('body,html').animate({
580      scrollTop: 0
581    }, 800);
582    e.preventDefault();
583  });
584});
Note: See TracBrowser for help on using the repository browser.

Sites map