-
Notifications
You must be signed in to change notification settings - Fork 10
/
ModalExtension.ts
52 lines (39 loc) · 1.48 KB
/
ModalExtension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import BSN from 'bootstrap.native';
export class ModalExtension {
constructor(naja: any) {
naja.snippetHandler.addEventListener('afterUpdate', (event: any) => this.processSnippet(event.snippet));
}
private processSnippet(snippet: HTMLElement): void {
if (! snippet.classList.contains('modal')) {
return;
}
const modal = new BSN.Modal(snippet);
if (snippet.innerHTML === '') {
modal.hide();
return;
}
this.initializeButtons(snippet);
modal.show();
}
private initializeButtons(modal: HTMLElement): void {
const forms = modal.querySelectorAll('form');
const footer = modal.querySelector('.modal-footer');
if (forms.length === 0 || footer === null) {
return;
}
const form: HTMLFormElement = forms.item(0);
const buttons = Array.from(form.querySelectorAll<HTMLInputElement>('input[type="submit"]'));
// Add "copy" of buttons to footer
footer.prepend(
...buttons.map(button => {
const newButton = document.createElement('button');
newButton.classList.add(...button.classList);
newButton.innerHTML = button.value;
newButton.addEventListener('click', () => button.click());
return newButton;
})
);
// Hide original buttons
buttons.forEach(button => button.classList.add('d-none'));
}
}