2015-09-11 03:05:53 +00:00
|
|
|
/* global $, APP, jQuery, toastr, Impromptu */
|
2015-09-11 02:42:15 +00:00
|
|
|
/* jshint -W101 */
|
2015-08-06 23:34:40 +00:00
|
|
|
|
2016-02-12 12:48:57 +00:00
|
|
|
import UIUtil from './UIUtil';
|
|
|
|
|
2015-08-06 23:34:40 +00:00
|
|
|
/**
|
|
|
|
* Flag for enable/disable of the notifications.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
let notificationsEnabled = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag for enabling/disabling popups.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
let popupEnabled = true;
|
|
|
|
|
2016-07-06 16:00:04 +00:00
|
|
|
/**
|
|
|
|
* Currently displayed two button dialog.
|
|
|
|
* @type {null}
|
|
|
|
*/
|
|
|
|
let twoButtonDialog = null;
|
|
|
|
|
2016-06-08 19:34:02 +00:00
|
|
|
var messageHandler = {
|
|
|
|
OK: "dialog.OK",
|
|
|
|
CANCEL: "dialog.Cancel",
|
2015-08-06 23:34:40 +00:00
|
|
|
|
2014-08-14 15:29:28 +00:00
|
|
|
/**
|
|
|
|
* Shows a message to the user.
|
|
|
|
*
|
2015-09-02 22:29:53 +00:00
|
|
|
* @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.
|
2016-07-06 18:10:45 +00:00
|
|
|
* @param closeFunction function to be called after
|
|
|
|
* the prompt is closed (optional)
|
|
|
|
* @return the prompt that was created, or null
|
2014-08-14 15:29:28 +00:00
|
|
|
*/
|
2016-07-06 18:10:45 +00:00
|
|
|
openMessageDialog: function(titleKey, messageKey, title, message,
|
2016-07-06 18:52:59 +00:00
|
|
|
closeFunction) {
|
2016-06-08 19:34:02 +00:00
|
|
|
if (!popupEnabled)
|
2016-07-06 18:10:45 +00:00
|
|
|
return null;
|
2016-06-08 19:34:02 +00:00
|
|
|
|
2015-09-02 22:29:53 +00:00
|
|
|
if (!title) {
|
2015-07-03 09:34:05 +00:00
|
|
|
title = APP.translation.generateTranslationHTML(titleKey);
|
2015-02-20 16:17:46 +00:00
|
|
|
}
|
2015-09-02 22:29:53 +00:00
|
|
|
if (!message) {
|
|
|
|
message = APP.translation.generateTranslationHTML(messageKey);
|
|
|
|
}
|
|
|
|
|
2016-07-06 18:10:45 +00:00
|
|
|
return $.prompt(message, {
|
|
|
|
title: title,
|
|
|
|
persistent: false,
|
2016-07-06 18:52:59 +00:00
|
|
|
close: function (e, v, m, f) {
|
|
|
|
if(closeFunction)
|
|
|
|
closeFunction(e, v, m, f);
|
2016-07-06 18:10:45 +00:00
|
|
|
}
|
|
|
|
});
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2014-08-14 15:29:28 +00:00
|
|
|
/**
|
2015-07-28 21:52:32 +00:00
|
|
|
* Shows a message to the user with two buttons: first is given as a
|
|
|
|
* parameter and the second is Cancel.
|
2014-08-14 15:29:28 +00:00
|
|
|
*
|
|
|
|
* @param titleString the title of the message
|
|
|
|
* @param msgString the text of the message
|
2015-07-28 21:52:32 +00:00
|
|
|
* @param persistent boolean value which determines whether the message is
|
|
|
|
* persistent or not
|
2014-08-14 15:29:28 +00:00
|
|
|
* @param leftButton the fist button's text
|
|
|
|
* @param submitFunction function to be called on submit
|
2015-07-28 21:52:32 +00:00
|
|
|
* @param loadedFunction function to be called after the prompt is fully
|
|
|
|
* loaded
|
2014-10-24 13:33:48 +00:00
|
|
|
* @param closeFunction function to be called after the prompt is closed
|
2015-03-11 09:03:32 +00:00
|
|
|
* @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.
|
2016-07-06 18:10:45 +00:00
|
|
|
* @return the prompt that was created, or null
|
2014-08-14 15:29:28 +00:00
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
openTwoButtonDialog: function(titleKey, titleString, msgKey, msgString,
|
2015-02-20 16:17:46 +00:00
|
|
|
persistent, leftButtonKey, submitFunction, loadedFunction,
|
2015-07-28 21:52:32 +00:00
|
|
|
closeFunction, focus, defaultButton) {
|
2016-06-08 19:34:02 +00:00
|
|
|
|
2016-07-06 16:00:04 +00:00
|
|
|
if (!popupEnabled || twoButtonDialog)
|
2016-07-06 18:10:45 +00:00
|
|
|
return null;
|
2016-06-08 19:34:02 +00:00
|
|
|
|
2015-03-10 12:47:47 +00:00
|
|
|
var buttons = [];
|
|
|
|
|
2015-07-03 09:34:05 +00:00
|
|
|
var leftButton = APP.translation.generateTranslationHTML(leftButtonKey);
|
2015-03-10 12:47:47 +00:00
|
|
|
buttons.push({ title: leftButton, value: true});
|
|
|
|
|
|
|
|
var cancelButton
|
2015-07-03 09:34:05 +00:00
|
|
|
= APP.translation.generateTranslationHTML("dialog.Cancel");
|
2015-03-10 12:47:47 +00:00
|
|
|
buttons.push({title: cancelButton, value: false});
|
|
|
|
|
2015-02-20 16:17:46 +00:00
|
|
|
var message = msgString, title = titleString;
|
2015-07-28 21:52:32 +00:00
|
|
|
if (titleKey) {
|
2015-07-03 09:34:05 +00:00
|
|
|
title = APP.translation.generateTranslationHTML(titleKey);
|
2015-02-20 16:17:46 +00:00
|
|
|
}
|
2015-03-10 12:47:47 +00:00
|
|
|
if (msgKey) {
|
2015-07-03 09:34:05 +00:00
|
|
|
message = APP.translation.generateTranslationHTML(msgKey);
|
2015-02-20 16:17:46 +00:00
|
|
|
}
|
2016-07-06 16:00:04 +00:00
|
|
|
twoButtonDialog = $.prompt(message, {
|
2015-02-20 16:17:46 +00:00
|
|
|
title: title,
|
2014-08-14 15:29:28 +00:00
|
|
|
persistent: false,
|
|
|
|
buttons: buttons,
|
2015-03-11 09:03:32 +00:00
|
|
|
defaultButton: defaultButton,
|
|
|
|
focus: focus,
|
2014-08-14 15:29:28 +00:00
|
|
|
loaded: loadedFunction,
|
2016-07-06 16:00:04 +00:00
|
|
|
submit: function (e, v, m, f) {
|
|
|
|
twoButtonDialog = null;
|
|
|
|
if (submitFunction)
|
|
|
|
submitFunction(e, v, m, f);
|
|
|
|
},
|
2016-07-06 18:52:59 +00:00
|
|
|
close: function (e, v, m, f) {
|
2016-07-06 16:00:04 +00:00
|
|
|
twoButtonDialog = null;
|
|
|
|
if (closeFunction)
|
2016-07-06 18:52:59 +00:00
|
|
|
closeFunction(e, v, m, f);
|
2016-07-06 16:00:04 +00:00
|
|
|
}
|
2014-08-14 15:29:28 +00:00
|
|
|
});
|
2016-07-06 18:10:45 +00:00
|
|
|
return twoButtonDialog;
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2014-08-14 15:29:28 +00:00
|
|
|
|
2014-09-24 14:35:35 +00:00
|
|
|
/**
|
2015-11-05 17:27:26 +00:00
|
|
|
* Shows a message to the user with two buttons: first is given as a
|
|
|
|
* parameter and the second is Cancel.
|
2014-09-24 14:35:35 +00:00
|
|
|
*
|
|
|
|
* @param titleString the title of the message
|
|
|
|
* @param msgString the text of the message
|
2015-07-28 21:52:32 +00:00
|
|
|
* @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
|
2014-09-24 14:35:35 +00:00
|
|
|
* @param submitFunction function to be called on submit
|
2015-07-28 21:52:32 +00:00
|
|
|
* @param loadedFunction function to be called after the prompt is fully
|
|
|
|
* loaded
|
2016-06-21 14:39:00 +00:00
|
|
|
* @param closeFunction function to be called on dialog close
|
2014-09-24 14:35:35 +00:00
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
openDialog: function (titleString, msgString, persistent, buttons,
|
2016-06-21 14:39:00 +00:00
|
|
|
submitFunction, loadedFunction, closeFunction) {
|
2016-06-08 19:34:02 +00:00
|
|
|
if (!popupEnabled)
|
|
|
|
return;
|
|
|
|
|
2015-01-07 11:28:16 +00:00
|
|
|
var args = {
|
2014-09-24 14:35:35 +00:00
|
|
|
title: titleString,
|
2015-01-07 11:28:16 +00:00
|
|
|
persistent: persistent,
|
2014-09-24 14:35:35 +00:00
|
|
|
buttons: buttons,
|
|
|
|
defaultButton: 1,
|
|
|
|
loaded: loadedFunction,
|
2016-06-21 14:39:00 +00:00
|
|
|
submit: submitFunction,
|
|
|
|
close: closeFunction
|
2015-01-07 11:28:16 +00:00
|
|
|
};
|
2016-06-21 14:39:00 +00:00
|
|
|
|
2015-01-07 11:28:16 +00:00
|
|
|
if (persistent) {
|
|
|
|
args.closeText = '';
|
|
|
|
}
|
2016-07-06 16:00:04 +00:00
|
|
|
|
2015-03-13 14:01:42 +00:00
|
|
|
return new Impromptu(msgString, args);
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2015-01-07 11:28:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes currently opened dialog.
|
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
closeDialog: function () {
|
2015-01-07 11:28:16 +00:00
|
|
|
$.prompt.close();
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2014-09-24 14:35:35 +00:00
|
|
|
|
2014-08-14 15:29:28 +00:00
|
|
|
/**
|
|
|
|
* Shows a dialog with different states to the user.
|
|
|
|
*
|
2015-07-28 21:52:32 +00:00
|
|
|
* @param statesObject object containing all the states of the dialog.
|
2014-08-14 15:29:28 +00:00
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
openDialogWithStates: function (statesObject, options) {
|
|
|
|
if (!popupEnabled)
|
|
|
|
return;
|
|
|
|
|
2015-03-13 14:01:42 +00:00
|
|
|
return new Impromptu(statesObject, options);
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2014-08-14 15:29:28 +00:00
|
|
|
|
2014-12-16 13:54:13 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2014-12-16 18:04:22 +00:00
|
|
|
*
|
2015-07-28 21:52:32 +00:00
|
|
|
* @returns {object} popup window object if opened successfully or undefined
|
2014-12-16 18:04:22 +00:00
|
|
|
* in case we failed to open it(popup blocked)
|
2014-12-16 13:54:13 +00:00
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
openCenteredPopup: function (url, w, h, onPopupClosed) {
|
|
|
|
if (!popupEnabled)
|
|
|
|
return;
|
|
|
|
|
2014-12-16 13:54:13 +00:00
|
|
|
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 + '');
|
2014-12-16 18:04:22 +00:00
|
|
|
if (popup && onPopupClosed) {
|
2014-12-16 13:54:13 +00:00
|
|
|
var pollTimer = window.setInterval(function () {
|
|
|
|
if (popup.closed !== false) {
|
|
|
|
window.clearInterval(pollTimer);
|
|
|
|
onPopupClosed();
|
|
|
|
}
|
|
|
|
}, 200);
|
|
|
|
}
|
2014-12-16 18:04:22 +00:00
|
|
|
return popup;
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2014-12-16 13:54:13 +00:00
|
|
|
|
2014-08-14 15:29:28 +00:00
|
|
|
/**
|
|
|
|
* Shows a dialog prompting the user to send an error report.
|
|
|
|
*
|
2015-07-28 21:52:32 +00:00
|
|
|
* @param titleKey the title of the message
|
|
|
|
* @param msgKey the text of the message
|
2014-08-14 15:29:28 +00:00
|
|
|
* @param error the error that is being reported
|
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
openReportDialog: function(titleKey, msgKey, error) {
|
|
|
|
this.openMessageDialog(titleKey, msgKey);
|
2014-08-14 15:29:28 +00:00
|
|
|
console.log(error);
|
|
|
|
//FIXME send the error to the server
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2014-08-14 15:29:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows an error dialog to the user.
|
2015-07-28 21:52:32 +00:00
|
|
|
* @param titleKey the title of the message.
|
|
|
|
* @param msgKey the text of the message.
|
2014-08-14 15:29:28 +00:00
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
showError: function(titleKey, msgKey) {
|
2015-02-20 16:17:46 +00:00
|
|
|
|
2015-07-28 21:52:32 +00:00
|
|
|
if (!titleKey) {
|
2015-02-20 16:17:46 +00:00
|
|
|
titleKey = "dialog.oops";
|
|
|
|
}
|
2015-07-28 21:52:32 +00:00
|
|
|
if (!msgKey) {
|
2015-02-20 16:17:46 +00:00
|
|
|
msgKey = "dialog.defaultError";
|
2014-08-14 15:29:28 +00:00
|
|
|
}
|
2015-02-26 15:35:35 +00:00
|
|
|
messageHandler.openMessageDialog(titleKey, msgKey);
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2014-08-14 15:29:28 +00:00
|
|
|
|
2015-08-06 23:34:40 +00:00
|
|
|
/**
|
2016-06-20 21:13:17 +00:00
|
|
|
* 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.
|
2015-08-06 23:34:40 +00:00
|
|
|
* @param cls css class for the notification
|
2016-06-20 21:13:17 +00:00
|
|
|
* @param messageKey the key from the language file for the text of the
|
|
|
|
* message.
|
2015-08-06 23:34:40 +00:00
|
|
|
* @param messageArguments object with the arguments for the message.
|
|
|
|
* @param options object with language options.
|
|
|
|
*/
|
2016-06-20 21:13:17 +00:00
|
|
|
notify: function(displayName, displayNameKey, cls, messageKey,
|
|
|
|
messageArguments, options) {
|
2016-06-08 19:34:02 +00:00
|
|
|
|
2016-09-13 04:10:18 +00:00
|
|
|
// If we're in ringing state we skip all toaster notifications.
|
|
|
|
if(!notificationsEnabled || APP.UI.isRingOverlayVisible())
|
2015-08-06 23:34:40 +00:00
|
|
|
return;
|
2016-06-08 19:34:02 +00:00
|
|
|
|
2015-02-13 16:28:35 +00:00
|
|
|
var displayNameSpan = '<span class="nickname" ';
|
2015-07-28 21:52:32 +00:00
|
|
|
if (displayName) {
|
2016-02-12 12:48:57 +00:00
|
|
|
displayNameSpan += ">" + UIUtil.escapeHtml(displayName);
|
2015-07-28 21:52:32 +00:00
|
|
|
} else {
|
2015-02-13 16:28:35 +00:00
|
|
|
displayNameSpan += "data-i18n='" + displayNameKey +
|
2015-02-26 15:35:35 +00:00
|
|
|
"'>" + APP.translation.translateString(displayNameKey);
|
2015-02-13 16:28:35 +00:00
|
|
|
}
|
|
|
|
displayNameSpan += "</span>";
|
2015-08-06 03:18:45 +00:00
|
|
|
return toastr.info(
|
2015-02-13 16:28:35 +00:00
|
|
|
displayNameSpan + '<br>' +
|
|
|
|
'<span class=' + cls + ' data-i18n="' + messageKey + '"' +
|
|
|
|
(messageArguments?
|
2015-02-27 18:05:32 +00:00
|
|
|
" data-i18n-options='" + JSON.stringify(messageArguments) + "'"
|
2015-02-13 16:28:35 +00:00
|
|
|
: "") + ">" +
|
2015-02-26 15:35:35 +00:00
|
|
|
APP.translation.translateString(messageKey,
|
|
|
|
messageArguments) +
|
2015-04-22 10:27:14 +00:00
|
|
|
'</span>', null, options);
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2014-10-30 16:07:11 +00:00
|
|
|
|
2015-08-06 03:18:45 +00:00
|
|
|
/**
|
|
|
|
* Removes the toaster.
|
|
|
|
* @param toasterElement
|
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
remove: function(toasterElement) {
|
2015-08-06 03:18:45 +00:00
|
|
|
toasterElement.remove();
|
2016-06-08 19:34:02 +00:00
|
|
|
},
|
2015-08-06 23:34:40 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-08 19:34:02 +00:00
|
|
|
* Enables / disables notifications.
|
2015-08-06 23:34:40 +00:00
|
|
|
*/
|
2016-06-08 19:34:02 +00:00
|
|
|
enableNotifications: function (enable) {
|
|
|
|
notificationsEnabled = enable;
|
|
|
|
},
|
2015-08-06 23:34:40 +00:00
|
|
|
|
2016-06-08 19:34:02 +00:00
|
|
|
enablePopups: function (enable) {
|
|
|
|
popupEnabled = enable;
|
|
|
|
}
|
|
|
|
};
|
2014-08-14 15:29:28 +00:00
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
module.exports = messageHandler;
|