|
| 1 | +/* Copyright 2025 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { noContextMenu, stopEvent } from "pdfjs-lib"; |
| 17 | + |
| 18 | +class Menu { |
| 19 | + #triggeringButton; |
| 20 | + |
| 21 | + #menu; |
| 22 | + |
| 23 | + #menuItems; |
| 24 | + |
| 25 | + #openMenuAC = null; |
| 26 | + |
| 27 | + #menuAC = new AbortController(); |
| 28 | + |
| 29 | + #lastIndex = -1; |
| 30 | + |
| 31 | + /** |
| 32 | + * Create a menu for the given button. |
| 33 | + * @param {HTMLElement} menuContainer |
| 34 | + * @param {HTMLElement} triggeringButton |
| 35 | + * @param {Array<HTMLElement>|null} menuItems |
| 36 | + */ |
| 37 | + constructor(menuContainer, triggeringButton, menuItems) { |
| 38 | + this.#menu = menuContainer; |
| 39 | + this.#triggeringButton = triggeringButton; |
| 40 | + if (Array.isArray(menuItems)) { |
| 41 | + this.#menuItems = menuItems; |
| 42 | + } else { |
| 43 | + this.#menuItems = []; |
| 44 | + for (const button of this.#menu.querySelectorAll("button")) { |
| 45 | + this.#menuItems.push(button); |
| 46 | + } |
| 47 | + } |
| 48 | + this.#setUpMenu(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Close the menu. |
| 53 | + */ |
| 54 | + #closeMenu() { |
| 55 | + if (!this.#openMenuAC) { |
| 56 | + return; |
| 57 | + } |
| 58 | + const menu = this.#menu; |
| 59 | + menu.classList.toggle("hidden", true); |
| 60 | + this.#triggeringButton.ariaExpanded = "false"; |
| 61 | + this.#openMenuAC.abort(); |
| 62 | + this.#openMenuAC = null; |
| 63 | + if (menu.contains(document.activeElement)) { |
| 64 | + // If the menu is closed while focused, focus the actions button. |
| 65 | + setTimeout(() => { |
| 66 | + if (!menu.contains(document.activeElement)) { |
| 67 | + this.#triggeringButton.focus(); |
| 68 | + } |
| 69 | + }, 0); |
| 70 | + } |
| 71 | + this.#lastIndex = -1; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Set up the menu. |
| 76 | + */ |
| 77 | + #setUpMenu() { |
| 78 | + this.#triggeringButton.addEventListener("click", e => { |
| 79 | + if (this.#openMenuAC) { |
| 80 | + this.#closeMenu(); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const menu = this.#menu; |
| 85 | + menu.classList.toggle("hidden", false); |
| 86 | + this.#triggeringButton.ariaExpanded = "true"; |
| 87 | + this.#openMenuAC = new AbortController(); |
| 88 | + const signal = AbortSignal.any([ |
| 89 | + this.#menuAC.signal, |
| 90 | + this.#openMenuAC.signal, |
| 91 | + ]); |
| 92 | + window.addEventListener( |
| 93 | + "pointerdown", |
| 94 | + ({ target }) => { |
| 95 | + if (target !== this.#triggeringButton && !menu.contains(target)) { |
| 96 | + this.#closeMenu(); |
| 97 | + } |
| 98 | + }, |
| 99 | + { signal } |
| 100 | + ); |
| 101 | + window.addEventListener("blur", this.#closeMenu.bind(this), { signal }); |
| 102 | + }); |
| 103 | + |
| 104 | + const { signal } = this.#menuAC; |
| 105 | + |
| 106 | + this.#menu.addEventListener( |
| 107 | + "keydown", |
| 108 | + e => { |
| 109 | + switch (e.key) { |
| 110 | + case "Escape": |
| 111 | + this.#closeMenu(); |
| 112 | + stopEvent(e); |
| 113 | + break; |
| 114 | + case "ArrowDown": |
| 115 | + case "Tab": |
| 116 | + this.#goToNextItem(e.target, true); |
| 117 | + stopEvent(e); |
| 118 | + break; |
| 119 | + case "ArrowUp": |
| 120 | + case "ShiftTab": |
| 121 | + this.#goToNextItem(e.target, false); |
| 122 | + stopEvent(e); |
| 123 | + break; |
| 124 | + case "Home": |
| 125 | + this.#menuItems |
| 126 | + .find( |
| 127 | + item => !item.disabled && !item.classList.contains("hidden") |
| 128 | + ) |
| 129 | + .focus(); |
| 130 | + stopEvent(e); |
| 131 | + break; |
| 132 | + case "End": |
| 133 | + this.#menuItems |
| 134 | + .findLast( |
| 135 | + item => !item.disabled && !item.classList.contains("hidden") |
| 136 | + ) |
| 137 | + .focus(); |
| 138 | + stopEvent(e); |
| 139 | + break; |
| 140 | + } |
| 141 | + }, |
| 142 | + { signal, capture: true } |
| 143 | + ); |
| 144 | + this.#menu.addEventListener("contextmenu", noContextMenu, { signal }); |
| 145 | + this.#menu.addEventListener("click", this.#closeMenu.bind(this), { |
| 146 | + signal, |
| 147 | + capture: true, |
| 148 | + }); |
| 149 | + this.#triggeringButton.addEventListener( |
| 150 | + "keydown", |
| 151 | + ev => { |
| 152 | + if (!this.#openMenuAC) { |
| 153 | + return; |
| 154 | + } |
| 155 | + switch (ev.key) { |
| 156 | + case "ArrowDown": |
| 157 | + case "Home": |
| 158 | + this.#menuItems |
| 159 | + .find( |
| 160 | + item => !item.disabled && !item.classList.contains("hidden") |
| 161 | + ) |
| 162 | + .focus(); |
| 163 | + stopEvent(ev); |
| 164 | + break; |
| 165 | + case "ArrowUp": |
| 166 | + case "End": |
| 167 | + this.#menuItems |
| 168 | + .findLast( |
| 169 | + item => !item.disabled && !item.classList.contains("hidden") |
| 170 | + ) |
| 171 | + .focus(); |
| 172 | + stopEvent(ev); |
| 173 | + break; |
| 174 | + case "Escape": |
| 175 | + this.#closeMenu(); |
| 176 | + stopEvent(ev); |
| 177 | + } |
| 178 | + }, |
| 179 | + { signal } |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Go to the next/previous menu item. |
| 185 | + * @param {HTMLElement} element |
| 186 | + * @param {boolean} forward |
| 187 | + */ |
| 188 | + #goToNextItem(element, forward) { |
| 189 | + const index = |
| 190 | + this.#lastIndex === -1 |
| 191 | + ? this.#menuItems.indexOf(element) |
| 192 | + : this.#lastIndex; |
| 193 | + const len = this.#menuItems.length; |
| 194 | + const increment = forward ? 1 : len - 1; |
| 195 | + for ( |
| 196 | + let i = (index + increment) % len; |
| 197 | + i !== index; |
| 198 | + i = (i + increment) % len |
| 199 | + ) { |
| 200 | + const menuItem = this.#menuItems[i]; |
| 201 | + if (!menuItem.disabled && !menuItem.classList.contains("hidden")) { |
| 202 | + menuItem.focus(); |
| 203 | + this.#lastIndex = i; |
| 204 | + break; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + destroy() { |
| 210 | + this.#closeMenu(); |
| 211 | + this.#menuAC?.abort(); |
| 212 | + this.#menuAC = null; |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +export { Menu }; |
0 commit comments