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}
|
|
|
|
*/
|
|
|
|
var notificationsEnabled = true;
|
|
|
|
|
2014-08-14 15:29:28 +00:00
|
|
|
var messageHandler = (function(my) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2014-08-14 15:29:28 +00:00
|
|
|
*/
|
2015-09-02 22:29:53 +00:00
|
|
|
my.openMessageDialog = function(titleKey, messageKey, title, message) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-02-20 16:17:46 +00:00
|
|
|
$.prompt(message,
|
2015-07-28 21:52:32 +00:00
|
|
|
{title: title, persistent: false}
|
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.
|
2014-08-14 15:29:28 +00:00
|
|
|
*/
|
2015-02-20 16:17:46 +00:00
|
|
|
my.openTwoButtonDialog = function(titleKey, titleString, msgKey, msgString,
|
|
|
|
persistent, leftButtonKey, submitFunction, loadedFunction,
|
2015-07-28 21:52:32 +00:00
|
|
|
closeFunction, focus, defaultButton) {
|
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
|
|
|
}
|
|
|
|
$.prompt(message, {
|
|
|
|
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,
|
2014-10-24 13:33:48 +00:00
|
|
|
submit: submitFunction,
|
|
|
|
close: closeFunction
|
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
|
2014-09-24 14:35:35 +00:00
|
|
|
*/
|
2015-02-26 15:35:35 +00:00
|
|
|
my.openDialog = function (titleString, msgString, persistent, buttons,
|
2015-01-07 11:28:16 +00:00
|
|
|
submitFunction, loadedFunction) {
|
|
|
|
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,
|
|
|
|
submit: submitFunction
|
2015-01-07 11:28:16 +00:00
|
|
|
};
|
|
|
|
if (persistent) {
|
|
|
|
args.closeText = '';
|
|
|
|
}
|
2015-03-13 14:01:42 +00:00
|
|
|
return new Impromptu(msgString, args);
|
2015-01-07 11:28:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes currently opened dialog.
|
|
|
|
*/
|
|
|
|
my.closeDialog = function () {
|
|
|
|
$.prompt.close();
|
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
|
|
|
*/
|
2015-03-13 14:01:42 +00:00
|
|
|
my.openDialogWithStates = function (statesObject, options) {
|
|
|
|
return new Impromptu(statesObject, options);
|
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
|
|
|
*/
|
|
|
|
my.openCenteredPopup = function (url, w, h, onPopupClosed) {
|
|
|
|
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;
|
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
|
|
|
|
*/
|
2015-02-26 15:35:35 +00:00
|
|
|
my.openReportDialog = function(titleKey, msgKey, error) {
|
|
|
|
my.openMessageDialog(titleKey, msgKey);
|
2014-08-14 15:29:28 +00:00
|
|
|
console.log(error);
|
|
|
|
//FIXME send the error to the server
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2015-02-26 15:35:35 +00:00
|
|
|
my.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);
|
2014-08-14 15:29:28 +00:00
|
|
|
};
|
|
|
|
|
2015-08-06 23:34:40 +00:00
|
|
|
/**
|
|
|
|
* Displayes notification.
|
|
|
|
* @param displayName display name of the participant that is associated with the notification.
|
|
|
|
* @param displayNameKey the key from the language file for the display name.
|
|
|
|
* @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.
|
|
|
|
*/
|
2015-02-26 15:35:35 +00:00
|
|
|
my.notify = function(displayName, displayNameKey,
|
2015-04-22 10:27:14 +00:00
|
|
|
cls, messageKey, messageArguments, options) {
|
2015-08-06 23:34:40 +00:00
|
|
|
if(!notificationsEnabled)
|
|
|
|
return;
|
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);
|
2014-10-30 16:07:11 +00:00
|
|
|
};
|
|
|
|
|
2015-08-06 03:18:45 +00:00
|
|
|
/**
|
|
|
|
* Removes the toaster.
|
|
|
|
* @param toasterElement
|
|
|
|
*/
|
|
|
|
my.remove = function(toasterElement) {
|
|
|
|
toasterElement.remove();
|
|
|
|
};
|
|
|
|
|
2015-08-06 23:34:40 +00:00
|
|
|
/**
|
|
|
|
* Disables notifications.
|
|
|
|
*/
|
|
|
|
my.disableNotifications = function () {
|
|
|
|
notificationsEnabled = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables notifications.
|
|
|
|
*/
|
|
|
|
my.enableNotifications = function () {
|
|
|
|
notificationsEnabled = true;
|
|
|
|
};
|
|
|
|
|
2014-08-14 15:29:28 +00:00
|
|
|
return my;
|
|
|
|
}(messageHandler || {}));
|
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
module.exports = messageHandler;
|