Dotclear

source: admin/js/common.js @ 2225:7d94f2597021

Revision 2225:7d94f2597021, 13.4 KB checked in by Nicolas <nikrou77@…>, 12 years ago (diff)

Open outgoing links in new window and append "new window" to title attribute only if javascript is enabled
Closes #1586

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
153jQuery.fn.helpViewer = function() {
154     if (this.length < 1) {
155          return this;
156     }
157
158     var p = {
159          img_on_src: dotclear.img_plus_src,
160          img_on_alt: dotclear.img_plus_alt,
161          img_off_src: dotclear.img_minus_src,
162          img_off_alt: dotclear.img_minus_alt
163     };
164     var This = this;
165     var toggle = function() {
166          $('#content').toggleClass('with-help');
167          if (document.all) {
168               if ($('#content').hasClass('with-help')) {
169                    select = $('#content select:visible').hide();
170               } else {
171                    select.show();
172               }
173          }
174          $('p#help-button span').text($('#content').hasClass('with-help') ? dotclear.msg.help_hide : dotclear.msg.help);
175          sizeBox();
176          return false;
177     };
178
179     var sizeBox = function() {
180          This.css('height','auto');
181          if ($('#wrapper').height() > This.height()) {
182               This.css('height',$('#wrapper').height() + 'px');
183          }
184     };
185
186     var textToggler = function(o) {
187          var i = $('<img src="'+p.img_on_src+'" alt="'+p.img_on_alt+'" />');
188          o.css('cursor','pointer');
189          var hide = true;
190
191          o.prepend(' ').prepend(i);
192          o.click(function() {
193               $(this).nextAll().each(function() {
194                    if ($(this).is('h4')) {
195                         return false;
196                    }
197                    $(this).toggle();
198                    sizeBox();
199                    return true;
200               });
201               hide = !hide;
202               var img = $(this).find('img');
203               if (!hide) {
204                    img.attr('src',p.img_off_src);
205               } else {
206                    img.attr('src',p.img_on_src);
207               }
208          });
209     };
210
211     this.addClass('help-box');
212     this.find('>hr').remove();
213
214     this.find('h4').each(function() { textToggler($(this)); });
215     this.find('h4:first').nextAll('*:not(h4)').hide();
216     sizeBox();
217
218     var img = $('<p id="help-button"><span>'+dotclear.msg.help+'</span></p>');
219     var select = $();
220     img.click(function() { return toggle(); });
221
222     $('#content').append(img);
223     
224     // listen for scroll
225     var peInPage = $('#help-button').offset().top;
226     $('#help-button').addClass("floatable");
227     var peInFloat = $('#help-button').offset().top;
228     $('#help-button').removeClass("floatable");
229     $(window).scroll(
230          function() {
231               if ($(window).scrollTop() >= peInPage - peInFloat ) {
232                    $('#help-button').addClass("floatable");
233               } else {
234                    $('#help-button').removeClass("floatable");
235               }
236          }
237     );
238
239     return this;
240};
241
242
243/* Dotclear common object
244-------------------------------------------------------- */
245var dotclear = {
246     msg: {},
247
248     hideLockable: function() {
249          $('div.lockable').each(function() {
250               var current_lockable_div = this;
251               $(this).find('p.form-note').hide();
252               $(this).find('input').each(function() {
253                    this.disabled = true;
254                    $(this).width(($(this).width()-14) + 'px');
255
256                    var imgE = document.createElement('img');
257                    imgE.src = 'images/locker.png';
258                    imgE.style.position = 'absolute';
259                    imgE.style.top = '1.7em';
260                    imgE.style.left = ($(this).width()+12)+'px';
261                    imgE.alt=dotclear.msg.click_to_unlock;
262                    $(imgE).css('cursor','pointer');
263
264                    $(imgE).click(function() {
265                         $(this).hide();
266                         $(this).prev('input').each(function() {
267                              this.disabled = false;
268                              $(this).width(($(this).width()+14) + 'px');
269                         });
270                         $(current_lockable_div).find('p.form-note').show();
271                    });
272
273                    $(this).parent().css('position','relative');
274                    $(this).after(imgE);
275               });
276          });
277     },
278
279     checkboxesHelpers: function(e, target) {
280          $(e).append(document.createTextNode(dotclear.msg.to_select));
281          $(e).append(document.createTextNode(' '));
282
283          $('<a href="#">'+dotclear.msg.select_all+'</a>').click(function() {
284               if (target !== undefined) {
285                    target.check();
286               } else {
287                    $(e).parents('form').find('input[type="checkbox"]').check();
288               }
289
290               return false;
291          }).appendTo($(e));
292          $(e).append(document.createTextNode(' | '));
293
294          $('<a href="#">'+dotclear.msg.no_selection+'</a>').click(function() {
295               if (target !== undefined) {
296                    target.unCheck();
297               } else {
298                    $(e).parents('form').find('input[type="checkbox"]').unCheck();
299               }
300
301               return false;
302          }).appendTo($(e));
303          $(e).append(document.createTextNode(' - '));
304
305          $('<a href="#">'+dotclear.msg.invert_sel+'</a>').click(function() {
306               if (target !== undefined) {
307                    target.toggleCheck();
308               } else {
309                    $(e).parents('form').find('input[type="checkbox"]').toggleCheck();
310               }
311
312               return false;
313          }).appendTo($(e));
314     },
315
316     postsActionsHelper: function() {
317          $('#form-entries').submit(function() {
318               var action = $(this).find('select[name="action"]').val();
319               var checked = false;
320
321               $(this).find('input[name="entries[]"]').each(function() {
322                    if (this.checked) {
323                         checked = true;
324                    }
325               });
326
327               if (!checked) { return false; }
328
329               if (action == 'delete') {
330                    return window.confirm(dotclear.msg.confirm_delete_posts.replace('%s',$('input[name="entries[]"]:checked').size()));
331               }
332
333               return true;
334          });
335     },
336
337     commentsActionsHelper: function() {
338          $('#form-comments').submit(function() {
339               var action = $(this).find('select[name="action"]').val();
340               var checked = false;
341
342               $(this).find('input[name="comments[]"]').each(function() {
343                    if (this.checked) {
344                         checked = true;
345                    }
346               });
347
348               if (!checked) { return false; }
349
350               if (action == 'delete') {
351                    return window.confirm(dotclear.msg.confirm_delete_comments.replace('%s',$('input[name="comments[]"]:checked').size()));
352               }
353
354               return true;
355          });
356     },
357     getCSSColor : function ( clazz) {
358          $('<div class="'+clazz+'" id="dotclear-obj-test-color" style="display:none"></div>').appendTo(document.body);
359          var tag2 = $('#dotclear-obj-test-color');
360          var color = $.trim(tag2.css("color").toLowerCase());
361          tag2.remove();
362          if ( color.charAt(0) === '#') {
363               return color;
364          }
365          var result = /^rgb\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\)$/.exec(color);       
366          if ( result === null) {
367               return '';
368          }
369          var ret = '#';
370          for ( var i = 1; i < 4; i++) {
371               var val = parseInt(result[i],10);
372               ret += (val < 16 ? '0'+val.toString(16) : val.toString(16));
373          }
374          return ret;
375     },
376     initFadeColor : function() {
377          dotclear.fadeColor = {
378               beginPassword : dotclear.getCSSColor('colorBeginPassword'),
379               endPassword : dotclear.getCSSColor('colorEndPassword'),
380               beginMessage : dotclear.getCSSColor('colorBeginMessage'),
381               endMessage : dotclear.getCSSColor('colorEndMessage'),
382               beginError : dotclear.getCSSColor('colorBeginError'),
383               endError : dotclear.getCSSColor('colorEndError'),
384               beginSuccess : dotclear.getCSSColor('colorBeginSuccess'),
385               endSuccess : dotclear.getCSSColor('colorEndSuccess'),
386               beginValidatorMsg : dotclear.getCSSColor('colorBeginValidatorMsg'),
387               endValidatorMsg : dotclear.getCSSColor('colorEndValidatorMsg'),
388               beginValidatorErr : dotclear.getCSSColor('colorBeginValidatorErr'),
389               endValidatorErr : dotclear.getCSSColor('colorEndValidatorErr'),
390               beginUserMail : dotclear.getCSSColor('colorBeginUserMail'),
391               endUserMail : dotclear.getCSSColor('colorEndUserMail')
392          };
393     }};
394
395/* On document ready
396-------------------------------------------------------- */
397$(function() {
398     dotclear.initFadeColor();
399     // remove class no-js from html tag; cf style/default.css for examples
400     $('body').removeClass('no-js').addClass('with-js');
401     
402     $('#wrapper').contents().each(function() {
403          if (this.nodeType==8) {
404               $('#footer a[href!="help.php"]').attr('title', $('#footer a[href!="help.php"]').attr('title') + this.data );
405          }
406     });
407
408     // manage outgoing links
409     $('.outgoing').each(function() {
410          $(this).prop('title',$(this).attr('title')+' ('+dotclear.msg.new_window+')');
411     }).click(function(e) {
412          e.preventDefault();
413          window.open($(this).attr('href'));
414     });
415
416     // Blog switcher
417     $('#switchblog').change(function() {
418          this.form.submit();
419     });
420
421     var menu_settings = {
422          img_on_src: dotclear.img_menu_off,
423          img_off_src: dotclear.img_menu_on,
424          legend_click: true,
425          speed: 100
426     }
427     $('#blog-menu h3:first').toggleWithLegend($('#blog-menu ul:first'),
428          $.extend({user_pref:'dc_blog_menu'},menu_settings)
429     );
430     $('#system-menu h3:first').toggleWithLegend($('#system-menu ul:first'),
431          $.extend({user_pref:'dc_system_menu'},menu_settings)
432     );
433     $('#plugins-menu h3:first').toggleWithLegend($('#plugins-menu ul:first'),
434          $.extend({user_pref:'dc_plugins_menu'},menu_settings)
435     );
436     $('#favorites-menu h3:first').toggleWithLegend($('#favorites-menu ul:first'),
437          $.extend({user_pref:'dc_favorites_menu',hide:false,reverse_user_pref:true},menu_settings)
438     );
439
440     $('#help').helpViewer();
441
442     $('.message').backgroundFade({sColor: dotclear.fadeColor.beginMessage, eColor: dotclear.fadeColor.endMessage, steps:20});
443     $('.error').backgroundFade({sColor: dotclear.fadeColor.beginError, eColor: dotclear.fadeColor.endError, steps:20});
444     $('.success').backgroundFade({sColor: dotclear.fadeColor.beginSuccess, eColor: dotclear.fadeColor.endSuccess, steps:20});
445
446     $('form:has(input[type=password][name=your_pwd])').submit(function() {
447          var e = this.elements['your_pwd'];
448          if (e.value == '') {
449               e.focus();
450               $(e).backgroundFade({sColor: dotclear.fadeColor.beginPassword,eColor: dotclear.fadeColor.endPassword,steps:50},function() {
451                    $(this).backgroundFade({sColor: dotclear.fadeColor.endPassword,eColor: dotclear.fadeColor.beginPassword});
452               });
453               return false;
454          }
455          return true;
456     });
457
458     // Main menu collapser
459    var objMain = $('#wrapper');
460    function showSidebar(){
461         // Show sidebar
462        objMain.removeClass('hide-mm');
463        $.cookie('sidebar-pref',null,{expires:30});
464    }
465    function hideSidebar(){
466         // Hide sidebar
467        objMain.addClass('hide-mm');
468        $.cookie('sidebar-pref','hide-mm',{expires:30});
469    }
470    // Sidebar separator
471    var objSeparator = $('#collapser');
472    objSeparator.click(function(e){
473        e.preventDefault();
474        if ( objMain.hasClass('hide-mm') ){
475            showSidebar();
476        }
477        else {
478            hideSidebar();
479        }
480    });
481     if ( $.cookie('sidebar-pref') == 'hide-mm' ){
482          objMain.addClass('hide-mm');
483     } else {
484          objMain.removeClass('hide-mm');
485     }
486});
Note: See TracBrowser for help on using the repository browser.

Sites map