| 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 | var elt = document.getElementById(subhash); |
|---|
| 55 | // Tab displayed, now scroll to the sub-part if defined in original document.location (#tab.sub-part) |
|---|
| 56 | elt.scrollIntoView() |
|---|
| 57 | // Give focus to the sub-part if possible |
|---|
| 58 | $('#' + subhash).addClass('focus').focusout(function() { |
|---|
| 59 | $(this).removeClass('focus'); |
|---|
| 60 | }); |
|---|
| 61 | elt.focus(); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | return this; |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | var createTabs = function createTabs() { |
|---|
| 68 | var lis = [], |
|---|
| 69 | li_class = ''; |
|---|
| 70 | |
|---|
| 71 | $('.' + $.pageTabs.options.contentClass).each(function() { |
|---|
| 72 | $(this).hide(); |
|---|
| 73 | lis.push('<li id="' + $.pageTabs.options.idTabPrefix + $(this).attr('id') + '">' + |
|---|
| 74 | '<a href="#' + $(this).attr('id') + '">' + $(this).attr('title') + '</a></li>'); |
|---|
| 75 | $(this).attr('id', $.pageTabs.options.partPrefix + $(this).attr('id')).prop('title', ''); |
|---|
| 76 | }); |
|---|
| 77 | |
|---|
| 78 | $('<div class="' + $.pageTabs.options.containerClass + '"><ul>' + lis.join('') + '</ul></div>') |
|---|
| 79 | .insertBefore($('.' + $.pageTabs.options.contentClass).get(0)); |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | var getHash = function getHash(href) { |
|---|
| 83 | var href = href || ''; |
|---|
| 84 | |
|---|
| 85 | return href.replace(/.*#/, ''); |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | $.pageTabs.clickTab = function(tab) { |
|---|
| 89 | if (tab == '') { |
|---|
| 90 | tab = getHash($('ul li a', '.' + $.pageTabs.options.containerClass + ':eq(0)').attr('href')); |
|---|
| 91 | } else if ($('#' + $.pageTabs.options.idTabPrefix + tab, '.' + $.pageTabs.options.containerClass).length == 0) { |
|---|
| 92 | // try to find anchor in a .multi-part div |
|---|
| 93 | if ($('#' + tab).length == 1) { |
|---|
| 94 | var div_content = $('#' + tab).parents('.' + $.pageTabs.options.contentClass); |
|---|
| 95 | if (div_content.length == 1) { |
|---|
| 96 | tab = div_content.attr('id').replace($.pageTabs.options.partPrefix, ''); |
|---|
| 97 | } else { |
|---|
| 98 | tab = getHash($('ul li a', '.' + $.pageTabs.options.containerClass + ':eq(0)').attr('href')); |
|---|
| 99 | } |
|---|
| 100 | } else { |
|---|
| 101 | tab = getHash($('ul li a', '.' + $.pageTabs.options.containerClass + ':eq(0)').attr('href')); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | $('ul li a', '.' + $.pageTabs.options.containerClass).filter(function() { |
|---|
| 106 | return getHash($(this).attr('href')) == tab; |
|---|
| 107 | }).parent().click(); |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | $.pageTabs.getLocationHash = function() { |
|---|
| 111 | // Return the URL hash (without subhash — #hash[.subhash]) |
|---|
| 112 | var h = getHash(document.location.hash).split('.'); |
|---|
| 113 | return h[0]; |
|---|
| 114 | }; |
|---|
| 115 | $.pageTabs.getLocationSubhash = function() { |
|---|
| 116 | // Return the URL subhash if present (without hash — #hash[.subhash]) |
|---|
| 117 | var sh = getHash(document.location.hash).split('.'); |
|---|
| 118 | return sh[1]; |
|---|
| 119 | }; |
|---|
| 120 | })(jQuery); |
|---|
| 121 | |
|---|
| 122 | jQuery.fn.tabload = function(f) { |
|---|
| 123 | this.each(function() { |
|---|
| 124 | if (f) { |
|---|
| 125 | chainHandler(this, 'tabload', f) |
|---|
| 126 | } else { |
|---|
| 127 | var h = this.tabload; |
|---|
| 128 | if (h) { |
|---|
| 129 | h.apply(this); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | }); |
|---|
| 133 | return this; |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | jQuery.fn.onetabload = function(f) { |
|---|
| 137 | this.each(function() { |
|---|
| 138 | if (f) { |
|---|
| 139 | chainHandler(this, 'onetabload', f); |
|---|
| 140 | } else { |
|---|
| 141 | var h = this.onetabload; |
|---|
| 142 | if (h != null) { |
|---|
| 143 | h.apply(this); |
|---|
| 144 | this.onetabload = null; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | }); |
|---|
| 148 | return this; |
|---|
| 149 | }; |
|---|