/* * This file is part of the Kimai time-tracking app. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /*! * [KIMAI] KimaiAlert: notifications for Kimai */ import KimaiPlugin from "../KimaiPlugin"; import {Modal, Toast} from "bootstrap"; export default class KimaiAlert extends KimaiPlugin { /** * @return {string} */ getId() { return 'alert'; } /** * @param {string} title * @param {string|array|undefined} message */ error(title, message) { const translation = this.getTranslation(); if (translation.has(title)) { title = translation.get(title); } title = title.replace('%reason%', ''); if (message === undefined) { message = null; } if (message !== null) { if (translation.has(message)) { message = translation.get(message); } if (Array.isArray(message)) { message = message.join('
'); } } const id = 'alert_global_error'; const oldModalElement = document.getElementById(id); if (oldModalElement !== null) { Modal.getOrCreateInstance(oldModalElement).hide(); } const html = ` `; this._showModal(html); } /** * @param {string} message */ warning(message) { this._show('warning', message); } /** * @param {string} message */ success(message) { this._toast('success', message); } /** * @param {string} message */ info(message) { this._show('info', message); } /** * @param {string} html * @private */ _showModal(html) { const container = document.body; const template = document.createElement('template'); template.innerHTML = html.trim(); const element = template.content.firstChild; container.appendChild(element); const modal = new Modal(element); element.addEventListener('hidden.bs.modal', function () { container.removeChild(element); }); modal.show(); } /** * @param {string} type * @param {string} message * @private */ _show(type, message) { const translation = this.getTranslation(); if (translation.has(message)) { message = translation.get(message); } const html = ` `; this._showModal(html); } /** * @param {string} type * @return {string} * @private */ _mapClass(type) { if (type === 'info' || type === 'success' || type === 'warning' || type === 'danger') { return type; } else if (type === 'error') { return 'danger'; } return 'primary'; } /** * @param type * @param message * @private */ _toast(type, message) { const translation = this.getTranslation(); if (translation.has(message)) { message = translation.get(message); } let icon = ''; if (type === 'success') { icon = ''; } else if (type === 'warning') { icon = ''; } else if (type === 'danger' || type === 'error') { icon = ''; } const html = ``; const container = document.getElementById('toast-container'); const template = document.createElement('template'); template.innerHTML = html.trim(); const element = template.content.firstChild; container.appendChild(element); const toast = new Toast(element); element.addEventListener('hidden.bs.toast', function () { container.removeChild(element); }); toast.show(); } /** * Callback receives a bool value (true = confirm, false = cancel / close without action). * * @param message * @param callback */ question(message, callback) { const translation = this.getTranslation(); if (translation.has(message)) { message = translation.get(message); } const css = this._mapClass('info'); const html = ` `; const container = document.body; const template = document.createElement('template'); template.innerHTML = html.trim(); const element = template.content.firstChild; container.appendChild(element); element.querySelector('.question-confirm').addEventListener('click', () => { callback(true); }); element.querySelector('.question-cancel').addEventListener('click', () => { callback(false); }); const modal = new Modal(element); element.addEventListener('hidden.bs.modal', () => { container.removeChild(element); }); modal.show(); } }