4. lib / js / muestraObjeto.js

1import { exportaAHtml } from "./exportaAHtml.js"
2
3/**
4 * @param { Document | HTMLElement } raizHtml
5 * @param { any } objeto
6 */
7export 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}
36exportaAHtml(muestraObjeto)
37
38/**
39 * @param { Document | HTMLElement } raizHtml
40 * @param { string } nombre
41 */
42export function buscaElementoHtml(raizHtml, nombre) {
43 return raizHtml.querySelector(
44 `[id="${nombre}"],[name="${nombre}"],[data-name="${nombre}"]`)
45}
46
47/**
48 * @param { Document | HTMLElement } raizHtml
49 * @param { string } propiedad
50 * @param {any[]} valores
51 */
52function 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 * @param { Document | HTMLElement } raizHtml
83 * @param { HTMLInputElement } input
84 * @param { any } definiciones
85 */
86function 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 * @param { Document | HTMLElement } raizHtml
116 * @param { HTMLElement } elementoHtml
117 */
118export 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}
skip_previous skip_next