-
Notifications
You must be signed in to change notification settings - Fork 12
/
element.js
186 lines (149 loc) · 4.35 KB
/
element.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
'use strict';
;(function (window, Element) {
'use strict';
// defineGetter
var defineGetter = function (obj, key, fn) {
if (Object.defineProperty) {
Object.defineProperty(obj, key, {
get: fn,
configurable: true
});
return true;
} else if (obj.__defineGetter__) {
obj.__defineGetter__(key, fn);
return true;
}
return false;
};
// classList
(function () {
if ('classList' in document.documentElement) {
return;
}
window.DOMTokenList = DOMTokenList;
function DOMTokenList(el) {
var classArray = [];
var list = this;
this.__element = el;
this.__class = classArray;
defineGetter(this, 'length', function () {
return list.__length;
});
syncElementClassList(this);
}
var array = Array.prototype;
DOMTokenList.prototype = {
indexOf: array.indexOf,
slice: array.slice,
push: array.push,
splice: array.splice,
join: array.join
};
DOMTokenList.prototype.add = function () {
var tokens = arguments;
var tlen = tokens.length;
var map = this.__map;
var el = this.__element;
var cls;
syncElementClassList(this);
while (tlen--) {
cls = tokens[tlen].replace(/(^\s+|\s+$)/g, '');
if (!cls) {
throw new Error('Failed to execute \'add\' on \'DOMTokenList\': The token provided must not be empty.');
}
if (!map.hasOwnProperty(cls)) {
this[this.__length] = cls;
map[cls] = this.__length;
this.__length += 1;
}
}
el.setAttribute('class', Array.prototype.join.call(this, ' '));
};
DOMTokenList.prototype.contains = function (cls) {
syncElementClassList(this);
return this.__map.hasOwnProperty(cls);
};
DOMTokenList.prototype.item = function (index) {
syncElementClassList(this);
return this[index];
};
DOMTokenList.prototype.remove = function () {
syncElementClassList(this);
var tokens = arguments;
var tlen = tokens.length;
var map = this.__map;
var index, token;
while (tlen--) {
token = tokens[tlen];
if (map.hasOwnProperty(token)) {
index = map[token];
delete this[index];
delete map[token];
this.__length -= 1;
}
}
this.__element.setAttribute('class', Array.prototype.join.call(this, ' '));
};
DOMTokenList.prototype.toggle = function (cls) {
if (!cls) {
throw new Error('Failed to execute \'toggle\' on \'DOMTokenList\': The token provided must not be empty.');
}
syncElementClassList(this);
var map = this.__map;
var el = this.__element;
var ret;
if (map.hasOwnProperty(cls)) {
delete this[map[cls]];
delete map[cls];
ret = false;
} else {
this[this.__length] = cls;
this.__map[cls] = this.__length;
this.__length += 1;
ret = true;
}
el.setAttribute('class', Array.prototype.join.call(this, ' '));
return ret;
};
DOMTokenList.prototype.toString = function () {
syncElementClassList(this);
return Array.prototype.join.call(this, ' ');
};
function syncElementClassList(list) {
var el = list.__element;
var classAttr = el.getAttribute('class');
if (classAttr === list.__cache) {
return;
}
var classList = classAttr ? classAttr.split(/\s+/) : [];
var len = classList.length;
var val, map = {};
list.__cache = classAttr;
list.__map = map;
list.__length = len;
while (len--) {
val = classList[len];
list[len] = val;
map[val] = len;
}
}
defineGetter(Element.prototype, 'classList', function () {
return this.__classList || (this.__classList = new DOMTokenList(this));
});
})();
//countdown
(function(){
// Instanciating a new countdown with all defaults
new Countdown();
// Instanciating a custom countdown
//
var countdown = new Countdown({
selector: '#countblock',
msgBefore: "JSDC 2014",
msgAfter: "",
msgPattern: "<div>{days}</div><div>{hours}</div><div>{minutes}</div><div>{seconds}</div>",
dateStart: new Date(),
dateEnd: new Date('2014/10/11 08:00')
});
})()
})(this, Element);