| 1 |
if (!window.SequraFE) { |
| 2 |
window.SequraFE = {}; |
| 3 |
} |
| 4 |
|
| 5 |
if (!window.SequraFE.components) { |
| 6 |
window.SequraFE.components = {}; |
| 7 |
} |
| 8 |
|
| 9 |
(function () { |
| 10 |
/** |
| 11 |
* @typedef ButtonConfig |
| 12 |
* @property {string} label |
| 13 |
* @property {string?} className |
| 14 |
* @property {'primary' | 'secondary'} type |
| 15 |
* @property {() => void} onClick |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* @typedef ModalConfiguration |
| 20 |
* @property {string?} title |
| 21 |
* @property {string?} className |
| 22 |
* @property {HTMLElement} content The content of the body. |
| 23 |
* @property {ButtonConfig[]} buttons Footer buttons. |
| 24 |
* @property {(modal: HTMLDivElement) => void?} onOpen Will fire after the modal is opened. |
| 25 |
* @property {() => boolean?} onClose Will fire before the modal is closed. |
| 26 |
* If the return value is false, the modal will not be closed. |
| 27 |
* @property {boolean} [footer=false] Indicates whether to use footer. Defaults to false. |
| 28 |
* @property {boolean} [canClose=true] Indicates whether to use an (X) button or click outside the modal |
| 29 |
* to close it. Defaults to true. |
| 30 |
* @property {boolean} [fullWidthBody=false] Indicates whether to make body full width |
| 31 |
*/ |
| 32 |
|
| 33 |
/** |
| 34 |
* @param {ModalConfiguration} config |
| 35 |
* @constructor |
| 36 |
*/ |
| 37 |
function ModalComponent(config) { |
| 38 |
const { templateService, translationService, utilities, elementGenerator } = SequraFE; |
| 39 |
|
| 40 |
/** |
| 41 |
* @type {HTMLDivElement} |
| 42 |
*/ |
| 43 |
let modal; |
| 44 |
|
| 45 |
/** |
| 46 |
* Closes the modal on Esc key. |
| 47 |
* |
| 48 |
* @param {KeyboardEvent} event |
| 49 |
*/ |
| 50 |
const closeOnEsc = (event) => { |
| 51 |
if (event.key === 'Escape') { |
| 52 |
this.close(); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* Closes the modal. |
| 58 |
*/ |
| 59 |
this.close = () => { |
| 60 |
if (!config.onClose || config.onClose()) { |
| 61 |
window.removeEventListener('keyup', closeOnEsc); |
| 62 |
modal?.remove(); |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
/** |
| 67 |
* Opens the modal. |
| 68 |
*/ |
| 69 |
this.open = () => { |
| 70 |
const modalTemplate = |
| 71 |
'<div id="sq-modal" class="sq-modal sqs--hidden">\n' + |
| 72 |
' <div class="sq-modal-backdrop"></div>' + |
| 73 |
' <div class="sqp-modal-content">' + |
| 74 |
' <button class="sqp-close-button"><span></span></button>' + |
| 75 |
' <div class="sqp-title"></div>' + |
| 76 |
' <div class="sqp-body"></div>' + |
| 77 |
' <div class="sqp-footer"></div>' + |
| 78 |
' </div>' + |
| 79 |
'</div>'; |
| 80 |
|
| 81 |
modal = SequraFE.elementGenerator.createElementFromHTML(modalTemplate); |
| 82 |
const closeBtn = modal.querySelector('.sqp-close-button'), |
| 83 |
closeBtnSpan = modal.querySelector('.sqp-close-button span'), |
| 84 |
title = modal.querySelector('.sqp-title'), |
| 85 |
body = modal.querySelector('.sqp-body'), |
| 86 |
footer = modal.querySelector('.sqp-footer'); |
| 87 |
|
| 88 |
utilities.showElement(modal); |
| 89 |
if (config.canClose === false) { |
| 90 |
utilities.hideElement(closeBtn); |
| 91 |
} else { |
| 92 |
window.addEventListener('keyup', closeOnEsc); |
| 93 |
closeBtn.addEventListener('click', this.close); |
| 94 |
closeBtnSpan.style.display = 'flex'; |
| 95 |
modal.addEventListener('click', (event) => { |
| 96 |
if (event.target.id === 'sq-modal') { |
| 97 |
event.preventDefault(); |
| 98 |
this.close(); |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
if (config.title) { |
| 106 |
title.innerHTML = translationService.translate(config.title); |
| 107 |
} else { |
| 108 |
utilities.hideElement(title); |
| 109 |
} |
| 110 |
|
| 111 |
if (config.className) { |
| 112 |
modal.classList.add(...config.className.split(' ')); |
| 113 |
} |
| 114 |
|
| 115 |
config.content && body.append(...(Array.isArray(config.content) ? config.content : [config.content])); |
| 116 |
if (config.fullWidthBody) { |
| 117 |
body.classList.add('sqm--full-width'); |
| 118 |
} |
| 119 |
|
| 120 |
if (config.footer === false || !config.buttons) { |
| 121 |
utilities.hideElement(footer); |
| 122 |
} else { |
| 123 |
config.buttons.forEach((button) => { |
| 124 |
footer.appendChild(elementGenerator.createButton(button)); |
| 125 |
}); |
| 126 |
} |
| 127 |
|
| 128 |
templateService.getContentWrapper().appendChild(modal); |
| 129 |
if (config.onOpen) { |
| 130 |
config.onOpen(modal); |
| 131 |
} |
| 132 |
}; |
| 133 |
} |
| 134 |
|
| 135 |
SequraFE.components.Modal = { |
| 136 |
/** @param {ModalConfiguration} config */ |
| 137 |
create: (config) => new ModalComponent(config) |
| 138 |
}; |
| 139 |
})(); |
| 140 |
|