@zextras/carbonio-shell-ui
Version:
The Zextras Carbonio web client
153 lines • 5.04 kB
JavaScript
;
/*
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNotificationManager = void 0;
const lodash_1 = require("lodash");
const notification_mp3_1 = __importDefault(require("../../assets/notification.mp3"));
const getters_1 = require("../store/login/getters");
/**
* The main goals of the NotificationManager are:
* - to provide a single and rich implementation for all the Carbonio modules,
* reducing the boilerplate code needed to send a notification to the user
* - to optimize the audio notifications avoiding to spam the same sound file
* in a short period of time
* - to act as a collector for all the notification (for possible future
* implementations)
*
* In order to reduce the effort needed to send a notification the class
* provided a set of default values/assets (e.g. icon, sound, title, ...)
*
* The class is provided as a singleton
*/
class NotificationManager {
static instance;
/**
* Minimum time (ms) to wait before the same audio file will be played
* @private
*/
static DEBOUNCE_TIME = 1000;
/**
* Default configuration for the popup-only notification
* @private
*/
PopupNotificationDefaultConfig = {
title: '',
vibrate: [200, 100, 200],
icon: (0, getters_1.getFavicon)()
};
/**
* Default configuration for the audio-only notification
* @private
*/
AudioNotificationDefaultConfig = {
sound: notification_mp3_1.default
};
/**
* Default configuration for a notification with both popup and audio
* @private
*/
NotificationDefaultConfig = {
...this.PopupNotificationDefaultConfig,
...this.AudioNotificationDefaultConfig,
showPopup: true,
playSound: false
};
/**
* Map of functions to play a specific audio file
* @private
*/
functions = new Map();
/**
* Gets or creates the (debounced) function to play the audio file
* @param sound - relative path to the audio file to play
*/
getAudioFileFunction = (sound) => {
if (!this.functions.has(sound)) {
this.functions.set(sound, (0, lodash_1.debounce)(() => {
new Audio(sound).play().then();
this.functions.delete(sound);
}, NotificationManager.DEBOUNCE_TIME));
}
const result = this.functions.get(sound);
return result ?? lodash_1.noop;
};
/**
* Executes the debounced function to play the audio file
* @param config - Configuration for the audio notification. In case of
* missing properties default values are used
*/
playSound = (config) => {
const defConfig = {
...this.AudioNotificationDefaultConfig,
...config
};
if (!defConfig.sound) {
return;
}
this.getAudioFileFunction(defConfig.sound)();
};
/**
* Shows a popup notification
* @param config - Configuration for the popup notification. In case of
* missing properties default values are used
*/
showPopup = (config) => {
const defConfig = {
...this.PopupNotificationDefaultConfig,
...config
};
const n = new Notification(defConfig.title, {
body: defConfig.message,
vibrate: defConfig.vibrate,
icon: defConfig.icon,
tag: defConfig?.tag
});
if (defConfig.onClick) {
n.addEventListener('click', defConfig.onClick);
}
};
/**
* Sends a popup/audio notification to the user
* @param config - Configuration for the notification. In case of
* missing properties default values are used
*/
notify = (config) => {
const defConfig = {
...this.NotificationDefaultConfig,
...config
};
if (defConfig?.showPopup) {
this.showPopup(defConfig);
}
if (defConfig?.playSound) {
this.playSound(defConfig);
}
};
/**
* Sends multiple notifications
* @param config - Array of configurations for the notifications. In case of
* missing properties default values are used
*/
multipleNotify = (config) => {
config.forEach((conf) => this.notify(conf));
};
/**
* Return the singleton instance
*/
static getInstance() {
if (!NotificationManager.instance) {
NotificationManager.instance = new NotificationManager();
}
return NotificationManager.instance;
}
}
const getNotificationManager = () => NotificationManager.getInstance();
exports.getNotificationManager = getNotificationManager;
//# sourceMappingURL=NotificationManager.js.map