-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtabs.js
executable file
·214 lines (182 loc) · 4.08 KB
/
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
/*
Tabs Tool
http://imperavi.com/kube/
Copyright (c) 2009-2014, Imperavi LLC.
*/
(function($)
{
// Plugin
$.fn.tabs = function(options)
{
var val = [];
var args = Array.prototype.slice.call(arguments, 1);
if (typeof options === 'string')
{
this.each(function()
{
var instance = $.data(this, 'tabs');
if (typeof instance !== 'undefined' && $.isFunction(instance[options]))
{
var methodVal = instance[options].apply(instance, args);
if (methodVal !== undefined && methodVal !== instance) val.push(methodVal);
}
else return $.error('No such method "' + options + '" for Tabs');
});
}
else
{
this.each(function()
{
$.data(this, 'tabs', {});
$.data(this, 'tabs', Tabs(this, options));
});
}
if (val.length === 0) return this;
else if (val.length === 1) return val[0];
else return val;
};
// Initialization
function Tabs(el, options)
{
return new Tabs.prototype.init(el, options);
}
$.Tabs = Tabs;
$.Tabs.NAME = 'tabs';
$.Tabs.VERSION = '1.0';
$.Tabs.opts = {
equals: false,
active: false
};
// Functionality
Tabs.fn = $.Tabs.prototype = {
// Initialization
init: function(el, options)
{
this.$element = el !== false ? $(el) : false;
this.loadOptions(options);
this.links = this.$element.find('a');
this.tabs = [];
this.links.each($.proxy(this.load, this));
this.setEquals();
this.setCallback('init');
},
loadOptions: function(options)
{
this.opts = $.extend(
{},
$.extend(true, {}, $.Tabs.opts),
this.$element.data(),
options
);
},
setCallback: function(type, e, data)
{
var events = $._data(this.$element[0], 'events');
if (events && typeof events[type] != 'undefined')
{
var value = [];
var len = events[type].length;
for (var i = 0; i < len; i++)
{
var namespace = events[type][i].namespace;
if (namespace == 'tools.' + $.Tabs.NAME || namespace == $.Tabs.NAME + '.tools')
{
var callback = events[type][i].handler;
value.push((typeof data == 'undefined') ? callback.call(this, e) : callback.call(this, e, data));
}
}
if (value.length == 1) return value[0];
else return value;
}
return (typeof data == 'undefined') ? e : data;
},
load: function(i, el)
{
var $el = $(el);
var hash = $el.attr('href');
$el.attr('rel', hash);
this.tabs.push($(hash));
if (!$el.parent().hasClass('active'))
{
$(hash).hide();
}
// is hash in url
this.readLocationHash(hash);
// is active
if (this.opts.active !== false && this.opts.active === hash)
{
this.show(hash);
}
$el.on('click', $.proxy(this.onClick, this));
},
onClick: function(e)
{
e.preventDefault();
var hash = $(e.target).attr('rel');
top.location.hash = hash;
this.show(hash);
},
readLocationHash: function(hash)
{
if (top.location.hash === '' || top.location.hash != hash) return;
this.opts.active = top.location.hash;
},
setActive: function(hash)
{
this.activeHash = hash;
this.activeTab = $('[rel=' + hash + ']');
this.links.parent().removeClass('active');
this.activeTab.parent().addClass('active');
},
getActiveHash: function()
{
return this.activeHash;
},
getActiveTab: function()
{
return this.activeTab;
},
show: function(hash)
{
this.hideAll();
$(hash).show();
this.setActive(hash);
this.setCallback('show', $('[rel=' + hash + ']'), hash);
},
hideAll: function()
{
$.each(this.tabs, function()
{
$(this).hide();
});
},
setEquals: function()
{
if (!this.opts.equals) return;
this.setMaxHeight(this.getMaxHeight());
},
setMaxHeight: function(height)
{
$.each(this.tabs, function()
{
$(this).css('min-height', height + 'px');
});
},
getMaxHeight: function()
{
var max = 0;
$.each(this.tabs, function()
{
var h = $(this).height();
max = h > max ? h : max;
});
return max;
}
};
$(window).on('load.tools.tabs', function()
{
$('[data-tools="tabs"]').tabs();
});
// constructor
Tabs.prototype.init.prototype = Tabs.prototype;
})(jQuery);