Dotclear

source: admin/js/jquery/jquery.pageTabs.js @ 3333:11107ba2fc59

Revision 3333:11107ba2fc59, 4.1 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Cope with settings URLs for modules (defined in _define.php).
The settings URLs are displayed on the plugins maganement page, and at the bottom of each plugin main page if any (index.php).

The URLs are set in _define.php, as a new property using this schema:

'settings' => array( <list of URLs> )

With:

<list of URLs> = '<type>' => '<location>', …
<type> = 'self' (own plugin page), 'blog' (in blog parameters page) or 'pref' (in user preferences page)
<location> = (empty) or #<tab>[.<id>] with <tab> = id of the corresponding tab, and <id> = id of fieldset, h4, h5, field, … of first corresponding field

The list of URLs are displayed in the order defined in the array above.

Examples:

Antispam plugin:

'settings' => array(

'self' => ,
'blog' => '#params.antispam_params'

)

self → for main settings of the plugin on its own page (index.php)
blog → for secondary settings in the blog parameters

Tags plugin:

'settings' => array(

'pref' => '#user-options.tags_prefs'

)

pref → for tags list format in user preferences

Maintenance plugin:

'settings' => array(

'self' => '#settings'

)

self → "settings" tab of its own page (index.php)

Line 
1(function($) {
2     'use strict';
3
4     $.pageTabs = function(start_tab, opts) {
5          var defaults = {
6               containerClass: 'part-tabs',
7               partPrefix: 'part-',
8               contentClass: 'multi-part',
9               activeClass: 'part-tabs-active',
10               idTabPrefix: 'part-tabs-'
11          };
12
13          $.pageTabs.options = $.extend({}, defaults, opts);
14          var active_tab = start_tab || '';
15          var hash = $.pageTabs.getLocationHash();
16          var subhash = $.pageTabs.getLocationSubhash();
17
18          if (hash !== undefined && hash) {
19               window.scrollTo(0,0);
20               active_tab = hash;
21          } else if (active_tab == '') { // open first part
22               active_tab = $('.'+$.pageTabs.options.contentClass+':eq(0)').attr('id');
23          }
24
25          createTabs();
26
27          $('ul li', '.'+$.pageTabs.options.containerClass).click(function(e) {
28               if ($(this).hasClass($.pageTabs.options.activeClass)) {
29                    return;
30               }
31
32               $(this).parent().find('li.'+$.pageTabs.options.activeClass).removeClass($.pageTabs.options.activeClass);
33               $(this).addClass($.pageTabs.options.activeClass);
34               $('.'+$.pageTabs.options.contentClass+'.active').removeClass('active').hide();
35
36               var part_to_activate = $('#'+$.pageTabs.options.partPrefix+getHash($(this).find('a').attr('href')));
37
38               part_to_activate.addClass('active').show();
39               if (!part_to_activate.hasClass('loaded')) {
40                    part_to_activate.onetabload();
41                    part_to_activate.addClass('loaded');
42               }
43
44               part_to_activate.tabload();
45          });
46
47          $(window).bind('hashchange onhashchange', function(e) {
48               $.pageTabs.clickTab($.pageTabs.getLocationHash());
49          });
50
51          $.pageTabs.clickTab(active_tab);
52
53          if (subhash !== undefined) {
54               // Tab displayed, now scroll to the sub-part if defined in original document.location (#tab.sub-part)
55               document.getElementById(subhash).scrollIntoView();
56          }
57
58          return this;
59     };
60
61     var createTabs = function createTabs() {
62          var lis = [], li_class = '';
63
64          $('.'+$.pageTabs.options.contentClass).each(function() {
65               $(this).hide();
66               lis.push('<li id="'+$.pageTabs.options.idTabPrefix+$(this).attr('id')+'">'
67                     +'<a href="#'+$(this).attr('id')+'">'+$(this).attr('title')+'</a></li>');
68               $(this).attr('id', $.pageTabs.options.partPrefix + $(this).attr('id')).prop('title','');
69          });
70
71          $('<div class="'+$.pageTabs.options.containerClass+'"><ul>'+lis.join('')+'</ul></div>')
72               .insertBefore($('.'+$.pageTabs.options.contentClass).get(0));
73     };
74
75     var getHash = function getHash(href) {
76          var href = href || '';
77
78          return href.replace(/.*#/, '');
79     };
80
81     $.pageTabs.clickTab = function(tab) {
82          if (tab=='') {
83               tab = getHash($('ul li a', '.'+$.pageTabs.options.containerClass+':eq(0)').attr('href'));
84          } else if ($('#'+$.pageTabs.options.idTabPrefix+tab, '.'+$.pageTabs.options.containerClass).length==0) {
85               // try to find anchor in a .multi-part div
86               if ($('#'+tab).length==1) {
87                    var div_content = $('#'+tab).parents('.'+$.pageTabs.options.contentClass);
88                    if (div_content.length==1) {
89                         tab = div_content.attr('id').replace($.pageTabs.options.partPrefix,'');
90                    } else {
91                         tab = getHash($('ul li a', '.'+$.pageTabs.options.containerClass+':eq(0)').attr('href'));
92                    }
93               } else {
94                    tab = getHash($('ul li a', '.'+$.pageTabs.options.containerClass+':eq(0)').attr('href'));
95               }
96          }
97
98          $('ul li a', '.'+$.pageTabs.options.containerClass).filter(function() {
99               return getHash($(this).attr('href'))==tab;
100          }).parent().click();
101     };
102
103     $.pageTabs.getLocationHash = function() {
104          // Return the URL hash (without subhash — #hash[.subhash])
105          var h = getHash(document.location.hash).split('.');
106          return h[0];
107     };
108     $.pageTabs.getLocationSubhash = function() {
109          // Return the URL subhash if present (without hash — #hash[.subhash])
110          var sh = getHash(document.location.hash).split('.');
111          return sh[1];
112     };
113})(jQuery);
114
115jQuery.fn.tabload = function(f) {
116     this.each(function() {
117          if (f) {
118               chainHandler(this,'tabload',f)
119          } else {
120               var h = this.tabload;
121               if (h) { h.apply(this); }
122          }
123     });
124     return this;
125};
126
127jQuery.fn.onetabload = function(f) {
128     this.each(function() {
129          if (f) {
130               chainHandler(this,'onetabload',f);
131          } else {
132               var h = this.onetabload;
133               if (h != null) {
134                    h.apply(this);
135                    this.onetabload = null;
136               }
137          }
138     });
139     return this;
140};
Note: See TracBrowser for help on using the repository browser.

Sites map