-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
native-dialog-renderer.ts
193 lines (166 loc) · 6.9 KB
/
native-dialog-renderer.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { DOM } from 'aurelia-pal';
import { transient } from 'aurelia-dependency-injection';
import { Renderer } from '../renderer';
import { DialogController } from '../dialog-controller';
import { transitionEvent, hasTransition } from './ux-dialog-renderer';
const containerTagName = 'dialog';
let body: HTMLBodyElement;
@transient()
export class NativeDialogRenderer implements Renderer {
public static dialogControllers: DialogController[] = [];
public static keyboardEventHandler(e: KeyboardEvent) {
const key = (e.code || e.key) === 'Enter' || e.keyCode === 13
? 'Enter'
: undefined;
if (!key) { return; }
const top = NativeDialogRenderer.dialogControllers[NativeDialogRenderer.dialogControllers.length - 1];
if (!top || !top.settings.keyboard) { return; }
const keyboard = top.settings.keyboard;
if (key === 'Enter' && (keyboard === key || (Array.isArray(keyboard) && keyboard.indexOf(key) > -1))) {
top.ok();
}
}
public static trackController(dialogController: DialogController): void {
if (!NativeDialogRenderer.dialogControllers.length) {
DOM.addEventListener('keyup', NativeDialogRenderer.keyboardEventHandler, false);
}
NativeDialogRenderer.dialogControllers.push(dialogController);
}
public static untrackController(dialogController: DialogController): void {
const i = NativeDialogRenderer.dialogControllers.indexOf(dialogController);
if (i !== -1) {
NativeDialogRenderer.dialogControllers.splice(i, 1);
}
if (!NativeDialogRenderer.dialogControllers.length) {
DOM.removeEventListener('keyup', NativeDialogRenderer.keyboardEventHandler, false);
}
}
private stopPropagation: (e: MouseEvent & { _aureliaDialogHostClicked: boolean }) => void;
private closeDialogClick: (e: MouseEvent & { _aureliaDialogHostClicked: boolean }) => void;
private dialogCancel: (e: Event) => void;
public dialogContainer: HTMLDialogElement;
public lastActiveElement: HTMLElement;
public host: Element;
public anchor: Element;
private getOwnElements(parent: Element, selector: string): Element[] {
const elements = parent.querySelectorAll(selector);
const own: Element[] = [];
for (let i = 0; i < elements.length; i++) {
if (elements[i].parentElement === parent) {
own.push(elements[i]);
}
}
return own;
}
private attach(dialogController: DialogController): void {
this.lastActiveElement = DOM.activeElement as HTMLElement;
const spacingWrapper = DOM.createElement('div'); // TODO: check if redundant
spacingWrapper.appendChild(this.anchor);
this.dialogContainer = DOM.createElement(containerTagName) as HTMLDialogElement;
if ((window as any).dialogPolyfill) {
(window as any).dialogPolyfill.registerDialog(this.dialogContainer);
}
this.dialogContainer.appendChild(spacingWrapper);
const lastContainer = this.getOwnElements(this.host, containerTagName).pop();
if (lastContainer && lastContainer.parentElement) {
this.host.insertBefore(this.dialogContainer, lastContainer.nextSibling);
} else {
this.host.insertBefore(this.dialogContainer, this.host.firstChild);
}
dialogController.controller.attached();
this.host.classList.add('ux-dialog-open');
}
private detach(dialogController: DialogController): void {
// This check only seems required for the polyfill
if (this.dialogContainer.hasAttribute('open')) {
this.dialogContainer.close();
}
this.host.removeChild(this.dialogContainer);
dialogController.controller.detached();
if (!NativeDialogRenderer.dialogControllers.length) {
this.host.classList.remove('ux-dialog-open');
}
if (dialogController.settings.restoreFocus) {
dialogController.settings.restoreFocus(this.lastActiveElement);
}
}
private setAsActive(): void {
this.dialogContainer.showModal();
this.dialogContainer.classList.add('active');
}
private setAsInactive(): void {
this.dialogContainer.classList.remove('active');
}
private setupEventHandling(dialogController: DialogController): void {
this.stopPropagation = e => { e._aureliaDialogHostClicked = true; };
this.closeDialogClick = e => {
if (dialogController.settings.overlayDismiss && !e._aureliaDialogHostClicked) {
dialogController.cancel();
}
};
this.dialogCancel = e => {
const keyboard = dialogController.settings.keyboard;
const key = 'Escape';
if (keyboard === true || keyboard === key || (Array.isArray(keyboard) && keyboard.indexOf(key) > -1)) {
dialogController.cancel();
} else {
e.preventDefault();
}
};
this.dialogContainer.addEventListener('click', this.closeDialogClick);
this.dialogContainer.addEventListener('cancel', this.dialogCancel);
this.anchor.addEventListener('click', this.stopPropagation);
}
private clearEventHandling(): void {
this.dialogContainer.removeEventListener('click', this.closeDialogClick);
this.dialogContainer.removeEventListener('cancel', this.dialogCancel);
this.anchor.removeEventListener('click', this.stopPropagation);
}
private awaitTransition(setActiveInactive: () => void, ignore: boolean): Promise<void> {
return new Promise<void>(resolve => {
// tslint:disable-next-line:no-this-assignment
const renderer = this;
const eventName = transitionEvent();
function onTransitionEnd(e: TransitionEvent): void {
if (e.target !== renderer.dialogContainer) {
return;
}
renderer.dialogContainer.removeEventListener(eventName, onTransitionEnd);
resolve();
}
if (ignore || !hasTransition(this.dialogContainer)) {
resolve();
} else {
this.dialogContainer.addEventListener(eventName, onTransitionEnd);
}
setActiveInactive();
});
}
public getDialogContainer(): Element {
return this.anchor || (this.anchor = DOM.createElement('div'));
}
public showDialog(dialogController: DialogController): Promise<void> {
if (!body) {
body = DOM.querySelector('body') as HTMLBodyElement;
}
if (dialogController.settings.host) {
this.host = dialogController.settings.host;
} else {
this.host = body;
}
const settings = dialogController.settings;
this.attach(dialogController);
if (typeof settings.position === 'function') {
settings.position(this.dialogContainer);
}
NativeDialogRenderer.trackController(dialogController);
this.setupEventHandling(dialogController);
return this.awaitTransition(() => this.setAsActive(), dialogController.settings.ignoreTransitions as boolean);
}
public hideDialog(dialogController: DialogController): Promise<void> {
this.clearEventHandling();
NativeDialogRenderer.untrackController(dialogController);
return this.awaitTransition(() => this.setAsInactive(), dialogController.settings.ignoreTransitions as boolean)
.then(() => { this.detach(dialogController); });
}
}