Dotclear


Ignore:
Timestamp:
09/22/13 16:17:26 (12 years ago)
Author:
Nicolas <nikrou77@…>
Branch:
default
Message:

Rewrite pageTabs plugin
Remove dependency with hashchange plugin
Fix issue : when click on tab focus must not follow anchor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin/js/jquery/jquery.pageTabs.js

    r1792 r2010  
    1 jQuery.pageTabs = function(start_tab,settings) { 
    2      return new jQuery._pageTabs(start_tab,settings); 
    3 }; 
     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          var options = $.extend({}, defaults, opts); 
     14          var active_tab = start_tab || ''; 
     15          var hash = $.pageTabsGetHash(); 
    416 
    5 jQuery._pageTabs = function(start_tab,settings) { 
    6      var defaults = { 
    7           className: 'multi-part', 
    8           listClassName: 'part-tabs', 
    9           breakerClassName: 'clear' 
     17          if (hash !== undefined && hash) { 
     18               $('ul li a[href$="#'+hash+'"]').parent().trigger('click'); 
     19               active_tab = hash; 
     20          } else { // open first part 
     21               active_tab = $('.'+options.contentClass+':eq(0)').attr('id'); 
     22          } 
     23           
     24          createTabs(active_tab, options); 
     25           
     26          $('ul li', '.'+options.containerClass).click(function(e) { 
     27               $(this).parent().find('li.'+options.activeClass).removeClass(options.activeClass); 
     28               $(this).addClass(options.activeClass); 
     29               $('.'+options.contentClass+'.active').removeClass('active').hide(); 
     30               $('#'+options.partPrefix+getId($(this).find('a').attr('href'))).addClass('active').show(); 
     31          }); 
     32           
     33          return this; 
     34     }; 
     35      
     36     var createTabs = function createTabs(start_tab, options) { 
     37          var lis = [], li_class = '', to_trigger = null; 
     38           
     39          $('.'+options.contentClass).each(function() { 
     40               if (start_tab != $(this).attr('id')) { 
     41                    $(this).hide(); 
     42                    li_class = ''; 
     43               } else { 
     44                    $(this).addClass('active'); 
     45                    to_trigger = $(this); 
     46                    li_class = ' class="'+options.activeClass+'"'; 
     47               } 
     48               lis.push('<li id="'+options.idTabPrefix+$(this).attr('id')+'"'+li_class 
     49                     +'><a href="#'+$(this).attr('id')+'">'+$(this).attr('title')+'</a></li>'); 
     50               $(this).attr('id', options.partPrefix + $(this).attr('id')); 
     51          }); 
     52           
     53          $('<div class="'+options.containerClass+'"><ul>'+lis.join('')+'</ul></div>') 
     54               .insertBefore($('.'+options.contentClass).get(0));      
     55 
     56          if (to_trigger != null) { 
     57               $(to_trigger).onetabload(); 
     58               $(to_trigger).tabload();  
     59          } 
     60 
     61     }; 
     62      
     63     var getId = function getId(href) { 
     64          return href.split('#').join(''); 
    1065     }; 
    1166 
    12      var index = start_tab ? start_tab : 0; 
    13      var hash = document.location.hash.split('#').join(''); 
    14      if( hash != '' ) { 
    15           var index = hash; 
    16      } 
    17       
    18      this.params = jQuery.extend(defaults,settings); 
    19      this.divs = jQuery('div.'+this.params.className); 
    20      this.createList(); 
    21      this.showDiv(index); 
    22      var pageTabs = this; 
    23       
    24      window.onhashchange = function (event) { 
    25           pageTabs.showDiv(document.location.hash.split('#').join('')); 
    26     } 
    27 }; 
    28  
    29 jQuery._pageTabs.prototype = { 
    30      items: new Array(), 
    31  
    32      createList: function() { 
    33           if (this.divs.length <= 0) { 
    34                return; 
    35           } 
    36  
    37           this.block = document.createElement('div'); 
    38           this.block.className = this.params.listClassName; 
    39           this.list = document.createElement('ul'); 
    40           //this.list.className = this.params.listClassName; 
    41           this.block.appendChild(this.list); 
    42           var li, a; 
    43  
    44           var This = this; 
    45           var i=0; 
    46           jQuery('.'+this.params.className).each(function() { 
    47                if (this.tagName == "DIV") { 
    48                     li = document.createElement('li'); 
    49                     a = document.createElement('a'); 
    50                     $(a).html(this.title); 
    51                     this.title = ''; 
    52                     a.fn = This.showDiv; 
    53                     a.index = this.id || i; 
    54                     a.href = '#'+a.index; 
    55                     li.id = "part-tabs-"+a.index; 
    56                     a.obj = This; 
    57                     li.appendChild(a); 
    58                     This.list.appendChild(li); 
    59                     This.items[i] = li; 
    60                     i++; 
    61                } else { 
    62                     li = document.createElement('li'); 
    63                     li.className = This.params.listClassName+'-link'; 
    64                     li.appendChild(this); 
    65                     This.list.appendChild(li); 
    66                } 
    67           }); 
    68  
    69           this.breaker = document.createElement('br'); 
    70           this.breaker.className = this.params.breakerClassName; 
    71  
    72           jQuery(this.divs.get(0)).before(this.block); 
    73           jQuery(this.block).after(this.breaker); 
    74      }, 
    75  
    76      showDiv: function(index) { 
    77           var This = this; 
    78           var i = 0; 
    79           var to_trigger = null; 
    80           var exists = false; 
    81  
    82           this.divs.each(function() { 
    83                if ((this.id != '' && this.id == index) || i == index) { 
    84                     exists = true; 
    85                } 
    86                i++; 
    87           }); 
    88            
    89           i = 0; 
    90            
    91           if( exists ) { 
    92                this.divs.each(function() { 
    93                     if ((this.id != '' && this.id == index) || i == index) { 
    94                          jQuery(this).show(0); 
    95                          This.items[i].className = This.params.listClassName+'-active'; 
    96                          to_trigger = i; 
    97                     } else { 
    98                          jQuery(this).hide(0); 
    99                          This.items[i].className = ''; 
    100                     } 
    101       
    102                     i++; 
    103                }); 
    104           } 
    105  
    106           if (to_trigger != null) { 
    107                jQuery(this.divs[to_trigger]).onetabload(); 
    108                jQuery(this.divs[to_trigger]).tabload(); 
    109           } 
    110      } 
    111 }; 
     67     $.pageTabsGetHash = function() { 
     68          return document.location.hash.split('#').join(''); 
     69     }; 
     70})(jQuery); 
    11271 
    11372jQuery.fn.tabload = function(f) { 
     
    12281     return this; 
    12382}; 
     83 
    12484jQuery.fn.onetabload = function(f) { 
    12585     this.each(function() { 
Note: See TracChangeset for help on using the changeset viewer.

Sites map