-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontentTabs.js
More file actions
202 lines (182 loc) · 5.79 KB
/
contentTabs.js
File metadata and controls
202 lines (182 loc) · 5.79 KB
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
/*!
contentTabs v1.0.6 (https://github.com/TechTarget/contentTabs)
Author: Morgan Wigmanich <okize123@gmail.com> (http://github.com/okize)
Copyright (c) 2013 | Licensed under the MIT license
http://www.opensource.org/licenses/mit-license.php
*/
(function() {
(function(factory) {
if (typeof define === 'function' && define.amd) {
return define(['jquery'], factory);
} else {
return factory(jQuery);
}
})(function($) {
'use strict';
var Tabs, defaults, pluginName;
pluginName = 'contentTabs';
defaults = {
displayTabs: true,
tabLocation: 'left',
tabActiveClass: 'active',
maintainState: false,
indexOfOpenPanel: 0,
pinPanelIntro: false,
pinnedPanelLocation: 'top'
};
Tabs = (function() {
function Tabs(element, options) {
this.element = element;
this.el = $(this.element);
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.tabs = null;
this.panels = null;
this.tabLocationClassMap = {
top: 'tabsHorizontalTop',
bottom: 'tabsHorizontalBottom',
left: 'tabsVerticalLeft',
right: 'tabsVerticalRight'
};
this.pinnedPanelLocationClassMap = {
top: 'pinnedPanelTop',
left: 'pinnedPanelLeft'
};
this.activeTab = this.options.indexOfOpenPanel;
this.stateKey = 'tabState';
this.hashObject = null;
this.init();
}
Tabs.prototype.init = function() {
var tabs,
_this = this;
if (!this.options.displayTabs) {
this.removeTabs();
return;
}
if (this.options.maintainState && (this.getStateFromHash() != null)) {
this.updateState(this.activeTab);
}
this.setTabsPosition(this.tabLocationClassMap[this.options.tabLocation]);
if (this.options.pinPanelIntro) {
this.pinPanels(this.el);
}
tabs = this.getTabs();
if (!tabs.hasClass(this.options.tabActiveClass)) {
tabs.eq(this.activeTab).addClass(this.options.tabActiveClass);
}
tabs.eq(tabs.length - 1).addClass('last');
return tabs.on('click', function(e) {
e.preventDefault();
return _this.updateState($(e.currentTarget).index());
});
};
Tabs.prototype.getStateFromHash = function() {
var state, _ref;
this.hashObject = this.getHashObject();
if (!this.hashObject) {
return null;
}
state = (_ref = this.hashObject[this.stateKey]) != null ? _ref : null;
if (!state) {
return null;
}
return this.activeTab = this.hashObject[this.stateKey];
};
Tabs.prototype.getHashObject = function() {
var arg, args, arr, hash, item, _i, _len;
hash = this.getUrlHash();
if (!hash) {
return null;
}
args = {};
arr = hash.split('&');
for (_i = 0, _len = arr.length; _i < _len; _i++) {
item = arr[_i];
arg = item.split('=');
if (arg.length > 1) {
args[arg[0]] = arg[1];
} else {
args[arg[0]] = void 0;
}
}
return args;
};
Tabs.prototype.buildHashObject = function() {
return $.param(this.hashObject);
};
Tabs.prototype.updateHash = function(eq) {
eq += '';
this.hashObject = this.getHashObject();
if (!this.hashObject) {
this.hashObject = {};
}
this.hashObject[this.stateKey] = eq;
return this.setUrlHash(this.buildHashObject());
};
Tabs.prototype.getUrlHash = function() {
if (window.location.hash) {
return window.location.hash.substring(1);
} else {
return null;
}
};
Tabs.prototype.setUrlHash = function(hash) {
return window.location.hash = hash;
};
Tabs.prototype.updateState = function(eq) {
this.activeTab = eq;
this.selectTab(eq);
return this.selectPanel(eq);
};
Tabs.prototype.removeTabs = function() {
this.el.addClass('tabsNone');
return this.getTabs().remove();
};
Tabs.prototype.setTabsPosition = function(pos) {
return this.el.addClass(pos);
};
Tabs.prototype.getTabs = function() {
if (!this.tabs) {
this.tabs = this.el.find('.contentTabsNav').find('li');
}
return this.tabs;
};
Tabs.prototype.selectTab = function(eq) {
if (this.options.maintainState) {
this.updateHash(eq);
}
return this.getTabs().removeClass(this.options.tabActiveClass).eq(eq).addClass(this.options.tabActiveClass);
};
Tabs.prototype.getPanels = function() {
if (!this.panels) {
this.panels = this.el.find('.contentTabsPanel');
}
return this.panels;
};
Tabs.prototype.selectPanel = function(eq) {
return this.getPanels().hide().eq(eq).show();
};
Tabs.prototype.pinPanels = function() {
var $this, sectionsToPin;
sectionsToPin = void 0;
$this = void 0;
this.el.addClass('pinPanelIntro').addClass(this.pinnedPanelLocationClassMap[this.options.pinnedPanelLocation]);
sectionsToPin = this.el.find('.contentTabsPanelIntro');
return sectionsToPin.each(function() {
$this = $(this);
return $this.insertBefore($this.parent());
});
};
return Tabs;
})();
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, 'plugin_#{pluginName}')) {
$.data(this, 'plugin_#{pluginName}', new Tabs(this, options));
}
});
};
});
}).call(this);