-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-notifications.component.ts
230 lines (190 loc) · 7.52 KB
/
simple-notifications.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
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
import { Component, EventEmitter, OnInit, OnDestroy, ViewEncapsulation, Input, Output } from '@angular/core';
import { Notification } from './notification.type';
import { NotificationsService } from './notifications.service';
import { Options } from './options.type';
import { Subscription } from 'rxjs/Subscription';
@Component({
selector: 'simple-notifications',
encapsulation: ViewEncapsulation.None,
template: `
<div class="simple-notification-wrapper" [ngClass]="position">
<simple-notification
*ngFor="let a of notifications; let i = index"
[item]="a"
[timeOut]="timeOut"
[clickToClose]="clickToClose"
[maxLength]="maxLength"
[showProgressBar]="showProgressBar"
[pauseOnHover]="pauseOnHover"
[theClass]="theClass"
[rtl]="rtl"
[position]="i"
>
</simple-notification>
</div>
`,
styles: [`
.simple-notification-wrapper {
position: fixed;
width: 300px;
z-index: 1000;
}
.simple-notification-wrapper.left { left: 20px; }
.simple-notification-wrapper.top { top: 20px; }
.simple-notification-wrapper.right { right: 20px; }
.simple-notification-wrapper.bottom { bottom: 20px; }
@media (max-width: 340px) {
.simple-notification-wrapper {
width: auto;
left: 20px;
right: 20px;
}
}
`]
})
export class SimpleNotificationsComponent implements OnInit, OnDestroy {
@Input() set options(opt: Options) {
this.attachChanges(opt);
}
@Output() onCreate = new EventEmitter();
@Output() onDestroy = new EventEmitter();
public notifications: Notification[] = [];
public position: ['top' | 'bottom', 'right' | 'left'] = ['bottom', 'right'];
private lastNotificationCreated: Notification;
private listener: Subscription;
// Received values
private lastOnBottom: boolean = true;
private maxStack: number = 8;
private preventLastDuplicates: any = false;
private preventDuplicates: boolean = false;
// Sent values
public timeOut: number = 0;
public maxLength: number = 0;
public clickToClose: boolean = true;
public showProgressBar: boolean = true;
public pauseOnHover: boolean = true;
public theClass: string = '';
public rtl: boolean = false;
constructor(private _service: NotificationsService) { }
ngOnInit(): void {
// Listen for changes in the service
this.listener = this._service.getChangeEmitter()
.subscribe(item => {
switch (item.command) {
case 'cleanAll':
this.notifications = [];
break;
case 'clean':
this.cleanSingle(item.id!);
break;
case 'set':
if (item.add) this.add(item.notification!);
else this.defaultBehavior(item);
break;
default:
this.defaultBehavior(item);
break;
}
});
}
// Default behavior on event
defaultBehavior(value: any): void {
this.notifications.splice(this.notifications.indexOf(value.notification), 1);
this.onDestroy.emit(this.buildEmit(value.notification, false));
}
// Add the new notification to the notification array
add(item: Notification): void {
item.createdOn = new Date();
let toBlock: boolean = this.preventLastDuplicates || this.preventDuplicates ? this.block(item) : false;
// Save this as the last created notification
this.lastNotificationCreated = item;
if (!toBlock) {
// Check if the notification should be added at the start or the end of the array
if (this.lastOnBottom) {
if (this.notifications.length >= this.maxStack) this.notifications.splice(0, 1);
this.notifications.push(item);
} else {
if (this.notifications.length >= this.maxStack) this.notifications.splice(this.notifications.length - 1, 1);
this.notifications.splice(0, 0, item);
}
this.onCreate.emit(this.buildEmit(item, true));
}
}
// Check if notifications should be prevented
block(item: Notification): boolean {
let toCheck = item.html ? this.checkHtml : this.checkStandard;
if (this.preventDuplicates && this.notifications.length > 0) {
for (let i = 0; i < this.notifications.length; i++) {
if (toCheck(this.notifications[i], item)) {
return true;
}
}
}
if (this.preventLastDuplicates) {
let comp: Notification;
if (this.preventLastDuplicates === 'visible' && this.notifications.length > 0) {
if (this.lastOnBottom) {
comp = this.notifications[this.notifications.length - 1];
} else {
comp = this.notifications[0];
}
} else if (this.preventLastDuplicates === 'all' && this.lastNotificationCreated) {
comp = this.lastNotificationCreated;
} else {
return false;
}
return toCheck(comp, item);
}
return false;
}
checkStandard(checker: Notification, item: Notification): boolean {
return checker.type === item.type && checker.title === item.title && checker.content === item.content;
}
checkHtml(checker: Notification, item: Notification): boolean {
return checker.html ? checker.type === item.type && checker.title === item.title && checker.content === item.content && checker.html === item.html : false;
}
// Attach all the changes received in the options object
attachChanges(options: any): void {
Object.keys(options).forEach(a => {
if (this.hasOwnProperty(a)) {
(<any>this)[a] = options[a];
}
});
}
buildEmit(notification: Notification, to: boolean) {
let toEmit: Notification = {
createdOn: notification.createdOn,
type: notification.type,
icon: notification.icon,
id: notification.id
};
if (notification.html) {
toEmit.html = notification.html;
} else {
toEmit.title = notification.title;
toEmit.content = notification.content;
}
if (!to) {
toEmit.destroyedOn = new Date();
}
return toEmit;
}
cleanSingle(id: string): void {
let indexOfDelete: number = 0;
let doDelete: boolean = false;
this.notifications.forEach((notification, idx) => {
if (notification.id === id) {
indexOfDelete = idx;
doDelete = true;
}
});
if (doDelete) {
this.notifications.splice(indexOfDelete, 1);
}
}
ngOnDestroy(): void {
if (this.listener) {
this.listener.unsubscribe();
}
}
}