-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbox.js
More file actions
440 lines (362 loc) · 13.6 KB
/
toolbox.js
File metadata and controls
440 lines (362 loc) · 13.6 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
export class $dom {
static insertBefore(element, newElement) {
element.before(newElement);
}
static insertAfter(element, newElement) {
element.after(newElement);
}
static addElementBefore(targetSelector, newElement) {
const targetElement = this.getElement(targetSelector);
targetElement && this.insertBefore(targetElement, newElement);
}
static addElementAfter(targetSelector, newElement) {
const targetElement = this.getElement(targetSelector);
targetElement && this.insertAfter(targetElement, newElement);
}
static addElementToElement(targetElement, newElement) {
targetElement && targetElement.appendChild(newElement);
}
static addElementBeforeElement(targetElement, newElement) {
targetElement && this.insertBefore(targetElement, newElement);
}
static addElementAfterElement(targetElement, newElement) {
targetElement && this.insertAfter(targetElement, newElement);
}
static getElement(selectorOrElement) {
if (typeof selectorOrElement === 'string') {
return document.querySelector(selectorOrElement);
} else if (selectorOrElement instanceof Element) {
return selectorOrElement;
} else {
console.error('Invalid selector or element:', selectorOrElement);
return null;
}
}
static removeElement(element) {
element.parentNode.removeChild(element);
}
static addClassToElement(element, className) {
element.classList.add(className);
}
static removeClassFromElement(element, className) {
element.classList.remove(className);
}
static changeElementText(element, newText) {
element.textContent = newText;
}
static addAttributeToElement(element, attributeName, attributeValue) {
element.setAttribute(attributeName, attributeValue);
}
static addHtmlBefore(element, htmlString) {
element.insertAdjacentHTML('beforebegin', htmlString);
}
static addHtmlAfter(element, htmlString) {
element.insertAdjacentHTML('afterend', htmlString);
}
static addHtmlToElement(element, htmlString) {
element.insertAdjacentHTML('beforeend', htmlString);
}
static addHtmlBeforeSelector(targetSelector, htmlString) {
const targetElement = this.getElement(targetSelector);
targetElement && this.addHtmlBefore(targetElement, htmlString);
}
static addHtmlAfterSelector(targetSelector, htmlString) {
const targetElement = this.getElement(targetSelector);
targetElement && this.addHtmlAfter(targetElement, htmlString);
}
static addHtmlToElementSelector(targetSelector, htmlString) {
const targetElement = this.getElement(targetSelector);
targetElement && this.addHtmlToElement(targetElement, htmlString);
}
static addIdToElement(element, id) {
element.id = id;
}
static removeIdFromElement(element) {
element.id = '';
}
static addMultipleElementsToElement(targetElement, ...newElements) {
if (targetElement instanceof Node) {
newElements.forEach(newElement => {
if (newElement instanceof Node) {
targetElement.appendChild(newElement);
} else {
console.error('Invalid element:', newElement);
}
});
} else {
console.error('Invalid target element:', targetElement);
}
}
static toggleClass(element, className) {
if (element.classList.contains(className)) {
this.removeClassFromElement(element, className);
} else {
this.addClassToElement(element, className);
}
}
static toggleId(element, id) {
if (element.id === id) {
element.id = '';
} else {
element.id = id;
}
}
static addMultipleElementsAfter(targetElement, ...newElements) {
if (targetElement instanceof Node) {
newElements.forEach(newElement => {
if (newElement instanceof Node) {
targetElement.parentNode.insertBefore(newElement, targetElement.nextSibling);
} else {
console.error('Invalid element:', newElement);
}
});
} else {
console.error('Invalid target element:', targetElement);
}
}
static addMultipleElementsBefore(targetElement, ...newElements) {
if (targetElement instanceof Node) {
newElements.forEach(newElement => {
if (newElement instanceof Node) {
targetElement.parentNode.insertBefore(newElement, targetElement);
} else {
console.error('Invalid element:', newElement);
}
});
} else {
console.error('Invalid target element:', targetElement);
}
}
static addFirst(newElement, targetElement) {
if (targetElement instanceof Node) {
this.addMultipleElementsBefore(targetElement, newElement);
} else {
console.error('Invalid target element:', targetElement);
}
}
static addLast(newElement, targetElement) {
if (targetElement instanceof Node) {
this.addMultipleElementsAfter(targetElement, newElement);
} else {
console.error('Invalid target element:', targetElement);
}
}
static addHtmlFirst(newHtml, targetElement) {
if (targetElement instanceof Node) {
targetElement.insertAdjacentHTML('afterbegin', newHtml);
} else {
console.error('Invalid target element:', targetElement);
}
}
static addHtmlLast(newHtml, targetElement) {
if (targetElement instanceof Node) {
targetElement.insertAdjacentHTML('beforeend', newHtml);
} else {
console.error('Invalid target element:', targetElement);
}
}
}
export const $input = {
limitInputLength: (inputElement, maxLength, onMaxLengthExceeded) => {
if (inputElement instanceof HTMLElement) {
inputElement.addEventListener('input', (e) => {
const inputValue = e.target.value;
if (inputValue.length > maxLength) {
inputElement.value = inputValue.slice(0, maxLength);
if (typeof onMaxLengthExceeded === 'function') {
onMaxLengthExceeded(e.target.value);
}
} else if (inputValue.length < maxLength && inputValue.length === 8 && typeof onSpecialOperation === 'function') {
}
});
} else {
console.error('Invalid input element provided.');
}
},
enableInput:(event, keyCode, inputElement) => {
if (event.keyCode === keyCode) {
inputElement.focus();
}
},
handleShortcut:(event, keyCode, inputElement) => {
if (event.keyCode === keyCode && event.ctrlKey) {
inputElement.focus();
}
},
disableEmptyInput: (inputElement, onEmptyInput) => {
if (inputElement instanceof HTMLElement) {
inputElement.addEventListener('blur', () => {
if (inputElement.value.trim() === '') {
if (typeof onEmptyInput === 'function') {
onEmptyInput();
}
}
});
} else {
console.error('Invalid input element provided.');
}
},
allowOnlyString: (inputElement, callback) => {
if (inputElement instanceof HTMLElement) {
inputElement.addEventListener('input', (e) => {
const inputValue = e.target.value;
const cleanedValue = inputValue.replace(/[٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹]/g, '');
if (inputValue !== cleanedValue) {
inputElement.value = cleanedValue;
if (typeof callback === 'function') {
callback();
}
}
});
} else {
console.error('Invalid input element provided.');
}
},
allowOnlyNumbers:(inputElement,callback) => {
if (inputElement instanceof HTMLElement) {
inputElement.addEventListener('input', (e) => {
const inputValue = e.target.value;
const numericValue = inputValue.replace(/[^0-9]/g, '');
if (inputValue !== numericValue) {
inputElement.value = numericValue;
callback();
}
});
} else {
console.error('Invalid input element provided.');
}
},
allowOnlyPersian: (inputElement, callback) => {
if (inputElement instanceof HTMLElement) {
inputElement.addEventListener('input', (e) => {
const inputValue = e.target.value;
const persianValue = inputValue.replace(/[^آ-ی ]/gu, '');
if (inputValue !== persianValue) {
inputElement.value = persianValue;
callback();
}
});
} else {
console.error('Invalid input element provided.');
}
},
allowOnlyEnglish:(inputElement, callback) => {
if (inputElement instanceof HTMLElement) {
inputElement.addEventListener('input', (e) => {
const inputValue = e.target.value;
const englishValue = inputValue.replace(/[^a-zA-Z]/g, '');
if (inputValue !== englishValue) {
inputElement.value = englishValue;
if (typeof callback === 'function') {
callback();
}
}
});
} else {
console.error('Invalid input element provided.');
}
},
isGreaterThan:(inputElement, threshold, callback) => {
if (inputElement instanceof HTMLElement) {
inputElement.addEventListener('input', (e) => {
const inputValue = parseFloat(e.target.value);
if (!isNaN(inputValue) && inputValue > threshold) {
callback(true);
} else {
callback(false);
}
});
} else {
console.error('Invalid input element provided.');
}
},
isLessThan:(inputElement, threshold, callback) => {
if (inputElement instanceof HTMLElement) {
inputElement.addEventListener('input', (e) => {
const inputValue = parseFloat(e.target.value);
if (!isNaN(inputValue) && inputValue < threshold) {
callback(true);
} else {
callback(false);
}
});
} else {
console.error('Invalid input element provided.');
}
},
};
export const scheduleAction = (delayInSeconds, callback) => {
const delayInMillis = delayInSeconds * 1000;
setTimeout(callback, delayInMillis);
};
export const $URL = {
navigateToPage: (page, params) => {
const url = new URL(page.endsWith('.html') ? page : page + '.html', window.location.href);
for (const key in params) {
url.searchParams.set(key, params[key]);
}
window.location.href = url.href;
},
getDataFromUrl :() => {
const urlParams = new URLSearchParams(window.location.search);
const data = {};
for (const [key, value] of urlParams) {
data[key] = value;
}
return data;
},
}
export class TimerLibrary {
constructor() {
this.intervalId = null;
this.totalTimeInSeconds = 0;
this.currentTimeInSeconds = 0;
this.displayElement = null;
this.onFinishCallback = null;
}
startCountUp(durationInMinutes, displayElement, onFinishCallback) {
this.totalTimeInSeconds = durationInMinutes * 60;
this.currentTimeInSeconds = 0;
this.displayElement = displayElement;
this.onFinishCallback = onFinishCallback;
if (!(this.displayElement instanceof HTMLElement)) {
console.error('Invalid display element provided.');
return;
}
this.intervalId = setInterval(() => {
this.updateTime();
}, 1000);
}
updateTime() {
const minutes = Math.floor(this.currentTimeInSeconds / 60);
const seconds = this.currentTimeInSeconds % 60;
const formattedTime = `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
this.displayElement.textContent = formattedTime;
this.currentTimeInSeconds++;
if (this.currentTimeInSeconds > this.totalTimeInSeconds) {
this.stopCountUp();
if (this.onFinishCallback && typeof this.onFinishCallback === 'function') {
this.onFinishCallback();
}
}
}
stopCountUp() {
clearInterval(this.intervalId);
}
resumeCountUp() {
this.intervalId = setInterval(() => {
this.updateTime();
}, 1000);
}
getElapsedTime() {
const milliseconds = this.currentTimeInSeconds * 1000;
const seconds = Math.floor(this.currentTimeInSeconds % 60);
const minutes = Math.floor(this.currentTimeInSeconds / 60);
return {
minutes,
seconds,
milliseconds,
formattedTime: `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`,
};
}
}