-
Notifications
You must be signed in to change notification settings - Fork 0
/
domUtils-by-whitedevil.js
263 lines (216 loc) · 7.18 KB
/
domUtils-by-whitedevil.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const domUtils = {
// Select single element
selector: function(query) {
return document.querySelector(query);
},
// Select multiple elements
selectorAll: function(query) {
return document.querySelectorAll(query);
},
// Create an element
createElement: function(tagName) {
return document.createElement(tagName);
},
// Append a child element to a parent
appendChild: function(parent, child) {
parent.appendChild(child);
},
// Remove an element
removeElement: function(element) {
return element.parentNode.removeChild(element);
},
// Add a class to an element
addClass: function(element, className) {
element.classList.add(className);
},
// Remove a class from an element
removeClass: function(element, className) {
element.classList.remove(className);
},
// Check if an element has a class
hasClass: function(element, className) {
return element.classList.contains(className);
},
// Toggle a class on an element
toggleClass: function(element, className) {
element.classList.toggle(className);
},
// Add an event listener to an element
addEventListener: function(element, event, handler) {
element.addEventListener(event, handler);
},
// Remove an event listener from an element
removeEventListener: function(element, event, handler) {
element.removeEventListener(event, handler);
},
// Set innerHTML of an element
setInnerHTML: function(element, html) {
element.innerHTML = html;
},
// Get innerHTML of an element
getInnerHTML: function(element) {
return element.innerHTML;
},
// Set textContent of an element
setTextContent: function(element, text) {
element.textContent = text;
},
// Get textContent of an element
getTextContent: function(element) {
return element.textContent;
},
// Set an attribute of an element
setAttribute: function(element, attr, value) {
element.setAttribute(attr, value);
},
// Get an attribute of an element
getAttribute: function(element, attr) {
return element.getAttribute(attr);
},
// Remove an attribute from an element
removeAttribute: function(element, attr) {
element.removeAttribute(attr);
},
// Set a style property of an element
setStyle: function(element, property, value) {
element.style[property] = value;
},
// Get a style property of an element
getStyle: function(element, property) {
return getComputedStyle(element)[property];
},
// Hide an element
hideElement: function(element) {
element.style.display = 'none';
},
// Show an element
showElement: function(element, displayStyle = 'block') {
element.style.display = displayStyle;
},
// Clone an element
cloneElement: function(element, deep = true) {
return element.cloneNode(deep);
},
// Insert an element before another element
insertBefore: function(newElement, referenceElement) {
referenceElement.parentNode.insertBefore(newElement, referenceElement);
},
// Insert an element after another element
insertAfter: function(newElement, referenceElement) {
referenceElement.parentNode.insertBefore(newElement, referenceElement.nextSibling);
},
// Replace an element with another element
replaceElement: function(newElement, oldElement) {
oldElement.parentNode.replaceChild(newElement, oldElement);
},
// Get parent element
getParent: function(element) {
return element.parentNode;
},
// Get child elements
getChildren: function(element) {
return element.children;
},
// Get next sibling
getNextSibling: function(element) {
return element.nextElementSibling;
},
// Get previous sibling
getPreviousSibling: function(element) {
return element.previousElementSibling;
},
// Check if an element matches a selector
matchesSelector: function(element, selector) {
return element.matches(selector);
},
// Find closest ancestor matching a selector
closest: function(element, selector) {
return element.closest(selector);
},
// Get all descendants matching a selector
findDescendants: function(element, selector) {
return element.querySelectorAll(selector);
},
// Set multiple attributes
setAttributes: function(element, attrs) {
for (let key in attrs) {
if (attrs.hasOwnProperty(key)) {
element.setAttribute(key, attrs[key]);
}
}
},
// Get the value of a form element
getValue: function(element) {
return element.value;
},
// Set the value of a form element
setValue: function(element, value) {
element.value = value;
},
// Check if an element is visible
isVisible: function(element) {
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
},
// Scroll an element into view
scrollIntoView: function(element, options) {
element.scrollIntoView(options);
},
// Add multiple classes
addClasses: function(element, classes) {
classes.forEach(cls => element.classList.add(cls));
},
// Remove multiple classes
removeClasses: function(element, classes) {
classes.forEach(cls => element.classList.remove(cls));
},
// Set multiple styles
setStyles: function(element, styles) {
for (let property in styles) {
if (styles.hasOwnProperty(property)) {
element.style[property] = styles[property];
}
}
},
// Get the computed style of an element
getComputedStyle: function(element, property) {
return window.getComputedStyle(element).getPropertyValue(property);
},
// Fade in an element
fadeIn: function(element, duration = 400) {
element.style.opacity = 0;
element.style.display = 'block';
let last = +new Date();
let tick = function() {
element.style.opacity = +element.style.opacity + (new Date() - last) / duration;
last = +new Date();
if (+element.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
}
};
tick();
},
// Fade out an element
fadeOut: function(element, duration = 400) {
element.style.opacity = 1;
let last = +new Date();
let tick = function() {
element.style.opacity = +element.style.opacity - (new Date() - last) / duration;
last = +new Date();
if (+element.style.opacity > 0) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
} else {
element.style.display = 'none';
}
};
tick();
},
// Toggle visibility
toggleVisibility: function(element) {
if (element.style.display === 'none' || window.getComputedStyle(element).display === 'none') {
element.style.display = 'block';
} else {
element.style.display = 'none';
}
}
};
export domUtils;