123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- /* global $, APP, config*/
-
- var messageHandler = require('../util/MessageHandler');
-
- function getPasswordInputHtml() {
- let placeholder = config.hosts.authdomain
- ? "user identity"
- : "user@domain.net";
- let passRequiredMsg = APP.translation.translateString(
- "dialog.passwordRequired"
- );
- return `
- <h2 data-i18n="dialog.passwordRequired">${passRequiredMsg}</h2>
- <input name="username" type="text" placeholder=${placeholder} autofocus>
- <input name="password" type="password"
- data-i18n="[placeholder]dialog.userPassword"
- placeholder="user password">
- `;
- }
-
- function toJid(id) {
- if (id.indexOf("@") >= 0) {
- return id;
- }
-
- let jid = id.concat('@');
- if (config.hosts.authdomain) {
- jid += config.hosts.authdomain;
- } else {
- jid += config.hosts.domain;
- }
-
- return jid;
- }
-
- function cancelButton() {
- return {
- title: APP.translation.generateTranslationHTML("dialog.Cancel"),
- value: false
- };
- }
-
- function Dialog(successCallback, cancelCallback) {
- let loginButtons = [{
- title: APP.translation.generateTranslationHTML("dialog.Ok"),
- value: true
- }];
- let finishedButtons = [{
- title: APP.translation.translateString('dialog.retry'),
- value: 'retry'
- }];
-
- // show "cancel" button only if cancelCallback provided
- if (cancelCallback) {
- loginButtons.push(cancelButton());
- finishedButtons.push(cancelButton());
- }
-
- const states = {
- login: {
- html: getPasswordInputHtml(),
- buttons: loginButtons,
- focus: ':input:first',
- submit: function (e, v, m, f) {
- e.preventDefault();
- if (v) {
- let jid = f.username;
- let password = f.password;
- if (jid && password) {
- connDialog.goToState('connecting');
- successCallback(toJid(jid), password);
- }
- } else {
- // User cancelled
- cancelCallback();
- }
- }
- },
- connecting: {
- title: APP.translation.translateString('dialog.connecting'),
- html: '<div id="connectionStatus"></div>',
- buttons: [],
- defaultButton: 0
- },
- finished: {
- title: APP.translation.translateString('dialog.error'),
- html: '<div id="errorMessage"></div>',
- buttons: finishedButtons,
- defaultButton: 0,
- submit: function (e, v, m, f) {
- e.preventDefault();
- if (v === 'retry') {
- connDialog.goToState('login');
- } else {
- // User cancelled
- cancelCallback();
- }
- }
- }
- };
-
- var connDialog = messageHandler.openDialogWithStates(
- states, { persistent: true, closeText: '' }, null
- );
-
- /**
- * Displays error message in 'finished' state which allows either to cancel
- * or retry.
- * @param message the final message to be displayed.
- */
- this.displayError = function (message) {
-
- let finishedState = connDialog.getState('finished');
-
- let errorMessageElem = finishedState.find('#errorMessage');
- errorMessageElem.text(message);
-
- connDialog.goToState('finished');
- };
-
- this.displayConnectionStatus = function (message) {
- let connectingState = connDialog.getState('connecting');
-
- let connectionStatus = connectingState.find('#connectionStatus');
- connectionStatus.text(message);
- };
-
- /**
- * Closes LoginDialog.
- */
- this.close = function () {
- connDialog.close();
- };
- }
-
- const LoginDialog = {
-
- showAuthDialog: function (successCallback, cancelCallback) {
- return new Dialog(successCallback, cancelCallback);
- },
-
- showExternalAuthDialog: function (url, callback) {
- var dialog = messageHandler.openCenteredPopup(
- url, 910, 660,
- // On closed
- callback
- );
-
- if (!dialog) {
- messageHandler.openMessageDialog(null, "dialog.popupError");
- }
-
- return dialog;
- },
-
- showAuthRequiredDialog: function (roomName, onAuthNow) {
- var title = APP.translation.generateTranslationHTML(
- "dialog.WaitingForHost"
- );
- var msg = APP.translation.generateTranslationHTML(
- "dialog.WaitForHostMsg", {room: roomName}
- );
-
- var buttonTxt = APP.translation.generateTranslationHTML(
- "dialog.IamHost"
- );
- var buttons = [{title: buttonTxt, value: "authNow"}];
-
- return APP.UI.messageHandler.openDialog(
- title,
- msg,
- true,
- buttons,
- function (e, submitValue) {
-
- // Do not close the dialog yet
- e.preventDefault();
-
- // Open login popup
- if (submitValue === 'authNow') {
- onAuthNow();
- }
- }
- );
- }
- };
-
- export default LoginDialog;
|