Core.UI = Core.UI || {}; Core.UI.Notifications = class extends _core { /** * @param {string} property * @return {*} */ static _get(property) { return this.hasOwnProperty(property) ? this[property] : null; } /** * @param {string} property * @param {*} value */ static _set(property, value) { this[property] = value; } /** * @return {number} */ static get delay() { return this._get('_delay') || 5000; } /** * @param {number} newDelay */ static set delay(newDelay) { this._set('_delay', newDelay); } /** * @param {number} lastChecked Use Date.now() */ static set lastChecked(lastChecked) { this._set('_lastChecked', lastChecked); } /** * @return {number} */ static get lastChecked() { return this._get('_lastChecked'); } /** * Get notification container. * * @return {jQuery} */ static getContainer() { return Core.UI.MainMenu.getElement().find('.right.item'); } /** * Poll the server for notification status. */ static poll() { this.lastChecked = Date.now(); Core.API.call({ url: `/api/${Page.Meta.api_version}/page/home/notifications/index.php`, data: { func: 'apiGetNotifications', force_login: false, version: Page.Meta.version, error_return_code: 2 }, callback: (data) => { if (data.return == 2) { return; } if (!Core.UI.Notifications.getContainer().find('.user-menu.dropdown').exists()) { let $dropdown = $(data.user_menu_html); Core.UI.Notifications.getContainer().append($dropdown); $dropdown.dropdown(); $dropdown.dropdown('toggle').dropdown('toggle'); $dropdown.children('.menu').children('.item').each(function () { let $button = $(this); let notification = new Core.UI.Notification({ selector: '.user-menu .item', properties: { id: $button.data('id'), action: $button.data('action') } }); notification.bind(); }); } let enabledNotifications = []; if (!_.isEmpty(data.notifications)) { for (const name in data.notifications) { enabledNotifications.push(name); } } Core.UI.Notifications.getContainer().children('.notification.button').each(function () { let $button = $(this); if ($button.isMouseOver()) { $button.data('hovered', true); } let notificationName = $button.data('id'); if (enabledNotifications.indexOf(notificationName) == -1) { $button.remove(); } }); for (const name of enabledNotifications) { let notification = new Core.UI.Notification(data.notifications[name]); notification.render(); } Core.UI.Notifications.getContainer().children('.notification.button').each(function () { let $button = $(this); if ($button.data('hovered')) { $button.popup('show'); $button.data('hovered', null); } }); }, fadeParameters: { hideLoader: true }, complete: () => setTimeout(Core.UI.Notifications.poll, Core.UI.Notifications.delay) }); } };