-
Notifications
You must be signed in to change notification settings - Fork 46
/
util.js
247 lines (212 loc) · 4.72 KB
/
util.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* util.js module.
* @module util
*/
'use strict';
/**
* check if variable is an element
* @param {*} potential element
* @return {Boolean} return true if is an element
*/
function isElement(element) {
try {
// eslint-disable-next-line no-undef
return element instanceof Element;
} catch (error) {
return !!(element && element.nodeType === 1);
}
}
/**
* createElement shortcut
* @param {String} tag
* @return {Element} element
*/
function el(element) {
var classes = [];
var tag = element;
var el;
if (isElement(element)) {
return element;
}
classes = element.split('.');
if (classes.length > 1) {
tag = classes[0];
}
el = document.createElement(tag);
addClass(el, classes.slice(1));
return el;
}
/**
* createDocumentFragment shortcut
* @return {DocumentFragment}
*/
function frag() {
return document.createDocumentFragment();
}
/**
* createTextNode shortcut
* @return {TextNode}
*/
function text(text) {
return document.createTextNode(text);
}
/**
* remove element
* @param {Element} element to remove
* @return {Element} removed element
*/
function remove(element) {
if ('remove' in element) {
element.remove();
} else {
element.parentNode.removeChild(element);
}
return element;
}
/**
* Find first element that tests true, starting with the element itself
* and traversing up through its ancestors
* @param {Element} element
* @param {Function} test fn - return true when element located
* @return {Element}
*/
function closest(element, test) {
var el = element;
while (el) {
if (test(el)) {
return el;
}
el = el.parentNode;
}
return null;
}
/**
* Add one or more classnames to an element
* @param {Element} element
* @param {Array.<string>|String} array of classnames or string with
* classnames separated by whitespace
* @return {Element}
*/
function addClass(element, className) {
var classNames = className;
function _addClass(el, cn) {
if (!el.className) {
el.className = cn;
} else if (!hasClass(el, cn)) {
if (el.classList) {
el.classList.add(cn);
} else {
el.className += ' ' + cn;
}
}
}
if (!Array.isArray(className)) {
classNames = className.trim().split(/\s+/);
}
classNames.forEach(_addClass.bind(null, element));
return element;
}
/**
* Remove a class from an element
* @param {Element} element
* @param {Array.<string>|String} array of classnames or string with
* @return {Element}
*/
function removeClass(element, className) {
var classNames = className;
function _removeClass(el, cn) {
var classRegex;
if (el.classList) {
el.classList.remove(cn);
} else {
classRegex = new RegExp('(?:^|\\s)' + cn + '(?!\\S)', 'g');
el.className = el.className.replace(classRegex, '').trim();
}
}
if (!Array.isArray(className)) {
classNames = className.trim().split(/\s+/);
}
classNames.forEach(_removeClass.bind(null, element));
return element;
}
/**
* Check if element has a class
* @param {Element} element
* @param {String} className
* @return {boolean}
*/
function hasClass(element, className) {
if (!element || !('className' in element)) {
return false;
}
return element.className.split(/\s+/).indexOf(className) !== -1;
}
/**
* Return all next siblings
* @param {Element} element
* @return {Array.<element>}
*/
function nextSiblings(element) {
var next = element.nextSibling;
var siblings = [];
while (next) {
siblings.push(next);
next = next.nextSibling;
}
return siblings;
}
/**
* Return all prev siblings
* @param {Element} element
* @return {Array.<element>}
*/
function previousSiblings(element) {
var prev = element.previousSibling;
var siblings = [];
while (prev) {
siblings.push(prev);
prev = prev.previousSibling;
}
return siblings;
}
/**
* Stop event propagation
* @param {Event} event
* @return {Event}
*/
function stop(event) {
event.stopPropagation();
event.preventDefault();
return event;
}
/**
* Returns first element in parent that matches selector
* @param {Element} parent
* @param {String} selector
* @return {Element | null}
*/
function first(parent, selector) {
return parent.querySelector(selector);
}
function append(parent, _children) {
var _frag = frag();
var children = Array.isArray(_children) ? _children : [_children];
children.forEach(_frag.appendChild.bind(_frag));
parent.appendChild(_frag);
return parent;
}
module.exports = {
el: el,
frag: frag,
text: text,
closest: closest,
addClass: addClass,
removeClass: removeClass,
hasClass: hasClass,
nextSiblings: nextSiblings,
previousSiblings: previousSiblings,
remove: remove,
stop: stop,
first: first,
append: append
};