1 | |
2 | |
3 | |
4 | |
5 | export async function muestraObjeto(raizHtml, objeto) { |
6 | for (const [nombre, definiciones] of Object.entries(objeto)) { |
7 | if (Array.isArray(definiciones)) { |
8 | muestraArray(raizHtml, nombre, definiciones) |
9 | } else if (definiciones !== undefined && definiciones !== null) { |
10 | const elementoHtml = buscaElementoHtml(raizHtml, nombre) |
11 | if (elementoHtml instanceof HTMLImageElement) { |
12 | await muestraImagen(raizHtml, elementoHtml, definiciones) |
13 | } else if (elementoHtml !== null) { |
14 | for (const [atributo, valor] of Object.entries(definiciones)) { |
15 | if (atributo in elementoHtml) { |
16 | elementoHtml[atributo] = valor |
17 | } |
18 | } |
19 | } |
20 | } |
21 | } |
22 | } |
23 | |
24 | window["muestraObjeto"] = muestraObjeto |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | export function buscaElementoHtml(raizHtml, nombre) { |
31 | return raizHtml.querySelector( |
32 | `#${nombre},[name="${nombre}"],[data-name="${nombre}"]`) |
33 | } |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | function muestraArray(raizHtml, propiedad, valores) { |
41 | const conjunto = new Set(valores) |
42 | const elementos = |
43 | raizHtml.querySelectorAll(`[name="${propiedad}"],[data-name="${propiedad}"]`) |
44 | if (elementos.length === 1) { |
45 | const elemento = elementos[0] |
46 | if (elemento instanceof HTMLSelectElement) { |
47 | const options = elemento.options |
48 | for (let i = 0, len = options.length; i < len; i++) { |
49 | const option = options[i] |
50 | option.selected = conjunto.has(option.value) |
51 | } |
52 | return |
53 | } |
54 | } |
55 | for (let i = 0, len = elementos.length; i < len; i++) { |
56 | const elemento = elementos[i] |
57 | if (elemento instanceof HTMLInputElement) { |
58 | elemento.checked = conjunto.has(elemento.value) |
59 | } |
60 | } |
61 | } |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | async function muestraImagen(raizHtml, img, definiciones) { |
69 | const input = getInputParaElementoHtml(raizHtml, img) |
70 | const src = definiciones.src |
71 | if (src !== undefined) { |
72 | img.dataset.inicial = src |
73 | if (input === null) { |
74 | img.src = src |
75 | if (src === "") { |
76 | img.hidden = true |
77 | } else { |
78 | img.hidden = false |
79 | } |
80 | } else { |
81 | const dataUrl = await getDataUrlDeSeleccion(input) |
82 | if (dataUrl !== "") { |
83 | img.hidden = false |
84 | img.src = dataUrl |
85 | } else if (src === "") { |
86 | img.src = "" |
87 | img.hidden = true |
88 | } else { |
89 | img.src = src |
90 | img.hidden = false |
91 | } |
92 | } |
93 | } |
94 | for (const [atributo, valor] of Object.entries(definiciones)) { |
95 | if (atributo !== "src" && atributo in img) { |
96 | img[atributo] = valor |
97 | } |
98 | } |
99 | } |
100 | |
101 | |
102 | |
103 | |
104 | export function getArchivoSeleccionado(input) { |
105 | const seleccion = input.files |
106 | if (seleccion === null || seleccion.length === 0) { |
107 | return null |
108 | } else { |
109 | return seleccion.item(0) |
110 | } |
111 | } |
112 | |
113 | window["getArchivoSeleccionado"] = getArchivoSeleccionado |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | export function getDataUrlDeSeleccion(input) { |
121 | return new Promise((resolve, reject) => { |
122 | try { |
123 | const seleccion = getArchivoSeleccionado(input) |
124 | if (seleccion === null) { |
125 | resolve("") |
126 | } else { |
127 | const reader = new FileReader() |
128 | reader.onload = () => { |
129 | const dataUrl = reader.result |
130 | if (typeof dataUrl === "string") { |
131 | resolve(dataUrl) |
132 | } else { |
133 | resolve("") |
134 | } |
135 | } |
136 | reader.onerror = () => reject(reader.error) |
137 | reader.readAsDataURL(seleccion) |
138 | } |
139 | } catch (error) { |
140 | resolve(error) |
141 | } |
142 | }) |
143 | } |
144 | |
145 | window["getDataUrlDeSeleccion"] = getDataUrlDeSeleccion |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | export function getInputParaElementoHtml(raizHtml, elementoHtml) { |
152 | const inputId = elementoHtml.getAttribute("data-input") |
153 | if (inputId === null) { |
154 | return null |
155 | } else { |
156 | const input = buscaElementoHtml(raizHtml, inputId) |
157 | if (input instanceof HTMLInputElement) { |
158 | return input |
159 | } else { |
160 | return null |
161 | } |
162 | } |
163 | } |