/* global $, APP, toastr, Impromptu */ import UIUtil from './UIUtil'; /** * Flag for enable/disable of the notifications. * @type {boolean} */ let notificationsEnabled = true; /** * Flag for enabling/disabling popups. * @type {boolean} */ let popupEnabled = true; /** * Currently displayed two button dialog. * @type {null} */ let twoButtonDialog = null; var messageHandler = { OK: "dialog.OK", CANCEL: "dialog.Cancel", /** * Shows a message to the user. * * @param titleKey the key used to find the translation of the title of the * message, if a message title is not provided. * @param messageKey the key used to find the translation of the message, * if a message is not provided. * @param title the title of the message. If a falsy value is provided, * titleKey will be used to get a title via the translation API. * @param message the message to show. If a falsy value is provided, * messageKey will be used to get a message via the translation API. * @param closeFunction function to be called after * the prompt is closed (optional) * @return the prompt that was created, or null */ openMessageDialog: function(titleKey, messageKey, title, message, closeFunction) { if (!popupEnabled) return null; if (!title) { title = APP.translation.generateTranslationHTML(titleKey); } if (!message) { message = APP.translation.generateTranslationHTML(messageKey); } return $.prompt(message, { title: this._getFormattedTitleString(title), persistent: false, promptspeed: 0, classes: this._getDialogClasses(), close: function (e, v, m, f) { if(closeFunction) closeFunction(e, v, m, f); } }); }, /** * Shows a message to the user with two buttons: first is given as a * parameter and the second is Cancel. * * @param titleString the title of the message * @param msgString the text of the message * @param persistent boolean value which determines whether the message is * persistent or not * @param leftButton the fist button's text * @param submitFunction function to be called on submit * @param loadedFunction function to be called after the prompt is fully * loaded * @param closeFunction function to be called after the prompt is closed * @param focus optional focus selector or button index to be focused after * the dialog is opened * @param defaultButton index of default button which will be activated when * the user press 'enter'. Indexed from 0. * @return the prompt that was created, or null */ openTwoButtonDialog: function(options) { let { titleKey, titleString, msgKey, msgString, leftButtonKey, submitFunction, loadedFunction, closeFunction, focus, size, defaultButton, wrapperClass, classes } = options; if (!popupEnabled || twoButtonDialog) return null; var buttons = []; var leftButton = leftButtonKey ? APP.translation.generateTranslationHTML(leftButtonKey) : APP.translation.generateTranslationHTML('dialog.Submit'); buttons.push({ title: leftButton, value: true}); var cancelButton = APP.translation.generateTranslationHTML("dialog.Cancel"); buttons.push({title: cancelButton, value: false}); var message = msgString, title = titleString; if (titleKey) { title = APP.translation.generateTranslationHTML(titleKey); } if (msgKey) { message = APP.translation.generateTranslationHTML(msgKey); } classes = classes || this._getDialogClasses(size); if (wrapperClass) { classes.prompt += ` ${wrapperClass}`; } twoButtonDialog = $.prompt(message, { title: this._getFormattedTitleString(title), persistent: false, buttons: buttons, defaultButton: defaultButton, focus: focus, loaded: loadedFunction, promptspeed: 0, classes, submit: function (e, v, m, f) { twoButtonDialog = null; if (v){ if (submitFunction) submitFunction(e, v, m, f); } }, close: function (e, v, m, f) { twoButtonDialog = null; if (closeFunction) { closeFunction(e, v, m, f); } } }); return twoButtonDialog; }, /** * Shows a message to the user with two buttons: first is given as a * parameter and the second is Cancel. * * @param titleString the title of the message * @param msgString the text of the message * @param persistent boolean value which determines whether the message is * persistent or not * @param buttons object with the buttons. The keys must be the name of the * button and value is the value that will be passed to * submitFunction * @param submitFunction function to be called on submit * @param loadedFunction function to be called after the prompt is fully * loaded * @param closeFunction function to be called on dialog close */ openDialog: function (titleString, msgString, persistent, buttons, submitFunction, loadedFunction, closeFunction) { if (!popupEnabled) return; let args = { title: this._getFormattedTitleString(titleString), persistent: persistent, buttons: buttons, defaultButton: 1, promptspeed: 0, loaded: loadedFunction, submit: submitFunction, close: closeFunction, classes: this._getDialogClasses() }; if (persistent) { args.closeText = ''; } return new Impromptu(msgString, args); }, /** * Returns the formatted title string. * * @return the title string formatted as a div. */ _getFormattedTitleString(titleString) { let $titleString = $('