Skip to content

Commit 388387f

Browse files
committed
Use browser helper functions in COG
1 parent 434e91b commit 388387f

File tree

2 files changed

+14
-40
lines changed

2 files changed

+14
-40
lines changed

Control/public/Model.js

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414

1515
// Import frontend framework
16-
import {Observable, WebSocketClient, QueryRouter, Loader, sessionService, RemoteData} from '/js/src/index.js';
16+
import {Observable, WebSocketClient, QueryRouter, Loader, sessionService, RemoteData, showNativeBrowserNotification} from '/js/src/index.js';
1717
import {Notification as O2Notification} from '/js/src/index.js';
1818
import Lock from './lock/Lock.js';
1919
import Environment from './environment/Environment.js';
@@ -153,7 +153,7 @@ export default class Model extends Observable {
153153
if (this.detectors.selected || this.session.role == ROLES.Guest) {
154154
this.handleLocationChange();
155155
}
156-
this.requestBrowserNotificationPermissions()
156+
157157
this.notify();
158158
}
159159

@@ -170,10 +170,14 @@ export default class Model extends Observable {
170170
const { payload: task } = message;
171171
if (task?.taskId) {
172172
// Notification is for the first task in error from an environment
173-
this.showNativeNotification({
173+
showNativeBrowserNotification({
174174
title: `TASK in ${task.state ?? 'unknown'} state`,
175175
body: `Task ${task.id} in environment ${task.environmentId} is in ${task.state ?? 'unknown'} state`,
176-
url: `?page=environment&id=${task.environmentId}`
176+
icon: '/o2_icon.png',
177+
onclick: (event) => {
178+
event?.preventDefault();
179+
this.router.go(`?page=environment&id=${task.environmentId}`, '_blank');
180+
}
177181
});
178182
}
179183
break;
@@ -361,36 +365,4 @@ export default class Model extends Observable {
361365
this.notify();
362366
}
363367
}
364-
365-
/**
366-
* Display a browser notification(Notification - Web API)
367-
* @param {String} message
368-
*/
369-
showNativeNotification(message) {
370-
const notification = new Notification(message.title, {body: message.body, icon: '/o2_icon.png'});
371-
notification.onclick = (event) => {
372-
event.preventDefault();
373-
this.router.go(message.url, '_blank');
374-
}
375-
}
376-
377-
/**
378-
* Request notification permission
379-
*/
380-
requestBrowserNotificationPermissions() {
381-
if (this.checkBrowserNotificationPermissions()) {
382-
Notification.requestPermission();
383-
}
384-
}
385-
386-
/**
387-
* Defines policy when user can trigger request for enabling notifications
388-
* This is used to display/hide enable button
389-
* @returns {boolean} True when notifications are not enabled and HTTPS is used
390-
*/
391-
checkBrowserNotificationPermissions() {
392-
return (Notification.permission === 'denied' ||
393-
Notification.permission === 'default') &&
394-
window.location.protocol === "https:";
395-
}
396368
}

Control/public/common/appHeader.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* or submit itself to any jurisdiction.
1313
*/
1414

15-
import {h, iconPerson} from '/js/src/index.js';
15+
import {h, iconPerson, areBrowserNotificationsGranted, requestBrowserNotificationPermissions} from '/js/src/index.js';
1616

1717
/**
1818
* Application header (left part): lockpad button and application name
@@ -39,11 +39,13 @@ const loginButton = (model) => h('.dropdown', {class: model.accountMenuEnabled ?
3939
h('button.btn', {onclick: () => model.toggleAccountMenu()}, iconPerson()),
4040
h('.dropdown-menu', [
4141
h('p.m3.mv2.text-ellipsis', `Welcome ${model.session.name}`, h('sup', model.session.role)),
42-
model.session.personid === 0 // anonymous user has id 0
42+
model.session.personid === 0 // anonymous user has id 0
4343
&& h('p.m3.gray-darker', 'You are connected as anonymous, no authentification needed for this application.'),
44-
model.checkBrowserNotificationPermissions() &&
44+
!areBrowserNotificationsGranted() &&
4545
h('a.menu-item', {onclick: () => {
46-
model.toggleAccountMenu(); model.requestBrowserNotificationPermissions()}
46+
requestBrowserNotificationPermissions();
47+
model.toggleAccountMenu();
48+
}
4749
}, 'Enable notifications'),
4850
model.session.personid !== 0 &&
4951
h('a.menu-item', {onclick: () => alert(`Not implemented`)}, 'Logout')

0 commit comments

Comments
 (0)