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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* global $, APP, config*/
  2. var messageHandler = require('../util/MessageHandler');
  3. /**
  4. * Build html for "password required" dialog.
  5. * @returns {string} html string
  6. */
  7. function getPasswordInputHtml() {
  8. let placeholder = config.hosts.authdomain
  9. ? "user identity"
  10. : "user@domain.net";
  11. let passRequiredMsg = APP.translation.translateString(
  12. "dialog.passwordRequired"
  13. );
  14. return `
  15. <h2 data-i18n="dialog.passwordRequired">${passRequiredMsg}</h2>
  16. <input name="username" type="text" placeholder=${placeholder} autofocus>
  17. <input name="password" type="password"
  18. data-i18n="[placeholder]dialog.userPassword"
  19. placeholder="user password">
  20. `;
  21. }
  22. /**
  23. * Convert provided id to jid if it's not jid yet.
  24. * @param {string} id user id or jid
  25. * @returns {string} jid
  26. */
  27. function toJid(id) {
  28. if (id.indexOf("@") >= 0) {
  29. return id;
  30. }
  31. let jid = id.concat('@');
  32. if (config.hosts.authdomain) {
  33. jid += config.hosts.authdomain;
  34. } else {
  35. jid += config.hosts.domain;
  36. }
  37. return jid;
  38. }
  39. /**
  40. * Generate cancel button config for the dialog.
  41. * @returns {Object}
  42. */
  43. function cancelButton() {
  44. return {
  45. title: APP.translation.generateTranslationHTML("dialog.Cancel"),
  46. value: false
  47. };
  48. }
  49. /**
  50. * Auth dialog for JitsiConnection which supports retries.
  51. * If no cancelCallback provided then there will be
  52. * no cancel button on the dialog.
  53. *
  54. * @class LoginDialog
  55. * @constructor
  56. *
  57. * @param {function(jid, password)} successCallback
  58. * @param {function} [cancelCallback] callback to invoke if user canceled.
  59. */
  60. function LoginDialog(successCallback, cancelCallback) {
  61. let loginButtons = [{
  62. title: APP.translation.generateTranslationHTML("dialog.Ok"),
  63. value: true
  64. }];
  65. let finishedButtons = [{
  66. title: APP.translation.translateString('dialog.retry'),
  67. value: 'retry'
  68. }];
  69. // show "cancel" button only if cancelCallback provided
  70. if (cancelCallback) {
  71. loginButtons.push(cancelButton());
  72. finishedButtons.push(cancelButton());
  73. }
  74. const states = {
  75. login: {
  76. html: getPasswordInputHtml(),
  77. buttons: loginButtons,
  78. focus: ':input:first',
  79. submit: function (e, v, m, f) {
  80. e.preventDefault();
  81. if (v) {
  82. let jid = f.username;
  83. let password = f.password;
  84. if (jid && password) {
  85. connDialog.goToState('connecting');
  86. successCallback(toJid(jid), password);
  87. }
  88. } else {
  89. // User cancelled
  90. cancelCallback();
  91. }
  92. }
  93. },
  94. connecting: {
  95. title: APP.translation.translateString('dialog.connecting'),
  96. html: '<div id="connectionStatus"></div>',
  97. buttons: [],
  98. defaultButton: 0
  99. },
  100. finished: {
  101. title: APP.translation.translateString('dialog.error'),
  102. html: '<div id="errorMessage"></div>',
  103. buttons: finishedButtons,
  104. defaultButton: 0,
  105. submit: function (e, v, m, f) {
  106. e.preventDefault();
  107. if (v === 'retry') {
  108. connDialog.goToState('login');
  109. } else {
  110. // User cancelled
  111. cancelCallback();
  112. }
  113. }
  114. }
  115. };
  116. var connDialog = messageHandler.openDialogWithStates(
  117. states, { persistent: true, closeText: '' }, null
  118. );
  119. /**
  120. * Displays error message in 'finished' state which allows either to cancel
  121. * or retry.
  122. * @param message the final message to be displayed.
  123. */
  124. this.displayError = function (message) {
  125. let finishedState = connDialog.getState('finished');
  126. let errorMessageElem = finishedState.find('#errorMessage');
  127. errorMessageElem.text(message);
  128. connDialog.goToState('finished');
  129. };
  130. /**
  131. * Show message as connection status.
  132. * @param {string} message
  133. */
  134. this.displayConnectionStatus = function (message) {
  135. let connectingState = connDialog.getState('connecting');
  136. let connectionStatus = connectingState.find('#connectionStatus');
  137. connectionStatus.text(message);
  138. };
  139. /**
  140. * Closes LoginDialog.
  141. */
  142. this.close = function () {
  143. connDialog.close();
  144. };
  145. }
  146. export default {
  147. /**
  148. * Show new auth dialog for JitsiConnection.
  149. *
  150. * @param {function(jid, password)} successCallback
  151. * @param {function} [cancelCallback] callback to invoke if user canceled.
  152. *
  153. * @returns {LoginDialog}
  154. */
  155. showAuthDialog: function (successCallback, cancelCallback) {
  156. return new LoginDialog(successCallback, cancelCallback);
  157. },
  158. /**
  159. * Show notification that external auth is required (using provided url).
  160. * @param {string} url URL to use for external auth.
  161. * @param {function} callback callback to invoke when auth popup is closed.
  162. * @returns auth dialog
  163. */
  164. showExternalAuthDialog: function (url, callback) {
  165. var dialog = messageHandler.openCenteredPopup(
  166. url, 910, 660,
  167. // On closed
  168. callback
  169. );
  170. if (!dialog) {
  171. messageHandler.openMessageDialog(null, "dialog.popupError");
  172. }
  173. return dialog;
  174. },
  175. /**
  176. * Show notification that authentication is required
  177. * to create the conference, so he should authenticate or wait for a host.
  178. * @param {string} roomName name of the conference
  179. * @param {function} onAuthNow callback to invoke if
  180. * user want to authenticate.
  181. * @returns dialog
  182. */
  183. showAuthRequiredDialog: function (roomName, onAuthNow) {
  184. var title = APP.translation.generateTranslationHTML(
  185. "dialog.WaitingForHost"
  186. );
  187. var msg = APP.translation.generateTranslationHTML(
  188. "dialog.WaitForHostMsg", {room: roomName}
  189. );
  190. var buttonTxt = APP.translation.generateTranslationHTML(
  191. "dialog.IamHost"
  192. );
  193. var buttons = [{title: buttonTxt, value: "authNow"}];
  194. return APP.UI.messageHandler.openDialog(
  195. title,
  196. msg,
  197. true,
  198. buttons,
  199. function (e, submitValue) {
  200. // Do not close the dialog yet
  201. e.preventDefault();
  202. // Open login popup
  203. if (submitValue === 'authNow') {
  204. onAuthNow();
  205. }
  206. }
  207. );
  208. }
  209. };