-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyetii.js
187 lines (159 loc) · 6.37 KB
/
yetii.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
/*
Yetii - Yet (E)Another Tab Interface Implementation
version 1.8.0
http://www.kminek.pl/lab/yetii/
Copyright (c) Grzegorz Wojcik
Code licensed under the BSD License:
http://www.kminek.pl/bsdlicense.txt
*/
function Yetii() {
this.defaults = {
id: null,
active: 1,
interval: null,
wait: null,
persist: null,
tabclass: 'tab',
activeclass: 'active',
callback: null,
leavecallback: null
};
this.activebackup = null;
for (var n in arguments[0]) {
this.defaults[n]=arguments[0][n];
};
this.getTabs = function() {
var retnode = [];
var elem = document.getElementById(this.defaults.id).getElementsByTagName('*');
var regexp = new RegExp("(^|\\s)" + this.defaults.tabclass.replace(/\-/g, "\\-") + "(\\s|$)");
for (var i = 0; i < elem.length; i++) {
if (regexp.test(elem[i].className)) retnode.push(elem[i]);
}
return retnode;
};
this.links = document.getElementById(this.defaults.id + '-nav').getElementsByTagName('a');
this.listitems = document.getElementById(this.defaults.id + '-nav').getElementsByTagName('li');
this.show = function(number) {
for (var i = 0; i < this.tabs.length; i++) {
this.tabs[i].style.display = ((i+1)==number) ? 'block' : 'none';
if ((i+1)==number) {
this.addClass(this.links[i], this.defaults.activeclass);
this.addClass(this.listitems[i], this.defaults.activeclass + 'li');
} else {
this.removeClass(this.links[i], this.defaults.activeclass);
this.removeClass(this.listitems[i], this.defaults.activeclass + 'li');
}
}
if (this.defaults.leavecallback && (number != this.activebackup)) this.defaults.leavecallback(this.defaults.active);
this.activebackup = number;
this.defaults.active = number;
if (this.defaults.callback) this.defaults.callback(number);
};
this.rotate = function(interval) {
this.show(this.defaults.active);
this.defaults.active++;
if (this.defaults.active > this.tabs.length) this.defaults.active = 1;
var self = this;
if (this.defaults.wait) clearTimeout(this.timer2);
this.timer1 = setTimeout(function(){
self.rotate(interval);
}, interval*1000);
};
this.next = function() {
var _target = (this.defaults.active + 1 > this.tabs.length) ? 1 : this.defaults.active + 1;
this.show(_target);
this.defaults.active = _target;
};
this.previous = function() {
var _target = ((this.defaults.active - 1) == 0) ? this.tabs.length : this.defaults.active - 1;
this.show(_target);
this.defaults.active = _target;
};
this.previous = function() {
this.defaults.active--;
if(!this.defaults.active) this.defaults.active = this.tabs.length;
this.show(this.defaults.active);
};
this.gup = function(name) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null) return null;
else return results[1];
};
this.guh = function() {
var regexS = "#([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec(window.location.hash);
if (results == null) return null;
else return results[1];
};
this.parseurl = function(tabinterfaceid) {
var result = this.gup(tabinterfaceid);
if (result == null) result = this.guh();
if (result == null) return null;
if (parseInt(result)) return parseInt(result);
if (document.getElementById(result)) {
for (var i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].id == result) {
return (i+1);
}
}
}
return null;
};
this.createCookie = function(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
};
this.readCookie = function(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
};
this.hasClass = function(el, className){
var classes = el.className.split(' ');
for (var i = 0; i < classes.length; i++) {
if (classes[i] == className) return true;
}
return false;
};
this.addClass = function(el, className){
if (!this.hasClass(el, className)) el.className = (el.className + ' ' + className).replace(/\s{2,}/g, ' ').replace(/^\s+|\s+$/g, '');
};
this.removeClass = function(el, className){
el.className = el.className.replace(new RegExp('(^|\\s)' + className + '(?:\\s|$)'), '$1');
el.className.replace(/\s{2,}/g, ' ').replace(/^\s+|\s+$/g, '');
};
this.tabs = this.getTabs();
this.defaults.active = (this.parseurl(this.defaults.id)) ? this.parseurl(this.defaults.id) : this.defaults.active;
if (this.defaults.persist && this.readCookie(this.defaults.id)) this.defaults.active = this.readCookie(this.defaults.id);
this.activebackup = this.defaults.active;
this.show(this.defaults.active);
var self = this;
for (var i = 0; i < this.links.length; i++) {
this.links[i].customindex = i+1;
this.links[i].onclick = function(){
if (self.timer1) clearTimeout(self.timer1);
if (self.timer2) clearTimeout(self.timer2);
self.show(this.customindex);
if (self.defaults.persist) self.createCookie(self.defaults.id, this.customindex, 0);
if (self.defaults.wait) self.timer2 = setTimeout(function(){
self.rotate(self.defaults.interval);
}, self.defaults.wait*1000);
return false;
};
}
if (this.defaults.interval) this.rotate(this.defaults.interval);
};