-
Notifications
You must be signed in to change notification settings - Fork 85
/
ngx-smart-modal.component.ts
194 lines (167 loc) · 6.99 KB
/
ngx-smart-modal.component.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
import {
Input,
Output,
OnInit,
Renderer2,
Component,
OnDestroy,
EventEmitter,
HostListener,
ChangeDetectorRef
} from '@angular/core';
import {NgxSmartModalService} from '../services/ngx-smart-modal.service';
@Component({
selector: 'ngx-smart-modal',
template: `
<div *ngIf="overlayVisible" class="overlay {{ openedClass ? 'nsm-overlay-open' : 'nsm-overlay-close'}}"
[style.z-index]="visible ? layerPosition-1 : -1"
[ngClass]="{'transparent':!backdrop}"
(click)="dismiss($event)">
</div>
<div [style.z-index]="visible ? layerPosition : -1"
class="nsm-dialog {{ openedClass ? 'nsm-dialog-open' : 'nsm-dialog-close'}}" [ngClass]="customClass">
<ng-content></ng-content>
<button type="button" *ngIf="closable" (click)="close()" aria-label="Close" class="nsm-dialog-btn-close">
<img
src="data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTkuMC4wLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVy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ODQuNzcxLDUwNS45NDIsNDc2LjY5NHoiIGZpbGw9IiMwMDAwMDAiLz4KCTwvZz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8L3N2Zz4K"/>
</button>
</div>
`
})
export class NgxSmartModalComponent implements OnInit, OnDestroy {
@Input() public closable: boolean = true;
@Input() public escapable: boolean = true;
@Input() public dismissable: boolean = true;
@Input() public identifier: string;
@Input() public customClass: string = 'nsm-dialog-animation-fade';
@Input() public visible: boolean = false;
@Input() public backdrop: boolean = true;
@Input() public force: boolean = true;
@Input() public hideDelay: number = 500;
@Output() public visibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
@Output() public onClose: EventEmitter<any> = new EventEmitter();
@Output() public onCloseFinished: EventEmitter<any> = new EventEmitter();
@Output() public onDismiss: EventEmitter<any> = new EventEmitter();
@Output() public onDismissFinished: EventEmitter<any> = new EventEmitter();
@Output() public onAnyCloseEvent: EventEmitter<any> = new EventEmitter();
@Output() public onAnyCloseEventFinished: EventEmitter<any> = new EventEmitter();
@Output() public onOpen: EventEmitter<any> = new EventEmitter();
@Output() public onEscape: EventEmitter<any> = new EventEmitter();
@Output() public onDataAdded: EventEmitter<any> = new EventEmitter();
@Output() public onDataRemoved: EventEmitter<any> = new EventEmitter();
public layerPosition: number = 1041;
public overlayVisible: boolean = false;
public openedClass: boolean = false;
private data: any = null;
constructor(private renderer: Renderer2,
private changeDetectorRef: ChangeDetectorRef,
private ngxSmartModalService: NgxSmartModalService) {
}
public ngOnInit() {
this.layerPosition += this.ngxSmartModalService.getModalStackCount();
this.ngxSmartModalService.addModal({id: this.identifier, modal: this}, this.force);
if (this.visible) {
this.open();
}
}
public ngOnDestroy() {
this.ngxSmartModalService.removeModal(this.identifier);
}
public open(top?: boolean): void {
if (top) {
this.layerPosition = this.ngxSmartModalService.getHigherIndex();
}
this.renderer.addClass(document.body, 'dialog-open');
this.overlayVisible = true;
this.visible = true;
this.openedClass = true;
this.onOpen.emit(this);
}
public close(): void {
const me = this;
this.openedClass = false;
this.onClose.emit(this);
this.onAnyCloseEvent.emit(this);
if (this.ngxSmartModalService.getOpenedModals().length < 2) {
this.renderer.removeClass(document.body, 'dialog-open');
}
setTimeout(() => {
me.visibleChange.emit(me.visible);
me.visible = false;
me.overlayVisible = false;
me.changeDetectorRef.markForCheck();
me.onCloseFinished.emit(me);
me.onAnyCloseEventFinished.emit(me);
}, this.hideDelay);
}
public dismiss(e: any): void {
const me = this;
if (!this.dismissable) {
return;
}
if (e.target.classList.contains('overlay')) {
this.openedClass = false;
this.onDismiss.emit(this);
this.onAnyCloseEvent.emit(this);
if (this.ngxSmartModalService.getOpenedModals().length < 2) {
this.renderer.removeClass(document.body, 'dialog-open');
}
setTimeout(() => {
me.visible = false;
me.visibleChange.emit(me.visible);
me.overlayVisible = false;
me.changeDetectorRef.markForCheck();
me.onDismissFinished.emit(me);
me.onAnyCloseEventFinished.emit(me);
}, this.hideDelay);
}
}
public addCustomClass(className: string): void {
if (!this.customClass.length) {
this.customClass = className;
} else {
this.customClass += ' ' + className;
}
}
public removeCustomClass(className?: string): void {
if (className) {
this.customClass = this.customClass.replace(className, '').trim();
} else {
this.customClass = '';
}
}
public isVisible(): boolean {
return this.visible;
}
public hasData(): boolean {
return !!this.data;
}
public setData(data: any, force?: boolean): any {
if (!this.data || (!!this.data && force)) {
setTimeout(() => {
this.data = data;
this.onDataAdded.emit(this.data);
});
}
}
public getData(): any {
return this.data;
}
public removeData(): void {
setTimeout(() => {
this.data = null;
this.onDataRemoved.emit(true);
});
}
@HostListener('document:keyup', ['$event'])
private escapeKeyboardEvent(event: KeyboardEvent) {
if (event.keyCode === 27 && this.visible) {
if (!this.escapable) {
return false;
} else {
this.onEscape.emit(this);
this.ngxSmartModalService.closeLatestModal();
}
}
}
}