-
Notifications
You must be signed in to change notification settings - Fork 32
/
tabs.js
108 lines (88 loc) · 2.18 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
/*
Copyright (c) 2011 BitTorrent, Inc. All rights reserved.
Use of this source code is governed by a BSD-style that can be
found in the LICENSE file.
*/
var ELE_A = new Element("a");
var ELE_LI = new Element("li");
var ELE_SPAN = new Element("span");
var Tabs = new Class({
"active": "",
"tabs": {},
"lazyshow": false,
"tabchange": Function.from(),
"initialize": function(ele, options) {
this.element = $(ele);
this.tabs = options.tabs;
this.lazyshow = !!options.lazyshow;
if (typeof(options.onChange) == 'function') {
this.tabchange = options.onChange;
}
var $me = this;
this.element.addStopEvent("click", function(ev) {
var targ = ev.target;
if (targ && (targ.get("tag") == "span"))
targ = targ.getParent("a");
if (targ && (targ.get("tag") == "a"))
$me.show(targ.retrieve("showId"));
});
},
"draw": function() {
this.element.set("html", "");
Object.each(this.tabs, function(text, id) {
var ele = ELE_LI.clone(false);
if (this.lazyshow) {
ele.hide();
var showCB = function() {
ele.show();
$(id).removeEvent("show", showCB);
};
$(id).addEvent("show", showCB);
}
this.element.adopt(ele
.set("id", "tab_" + id)
.adopt(ELE_A.clone(false)
.setProperty("href", "#")
.store("showId", id)
.adopt(ELE_SPAN.clone(false)
.appendText(text)
.set("id", "tab_title_" + id)
)
)
);
}, this);
return this;
},
"onChange": function() {
if (arguments.length > 0)
this.tabchange.apply(this, arguments);
else
this.tabchange.call(this, this.active);
},
"setNames": function(names) {
Object.each(names, function(name, id) {
var tab = $("tab_" + id);
var icon = tab.getElement("span span");
if (icon) icon.dispose();
tab.getElement("span").set("html", name);
if (icon) tab.getElement("span").grab(icon, "top");
});
return this;
},
"show": function(id) {
if (!has(this.tabs, id)) return;
Object.each(this.tabs, function(_, tab) {
if (tab == id) {
$(tab).show();
$("tab_" + tab).addClass("selected");
}
else {
$(tab).hide();
$("tab_" + tab).removeClass("selected");
}
});
this.active = id;
this.onChange(id);
return this;
}
});