Completed
Push — master ( e88c19...17669b )
by Rain
03:00
created

Notification.js ➔ ... ➔ ???   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 31
rs 5.3846
1
2
import window from 'window';
3
import ko from 'ko';
4
5
import {DesktopNotification, Magics} from 'Common/Enums';
6
import * as Events from 'Common/Events';
7
import Audio from 'Common/Audio';
8
9
import * as Settings from 'Storage/Settings';
10
11
class NotificationUserStore
12
{
13
	constructor() {
14
		this.enableSoundNotification = ko.observable(false);
15
		this.soundNotificationIsSupported = ko.observable(false);
16
17
		this.allowDesktopNotification = ko.observable(false);
18
19
		this.desktopNotificationPermissions = ko.computed(() => {
20
21
			this.allowDesktopNotification();
22
23
			let result = DesktopNotification.NotSupported;
24
25
			const NotificationClass = this.notificationClass();
26
			if (NotificationClass && NotificationClass.permission)
27
			{
28
				switch (NotificationClass.permission.toLowerCase())
29
				{
30
					case 'granted':
31
						result = DesktopNotification.Allowed;
32
						break;
33
					case 'denied':
34
						result = DesktopNotification.Denied;
35
						break;
36
					case 'default':
37
						result = DesktopNotification.NotAllowed;
38
						break;
39
					// no default
40
				}
41
			}
42
			else if (window.webkitNotifications && window.webkitNotifications.checkPermission)
43
			{
44
				result = window.webkitNotifications.checkPermission();
45
			}
46
47
			return result;
48
49
		}).extend({notify: 'always'});
50
51
		this.enableDesktopNotification = ko.computed({
52
			read: () => this.allowDesktopNotification() && DesktopNotification.Allowed === this.desktopNotificationPermissions(),
53
			write: (value) => {
54
				if (value)
55
				{
56
					const
57
						NotificationClass = this.notificationClass(),
58
						permission = this.desktopNotificationPermissions();
59
60
					if (NotificationClass && DesktopNotification.Allowed === permission)
61
					{
62
						this.allowDesktopNotification(true);
63
					}
64
					else if (NotificationClass && DesktopNotification.NotAllowed === permission)
65
					{
66
						NotificationClass.requestPermission(() => {
67
68
							this.allowDesktopNotification.valueHasMutated();
69
70
							if (DesktopNotification.Allowed === this.desktopNotificationPermissions())
71
							{
72
								if (this.allowDesktopNotification())
73
								{
74
									this.allowDesktopNotification.valueHasMutated();
75
								}
76
								else
77
								{
78
									this.allowDesktopNotification(true);
79
								}
80
							}
81
							else
82
							{
83
								if (this.allowDesktopNotification())
84
								{
85
									this.allowDesktopNotification(false);
86
								}
87
								else
88
								{
89
									this.allowDesktopNotification.valueHasMutated();
90
								}
91
							}
92
						});
93
					}
94
					else
95
					{
96
						this.allowDesktopNotification(false);
97
					}
98
				}
99
				else
100
				{
101
					this.allowDesktopNotification(false);
102
				}
103
			}
104
		}).extend({notify: 'always'});
105
106
		if (!this.enableDesktopNotification.valueHasMutated)
107
		{
108
			this.enableDesktopNotification.valueHasMutated = () => {
109
				this.allowDesktopNotification.valueHasMutated();
110
			};
111
		}
112
113
		this.computers();
114
115
		this.initNotificationPlayer();
116
	}
117
118
	computers() {
119
		this.isDesktopNotificationSupported = ko.computed(
120
			() => DesktopNotification.NotSupported !== this.desktopNotificationPermissions()
121
		);
122
123
		this.isDesktopNotificationDenied = ko.computed(
124
			() => DesktopNotification.NotSupported === this.desktopNotificationPermissions() ||
125
				DesktopNotification.Denied === this.desktopNotificationPermissions()
126
		);
127
	}
128
129
	initNotificationPlayer() {
130
		if (Audio && Audio.supportedNotification)
131
		{
132
			this.soundNotificationIsSupported(true);
133
		}
134
		else
135
		{
136
			this.enableSoundNotification(false);
137
			this.soundNotificationIsSupported(false);
138
		}
139
	}
140
141
	playSoundNotification(skipSetting) {
142
		if (Audio && Audio.supportedNotification && (skipSetting ? true : this.enableSoundNotification()))
143
		{
144
			Audio.playNotification();
145
		}
146
	}
147
148
	displayDesktopNotification(imageSrc, title, text, nessageData) {
149
		if (this.enableDesktopNotification())
150
		{
151
			const
152
				NotificationClass = this.notificationClass(),
153
				notification = NotificationClass ? new NotificationClass(title, {
154
					body: text,
155
					icon: imageSrc
156
				}) : null;
157
158
			if (notification)
159
			{
160
				if (notification.show)
161
				{
162
					notification.show();
163
				}
164
165
				if (nessageData)
166
				{
167
					notification.onclick = () => {
168
169
						window.focus();
170
171
						if (nessageData.Folder && nessageData.Uid)
172
						{
173
							Events.pub('mailbox.message.show', [nessageData.Folder, nessageData.Uid]);
174
						}
175
					};
176
				}
177
178
				window.setTimeout((function(localNotifications) {
179
					return () => {
180
						if (localNotifications.cancel)
181
						{
182
							localNotifications.cancel();
183
						}
184
						else if (localNotifications.close)
185
						{
186
							localNotifications.close();
187
						}
188
					};
189
				}(notification)), Magics.Time7s);
190
			}
191
		}
192
	}
193
194
	populate() {
195
		this.enableSoundNotification(!!Settings.settingsGet('SoundNotification'));
196
		this.enableDesktopNotification(!!Settings.settingsGet('DesktopNotifications'));
197
	}
198
199
	/**
200
	 * @returns {*|null}
201
	 */
202
	notificationClass() {
203
		return window.Notification && window.Notification.requestPermission ? window.Notification : null;
204
	}
205
}
206
207
module.exports = new NotificationUserStore();
208