-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (56 loc) · 1.44 KB
/
index.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
var classes = require('classes');
module.exports = Switch;
function Switch(el) {
if (!(this instanceof Switch)) return new Switch(el);
this.els = [];
this.current = 0;
if (el) this.extract(el);
this.findDefault();
this.show(this.default);
}
Switch.prototype.toggle = Switch.prototype.next = function () {
this.show(this.current + 1 < this.els.length ? this.current + 1 : 0);
};
Switch.prototype.addEl = function (el) {
this.els.push({
el: el
, classes: classes(el).add('hidden')
});
return this;
};
Switch.prototype.extract = function (el) {
var children = el.childNodes
, i = 0
, l = children.length
;
while (i < l) {
if (children[i].nodeType === 1) this.addEl(children[i]);
i += 1;
}
return this;
};
Switch.prototype.findDefault = function () {
var els = this.els
, i = 0
, l = els.length
;
while (i < l) {
if (els[i].el.getAttribute('data-default-content')) {
this.default = i;
return this;
}
i += 1;
}
this.default = 0;
return this;
};
Switch.prototype.show = function (i) {
var el = this.els[i];
if (el) {
if (typeof this.current !== 'undefined') {
this.els[this.current].classes.add('hidden');
}
el.classes.remove('hidden');
this.current = i;
}
};