| 1 | import { abreElementoHtml } from "../abreElementoHtml.js" |
| 2 | import { cierraElementoHtmo } from "../cierraElementoHtmo.js" |
| 3 | |
| 4 | export class MdOverflowMenu extends HTMLElement { |
| 5 | |
| 6 | getContent() { |
| 7 | return ` |
| 8 | |
| 9 | <style> |
| 10 | |
| 11 | :host { |
| 12 | position: fixed; |
| 13 | min-width: 7rem; |
| 14 | max-width: 280px; |
| 15 | } |
| 16 | |
| 17 | ::slotted(*) { |
| 18 | text-align: start; |
| 19 | width: 100%; |
| 20 | border: none; |
| 21 | background-color: transparent; |
| 22 | } |
| 23 | |
| 24 | </style> |
| 25 | |
| 26 | <slot></slot>` |
| 27 | } |
| 28 | |
| 29 | constructor() { |
| 30 | super() |
| 31 | const shadow = this.attachShadow({ mode: "open" }) |
| 32 | shadow.innerHTML = this.getContent() |
| 33 | this.clicCierra = this.clicCierra.bind(this) |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | this._toggleButton = null |
| 39 | } |
| 40 | |
| 41 | connectedCallback() { |
| 42 | this.classList.add("md-menu") |
| 43 | this.role = "menu" |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | alterna(toggleButton) { |
| 50 | this._toggleButton = toggleButton |
| 51 | const top = toggleButton.offsetTop + toggleButton.offsetHeight - 4 |
| 52 | const right = |
| 53 | innerWidth - (toggleButton.offsetLeft + toggleButton.offsetWidth) - 3 |
| 54 | this.style.top = `${top}px` |
| 55 | this.style.right = `${right}px` |
| 56 | const list = this.classList |
| 57 | if (list.contains("open")) { |
| 58 | this.cierra() |
| 59 | } else { |
| 60 | this.abre() |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | abre() { |
| 65 | document.addEventListener("click", this.clicCierra) |
| 66 | abreElementoHtml(this) |
| 67 | } |
| 68 | |
| 69 | cierra() { |
| 70 | document.removeEventListener("click", this.clicCierra) |
| 71 | cierraElementoHtmo(this) |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | clicCierra(evt) { |
| 78 | const target = evt.target |
| 79 | if (this.classList.contains("open") |
| 80 | && this._toggleButton !== null |
| 81 | && target instanceof HTMLElement |
| 82 | && !this._toggleButton.contains(target)) { |
| 83 | this.cierra() |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | customElements.define("md-overflow-menu", MdOverflowMenu) |