-
Notifications
You must be signed in to change notification settings - Fork 36
/
DesktopNotification.ts
285 lines (253 loc) · 9.34 KB
/
DesktopNotification.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
/*
* Copyright (c) 2010, 2023 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
import {DesktopNotificationEventMap, DesktopNotificationModel, Device, EnumObject, InitModelOf, Notification as ScoutNotification, scout, Status, StatusOrModel, strings} from '../../index';
export class DesktopNotification extends ScoutNotification implements DesktopNotificationModel {
declare model: DesktopNotificationModel;
declare eventMap: DesktopNotificationEventMap;
declare self: DesktopNotification;
duration: number;
removeTimeout: number;
nativeOnly: boolean;
nativeNotificationTitle: string;
nativeNotificationStatus: Status;
nativeNotificationVisibility: NativeNotificationVisibility;
nativeNotification: Notification;
nativeNotificationShown: boolean;
$loader: JQuery;
protected _removing: boolean;
constructor() {
super();
this.closable = true;
this.duration = 5000;
this.removeTimeout = null;
this._removing = false;
this.nativeOnly = false;
this.nativeNotificationTitle = null;
this.nativeNotificationStatus = null;
this.nativeNotificationVisibility = DesktopNotification.NativeNotificationVisibility.NONE;
this.nativeNotification = null;
this.nativeNotificationShown = false;
}
static NativeNotificationVisibility = {
/**
* No native notification is shown.
*/
NONE: 'none',
/**
* The native notification is only shown if the application is in background.
*/
BACKGROUND: 'background',
/**
* The native notification is always shown.
*/
ALWAYS: 'always'
} as const;
/**
* When duration is set to INFINITE, the notification is not removed automatically.
*/
static INFINITE = -1;
protected override _init(model: InitModelOf<this>) {
super._init(model);
let defaults = this.session.desktop.nativeNotificationDefaults;
if (defaults) {
this.nativeNotificationTitle = model.nativeNotificationTitle !== undefined ? model.nativeNotificationTitle : defaults.title;
if (this.nativeNotificationStatus) {
this.nativeNotificationStatus.iconId = this.nativeNotificationStatus.iconId !== undefined ? this.nativeNotificationStatus.iconId : defaults.iconId;
} else {
this.nativeNotificationStatus = new Status({
iconId: defaults.iconId
});
}
this.nativeNotificationVisibility = scout.nvl(model.nativeNotificationVisibility !== undefined ? model.nativeNotificationVisibility : defaults.visibility, DesktopNotification.NativeNotificationVisibility.NONE);
}
this.resolveTextKeys(['nativeNotificationTitle']);
}
protected override _render() {
this._initNativeNotification();
this.$container = this.$parent.prependDiv('desktop-notification');
this.$content = this.$container.appendDiv('desktop-notification-content');
this.$messageText = this.$content.appendDiv('desktop-notification-message');
this.$loader = this.$container.appendDiv('desktop-notification-loader');
if (Device.get().supportsCssAnimation()) {
this.$loader.addClass('animated');
}
if (this.nativeOnly) {
this.setVisible(false);
}
}
protected override _renderLoading() {
this.$container.toggleClass('loading', this.loading);
this.$loader.setVisible(this.loading);
}
protected override _destroy() {
if (this.nativeNotification) {
// No need to keep the native notification open if the regular one is closed (relevant if the user actively closes it)
this.nativeNotification.close();
}
super._destroy();
}
/** @internal */
_isDocumentHidden(): boolean {
return document.hidden;
}
protected _showNativeNotification(permission: NotificationPermission) {
if (permission === 'denied' || permission === 'default') {
if (this.nativeOnly) {
// See comment in _initNativeNotification
this.hide();
}
return;
}
let title = scout.nvl(this.nativeNotificationTitle, '');
let body = (this.nativeNotificationStatus || {}).message;
if (strings.empty(body)) {
body = (this.status || {}).message;
}
if (!body) {
body = '';
}
if (this.htmlEnabled) {
body = strings.plainText(body, {removeFontIcons: true});
}
let iconId = (this.nativeNotificationStatus || {}).iconId;
if (strings.empty(iconId)) {
// icon must not be null or empty. If no icon it must be undefined
iconId = undefined;
}
this.nativeNotification = new Notification(title, {
body: body,
icon: iconId
});
this.nativeNotification.addEventListener('show', event => {
this._setNativeNotificationShown(true);
});
this.nativeNotification.addEventListener('click', event => {
window.focus();
});
// Native notifications are closed when the regular notification is closed (either by the user, the timeout or programmatically)
this.nativeNotification.addEventListener('close', event => {
if (this.nativeOnly) {
// Only close it if nativeOnly is true.
// If nativeOnly is false, clicking the notification should reveal the app incl. the original notification which could contain more information (e.g. a link).
this.hide();
}
this.nativeNotification = null;
this._setNativeNotificationShown(false);
});
}
protected _initNativeNotification() {
if (this.nativeNotificationShown) {
// Don't show the same notification twice (could happen if the user reloads the page and the notification is still open. Especially important for nativeOnly with infinite duration).
return;
}
if (this.nativeNotificationVisibility === DesktopNotification.NativeNotificationVisibility.NONE) {
this._hideLaterIfNativeOnly();
return;
}
if (this.nativeNotificationVisibility === DesktopNotification.NativeNotificationVisibility.BACKGROUND && !this._isDocumentHidden()) {
this._hideLaterIfNativeOnly();
return;
}
if (!window.Notification || Notification.permission === 'denied') {
this._hideLaterIfNativeOnly();
return;
}
if (this._checkNotificationPromise()) {
Notification.requestPermission().then(this._showNativeNotification.bind(this));
} else {
// noinspection JSIgnoredPromiseFromCall
Notification.requestPermission(this._showNativeNotification.bind(this));
}
}
protected _hideLaterIfNativeOnly() {
if (!this.nativeOnly) {
return;
}
// If native notifications are not shown, there is no need to keep the (invisible) desktop notification open (prevent dom-leak)
setTimeout(() => this.hide()); // async because this method is called in render and removing the notification within render throws exception
}
/**
* Checks if browser supports the promise-based version of the method requestPermission. Safari only supports the older callback version.
*/
protected _checkNotificationPromise(): boolean {
try {
Notification.requestPermission().then();
} catch (e) {
return false;
}
return true;
}
protected override _onCloseIconClick() {
this.hide();
}
/**
* Displays the notification by adding it to the desktop and rendering it.
*/
show() {
this.session.desktop.addNotification(this);
}
/**
* Closes the notification by removing it from the desktop and destroying it. Also triggers a close event.
*/
hide() {
if (this._removing) {
return;
}
this.trigger('close');
this.session.desktop.removeNotification(this);
}
fadeIn($parent: JQuery) {
this.render($parent);
if (!Device.get().supportsCssAnimation()) {
return;
}
this.$container.addClassForAnimation('desktop-notification-slide-in');
}
fadeOut() {
// prevent fadeOut from running more than once (for instance from the click of a user).
if (this._removing) {
return;
}
this._removing = true;
if (!Device.get().supportsCssAnimation() || !this.rendered) {
this.destroy();
return;
}
if (!this.$container.isVisible()) {
// Destroy immediately if it is invisible because the animationend event would not be triggered (is the case if nativeOnly is true)
this.destroy();
return;
}
this.$container.addClass('desktop-notification-fade-out');
this.$container.oneAnimationEnd(() => {
this.destroy();
});
}
override invalidateLayoutTree() {
// called by notification.js. Since desktop notification has no htmlComp, no need to invalidate
}
protected _setNativeNotificationShown(shown: boolean) {
this._setProperty('nativeNotificationShown', shown);
}
setNativeNotificationTitle(title: string) {
this.setProperty('nativeNotificationTitle', title);
}
setNativeNotificationStatus(status: StatusOrModel) {
this.setProperty('nativeNotificationStatus', status);
}
protected _setNativeNotificationStatus(status: StatusOrModel) {
status = Status.ensure(status);
this._setProperty('nativeNotificationStatus', status);
}
setNativeNotificationVisibility(visibility: NativeNotificationVisibility) {
this.setProperty('nativeNotificationVisibility', visibility);
}
}
export type NativeNotificationVisibility = EnumObject<typeof DesktopNotification.NativeNotificationVisibility>;