forked from bailu/idTabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.idTabs.js
153 lines (146 loc) · 4.33 KB
/
jquery.idTabs.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
/* Options (in any order):
start (number|string)
Index number of default tab. ex: $(...).idTabs(0)
String of id of default tab. ex: $(...).idTabs("tab1")
default: class "selected" or index 0
Passing null will force it to not select a default tab
change (boolean)
True - Url will change. ex: $(...).idTabs(true)
False - Url will not change. ex: $(...).idTabs(false)
default: false
click (function)
Function will be called when a tab is clicked. ex: $(...).idTabs(foo)
If the function returns true, idTabs will show/hide content (as usual).
If the function returns false, idTabs will not take any action.
The function is passed four variables:
The id of the element to be shown
an array of all id's that can be shown
the element containing the tabs
and the current settings
selected (string)
Class to use for selected. ex: $(...).idTabs(".current")
default: ".selected"
event (string)
Event to trigger idTabs on. ex: $(...).idTabs("!mouseover")
default: "!click"
To bind multiple event, call idTabs multiple times
ex: $(...).idTabs("!click").idTabs("!focus")
automatic switchover (string)
ex: $(...).idTabs("~3000")
*/
(function() {
var init = function() { (function($) {
$.fn.idTabs = function() {
var s = {};
for (var i = 0; i < arguments.length; ++i) {
var a = arguments[i];
switch (a.constructor) {
case Object:
$.extend(s, a);
break;
case Boolean:
s.change = a;
break;
case Number:
s.start = a;
break;
case Function:
s.click = a;
break;
case String:
if (a.charAt(0) == '.') s.selected = a;
else if (a.charAt(0) == '!') s.event = a;
else if (a.charAt(0) == '~')
{
s.auto = true;
if(a.substr(1)) s.speed = a.substr(1);
}
else s.start = a;
break;
}
}
if (typeof s['return'] == "function")
s.change = s['return'];
return this.each(function() {
$.idTabs(this, s);
});
}
$.idTabs = function(tabs, options) {
var auto_time;
var auto_change = function(){
var $next = list.eq(0);
var _sel = null;
list.each(function() {
if(_sel) {
$next = $(this);
return false;
}
if($(this).hasClass(s.selected)) _sel = true;
});
$next.trigger(s.event);
auto_time = setTimeout(auto_change, s.speed);
}
var meta = ($.metadata) ? $(tabs).metadata() : {};
var s = $.extend({}, $.idTabs.settings, meta, options);
if (s.selected.charAt(0) == '.') s.selected = s.selected.substr(1);
if (s.event.charAt(0) == '!') s.event = s.event.substr(1);
if (s.start == null) s.start = -1;
var showId = function() {
if(s.auto) {
clearTimeout(auto_time);
$(this).one("mouseout", function(){
auto_time = setTimeout(auto_change, s.speed);
});
}
if ($(this).is('.' + s.selected))
return s.change;
var id = "#" + this.href.split('#')[1];
var aList = [];
var idList = [];
$("a", tabs).each(function() {
if (this.href.match(/#/)) {
aList.push(this);
idList.push("#" + this.href.split('#')[1]);
}
});
if (s.click && !s.click.apply(this, [id, idList, tabs, s])) return s.change;
for (i in aList) $(aList[i]).removeClass(s.selected);
for (i in idList) $(idList[i]).hide();
$(this).addClass(s.selected);
$(id).show();
return s.change;
}
var list = $("a[href*='#']", tabs).unbind(s.event, showId).bind(s.event, showId).attr('hidefocus', 'true');
list.each(function() {
$("#" + this.href.split('#')[1]).hide();
});
var test = false;
if ((test = list.filter('.' + s.selected)).length);
else if (typeof s.start == "number" && (test = list.eq(s.start)).length);
else if (typeof s.start == "string" && (test = list.filter("[href*='#" + s.start + "']")).length);
if (test) {
test.removeClass(s.selected);
test.trigger(s.event);
}
if(s.auto) {
auto_time = setTimeout(auto_change, s.speed);
}
return s;
}
$.idTabs.settings = {
start: 0,
change: false,
click: null,
selected: ".selected",
event: "!click",
auto: false,
speed: 3000
};
$.idTabs.version = "2.2";
$(function() {
$(".idTabs").idTabs();
});
})(jQuery);
}
return init();
})();