-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (105 loc) · 2.55 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
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
require('util.polyfill');
var RE_TRIM = /^\s+|\s+$/g;
/**
* Check if 'element' has class 'clas'
* @param {Element} element
* @param {String} clas
* @return {Boolean}
*/
exports.hasClass = function(element, clas) {
if (element.classList != null) {
return element.classList.contains(clas);
} else {
var classes = element.className.replace(RE_TRIM, '').split(' ');
return classes.indexOf(clas) >= 0;
}
};
/**
* Check if 'element' has a class matching 'pattern'
* @param {Element} element
* @param {String} pattern
* @return {String}
*/
exports.matchClass = function(element, pattern) {
var classes = element.className.replace(RE_TRIM, '').split(' ')
, clas;
for (var i = 0, n = classes.length; i < n; i++) {
clas = classes[i];
if (clas.indexOf(pattern) !== -1) {
return clas;
}
}
return '';
};
/**
* Add class 'clas' to 'element'
* @param {Element} element
* @param {String} clas
*/
exports.addClass = function(element, clas) {
if (element.classList != null) {
element.classList.add(clas);
} else {
element.className += ' ' + clas;
}
};
/**
* Remove class 'clas' from 'element'
* @param {Element} element
* @param {String} clas
*/
exports.removeClass = function(element, clas) {
var c, classes;
if (clas) {
if (element.classList != null) {
element.classList.remove(clas);
} else {
var classes = element.className.replace(RE_TRIM, '').split(' ')
, results = [];
for (var i = 0, n = classes.length; i < n; i++) {
if (classes[i] !== clas) results.push(classes[i]);
}
element.className = results.join(' ');
}
}
};
/**
* Toggle class 'clas' on 'element'
* @param {Element} element
* @param {String} clas
*/
exports.toggleClass = function(element, clas) {
if (exports.hasClass(element, clas)) {
exports.removeClass(element, clas);
} else {
exports.addClass(element, clas);
}
};
/**
* Replace class 'clasOld' with 'clasNew' on 'element'
* @param {Element} element
* @param {String} clas
*/
exports.replaceClass = function(element, clasOld, clasNew) {
if (clasOld) {
if (clasNew) {
element.className = element.className.replace(clasOld, clasNew);
} else {
exports.removeClass(element, clasOld);
}
} else if (clasNew) {
exports.addClass(element, clasNew);
}
};
/**
* Add class 'clas' to 'element', and remove after 'duration' milliseconds
* @param {Element} element
* @param {String} clas
* @param {Number} duration
*/
exports.addTemporaryClass = function(element, clas, duration) {
exports.addClass(element, clas);
setTimeout((function() {
exports.removeClass(element, clas);
}), duration);
};