This repository has been archived by the owner on Nov 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Modal.ts
176 lines (138 loc) · 5.37 KB
/
Modal.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
import {Component, Input, Output, EventEmitter, ElementRef, ViewChild} from "@angular/core";
@Component({
selector: "modal-header",
template: `<ng-content></ng-content>`
})
export class ModalHeader {
}
@Component({
selector: "modal-content",
template: `<ng-content></ng-content>`
})
export class ModalContent {
}
@Component({
selector: "modal-footer",
template: `<ng-content></ng-content>`
})
export class ModalFooter {
}
@Component({
selector: "modal",
template: `
<div class="modal"
tabindex="-1"
role="dialog"
#modalRoot
(keydown.esc)="closeOnEscape ? close() : 0"
[ngClass]="{ in: isOpened, fade: isOpened }"
[ngStyle]="{ display: isOpened ? 'block' : 'none' }"
(click)="closeOnOutsideClick ? close() : 0">
<div [class]="'modal-dialog ' + modalClass" (click)="preventClosing($event)">
<div class="modal-content" tabindex="0" *ngIf="isOpened">
<div class="modal-header">
<button *ngIf="!hideCloseButton" type="button" class="close" data-dismiss="modal" [attr.aria-label]="cancelButtonLabel || 'Close'" (click)="close()"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" *ngIf="title">{{ title }}</h4>
<ng-content select="modal-header"></ng-content>
</div>
<div class="modal-body">
<ng-content select="modal-content"></ng-content>
</div>
<div class="modal-footer">
<ng-content select="modal-footer"></ng-content>
<button *ngIf="cancelButtonLabel" type="button" class="btn btn-default" data-dismiss="modal" (click)="close()">{{ cancelButtonLabel }}</button>
<button *ngIf="submitButtonLabel" type="button" class="btn btn-primary" (click)="onSubmit.emit(undefined)">{{ submitButtonLabel }}</button>
</div>
</div>
</div>
</div>
`
})
export class Modal {
// -------------------------------------------------------------------------
// Inputs
// -------------------------------------------------------------------------
@Input()
public modalClass: string;
@Input()
public closeOnEscape: boolean = true;
@Input()
public closeOnOutsideClick: boolean = true;
@Input()
public title: string;
@Input()
public hideCloseButton = false;
@Input()
public cancelButtonLabel: string;
@Input()
public submitButtonLabel: string;
@Input()
public backdrop:boolen = true;
// -------------------------------------------------------------------------
// Outputs
// -------------------------------------------------------------------------
@Output()
public onOpen = new EventEmitter(false);
@Output()
public onClose = new EventEmitter(false);
@Output()
public onSubmit = new EventEmitter(false);
// -------------------------------------------------------------------------
// Public properties
// -------------------------------------------------------------------------
public isOpened = false;
// -------------------------------------------------------------------------
// Private properties
// -------------------------------------------------------------------------
@ViewChild("modalRoot")
public modalRoot: ElementRef;
private backdropElement: HTMLElement;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor() {
this.createBackDrop();
}
// -------------------------------------------------------------------------
// Lifecycle Methods
// -------------------------------------------------------------------------
ngOnDestroy() {
document.body.className = document.body.className.replace(/modal-open\b/, "");
if (this.backdropElement && this.backdropElement.parentNode === document.body)
document.body.removeChild(this.backdropElement);
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
open(...args: any[]) {
if (this.isOpened)
return;
this.isOpened = true;
this.onOpen.emit(args);
document.body.appendChild(this.backdropElement);
window.setTimeout(() => this.modalRoot.nativeElement.focus(), 0);
document.body.className += " modal-open";
}
close(...args: any[]) {
if (!this.isOpened)
return;
this.isOpened = false;
this.onClose.emit(args);
document.body.removeChild(this.backdropElement);
document.body.className = document.body.className.replace(/modal-open\b/, "");
}
// -------------------------------------------------------------------------
// Private Methods
// -------------------------------------------------------------------------
public preventClosing(event: MouseEvent) {
event.stopPropagation();
}
private createBackDrop() {
this.backdropElement = document.createElement("div");
this.backdropElement.classList.add("fade");
this.backdropElement.classList.add("in");
if(this.backdrop) {
this.backdropElement.classList.add("modal-backdrop");
}
}
}