1 | import { exportaAHtml } from "./exportaAHtml.js" |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | export function muestraObjeto(raizHtml, objeto) { |
8 | for (const [nombre, definiciones] of Object.entries(objeto)) { |
9 | |
10 | if (Array.isArray(definiciones)) { |
11 | |
12 | muestraArray(raizHtml, nombre, definiciones) |
13 | |
14 | } else if (definiciones !== undefined && definiciones !== null) { |
15 | |
16 | const elementoHtml = buscaElementoHtml(raizHtml, nombre) |
17 | |
18 | if (elementoHtml instanceof HTMLInputElement) { |
19 | |
20 | muestraInput(raizHtml, elementoHtml, definiciones) |
21 | |
22 | } else if (elementoHtml !== null) { |
23 | |
24 | for (const [propiedad, valor] of Object.entries(definiciones)) { |
25 | if (propiedad in elementoHtml) { |
26 | elementoHtml[propiedad] = valor |
27 | } |
28 | } |
29 | |
30 | } |
31 | |
32 | } |
33 | |
34 | } |
35 | } |
36 | exportaAHtml(muestraObjeto) |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | export function buscaElementoHtml(raizHtml, nombre) { |
43 | return raizHtml.querySelector( |
44 | `[id="${nombre}"],[name="${nombre}"],[data-name="${nombre}"]`) |
45 | } |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | function muestraArray(raizHtml, propiedad, valores) { |
53 | |
54 | const conjunto = new Set(valores) |
55 | const elementos = |
56 | raizHtml.querySelectorAll(`[name="${propiedad}"],[data-name="${propiedad}"]`) |
57 | |
58 | if (elementos.length === 1) { |
59 | |
60 | const elemento = elementos[0] |
61 | if (elemento instanceof HTMLSelectElement) { |
62 | for (let i = 0, options = elemento.options, len = options.length; i < len |
63 | ; i++) { |
64 | const option = options[i] |
65 | option.selected = conjunto.has(option.value) |
66 | } |
67 | return |
68 | } |
69 | |
70 | } |
71 | |
72 | for (let i = 0, len = elementos.length; i < len; i++) { |
73 | const elemento = elementos[i] |
74 | if (elemento instanceof HTMLInputElement) { |
75 | elemento.checked = conjunto.has(elemento.value) |
76 | } |
77 | } |
78 | |
79 | } |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | function muestraInput(raizHtml, input, definiciones) { |
87 | |
88 | for (const [propiedad, valor] of Object.entries(definiciones)) { |
89 | |
90 | if (propiedad == "data-file") { |
91 | |
92 | const img = getImgParaElementoHtml(raizHtml, input) |
93 | if (img !== null) { |
94 | input.dataset.file = valor |
95 | input.value = "" |
96 | if (valor === "") { |
97 | img.src = "" |
98 | img.hidden = true |
99 | } else { |
100 | img.src = valor |
101 | img.hidden = false |
102 | } |
103 | } |
104 | |
105 | } else if (propiedad in input) { |
106 | |
107 | input[propiedad] = valor |
108 | |
109 | } |
110 | } |
111 | |
112 | } |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | export function getImgParaElementoHtml(raizHtml, elementoHtml) { |
119 | const imgId = elementoHtml.getAttribute("data-img") |
120 | if (imgId === null) { |
121 | return null |
122 | } else { |
123 | const input = buscaElementoHtml(raizHtml, imgId) |
124 | if (input instanceof HTMLImageElement) { |
125 | return input |
126 | } else { |
127 | return null |
128 | } |
129 | } |
130 | } |