-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
166 lines (128 loc) · 4.13 KB
/
main.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
"use strict";
var self;
var config;
var Bind = require("github/jillix/bind");
var Events = require("github/jillix/events");
var EmitEvents = {
tabActivated: function (miid) {
self.on("ready", miid, function() {
self.emit("tabActivated", miid);
});
}
};
module.exports = function (conf) {
self = this;
config = processConfig(conf);
Events.call(self, conf);
var container = $(config.container, self.dom);
if (config.options.reuse) {
self.tabs = {};
}
// On tab click
$(config.tabs, self.dom).on("click", function() {
// Change the hash in URL
if (config.options.hash) {
if (!$(this).attr("data-hash")) {
return;
}
window.location.hash = $(this).attr("data-hash");
} else {
activateTab(config, $(this));
}
return false;
});
// hashchange handler
$(window).on("hashchange", function () {
var tab = $("[data-hash=" + window.location.hash.substring(1) + "]", self.dom);
activateTab(config, tab);
})
// Load first content
if ((config.options.first) && (!window.location.hash)) {
M(config.container, config.options.first, function () {
EmitEvents.tabActivated(config.options.first);
if (config.options.reuse) {
self.tabs[config.options.first] = true;
}
});
}
// Load the right content for hash
else if ((window.location.hash) && (config.options.hash)) {
var tab = $("[data-hash=" + window.location.hash.substring(1) + "]", self.dom);
var options = {
tab: tab,
firstLoad: true
};
activateTab(config, options);
}
if (typeof window[config.onInitEnd] === "function") {
window[config.onInitEnd].apply(self);
}
// run the binds
if (config.binds) {
for (var i = 0; i < config.binds.length; ++i) {
Bind.call(self, config.binds[i]);
}
}
self.emit("ready");
}
function activateTab(config, options) {
var tab = options.tab || options;
if (typeof tab === "function") {
tab = options;
}
if (!tab.length) {
return;
}
// gets the active tab
var active = $(config.tabs, self.dom).parent().find("." + config.options.classes.selected);
// Removes the active class
$(config.tabs, self.dom).removeClass(config.options.classes.selected);
// Adds active class
tab.addClass(config.options.classes.selected);
// TODO we cannot rely that the selected class exists and based on this
// to test the current tab. The current miid should be buffered
if (tab.attr("data-miid") === active.attr("data-miid") && !options.firstLoad) {
return;
}
// set title
var newTitle = active.attr("data-title");
if (newTitle) {
window.title = newTitle;
}
// Remove the content of container if reuse is false or undefined
if (!config.options.reuse) {
$(config.container).html("");
}
var miid = tab.attr("data-miid");
// Sets the content in container
// reuse is true
if (config.options.reuse) {
// miid was NOT loaded
if (!self.tabs[miid]) {
M(config.container, miid, function () {
EmitEvents.tabActivated(miid);
self.tabs[miid] = true;
$(config.container).children().hide(config.options.transition);
$("#" + miid).show(config.options.transition);
tab.constructor === jQuery ? tab.click() : "";
});
}
// miid was already loaded, show it!
else {
$(config.container).children().hide(config.options.transition);
$("#" + miid).show(config.options.transition);
EmitEvents.tabActivated(miid);
}
}
// reuse is false
else {
M(config.container, miid, function () {
EmitEvents.tabActivated(miid);
});
}
}
function processConfig (config) {
config.options = config.options || {};
config.options.classes = config.options.classes || {};
return config;
}