Debian Codegraph @

addons/web/static/lib/hoot/mock/notification.js

raw
1 /** @odoo-module */
2
3 import { MockEventTarget } from "../hoot_utils";
4 import { currentPermissions } from "./navigator";
5
6 //-----------------------------------------------------------------------------
7 // Global
8 //-----------------------------------------------------------------------------
9
10 const { Event, Promise, Set } = globalThis;
11
12 //-----------------------------------------------------------------------------
13 // Internal
14 //-----------------------------------------------------------------------------
15
16 /** @type {Set<MockNotification>} */
17 const notifications = new Set();
18
19 //-----------------------------------------------------------------------------
20 // Exports
21 //-----------------------------------------------------------------------------
22
23 /**
24 * Returns the list of notifications that have been created since the last call
25 * to this function, consuming it in the process.
26 *
27 * @returns {MockNotification[]}
28 */
29 export function flushNotifications() {
30 const result = [...notifications];
31 notifications.clear();
32 return result;
33 }
34
35 export class MockNotification extends MockEventTarget {
36 static publicListeners = ["click", "close", "error", "show"];
37
38 /** @type {NotificationPermission} */
39 static get permission() {
40 return currentPermissions.notifications.state;
41 }
42
43 /** @type {NotificationPermission} */
44 get permission() {
45 return this.constructor.permission;
46 }
47
48 /**
49 * @param {string} title
50 * @param {NotificationOptions} [options]
51 */
52 constructor(title, options) {
53 super(...arguments);
54
55 this.title = title;
56 this.options = options;
57
58 if (this.permission === "granted") {
59 notifications.push(this);
60 }
61 }
62
63 static requestPermission() {
64 return Promise.resolve(this.permission);
65 }
66
67 click() {
68 this.dispatchEvent(new Event("click"));
69 }
70
71 close() {
72 notifications.delete(this);
73 this.dispatchEvent(new Event("close"));
74 }
75
76 show() {
77 this.dispatchEvent(new Event("show"));
78 }
79 }