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(); |
---|
16 | |
---|
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')).prop('title',''); |
---|
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(''); |
---|
65 | }; |
---|
66 | |
---|
67 | $.pageTabsGetHash = function() { |
---|
68 | return document.location.hash.split('#').join(''); |
---|
69 | }; |
---|
70 | })(jQuery); |
---|
71 | |
---|
72 | jQuery.fn.tabload = function(f) { |
---|
73 | this.each(function() { |
---|
74 | if (f) { |
---|
75 | chainHandler(this,'tabload',f) |
---|
76 | } else { |
---|
77 | var h = this.tabload; |
---|
78 | if (h) { h.apply(this); } |
---|
79 | } |
---|
80 | }); |
---|
81 | return this; |
---|
82 | }; |
---|
83 | |
---|
84 | jQuery.fn.onetabload = function(f) { |
---|
85 | this.each(function() { |
---|
86 | if (f) { |
---|
87 | chainHandler(this,'onetabload',f); |
---|
88 | } else { |
---|
89 | var h = this.onetabload; |
---|
90 | if (h != null) { |
---|
91 | h.apply(this); |
---|
92 | this.onetabload = null; |
---|
93 | } |
---|
94 | } |
---|
95 | }); |
---|
96 | return this; |
---|
97 | }; |
---|