You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoginDialog.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* global $, APP, config*/
  2. var messageHandler = require('../util/MessageHandler');
  3. function getPasswordInputHtml() {
  4. let placeholder = config.hosts.authdomain
  5. ? "user identity"
  6. : "user@domain.net";
  7. let passRequiredMsg = APP.translation.translateString(
  8. "dialog.passwordRequired"
  9. );
  10. return `
  11. <h2 data-i18n="dialog.passwordRequired">${passRequiredMsg}</h2>
  12. <input name="username" type="text" placeholder=${placeholder} autofocus>
  13. <input name="password" type="password"
  14. data-i18n="[placeholder]dialog.userPassword"
  15. placeholder="user password">
  16. `;
  17. }
  18. function toJid(id) {
  19. if (id.indexOf("@") >= 0) {
  20. return id;
  21. }
  22. let jid = id.concat('@');
  23. if (config.hosts.authdomain) {
  24. jid += config.hosts.authdomain;
  25. } else {
  26. jid += config.hosts.domain;
  27. }
  28. return jid;
  29. }
  30. function cancelButton() {
  31. return {
  32. title: APP.translation.generateTranslationHTML("dialog.Cancel"),
  33. value: false
  34. };
  35. }
  36. function Dialog(successCallback, cancelCallback) {
  37. let loginButtons = [{
  38. title: APP.translation.generateTranslationHTML("dialog.Ok"),
  39. value: true
  40. }];
  41. let finishedButtons = [{
  42. title: APP.translation.translateString('dialog.retry'),
  43. value: 'retry'
  44. }];
  45. // show "cancel" button only if cancelCallback provided
  46. if (cancelCallback) {
  47. loginButtons.push(cancelButton());
  48. finishedButtons.push(cancelButton());
  49. }
  50. const states = {
  51. login: {
  52. html: getPasswordInputHtml(),
  53. buttons: loginButtons,
  54. focus: ':input:first',
  55. submit: function (e, v, m, f) {
  56. e.preventDefault();
  57. if (v) {
  58. let jid = f.username;
  59. let password = f.password;
  60. if (jid && password) {
  61. connDialog.goToState('connecting');
  62. successCallback(toJid(jid), password);
  63. }
  64. } else {
  65. // User cancelled
  66. cancelCallback();
  67. }
  68. }
  69. },
  70. connecting: {
  71. title: APP.translation.translateString('dialog.connecting'),
  72. html: '<div id="connectionStatus"></div>',
  73. buttons: [],
  74. defaultButton: 0
  75. },
  76. finished: {
  77. title: APP.translation.translateString('dialog.error'),
  78. html: '<div id="errorMessage"></div>',
  79. buttons: finishedButtons,
  80. defaultButton: 0,
  81. submit: function (e, v, m, f) {
  82. e.preventDefault();
  83. if (v === 'retry') {
  84. connDialog.goToState('login');
  85. } else {
  86. // User cancelled
  87. cancelCallback();
  88. }
  89. }
  90. }
  91. };
  92. var connDialog = messageHandler.openDialogWithStates(
  93. states, { persistent: true, closeText: '' }, null
  94. );
  95. /**
  96. * Displays error message in 'finished' state which allows either to cancel
  97. * or retry.
  98. * @param message the final message to be displayed.
  99. */
  100. this.displayError = function (message) {
  101. let finishedState = connDialog.getState('finished');
  102. let errorMessageElem = finishedState.find('#errorMessage');
  103. errorMessageElem.text(message);
  104. connDialog.goToState('finished');
  105. };
  106. this.displayConnectionStatus = function (message) {
  107. let connectingState = connDialog.getState('connecting');
  108. let connectionStatus = connectingState.find('#connectionStatus');
  109. connectionStatus.text(message);
  110. };
  111. /**
  112. * Closes LoginDialog.
  113. */
  114. this.close = function () {
  115. connDialog.close();
  116. };
  117. }
  118. const LoginDialog = {
  119. showAuthDialog: function (successCallback, cancelCallback) {
  120. return new Dialog(successCallback, cancelCallback);
  121. },
  122. showExternalAuthDialog: function (url, callback) {
  123. var dialog = messageHandler.openCenteredPopup(
  124. url, 910, 660,
  125. // On closed
  126. callback
  127. );
  128. if (!dialog) {
  129. messageHandler.openMessageDialog(null, "dialog.popupError");
  130. }
  131. return dialog;
  132. },
  133. showAuthRequiredDialog: function (roomName, onAuthNow) {
  134. var title = APP.translation.generateTranslationHTML(
  135. "dialog.WaitingForHost"
  136. );
  137. var msg = APP.translation.generateTranslationHTML(
  138. "dialog.WaitForHostMsg", {room: roomName}
  139. );
  140. var buttonTxt = APP.translation.generateTranslationHTML(
  141. "dialog.IamHost"
  142. );
  143. var buttons = [{title: buttonTxt, value: "authNow"}];
  144. return APP.UI.messageHandler.openDialog(
  145. title,
  146. msg,
  147. true,
  148. buttons,
  149. function (e, submitValue) {
  150. // Do not close the dialog yet
  151. e.preventDefault();
  152. // Open login popup
  153. if (submitValue === 'authNow') {
  154. onAuthNow();
  155. }
  156. }
  157. );
  158. }
  159. };
  160. export default LoginDialog;