-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·156 lines (141 loc) · 3.6 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
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
'use strict';
function getListOrArray(fn) {
return function(itmList) {
return fn(itmList) ? itmList : new Array(itmList);
};
}
function isArrayLike(itmList) {
return itmList instanceof NodeList || Array.isArray(itmList);
}
var getArray = getListOrArray(Array.isArray);
var getArrayLike = getListOrArray(isArrayLike);
function classListAction(action, itm, option) {
itm &&
itm.classList &&
itm.classList[action] &&
itm.classList[action](option);
}
/**
* Generate the function to manipulate classList
*
* @example
* var itm = document.querySelectorAll('.btn');
* var cls = ['d-none']
* generateClassListFn('add')(itmList, clsList);
*
* @param String action
* @return Function
*/
function generateClassListFn(action) {
return function(itmList, optList) {
itmList = getArrayLike(itmList);
optList = getArray(optList);
for (var i = itmList.length - 1; i >= 0; i--) {
for (var a = optList.length - 1; a >= 0; a--) {
classListAction(action, itmList[i], optList[a]);
}
}
};
}
/**
* Generate a thunk to manipulate classList
*
* @example
* var itm = document.querySelectorAll('.btn');
* var cls = ['d-none']
* generateThunkClassListFn('add')(itmList, clsList)();
*
* @param String action
* @return Function
*/
function generateThunkClassListFn(action) {
return function(itmList, optList) {
return function() {
generateClassListFn(action)(itmList, optList);
};
};
}
/**
* Add a css class to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var cls = ['d-none']
* addClass(itm, cls);
*
* @param NodeList | Node itmList
* @param String[] | String clsList
*/
var addClass = generateClassListFn('add');
/**
* Remove a css class to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var cls = ['d-none']
* removeClass(itm, cls);
*
* @param NodeList | Node itmList
* @param String[] | String clsList
*/
var removeClass = generateClassListFn('remove');
/**
* Toggle a css class to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var cls = ['d-none']
* toggleClass(itm, cls);
*
* @param NodeList | Node itmList
* @param String[] | String clsList
*/
var toggleClass = generateClassListFn('toggle');
/**
* Thunk to add a css class to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var cls = ['d-none']
* var thunk = addClassThunk(itm, cls);
* setTimeout(thunk, 3000);
*
* @param NodeList | Node itmList
* @param String[] | String clsList
* @return Function
*/
var addClassThunk = generateThunkClassListFn('add');
/**
* Thunk to remove a css class to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var cls = ['d-none']
* var thunk = removeClassThunk(itm, cls);
* setTimeout(thunk, 3000);
*
* @param NodeList | Node itmList
* @param String[] | String clsList
* @return Function
*/
var removeClassThunk = generateThunkClassListFn('remove');
/**
* Thunk to toggle a css class to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var cls = ['d-none']
* var thunk = addToggleThunk(itm, cls);
* setTimeout(thunk, 3000);
*
* @param NodeList | Node itmList
* @param String[] | String clsList
* @return Function
*/
var toggleClassThunk = generateThunkClassListFn('toggle');
module.exports.addClass = addClass;
module.exports.removeClass = removeClass;
module.exports.toggleClass = toggleClass;
module.exports.addClassThunk = addClassThunk;
module.exports.removeClassThunk = removeClassThunk;
module.exports.toggleClassThunk = toggleClassThunk;