diff --git a/umap/static/umap/js/modules/ui/dialog.js b/umap/static/umap/js/modules/ui/dialog.js index 798ef167..58343cd6 100644 --- a/umap/static/umap/js/modules/ui/dialog.js +++ b/umap/static/umap/js/modules/ui/dialog.js @@ -1,8 +1,40 @@ import { translate } from '../i18n.js' +const TEMPLATE = ` + +` + +class WithTemplate { + loadTemplate(html) { + const template = document.createElement('template') + template.innerHTML = html + this.element = template.content.firstElementChild + this.elements = {} + for (const element of this.element.querySelectorAll('[data-ref]')) { + this.elements[element.dataset.ref] = element + } + return this.element + } +} + // From https://css-tricks.com/replace-javascript-dialogs-html-dialog-element/ -export default class Dialog { +export default class Dialog extends WithTemplate { constructor(settings = {}) { + super() this.settings = Object.assign( { accept: translate('OK'), @@ -41,30 +73,12 @@ export default class Dialog { init() { this.dialogSupported = typeof HTMLDialogElement === 'function' - this.dialog = document.createElement('dialog') - this.dialog.role = 'dialog' + this.dialog = this.loadTemplate(TEMPLATE) this.dialog.dataset.component = this.dialogSupported ? 'dialog' : 'no-dialog' - this.dialog.innerHTML = ` -
` document.body.appendChild(this.dialog) - this.elements = {} this.focusable = [] - this.dialog - .querySelectorAll('[data-ref]') - .forEach((el) => (this.elements[el.dataset.ref] = el)) + this.dialog.setAttribute('aria-labelledby', this.elements.message.id) this.dialog.addEventListener('click', (event) => { if (event.target.closest('[data-close]')) {