B. js / lib / custom / md-options-menu.js

1
import { abreElementoHtml } from "../abreElementoHtml.js"
2
import { cierraElementoHtmo } from "../cierraElementoHtmo.js"
3
import { querySelector } from "../querySelector.js"
4
5
export class MdOptionsMenu extends HTMLElement {
6
7
 getContent() {
8
  return /* HTML */`
9
10
   <style>
11
12
    :host {
13
     position: absolute;
14
    }
15
16
   </style>
17
18
   <slot></slot>`
19
 }
20
21
 constructor() {
22
  super()
23
  const shadow = this.attachShadow({ mode: "open" })
24
  shadow.innerHTML = this.getContent()
25
  this._configuraOpciones = this._configuraOpciones.bind(this)
26
27
  /**
28
   * @private
29
   * @type { HTMLSlotElement }
30
   */
31
  this._slot = querySelector(shadow, "slot")
32
  /**
33
   * @private
34
   * @type { HTMLElement[] }
35
   */
36
  this._opciones = []
37
  this._slot.addEventListener("slotchange", this._configuraOpciones)
38
 }
39
40
 connectedCallback() {
41
  this.classList.add("md-menu")
42
  this.role = "listbox"
43
 }
44
45
 /**
46
  * @returns {readonly Readonly<HTMLElement>[]}
47
  */
48
 get opciones() {
49
  return this._opciones
50
 }
51
52
 get seleccion() {
53
  /** @type { HTMLInputElement | null } */
54
  const seleccionado = this.querySelector(".selected")
55
  return seleccionado === null ? "" : seleccionado.value
56
 }
57
58
 _configuraOpciones() {
59
  /**
60
   * @type {HTMLElement[]}
61
  */
62
  const opciones = []
63
  for (const opcion of this._slot.assignedElements()) {
64
   opcion.role = "option"
65
   if (opcion instanceof HTMLElement) {
66
    opciones.push(opcion)
67
   }
68
  }
69
  this._opciones = opciones
70
 }
71
72
 abre() {
73
  abreElementoHtml(this)
74
 }
75
76
77
 cierra() {
78
  cierraElementoHtmo(this)
79
 }
80
81
 /**
82
  * @param {string} value
83
  */
84
 muestraValue(value) {
85
  let texto = ""
86
  for (const opcion of this._opciones) {
87
   if (opcion.dataset.value === value) {
88
    opcion.classList.add("selected")
89
    let textContent = opcion.textContent
90
    if (texto === "" && textContent !== null) {
91
     textContent = textContent.trim()
92
     if (textContent !== "") {
93
      texto = textContent
94
     }
95
    }
96
   } else {
97
    opcion.classList.remove("selected")
98
   }
99
  }
100
  return texto
101
 }
102
103
}
104
105
customElements.define("md-options-menu", MdOptionsMenu)
skip_previous skip_next