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
|
export class DomOptions {
attributes;
children;
classes;
id;
styles;
/**
* @param {Object} [options]
* @param {Partial<CSSStyleDeclaration>} [options.styles]
* @param {string|null} [options.id]
* @param {string[]} [options.classes]
* @param {HTMLElement[]} [options.children]
* @param {Record<string, string>} [options.attributes]
*/
constructor({ id = "", styles = {}, classes = [], children = [], attributes = {} } = {}) {
this.attributes = attributes;
this.children = children;
this.classes = classes;
this.id = id;
this.styles = styles;
}
}
/**
* Toast notification types
* @enum {string}
*/
export const ToastType = {
error: "error",
success: "success",
warning: "warning",
};
export class Dom {
/**
* Create a `<div>`
* @param {DomOptions} options
* @returns {HTMLDivElement}
*/
static div(options = new DomOptions()) {
const div = document.createElement("div");
Object.assign(div.style, options.styles);
if (options.id) div.id = options.id;
for (const cls of options.classes) div.classList.add(cls);
for (const [k, v] of Object.entries(options.attributes)) div.setAttribute(k, v);
if (options.children) div.append(...options.children);
return div;
}
/**
* Create a `<span>`
* @param {string} text
* @param {DomOptions} options
* @returns {HTMLSpanElement}
*/
static span(text, options = new DomOptions()) {
const span = document.createElement("span");
Object.assign(span.style, options.styles);
if (options.id) span.id = options.id;
for (const cls of options.classes) span.classList.add(cls);
span.textContent = text;
for (const [k, v] of Object.entries(options.attributes)) span.setAttribute(k, v);
if (options.children) span.append(...options.children);
return span;
}
/**
* Create a `<button>`
* @param { string} text
* @param { (e: Event) => void } onClick
* @param {DomOptions} o
* @returns {HTMLButtonElement}
*/
static button(text, onClick, o = new DomOptions()) {
const button = document.createElement("button");
Object.assign(button.style, o.styles);
if (o.id) button.id = o.id;
for (const cls of o.classes) button.classList.add(cls);
if (text) button.textContent = text;
for (const [k, v] of Object.entries(o.attributes)) button.setAttribute(k, v);
if (o.children) button.append(...o.children);
if (onClick) button.addEventListener("click", onClick);
return button;
}
/**
* Create an `<input>`
* @param { string} type
* @param { (e: Event) => void } onChange
* @param { string|number} value
* @param { string} placeholder
* @param {DomOptions} [o]
* @returns {HTMLInputElement}
*/
static input(type, onChange, value = "", placeholder = "", o = new DomOptions()) {
const input = document.createElement("input");
Object.assign(input.style, o.styles);
if (o.id) input.id = o.id;
for (const cls of o.classes) input.classList.add(cls);
for (const [k, v] of Object.entries(o.attributes)) input.setAttribute(k, v);
input.type = type;
input.placeholder = placeholder;
input.value = value.toString();
if (onChange) {
input.addEventListener("change", onChange);
}
return input;
}
/**
* Create a `<strong>`
* @param {string} text
*/
static strong(text) {
const strong = document.createElement("strong");
strong.textContent = text;
return strong;
}
/**
* Create a `<a>`
* @param {string} url
* @param {DomOptions} o
* @param {string|undefined} text
*/
static a(url, o, text = undefined) {
const link = document.createElement("a");
if (text) link.text = text;
link.href = url;
Object.assign(link.style, o.styles);
if (o.id) link.id = o.id;
for (const cls of o.classes) link.classList.add(cls);
for (const [k, v] of Object.entries(o.attributes)) link.setAttribute(k, v);
if (o.children) link.append(...o.children);
return link;
}
/**
* Create a `<label>`
* @param {string} to
* @param {string} text
* @param {DomOptions} o
* @returns {HTMLLabelElement}
*/
static label(to, text, o = new DomOptions()) {
const label = document.createElement("label");
Object.assign(label.style, o.styles);
if (o.id) label.id = o.id;
for (const cls of o.classes) label.classList.add(cls);
for (const [k, v] of Object.entries(o.attributes)) label.setAttribute(k, v);
if (o.children) label.append(...o.children);
label.textContent = text;
label.htmlFor = to;
return label;
}
/**
* Create a heading `<h1>`–`<h6>`
* @param {1|2|3|4|5|6} level
* @param {string} text
* @param {DomOptions} o
* @returns {HTMLHeadingElement}
*/
static heading(level, text, o = new DomOptions()) {
const heading = document.createElement(`h${level}`);
Object.assign(heading.style, o.styles);
if (o.id) heading.id = o.id;
for (const cls of o.classes) heading.classList.add(cls);
if (text) heading.textContent = text;
for (const [k, v] of Object.entries(o.attributes)) heading.setAttribute(k, v);
if (o.children) heading.append(...o.children);
return heading;
}
/**
* Create an `<img>`
* @param {string} src
* @param {DomOptions} o
* @returns {HTMLImageElement}
*/
static img(src, o = new DomOptions()) {
const img = document.createElement("img");
Object.assign(img.style, o.styles);
if (o.id) img.id = o.id;
for (const cls of o.classes) img.classList.add(cls);
for (const [k, v] of Object.entries(o.attributes)) img.setAttribute(k, v);
img.src = src;
return img;
}
/**
* Create a `<select>`
* @param {DomOptions} o
* @param { (e: Event) => void } onChange
* @returns {HTMLSelectElement}
*/
static select(onChange, o = new DomOptions()) {
const select = document.createElement("select");
Object.assign(select.style, o.styles);
if (o.id) select.id = o.id;
for (const cls of o.classes) select.classList.add(cls);
for (const [k, v] of Object.entries(o.attributes)) select.setAttribute(k, v);
if (o.children) select.append(...o.children);
if (onChange) select.addEventListener("change", onChange);
return select;
}
/**
* Create an `<option>`
* @param {string} value
* @param {string} text
* @param {boolean} [selected=false]
* @returns {HTMLOptionElement}
*/
static option(value, text, selected = false) {
const opt = document.createElement("option");
opt.value = value;
opt.textContent = text;
opt.selected = selected;
return opt;
}
/**
* Create a `<p>`
* @param {string} text
* @param {DomOptions} o
* @returns {HTMLParagraphElement}
*/
static p(text, o = new DomOptions()) {
const p = document.createElement("p");
Object.assign(p.style, o.styles);
if (o.id) p.id = o.id;
for (const cls of o.classes) p.classList.add(cls);
if (text) p.textContent = text;
for (const [k, v] of Object.entries(o.attributes)) p.setAttribute(k, v);
if (o.children) p.append(...o.children);
return p;
}
/**
* Create a dual range input (min and max)
* @param {number} min - Minimum possible value
* @param {number} max - Maximum possible value
* @param {number} currentMin - Current minimum value
* @param {number} currentMax - Current maximum value
* @param {number} step - Step size
* @param {(min: number, max: number) => void} onChange - Callback when range changes
* @param {DomOptions} options - DOM options
* @returns {HTMLDivElement}
*/
static range(min, max, currentMin, currentMax, step, onChange, options = new DomOptions()) {
const container = document.createElement("div");
Object.assign(container.style, options.styles);
if (options.id) container.id = options.id;
for (const cls of options.classes) container.classList.add(cls);
for (const [k, v] of Object.entries(options.attributes)) container.setAttribute(k, v);
// Ensure current values are within bounds
const safeCurrentMin = Math.max(min, Math.min(currentMin, max));
const safeCurrentMax = Math.max(min, Math.min(currentMax, max));
// Create track container
const trackContainer = Dom.div(
new DomOptions({
styles: {
alignItems: "center",
display: "flex",
height: "20px",
margin: "10px 0",
position: "relative",
},
}),
);
// Create track
const track = Dom.div(
new DomOptions({
styles: {
background: "#e0e0e0",
borderRadius: "4px",
height: "6px",
position: "relative",
width: "100%",
},
}),
);
// Create active range
const activeRange = Dom.div(
new DomOptions({
styles: {
background: "#4caf50",
borderRadius: "4px",
height: "100%",
left: "0%",
position: "absolute",
width: "100%",
},
}),
);
// Create min slider
const minSlider = document.createElement("input");
minSlider.type = "range";
minSlider.min = min.toString();
minSlider.max = max.toString();
minSlider.step = step.toString();
minSlider.value = safeCurrentMin.toString();
Object.assign(minSlider.style, {
appearance: "none",
background: "transparent",
height: "100%",
pointerEvents: "none",
position: "absolute",
width: "100%",
zIndex: "2",
});
minSlider.style.pointerEvents = "auto";
// Create max slider
const maxSlider = document.createElement("input");
maxSlider.type = "range";
maxSlider.min = min.toString();
maxSlider.max = max.toString();
maxSlider.step = step.toString();
maxSlider.value = safeCurrentMax.toString();
Object.assign(maxSlider.style, {
appearance: "none",
background: "transparent",
height: "100%",
pointerEvents: "none",
position: "absolute",
width: "100%",
zIndex: "2",
});
maxSlider.style.pointerEvents = "auto";
// Value displays
const minValueDisplay = Dom.span(
safeCurrentMin.toString(),
new DomOptions({
styles: {
color: "#0066cc",
fontSize: "0.85rem",
fontWeight: "bold",
},
}),
);
const maxValueDisplay = Dom.span(
safeCurrentMax.toString(),
new DomOptions({
styles: {
color: "#0066cc",
fontSize: "0.85rem",
fontWeight: "bold",
},
}),
);
const valueDisplay = Dom.div(
new DomOptions({
children: [minValueDisplay, maxValueDisplay],
styles: {
display: "flex",
justifyContent: "space-between",
marginBottom: "5px",
},
}),
);
// Update active range position
const updateActiveRange = () => {
const minVal = parseInt(minSlider.value);
const maxVal = parseInt(maxSlider.value);
const minPercent = ((minVal - min) / (max - min)) * 100;
const maxPercent = ((maxVal - min) / (max - min)) * 100;
activeRange.style.left = `${minPercent}%`;
activeRange.style.width = `${maxPercent - minPercent}%`;
minValueDisplay.textContent = minVal.toString();
maxValueDisplay.textContent = maxVal.toString();
};
// Event listeners
minSlider.addEventListener("input", () => {
const minVal = parseInt(minSlider.value);
const maxVal = parseInt(maxSlider.value);
if (minVal > maxVal) {
minSlider.value = maxVal.toString();
}
updateActiveRange();
onChange(parseInt(minSlider.value), parseInt(maxSlider.value));
});
maxSlider.addEventListener("input", () => {
const minVal = parseInt(minSlider.value);
const maxVal = parseInt(maxSlider.value);
if (maxVal < minVal) {
maxSlider.value = minVal.toString();
}
updateActiveRange();
onChange(parseInt(minSlider.value), parseInt(maxSlider.value));
});
// Initial update
updateActiveRange();
// Assemble track
track.appendChild(activeRange);
track.appendChild(minSlider);
track.appendChild(maxSlider);
trackContainer.appendChild(track);
// Assemble container
container.appendChild(valueDisplay);
container.appendChild(trackContainer);
if (options.children) container.append(...options.children);
return container;
}
}
|