123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- /* global $, APP, jQuery, toastr, Impromptu */
- /* jshint -W101 */
-
- 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: title,
- persistent: false,
- 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(titleKey, titleString, msgKey, msgString,
- persistent, leftButtonKey, submitFunction, loadedFunction,
- closeFunction, focus, defaultButton) {
-
- if (!popupEnabled || twoButtonDialog)
- return null;
-
- var buttons = [];
-
- var leftButton = APP.translation.generateTranslationHTML(leftButtonKey);
- 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);
- }
- twoButtonDialog = $.prompt(message, {
- title: title,
- persistent: false,
- buttons: buttons,
- defaultButton: defaultButton,
- focus: focus,
- loaded: loadedFunction,
- submit: function (e, v, m, f) {
- twoButtonDialog = null;
- 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;
-
- var args = {
- title: titleString,
- persistent: persistent,
- buttons: buttons,
- defaultButton: 1,
- loaded: loadedFunction,
- submit: submitFunction,
- close: closeFunction
- };
-
- if (persistent) {
- args.closeText = '';
- }
-
- return new Impromptu(msgString, args);
- },
-
- /**
- * Closes currently opened dialog.
- */
- closeDialog: function () {
- $.prompt.close();
- },
-
- /**
- * Shows a dialog with different states to the user.
- *
- * @param statesObject object containing all the states of the dialog.
- */
- openDialogWithStates: function (statesObject, options) {
- if (!popupEnabled)
- return;
-
- return new Impromptu(statesObject, options);
- },
-
- /**
- * Opens new popup window for given <tt>url</tt> centered over current
- * window.
- *
- * @param url the URL to be displayed in the popup window
- * @param w the width of the popup window
- * @param h the height of the popup window
- * @param onPopupClosed optional callback function called when popup window
- * has been closed.
- *
- * @returns {object} popup window object if opened successfully or undefined
- * in case we failed to open it(popup blocked)
- */
- openCenteredPopup: function (url, w, h, onPopupClosed) {
- if (!popupEnabled)
- return;
-
- var l = window.screenX + (window.innerWidth / 2) - (w / 2);
- var t = window.screenY + (window.innerHeight / 2) - (h / 2);
- var popup = window.open(
- url, '_blank',
- 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
- if (popup && onPopupClosed) {
- var pollTimer = window.setInterval(function () {
- if (popup.closed !== false) {
- window.clearInterval(pollTimer);
- onPopupClosed();
- }
- }, 200);
- }
- return popup;
- },
-
- /**
- * Shows a dialog prompting the user to send an error report.
- *
- * @param titleKey the title of the message
- * @param msgKey the text of the message
- * @param error the error that is being reported
- */
- openReportDialog: function(titleKey, msgKey, error) {
- this.openMessageDialog(titleKey, msgKey);
- console.log(error);
- //FIXME send the error to the server
- },
-
- /**
- * Shows an error dialog to the user.
- * @param titleKey the title of the message.
- * @param msgKey the text of the message.
- */
- showError: function(titleKey, msgKey) {
-
- if (!titleKey) {
- titleKey = "dialog.oops";
- }
- if (!msgKey) {
- msgKey = "dialog.defaultError";
- }
- messageHandler.openMessageDialog(titleKey, msgKey);
- },
-
- /**
- * Displays a notification.
- * @param displayName the display name of the participant that is
- * associated with the notification.
- * @param displayNameKey the key from the language file for the display
- * name. Only used if displayName i not provided.
- * @param cls css class for the notification
- * @param messageKey the key from the language file for the text of the
- * message.
- * @param messageArguments object with the arguments for the message.
- * @param options object with language options.
- */
- notify: function(displayName, displayNameKey, cls, messageKey,
- messageArguments, options) {
-
- // If we're in ringing state we skip all toaster notifications.
- if(!notificationsEnabled || APP.UI.isRingOverlayVisible())
- return;
-
- var displayNameSpan = '<span class="nickname" ';
- if (displayName) {
- displayNameSpan += ">" + UIUtil.escapeHtml(displayName);
- } else {
- displayNameSpan += "data-i18n='" + displayNameKey +
- "'>" + APP.translation.translateString(displayNameKey);
- }
- displayNameSpan += "</span>";
- return toastr.info(
- displayNameSpan + '<br>' +
- '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
- (messageArguments?
- " data-i18n-options='" + JSON.stringify(messageArguments) + "'"
- : "") + ">" +
- APP.translation.translateString(messageKey,
- messageArguments) +
- '</span>', null, options);
- },
-
- /**
- * Removes the toaster.
- * @param toasterElement
- */
- remove: function(toasterElement) {
- toasterElement.remove();
- },
-
- /**
- * Enables / disables notifications.
- */
- enableNotifications: function (enable) {
- notificationsEnabled = enable;
- },
-
- enablePopups: function (enable) {
- popupEnabled = enable;
- }
- };
-
- module.exports = messageHandler;
|