-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-tabs.js
258 lines (220 loc) · 9.49 KB
/
jquery-tabs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* jQuery Chaos Tabs
* By Matthew Sigley
* Version 1.5.0
*/
(function( $ ) {
$.resizeChaosTabsEvent = function(e) {
cancelAnimationFrame( e.data.tabContainer.get(0).chaosTabsResizeFrame );
e.data.tabContainer.get(0).chaosTabsResizeFrame = requestAnimationFrame( e.data.tabContainer.get(0).resizeChaosTabs ); //Run event handler when not busy
}
$.clickArrowChaosTabsEvent = function(e) {
var tabMenuWrap = e.data.tabMenuWrap,
arrowDirection = e.data.arrowDirection;
tabMenuWrap.stop();
if (arrowDirection == 'left') {
tabMenuWrap.animate({'scrollLeft': tabMenuWrap.scrollLeft() - 100}, 100);
} else if (arrowDirection == 'right') {
tabMenuWrap.animate({'scrollLeft': tabMenuWrap.scrollLeft() + 100}, 100);
}
}
$.fn.chaosTabsCacheTabProperties = function() {
var thisElementObj = this.get(0),
tabMenuWrap = this.find('.tab-menu-wrap:first'),
tabMenu = this.find('.tab-menu:first'),
tabMenuWidth = tabMenu.width(),
isTabMenuWrapBorderBox = tabMenuWrap.css( "box-sizing" ) === "border-box",
isTabMenuBorderBox = tabMenu.css( "box-sizing" ) === "border-box",
tabArrows = this.find('.arrow');
if( isTabMenuWrapBorderBox )
tabMenuWrap.css({'padding-left': '', 'padding-right': ''});
var tabMenuWrapPaddingLeft = parseFloat( tabMenuWrap.css('padding-left') ),
tabMenuWrapPaddingRight = parseFloat( tabMenuWrap.css('padding-right') );
if( !isNaN(tabMenuWrapPaddingLeft) && isFinite(tabMenuWrapPaddingLeft) )
tabMenuWrapPaddingLeft = 0;
if( !isNaN(tabMenuWrapPaddingRight) && isFinite(tabMenuWrapPaddingRight) )
tabMenuWrapPaddingRight = 0;
if( isTabMenuWrapBorderBox )
tabMenuWrap.css({'padding-left': tabMenuWrapPaddingLeft, 'padding-right': tabMenuWrapPaddingRight});
// Cache calculated values to element obj
thisElementObj.tabMenuWrap = tabMenuWrap;
thisElementObj.tabMenu = tabMenu;
thisElementObj.tabMenuWidth = tabMenuWidth;
thisElementObj.isTabMenuWrapBorderBox = isTabMenuWrapBorderBox,
thisElementObj.isTabMenuBorderBox = isTabMenuBorderBox;
thisElementObj.tabArrows = tabArrows;
thisElementObj.tabMenuWrapPaddingLeft = tabMenuWrapPaddingLeft;
thisElementObj.tabMenuWrapPaddingRight = tabMenuWrapPaddingRight;
};
$.fn.chaosTabsAttachArrowEvents = function() {
var thisElementObj = this.get(0),
args = thisElementObj.chaosTabsArgs,
tabArrows = thisElementObj.tabArrows,
tabMenuWrap = thisElementObj.tabMenuWrap,
tabMenu = thisElementObj.tabMenu;
// Assume left arrow is index 0 and right arrow is index 1
// jQuery always returns collections in document order
tabArrows.eq(0).on('click', {tabMenuWrap: tabMenuWrap, arrowDirection: 'left'}, $.clickArrowChaosTabsEvent);
tabArrows.eq(1).on('click', {tabMenuWrap: tabMenuWrap, arrowDirection: 'right'}, $.clickArrowChaosTabsEvent);
//Scroll tabmenu items into view when they are clicked
tabMenu.find('li').on('click', function() {
var thisElement = $(this);
thisPosition = thisElement.position();
tabMenuWrap.stop().animate({'scrollLeft': thisPosition['left'] - args['arrowLeftPadding']}, 100);
});
};
$.fn.chaosTabsHandleOverflowArrows = function() {
var thisElementObj = this.get(0),
args = thisElementObj.chaosTabsArgs,
tabArrowsShown = thisElementObj.chaosTabsArrowsShown || false,
windowWidth = $(window).width();
if (typeof thisElementObj.chaosTabsLastWindowWidth != 'undefined') {
if (thisElementObj.chaosTabsLastWindowWidth == windowWidth
|| (thisElementObj.chaosTabsLastWindowWidth < windowWidth && !tabArrowsShown)
|| (thisElementObj.chaosTabsLastWindowWidth > windowWidth && tabArrowsShown) ) {
thisElementObj.chaosTabsArrowsShown = tabArrowsShown
thisElementObj.chaosTabsLastWindowWidth = windowWidth;
return;
}
}
var tabMenuWrap = thisElementObj.tabMenuWrap,
tabMenu = thisElementObj.tabMenu,
tabMenuWrapWidth = tabMenuWrap.width(),
tabMenuWidth = thisElementObj.tabMenuWidth,
isTabMenuWrapBorderBox = thisElementObj.isTabMenuWrapBorderBox,
isTabMenuBorderBox = thisElementObj.isTabMenuBorderBox,
tabMenuWrapPaddingLeft = thisElementObj.tabMenuWrapPaddingLeft,
tabMenuWrapPaddingRight = thisElementObj.tabMenuWrapPaddingRight,
tabArrows = thisElementObj.tabArrows;
if (isTabMenuWrapBorderBox && tabArrowsShown) {
tabMenuWrapWidth -= tabMenuWrapPaddingLeft + tabMenuWrapPaddingRight;
}
if (isTabMenuBorderBox && tabArrowsShown)
tabMenuWidth -= args['arrowRightPadding'] + args['arrowLeftPadding'];
if (tabMenuWidth > tabMenuWrapWidth && !tabArrowsShown) {
tabMenu.data({
'padding-right': tabMenu.css('padding-right'),
'padding-left': tabMenu.css('padding-left')
});
tabMenu.css({
'padding-right': '+='+args['arrowRightPadding'],
'padding-left': '+='+args['arrowLeftPadding']
});
tabArrows.show().css('content-visibility', 'visible');
tabArrowsShown = true;
} else if (tabMenuWidth <= tabMenuWrapWidth && tabArrowsShown) {
tabArrows.hide().css('content-visibility', 'hidden');
tabMenu.css({
'padding-right': tabMenu.data('padding-right'),
'padding-left': tabMenu.data('padding-left')
});
tabArrowsShown = false;
}
thisElementObj.chaosTabsArrowsShown = tabArrowsShown
thisElementObj.chaosTabsLastWindowWidth = windowWidth;
}
$.fn.chaosTabs = function(args) {
//Default args
var defaults = { arrows: false, arrowRightPadding: 20, arrowLeftPadding: 20, callback: false };
args = $.extend({}, defaults, args);
var callback = args['callback'];
var windowElement = $(window),
parentElement = this.parent();
//Minimize CSS recalulations from tab setup
this.css('contain', 'layout paint style');
this.css('position', 'relative');
//Parse HTML and setup tabs
var anchorOutput = '',
menuOutput = '',
contentElements = new Array();
this.children('.tab').each(function(index, e) {
var eElement = $(e),
title = eElement.find('.tab-title'),
titleText = title.text(),
titleFragment = encodeURIComponent( titleText.replace('?','') ),
content = eElement.find('.tab-content');
title.hide().css('content-visibility', 'hidden');
content.hide().css('content-visibility', 'hidden');
if( !titleFragment )
return;
if( $('[id="tab-'+titleFragment+'"], a[name="tab-'+titleFragment+'"]').length == 0 ) {
anchorOutput += '<div id="tab-'+titleFragment+'" style="position: absolute; top: -100px;"></div>';
}
menuOutput += '<li '+((index === 0) ? 'class="first"' : '')+'>'
+'<a href="#tab-'+titleFragment+'" rel="#tab-'+index+'">'+titleText+'</a></li>';
content.attr('id', 'tab-'+index);
});
menuOutput = '<ul class="tab-menu clearfix">'+menuOutput+'</ul>';
if( args['arrows'] ) {
menuOutput = '<div class="tab-menu-wrap" '
+'style="box-sizing: border-box; overflow-x: auto; overflow-y:hidden; -webkit-overflow-scrolling:touch;">'
+menuOutput+'</div>';
menuOutput = '<div class="tab-arrow-wrap">'
+'<a class="arrow-left arrow" style="display: none;">◄</a>'
+menuOutput
+'<a class="arrow-right arrow" style="display: none;">►</a></div>';
} else {
menuOutput = '<div class="tab-menu-wrap">'+menuOutput+'</div>';
}
this.prepend(anchorOutput + menuOutput);
//Cache args on tabContainer element
parentElement.get(0).chaosTabsArgs = args;
if( args['arrows'] ) {
parentElement.chaosTabsCacheTabProperties();
parentElement.chaosTabsAttachArrowEvents();
parentElement.chaosTabsHandleOverflowArrows();
parentElement.get(0).resizeChaosTabs = parentElement.chaosTabsHandleOverflowArrows.bind( parentElement );
var events = [ 'resize', 'load', 'transitionend', 'animationend' ];
for(i = 0; i < events.length; i++) {
windowElement.on( events[i], {tabContainer: parentElement}, $.resizeChaosTabsEvent );
}
}
//Check for predefined active tab in location hash
var activateTabTitle = currentTabtoHash(this);
if(!activateTabTitle) {
//Tab title not found in hash
//Set first tab as active and show its content
var firstTabMenuItem = this.find('.tab-menu li').first();
firstTabMenuItem.addClass('active');
this.find('.tab-content').first().show().css('content-visibility', 'visible');
if (callback && typeof(callback) === "function") {
callback(firstTabMenuItem.find('a').html());
}
}
var tabContainer = this;
//Bind onhashchange event. This allows links to tabs on a page
window.addEventListener( 'hashchange', function (e) {
var tabFound = currentTabtoHash(tabContainer);
if (tabFound && callback && typeof(callback) === "function") {
callback(tabFound);
}
});
return this;
};
function currentTabtoHash( tabsJQueryObj ) {
var hash = window.location.hash.substring(1);
if ( hash.substring(0, 4) !== 'tab-' ) {
//not a tab hash
return false;
}
//Set tab in location hash to active and show its content
var activeTabLink = tabsJQueryObj.find('.tab-menu li > a[href="#'+hash+'"]').first();
if( activeTabLink.length ) {
//Make this tab the active tab
tabsJQueryObj.find('.tab-menu').children('.active').removeClass('active');
activeTabLink.parent().addClass('active');
//Show active tab's content
tabsJQueryObj.find('.tab-content').hide().css('content-visibility', 'hidden');
$(activeTabLink.attr('rel')).show().css('content-visibility', 'visible');
} else {
//Matching tab title was not found
return false;
}
var anchorElement = tabsJQueryObj.find('[id="'+hash+'"], a[name="'+hash+'"]').first();
if( anchorElement.length ) {
anchorElement.get(0).scrollIntoView(true);
}
//Found and set currentTab
return window.location.hash;
};
})( jQuery );