|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 4 | + * See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 5 | + * All rights not expressly granted are reserved. |
| 6 | + * |
| 7 | + * This software is distributed under the terms of the GNU General Public |
| 8 | + * License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 9 | + * |
| 10 | + * In applying this license CERN does not waive the privileges and immunities |
| 11 | + * granted to it by virtue of its status as an Intergovernmental Organization |
| 12 | + * or submit itself to any jurisdiction. |
| 13 | + */ |
| 14 | + |
| 15 | +/* Global: window */ |
| 16 | + |
| 17 | +import { isContextSecure } from './browserContext.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Browser notification permission values. |
| 21 | + * Mirrors the Notification API specification. |
| 22 | + * @link https://developer.mozilla.org/en-US/docs/Web/API/Notification/permission_static |
| 23 | + */ |
| 24 | +export const BrowserNotificationPermission = Object.freeze({ |
| 25 | + GRANTED: 'granted', |
| 26 | + DEFAULT: 'default', |
| 27 | + DENIED: 'denied', |
| 28 | +}); |
| 29 | + |
| 30 | +/** |
| 31 | + * Get the current browser notification permission. |
| 32 | + * @returns {BrowserNotificationPermission|undefined} One of {@link BrowserNotificationPermission}, or `undefined` if unsupported. |
| 33 | + */ |
| 34 | +export const getBrowserNotificationPermission = () => |
| 35 | + window?.Notification?.permission; |
| 36 | + |
| 37 | +/** |
| 38 | + * Request browser notification permission (async/await). |
| 39 | + * @returns {Promise<BrowserNotificationPermission|undefined>} One of {@link BrowserNotificationPermission}, or `undefined` if unsupported |
| 40 | + */ |
| 41 | +export const requestBrowserNotificationPermissions = async () => { |
| 42 | + if (!isContextSecure()) { |
| 43 | + return undefined; |
| 44 | + } |
| 45 | + |
| 46 | + const permission = getBrowserNotificationPermission(); |
| 47 | + if (permission === BrowserNotificationPermission.GRANTED) { |
| 48 | + return permission; |
| 49 | + } |
| 50 | + |
| 51 | + return await window.Notification?.requestPermission(); |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Check if notifications can be shown immediately. |
| 56 | + * @returns {boolean} `true` if the Notification API is available, the context is secure |
| 57 | + * and the current {@link BrowserNotificationPermission} is {@link BrowserNotificationPermission.GRANTED}, `false` otherwise. |
| 58 | + */ |
| 59 | +export const areBrowserNotificationsGranted = () => |
| 60 | + isContextSecure() && getBrowserNotificationPermission() === BrowserNotificationPermission.GRANTED; |
| 61 | + |
| 62 | +/** |
| 63 | + * @typedef {object} NotificationOptions |
| 64 | + * @property {string} title Notification title |
| 65 | + * @property {string} [body] Notification body |
| 66 | + * @property {string} [icon] Notification icon URL (defaults to server icon) |
| 67 | + * @property {(event: Event) => void} [onclick] Triggered when the notification is clicked |
| 68 | + * @property {(event: Event) => void} [onerror] Triggered if the notification fails to display |
| 69 | + * @property {(event: Event) => void} [onshow] Triggered when the notification is shown |
| 70 | + * @property {(event: Event) => void} [onclose] Triggered when the notification is closed |
| 71 | + */ |
| 72 | + |
| 73 | +/** |
| 74 | + * Show a native browser notification. |
| 75 | + * @param {NotificationOptions} options - The browser notification options |
| 76 | + * @returns {Notification|null} {@link Notification} instance, or `null` if unavailable |
| 77 | + */ |
| 78 | +export const showNativeBrowserNotification = (options) => { |
| 79 | + if (!areBrowserNotificationsGranted()) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + const { |
| 84 | + title, |
| 85 | + onclick, |
| 86 | + onerror, |
| 87 | + onshow, |
| 88 | + onclose, |
| 89 | + icon = '/favicon.ico', |
| 90 | + ...notificationOptions |
| 91 | + } = options; |
| 92 | + if (!title) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + const notification = new window.Notification(title, { icon, ...notificationOptions }); |
| 97 | + Object.entries({ onclick, onerror, onshow, onclose }) |
| 98 | + .filter(([_, eventFunc]) => typeof eventFunc === 'function') |
| 99 | + .forEach(([eventName, eventFunc]) => { |
| 100 | + notification[eventName] = eventFunc; |
| 101 | + }); |
| 102 | + |
| 103 | + return notification; |
| 104 | +}; |
0 commit comments