|
| 1 | +/** |
| 2 | + * For the full copyright and license information, please view the |
| 3 | + * docs/licenses/LICENSE.txt file that was distributed with this source code. |
| 4 | + */ |
| 5 | + |
| 6 | +const {$} = window; |
| 7 | + |
| 8 | +interface HookableInfo { |
| 9 | + id: number; |
| 10 | + name: string; |
| 11 | + title: string; |
| 12 | + registered: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Handles dynamic hook selector on the "Hook a module" form. |
| 17 | + * When the module select changes, fetches possible hooks via AJAX and |
| 18 | + * repopulates the hook dropdown. |
| 19 | + */ |
| 20 | +export default class HookModuleHandler { |
| 21 | + private readonly moduleSelector: HTMLSelectElement | null | undefined; |
| 22 | + |
| 23 | + private readonly hookSelector: HTMLSelectElement | null | undefined; |
| 24 | + |
| 25 | + private readonly hookUrl: string | null | undefined; |
| 26 | + |
| 27 | + private readonly availableLabel: string | undefined; |
| 28 | + |
| 29 | + private readonly registeredLabel: string | undefined; |
| 30 | + |
| 31 | + // Shared select2 config so both selectors match the rest of the BO |
| 32 | + // (bootstrap4 theme) and always expose the search box, even for short lists. |
| 33 | + private readonly select2Options = { |
| 34 | + theme: 'bootstrap4', |
| 35 | + minimumResultsForSearch: 0, |
| 36 | + width: '100%', |
| 37 | + }; |
| 38 | + |
| 39 | + constructor() { |
| 40 | + const form = document.querySelector<HTMLFormElement>('[data-hook-url]'); |
| 41 | + |
| 42 | + if (!form) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + this.hookUrl = form.dataset.hookUrl ?? null; |
| 47 | + this.availableLabel = form.dataset.labelAvailable ?? 'Available hooks'; |
| 48 | + this.registeredLabel = form.dataset.labelRegistered ?? 'Already registered hooks'; |
| 49 | + this.moduleSelector = form.querySelector<HTMLSelectElement>('[data-module-selector="true"]'); |
| 50 | + this.hookSelector = form.querySelector<HTMLSelectElement>('[data-hook-selector="true"]'); |
| 51 | + |
| 52 | + if (!this.moduleSelector || !this.hookSelector || !this.hookUrl) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Enhance both selects with select2 (search helper). The hook select keeps |
| 57 | + // its initial disabled state until a module is chosen. |
| 58 | + $(this.moduleSelector).select2(this.select2Options); |
| 59 | + this.refreshHookSelect2(); |
| 60 | + |
| 61 | + // select2 emits a jQuery "change" event, which a native addEventListener |
| 62 | + // does not reliably catch — bind through jQuery instead. |
| 63 | + $(this.moduleSelector).on('change', () => this.onModuleChange()); |
| 64 | + } |
| 65 | + |
| 66 | + private async onModuleChange(): Promise<void> { |
| 67 | + if (!this.moduleSelector || !this.hookSelector || !this.hookUrl) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const moduleId = Number(this.moduleSelector.value); |
| 72 | + |
| 73 | + if (!moduleId) { |
| 74 | + this.clearHookSelector(); |
| 75 | + |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + const response = await fetch(this.hookUrl, { |
| 81 | + method: 'POST', |
| 82 | + headers: {'Content-Type': 'application/x-www-form-urlencoded'}, |
| 83 | + body: new URLSearchParams({module_id: String(moduleId)}), |
| 84 | + }); |
| 85 | + |
| 86 | + const json = await response.json(); |
| 87 | + |
| 88 | + if (json.hasError || !Array.isArray(json.hooks)) { |
| 89 | + this.clearHookSelector(); |
| 90 | + |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + this.populateHookSelector(json.hooks as HookableInfo[]); |
| 95 | + } catch { |
| 96 | + this.clearHookSelector(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private populateHookSelector(hooks: HookableInfo[]): void { |
| 101 | + if (!this.hookSelector) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + const currentValue = this.hookSelector.value; |
| 106 | + this.clearHookSelector(); |
| 107 | + |
| 108 | + const available = hooks.filter((hook) => !hook.registered); |
| 109 | + const registered = hooks.filter((hook) => hook.registered); |
| 110 | + |
| 111 | + const buildOption = (hook: HookableInfo): HTMLOptionElement => { |
| 112 | + const option = document.createElement('option'); |
| 113 | + option.value = String(hook.id); |
| 114 | + option.text = hook.title ? `${hook.name} (${hook.title})` : hook.name; |
| 115 | + |
| 116 | + if (String(hook.id) === currentValue) { |
| 117 | + option.selected = true; |
| 118 | + } |
| 119 | + |
| 120 | + return option; |
| 121 | + }; |
| 122 | + |
| 123 | + if (available.length > 0) { |
| 124 | + const availableGroup = document.createElement('optgroup'); |
| 125 | + |
| 126 | + if (this.availableLabel != null) { |
| 127 | + availableGroup.label = this.availableLabel; |
| 128 | + } |
| 129 | + available.forEach((hook) => availableGroup.appendChild(buildOption(hook))); |
| 130 | + this.hookSelector.appendChild(availableGroup); |
| 131 | + } |
| 132 | + |
| 133 | + if (registered.length > 0) { |
| 134 | + const registeredGroup = document.createElement('optgroup'); |
| 135 | + |
| 136 | + if (this.registeredLabel != null) { |
| 137 | + registeredGroup.label = this.registeredLabel; |
| 138 | + } |
| 139 | + registeredGroup.disabled = true; |
| 140 | + registered.forEach((hook) => registeredGroup.appendChild(buildOption(hook))); |
| 141 | + this.hookSelector.appendChild(registeredGroup); |
| 142 | + } |
| 143 | + |
| 144 | + // Re-enable the selector now that choices are available. |
| 145 | + this.setHookSelectorEnabled(true); |
| 146 | + } |
| 147 | + |
| 148 | + private clearHookSelector(): void { |
| 149 | + if (!this.hookSelector) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + // Keep only the placeholder option (the first direct <option> child if any) |
| 154 | + const placeholder = this.hookSelector.querySelector<HTMLOptionElement>(':scope > option'); |
| 155 | + this.hookSelector.innerHTML = ''; |
| 156 | + if (placeholder) { |
| 157 | + this.hookSelector.appendChild(placeholder); |
| 158 | + } |
| 159 | + |
| 160 | + // Without choices the selector is unusable — disable it. |
| 161 | + this.setHookSelectorEnabled(false); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Toggles the disabled state of the hook selector. |
| 166 | + * The Symfony form theme renders the field disabled by adding a `disabled` |
| 167 | + * CSS class on both the wrapping `.input-container` and the label. Toggling |
| 168 | + * the `disabled` property on the <select> alone leaves those classes behind, |
| 169 | + * which keeps the field visually greyed out — so they must be synced too. |
| 170 | + */ |
| 171 | + private setHookSelectorEnabled(enabled: boolean): void { |
| 172 | + if (!this.hookSelector) { |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + this.hookSelector.disabled = !enabled; |
| 177 | + |
| 178 | + const container = this.hookSelector.closest<HTMLElement>('.input-container'); |
| 179 | + container?.classList.toggle('disabled', !enabled); |
| 180 | + |
| 181 | + const formGroup = this.hookSelector.closest<HTMLElement>('.form-group'); |
| 182 | + formGroup |
| 183 | + ?.querySelector<HTMLElement>('.form-control-label') |
| 184 | + ?.classList.toggle('disabled', !enabled); |
| 185 | + |
| 186 | + // The options and the disabled state were changed programmatically: rebuild |
| 187 | + // select2 so the widget reflects the new choices and enabled/disabled state. |
| 188 | + this.refreshHookSelect2(); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * (Re)initializes select2 on the hook selector. select2 caches the original |
| 193 | + * options at init time, so after repopulating or toggling the field we must |
| 194 | + * destroy and recreate it for the widget to pick up the changes. |
| 195 | + */ |
| 196 | + private refreshHookSelect2(): void { |
| 197 | + if (!this.hookSelector) { |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + const $hookSelector = $(this.hookSelector); |
| 202 | + |
| 203 | + if ($hookSelector.hasClass('select2-hidden-accessible')) { |
| 204 | + $hookSelector.select2('destroy'); |
| 205 | + } |
| 206 | + $hookSelector.select2(this.select2Options); |
| 207 | + } |
| 208 | +} |
0 commit comments