-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
modal.directive.ts
433 lines (373 loc) · 11.1 KB
/
modal.directive.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* tslint:disable:max-file-line-count */
// todo: should we support enforce focus in?
// todo: in original bs there are was a way to prevent modal from showing
// todo: original modal had resize events
import {
ComponentRef, Directive, ElementRef, EventEmitter, HostListener, Input,
OnDestroy, OnInit, Output, Renderer2, ViewContainerRef
} from '@angular/core';
import { document, window } from '../utils/facade/browser';
import { isBs3 } from '../utils/theme-provider';
import { Utils } from '../utils/utils.class';
import { ModalBackdropComponent } from './modal-backdrop.component';
import {
CLASS_NAME, DISMISS_REASONS, modalConfigDefaults, ModalOptions
} from './modal-options.class';
import { ComponentLoader } from '../component-loader/component-loader.class';
import { ComponentLoaderFactory } from '../component-loader/component-loader.factory';
const TRANSITION_DURATION = 300;
const BACKDROP_TRANSITION_DURATION = 150;
/** Mark any code with directive to show it's content in modal */
@Directive({
selector: '[bsModal]',
exportAs: 'bs-modal'
})
export class ModalDirective implements OnDestroy, OnInit {
/** allows to set modal configuration via element property */
@Input()
set config(conf: ModalOptions) {
this._config = this.getConfig(conf);
}
get config(): ModalOptions {
return this._config;
}
/** This event fires immediately when the `show` instance method is called. */
@Output()
onShow: EventEmitter<ModalDirective> = new EventEmitter<ModalDirective>();
/** This event is fired when the modal has been made visible to the user
* (will wait for CSS transitions to complete)
*/
@Output()
onShown: EventEmitter<ModalDirective> = new EventEmitter<ModalDirective>();
/** This event is fired immediately when
* the hide instance method has been called.
*/
@Output()
onHide: EventEmitter<ModalDirective> = new EventEmitter<ModalDirective>();
/** This event is fired when the modal has finished being
* hidden from the user (will wait for CSS transitions to complete).
*/
@Output()
onHidden: EventEmitter<ModalDirective> = new EventEmitter<ModalDirective>();
/** This field contains last dismiss reason.
* Possible values: `backdrop-click`, `esc` and `null`
* (if modal was closed by direct call of `.hide()`).
*/
dismissReason: string;
get isShown(): boolean {
return this._isShown;
}
protected _config: ModalOptions;
protected _isShown = false;
protected isBodyOverflowing = false;
protected originalBodyPadding = 0;
protected scrollbarWidth = 0;
protected timerHideModal: any = 0;
protected timerRmBackDrop: any = 0;
// reference to backdrop component
protected backdrop: ComponentRef<ModalBackdropComponent>;
private _backdrop: ComponentLoader<ModalBackdropComponent>;
private isNested = false;
constructor(private _element: ElementRef,
_viewContainerRef: ViewContainerRef,
private _renderer: Renderer2,
clf: ComponentLoaderFactory) {
this._backdrop = clf.createLoader<ModalBackdropComponent>(
_element,
_viewContainerRef,
_renderer
);
}
@HostListener('click', ['$event'])
onClick(event: any): void {
if (
this.config.ignoreBackdropClick ||
this.config.backdrop === 'static' ||
event.target !== this._element.nativeElement
) {
return;
}
this.dismissReason = DISMISS_REASONS.BACKRDOP;
this.hide(event);
}
// todo: consider preventing default and stopping propagation
@HostListener('keydown.esc')
onEsc(): void {
if (this.config.keyboard) {
this.dismissReason = DISMISS_REASONS.ESC;
this.hide();
}
}
ngOnDestroy(): any {
this.config = void 0;
if (this._isShown) {
this._isShown = false;
this.hideModal();
this._backdrop.dispose();
}
}
ngOnInit(): any {
this._config = this._config || this.getConfig();
setTimeout(() => {
if (this._config.show) {
this.show();
}
}, 0);
}
/* Public methods */
/** Allows to manually toggle modal visibility */
toggle(): void {
return this._isShown ? this.hide() : this.show();
}
/** Allows to manually open modal */
show(): void {
this.dismissReason = null;
this.onShow.emit(this);
if (this._isShown) {
return;
}
clearTimeout(this.timerHideModal);
clearTimeout(this.timerRmBackDrop);
this._isShown = true;
this.checkScrollbar();
this.setScrollbar();
if (document && document.body) {
if (document.body.classList.contains(CLASS_NAME.OPEN)) {
this.isNested = true;
} else {
this._renderer.addClass(document.body, CLASS_NAME.OPEN);
}
}
this.showBackdrop(() => {
this.showElement();
});
}
/** Allows to manually close modal */
hide(event?: Event): void {
if (event) {
event.preventDefault();
}
this.onHide.emit(this);
// todo: add an option to prevent hiding
if (!this._isShown) {
return;
}
clearTimeout(this.timerHideModal);
clearTimeout(this.timerRmBackDrop);
this._isShown = false;
this._renderer.removeClass(this._element.nativeElement, CLASS_NAME.IN);
if (!isBs3()) {
this._renderer.removeClass(this._element.nativeElement, CLASS_NAME.SHOW);
}
// this._addClassIn = false;
if (this._config.animated) {
this.timerHideModal = setTimeout(
() => this.hideModal(),
TRANSITION_DURATION
);
} else {
this.hideModal();
}
}
/** Private methods @internal */
protected getConfig(config?: ModalOptions): ModalOptions {
return Object.assign({}, modalConfigDefaults, config);
}
/**
* Show dialog
* @internal
*/
protected showElement(): void {
// todo: replace this with component loader usage
if (
!this._element.nativeElement.parentNode ||
this._element.nativeElement.parentNode.nodeType !== Node.ELEMENT_NODE
) {
// don't move modals dom position
if (document && document.body) {
document.body.appendChild(this._element.nativeElement);
}
}
this._renderer.setAttribute(
this._element.nativeElement,
'aria-hidden',
'false'
);
this._renderer.setStyle(
this._element.nativeElement,
'display',
'block'
);
this._renderer.setProperty(
this._element.nativeElement,
'scrollTop',
0
);
if (this._config.animated) {
Utils.reflow(this._element.nativeElement);
}
// this._addClassIn = true;
this._renderer.addClass(this._element.nativeElement, CLASS_NAME.IN);
if (!isBs3()) {
this._renderer.addClass(this._element.nativeElement, CLASS_NAME.SHOW);
}
const transitionComplete = () => {
if (this._config.focus) {
this._element.nativeElement.focus();
}
this.onShown.emit(this);
};
if (this._config.animated) {
setTimeout(transitionComplete, TRANSITION_DURATION);
} else {
transitionComplete();
}
}
/** @internal */
protected hideModal(): void {
this._renderer.setAttribute(
this._element.nativeElement,
'aria-hidden',
'true'
);
this._renderer.setStyle(
this._element.nativeElement,
'display',
'none'
);
this.showBackdrop(() => {
if (!this.isNested) {
if (document && document.body) {
this._renderer.removeClass(document.body, CLASS_NAME.OPEN);
}
this.resetScrollbar();
}
this.resetAdjustments();
this.focusOtherModal();
this.onHidden.emit(this);
});
}
// todo: original show was calling a callback when done, but we can use
// promise
/** @internal */
protected showBackdrop(callback?: Function): void {
if (
this._isShown &&
this.config.backdrop &&
(!this.backdrop || !this.backdrop.instance.isShown)
) {
this.removeBackdrop();
this._backdrop
.attach(ModalBackdropComponent)
.to('body')
.show({isAnimated: this._config.animated});
this.backdrop = this._backdrop._componentRef;
if (!callback) {
return;
}
if (!this._config.animated) {
callback();
return;
}
setTimeout(callback, BACKDROP_TRANSITION_DURATION);
} else if (!this._isShown && this.backdrop) {
this.backdrop.instance.isShown = false;
const callbackRemove = () => {
this.removeBackdrop();
if (callback) {
callback();
}
};
if (this.backdrop.instance.isAnimated) {
this.timerRmBackDrop = setTimeout(
callbackRemove,
BACKDROP_TRANSITION_DURATION
);
} else {
callbackRemove();
}
} else if (callback) {
callback();
}
}
/** @internal */
protected removeBackdrop(): void {
this._backdrop.hide();
}
/** Events tricks */
// no need for it
// protected setEscapeEvent():void {
// if (this._isShown && this._config.keyboard) {
// $(this._element).on(Event.KEYDOWN_DISMISS, (event) => {
// if (event.which === 27) {
// this.hide()
// }
// })
//
// } else if (!this._isShown) {
// $(this._element).off(Event.KEYDOWN_DISMISS)
// }
// }
// protected setResizeEvent():void {
// console.log(this.renderer.listenGlobal('', Event.RESIZE));
// if (this._isShown) {
// $(window).on(Event.RESIZE, $.proxy(this._handleUpdate, this))
// } else {
// $(window).off(Event.RESIZE)
// }
// }
protected focusOtherModal() {
if (this._element.nativeElement.parentElement == null) return;
const otherOpenedModals = this._element.nativeElement.parentElement.querySelectorAll('.in[bsModal]');
if (!otherOpenedModals.length) {
return;
}
otherOpenedModals[otherOpenedModals.length - 1].focus();
}
/** @internal */
protected resetAdjustments(): void {
this._renderer.setStyle(
this._element.nativeElement,
'paddingLeft',
''
);
this._renderer.setStyle(
this._element.nativeElement,
'paddingRight',
''
);
}
/** Scroll bar tricks */
/** @internal */
protected checkScrollbar(): void {
this.isBodyOverflowing = document.body.clientWidth < window.innerWidth;
this.scrollbarWidth = this.getScrollbarWidth();
}
protected setScrollbar(): void {
if (!document) {
return;
}
this.originalBodyPadding = parseInt(
window
.getComputedStyle(document.body)
.getPropertyValue('padding-right') || 0,
10
);
if (this.isBodyOverflowing) {
document.body.style.paddingRight = `${this.originalBodyPadding +
this.scrollbarWidth}px`;
}
}
protected resetScrollbar(): void {
document.body.style.paddingRight = this.originalBodyPadding;
}
// thx d.walsh
protected getScrollbarWidth(): number {
const scrollDiv = this._renderer.createElement('div');
this._renderer.addClass(scrollDiv, CLASS_NAME.SCROLLBAR_MEASURER);
this._renderer.appendChild(document.body, scrollDiv);
const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
this._renderer.removeChild(document.body, scrollDiv);
return scrollbarWidth;
}
}