-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponents.js
730 lines (615 loc) · 17.8 KB
/
Components.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// Componentes interactivos del DOM
'use strict';
import {EventDispathcer} from './EventDispatcher.js';
export class CustomComponent extends EventDispathcer {
constructor(owner = null, id = null, tagName = null) {
super();
this.element = null;
if (id) {
this.element = document.getElementById(id);
if (!this.element && tagName) {
this.element = document.createElement(tagName);
this.element.id = id;
document.body.appendChild(this.container);
}
}
if (owner && owner instanceof CustomContainer) {
owner.addComponent(this);
}
}
get element() {
return this.htmlElement;
}
set element(element) {
this.htmlElement = element;
}
setEvent(type, listener) {
this.addEventListener(type, listener);
this.element.addEventListener(type, listener);
}
removeEvent(type, listener) {
this.removeEventListener(type, listener);
this.element.removeEventListener(type, listener);
}
hide() {
this.element.style.display = 'none';
}
show() {
this.element.style.display = '';
}
}
export class CustomContainer extends CustomComponent {
constructor(containerId = 'myCustomComponentContainer', owner = null) {
super(owner, id, 'div');
this.container = element;
this.components = [];
}
addComponent(component) {
if (component instanceof Array) {
component.forEach((comp) => {
this.addComponent(comp);
});
} else {
this.components.push(component);
this.container.appendChild(component.getElement());
}
}
removeComponent(component) {
if (component instanceof Array) {
component.forEach((comp) => {
this.addComponent(comp);
});
} else {
this.components = this.components.filter((c) => (c !== compoment) && (compoment.element.id !== component.element.id));
this.container.removeChild(component.element);
}
}
}
export class Button extends CustomComponent {
constructor(owner, text, onClick) {
super(owner, null, 'button');
this.caption = text;
this.setEvent('click', onClick.bind(this));
}
get caption() {
return this.element().innerText;
}
set caption(text) {
this.element().innerText = text;
}
}
export class Edit extends CustomComponent {
constructor(owner, text, onChange) {
super(owner, null, 'input');
this.element.type = 'text';
this.value = text;
this.setEvent('input', onChange.bind(this));
}
get value() {
return this.element().value;
}
set value(value) {
this.element().value = value;
}
}
export class Label extends CustomComponent {
constructor(owner, text) {
super(owner, null, 'label');
this.element.text = text;
}
get text() {
return this.element().innerText;
}
set text(text) {
this.element().innerText = text;
}
}
export class ComboBox extends CustomComponent {
constructor(owner, options = [
{value: 'option1', text: 'Option 1'},
{value: 'option2', text: 'Option 2'},
{value: 'option3', text: 'Option 3'},
]) {
super(owner, null, 'select');
options.forEach((option) => {
const optionElement = document.createElement('option');
optionElement.value = option.value;
optionElement.text = option.text;
this.element.add(optionElement);
});
}
get value() {
return this.element().value;
}
set value(value) {
this.element().value = value;
}
}
export class Checkbox extends CustomComponent {
constructor(owner, labelText, onChange) {
super(owner, null, 'input');
this.element.type = 'checkbox';
this.element.setEvent('change', onChange);
const label = document.createElement('label');
label.innerText = labelText;
label.prepend(this.element);
this.element = label;
}
get value() {
return this.element().querySelector('input').checked;
}
set value(value) {
this.element().querySelector('input').checked = value;
}
}
export class RadioButton extends CustomComponent {
constructor(owner, name, labelText, onChange) {
super(owner, name, 'input');
this.element.type = 'radio';
this.element.name = name;
this.setEvent('change', onChange);
const label = document.createElement('label');
label.innerText = labelText;
label.prepend(this.element);
this.element = label;
}
get value() {
return this.element().querySelector('input').checked;
}
set value(value) {
this.element().querySelector('input').checked = value;
}
}
export class Form extends CustomComponent {
#form;
constructor(
owner,
elements = [],
onSubmit = (value) => {
},
submitButton = null,
formId = 'myForm',
) {
super(owner);
this.#form = document.createElement('form');
this.#form.id = formId;
this.#form.addEventListener('submit', this.#onSubmit.bind(this));
this.element(this.#form);
elements.forEach((el) => {
this.#form.appendChild(el.getElement());
this.addComponent(el);
});
if (!submitButton) {
submitButton = document.createElement('button');
submitButton.type = 'submit';
submitButton.innerText = 'Enviar';
}
this.#form.appendChild(submitButton);
this.onSubmit = onSubmit;
}
#onSubmit(event) {
event.preventDefault();
const value = event.target.querySelector('input[type=\'text\']').value;
this.onSubmit(value);
}
}
export function unitaryTest() {
let myForm = new Form();
const myButton = new Button(myForm, 'Click me', function() {
console.log('Button clicked');
});
const myEdit = new Edit(myForm, function(event) {
console.log(`Edit value changed: ${event.target.value}`);
});
const myLabel = new Label(myForm, 'Hello, world!');
const myComboBox = new ComboBox(myForm, [
{value: 'option1', text: 'Option 1'},
{value: 'option2', text: 'Option 2'},
{value: 'option3', text: 'Option 3'},
]);
const myCheckbox1 = new Checkbox(myForm, 'Check me', function(event) {
console.log(`Checkbox value changed: ${event.target.checked}`);
});
const myCheckbox2 = new Checkbox(myForm, 'Check me', function(event) {
console.log(`Checkbox value changed: ${event.target.checked}`);
});
const myRadioButton1 = new RadioButton(myForm, 'myRadioGroup', 'Option 1', function(event) {
console.log(`Radio button 1 value changed: ${event.target.checked}`);
});
const myRadioButton2 = new RadioButton(myForm, 'myRadioGroup', 'Option 1', function(event) {
console.log(`Radio button 2 value changed: ${event.target.checked}`);
});
myButton.caption = 'Click me again';
myEdit.value = 'New value';
myLabel.caption = 'Hello, everyone! Hola a ti.';
myComboBox.value = 'option2';
myCheckbox1.value = true;
myCheckbox2.value = false;
myRadioButton1.value = true;
myRadioButton2.value = false;
// Para usar la clase, puedes crear una instancia de ella y pasar los datos y el ID del contenedor como argumentos:
const tableta = new Table(myForm, datax, 'container');
}
///////////////////////////////////
class Table extends CustomComponent {
constructor(owner, numRows, numCols, headers) {
const table = document.createElement('table');
// Create header row
if (headers) {
const headerRow = document.createElement('tr');
for (let i = 0; i < numCols; i++) {
const headerCell = document.createElement('th');
headerCell.textContent = headers[i];
headerRow.appendChild(headerCell);
}
table.appendChild(headerRow);
}
// Create data rows
for (let i = 0; i < numRows; i++) {
const row = document.createElement('tr');
for (let j = 0; j < numCols; j++) {
const cell = document.createElement('td');
row.appendChild(cell);
}
table.appendChild(row);
}
super(owner, null, null);
this.element = table;
}
setCell(row, col, value) {
this.element.rows[row].cells[col].textContent = value;
}
getCell(row, col) {
return this.element.rows[row].cells[col].textContent;
}
}
class Slider extends CustomComponent {
constructor(owner, labelText, minValue, maxValue, step, defaultValue, onValueChange) {
const slider = document.createElement('div');
slider.classList.add('slider');
const label = document.createElement('label');
label.innerText = labelText;
label.classList.add('slider-label');
slider.appendChild(label);
const input = document.createElement('input');
input.setAttribute('type', 'range');
input.setAttribute('min', minValue);
input.setAttribute('max', maxValue);
input.setAttribute('step', step);
input.setAttribute('value', defaultValue);
input.classList.add('slider-input');
slider.appendChild(input);
let value = defaultValue;
input.addEventListener('input', function() {
value = parseFloat(input.value);
if (onValueChange) {
onValueChange(value);
}
});
super(owner, null, null);
this.element = slider;
this.getValue = function() {
return value;
};
}
}
class ProgressBar extends CustomComponent {
constructor(owner, value, maxValue) {
const progressBar = document.createElement('progress');
progressBar.setAttribute('max', maxValue);
progressBar.setAttribute('value', value);
super(owner, null, null);
this.element = progressBar;
}
setValue(value) {
this.element.value = value;
}
getValue() {
return this.element.value;
}
}
class StatusBar extends CustomComponent {
constructor(owner, items) {
const statusBar = document.createElement('div');
statusBar.classList.add('status-bar');
for (let i = 0; i < items.length; i++) {
const item = document.createElement('div');
item.textContent = items[i];
item.classList.add('status-item');
statusBar.appendChild(item);
}
super(owner, null, null);
this.element = statusBar;
}
setText(index, text) {
this.element.children[index].textContent = text;
}
getText(index) {
return this.element.children[index].textContent;
}
}
class VerticalContainer extends CustomContainer {
constructor(id) {
super(id);
this.element.classList.add('vertical-container');
}
}
class HorizontalContainer extends CustomContainer {
constructor(id) {
super(id);
this.element.classList.add('horizontal-container');
}
}
// Probar
class Modal extends CustomContainer {
constructor(title, onAccept = null, onCancel = null) {
super(null, null, 'div');
this.title = title;
this.onAccept = onAccept;
this.onCancel = onCancel;
// Crear el título del modal
const modalTitle = document.createElement('h2');
modalTitle.textContent = this.title;
this.addComponent(modalTitle);
// Crear los campos del formulario
const form = document.createElement('form');
form.id = 'modal-form';
this.addComponent(form);
// Agregar el botón de aceptar al formulario
const acceptButton = new Button(null, 'Aceptar', async () => {
if (this.onAccept) {
this.onAccept();
}
this.close();
});
form.appendChild(acceptButton.getElement());
// Agregar el botón de cancelar al formulario
const cancelButton = new Button(null, 'Cancelar', async () => {
if (this.onCancel) {
this.onCancel();
}
this.close();
});
form.appendChild(cancelButton.getElement());
// Agregar el evento para cerrar el modal
const closeButton = new Button(null, 'Cerrar', () => {
this.close();
});
closeButton.element.style.position = 'absolute';
closeButton.element.style.top = '5px';
closeButton.element.style.right = '5px';
this.addComponent(closeButton);
// Establecer los estilos del modal
this.element.style.display = 'none';
this.element.style.backgroundColor = '#f1f1f1';
this.element.style.borderRadius = '5px';
this.element.style.padding = '20px';
this.element.style.position = 'fixed';
this.element.style.top = '50%';
this.element.style.left = '50%';
this.element.style.transform = 'translate(-50%, -50%)';
this.element.style.zIndex = '9999';
// Establecer los estilos del fondo del modal
const backdrop = document.createElement('div');
backdrop.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
backdrop.style.width = '100%';
backdrop.style.height = '100%';
backdrop.style.position = 'fixed';
backdrop.style.top = '0';
backdrop.style.left = '0';
backdrop.style.zIndex = '9998';
backdrop.addEventListener('click', () => {
this.close();
});
this.addComponent(backdrop);
}
open() {
this.element.style.display = 'block';
}
close() {
this.element.style.display = 'none';
}
}
class Modal2 extends Component {
constructor(title, onAccept = null, onCancel = null) {
super();
this.title = title;
this.onAccept = onAccept;
this.onCancel = onCancel;
this.modalTitle = new Component('h2', {
textContent: this.title,
});
this.acceptButton = new Button('Aceptar', {
onClick: async () => {
if (this.onAccept) {
this.onAccept();
}
this.close();
},
});
this.cancelButton = new Button('Cancelar', {
onClick: async () => {
if (this.onCancel) {
this.onCancel();
}
this.close();
},
});
this.closeButton = new Button('Cerrar', {
onClick: () => {
this.close();
},
});
this.form = new Component('form', {
children: [this.acceptButton, this.cancelButton],
});
this.element = new Component('div', {
children: [this.modalTitle, this.form, this.closeButton],
style: {
display: 'none',
backgroundColor: '#f1f1f1',
borderRadius: '5px',
padding: '20px',
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
zIndex: '9999',
},
});
this.backdrop = new Component('div', {
style: {
backgroundColor: 'rgba(0, 0, 0, 0.5)',
width: '100%',
height: '100%',
position: 'fixed',
top: '0',
left: '0',
zIndex: '9998',
},
onClick: () => {
this.close();
},
});
}
open() {
this.element.setStyle('display', 'block');
document.body.appendChild(this.backdrop.getElement());
document.body.appendChild(this.element.getElement());
}
close() {
this.element.setStyle('display', 'none');
document.body.removeChild(this.backdrop.getElement());
document.body.removeChild(this.element.getElement());
}
}
class Button extends Component {
constructor(label, {onClick}) {
super('button', {
textContent: label,
onClick,
style: {
margin: '5px',
},
});
}
}
class PresentableText extends CustomComponent {
constructor(owner, text, className = 'p') {
super(owner);
this.element = document.createElement(className);
this.element.textContent = text;
if (className) {
this.element.classList.add(className);
}
}
setText(text) {
this.element.textContent = text;
}
setClass(className) {
this.element.classList.add(className);
}
hide() {
this.element.style.display = 'none';
}
show() {
this.element.style.display = '';
}
}
class Paragraph extends PresentableText {
constructor(text, className) {
super(text, className);
this.element = document.createElement('p');
this.element.textContent = text;
if (className) {
this.element.classList.add(className);
}
}
}
class Heading extends PresentableText {
constructor(text, level, className) {
super(text, className);
this.element = document.createElement('h' + level);
this.element.textContent = text;
if (className) {
this.element.classList.add(className);
}
}
}
// Puramente teórico desde ChatGPT y sin probar
export class Menu extends CustomComponent {
constructor(owner, id, items, onSelect) {
super(owner, id, 'ul');
this.items = items;
this.onSelect = onSelect;
this.renderItems();
}
renderItems() {
this.items.forEach((item) => {
const li = document.createElement('li');
li.innerText = item;
li.addEventListener('click', () => {
this.onSelect(item);
});
this.element.appendChild(li);
});
}
get items() {
return this._items;
}
set items(items) {
this._items = items;
this.renderItems();
}
get onSelect() {
return this._onSelect;
}
set onSelect(onSelect) {
this._onSelect = onSelect;
}
}
export class ContextMenu extends Menu {
constructor(owner, id, items, onSelect) {
super(owner, id, items, onSelect);
}
renderItems() {
this.items.forEach((item) => {
const li = document.createElement('li');
if (item instanceof Menu) {
const subMenu = document.createElement('ul');
subMenu.style.display = 'none';
subMenu.appendChild(item.getElement());
li.innerText = 'Expand'; // Add a label for the expandable item
li.appendChild(subMenu);
li.addEventListener('click', () => {
subMenu.style.display = subMenu.style.display === 'none' ? 'block' : 'none';
});
} else {
li.innerText = item;
li.addEventListener('click', () => {
this.onSelect(item);
});
}
this.element.appendChild(li);
});
}
}
// Un menú estático de parte superior de pantalla:
export class StaticMenu extends Menu {
constructor(owner, id, items, onSelect) {
super(owner, id, items, onSelect);
}
render() {
const nav = document.createElement('nav');
nav.classList.add('menu');
nav.appendChild(this.getElement());
document.body.insertBefore(nav, document.body.firstChild);
}
unitaryTest() {
// Esta es una forma de probarlo en la aplicación principal.
const menu = new StaticMenu(null, 'menu', ['Inicio', 'Acerca de nosotros', 'Productos', 'Servicios', 'Contacto'], (item) => {
console.log(`Se seleccionó ${item}`);
});
menu.render();
}
}