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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* global $, APP, config */
  2. /**
  3. * Build html for "password required" dialog.
  4. * @returns {string} html string
  5. */
  6. function getPasswordInputHtml() {
  7. let placeholder = config.hosts.authdomain
  8. ? "user identity"
  9. : "user@domain.net";
  10. return `
  11. <input name="username" type="text"
  12. class="input-control"
  13. placeholder=${placeholder} autofocus>
  14. <input name="password" type="password"
  15. class="input-control"
  16. data-i18n="[placeholder]dialog.userPassword">`;
  17. }
  18. /**
  19. * Convert provided id to jid if it's not jid yet.
  20. * @param {string} id user id or jid
  21. * @returns {string} jid
  22. */
  23. function toJid(id) {
  24. if (id.indexOf("@") >= 0) {
  25. return id;
  26. }
  27. let jid = id.concat('@');
  28. if (config.hosts.authdomain) {
  29. jid += config.hosts.authdomain;
  30. } else {
  31. jid += config.hosts.domain;
  32. }
  33. return jid;
  34. }
  35. /**
  36. * Generate cancel button config for the dialog.
  37. * @returns {Object}
  38. */
  39. function cancelButton() {
  40. return {
  41. title: APP.translation.generateTranslationHTML("dialog.Cancel"),
  42. value: false
  43. };
  44. }
  45. /**
  46. * Auth dialog for JitsiConnection which supports retries.
  47. * If no cancelCallback provided then there will be
  48. * no cancel button on the dialog.
  49. *
  50. * @class LoginDialog
  51. * @constructor
  52. *
  53. * @param {function(jid, password)} successCallback
  54. * @param {function} [cancelCallback] callback to invoke if user canceled.
  55. */
  56. function LoginDialog(successCallback, cancelCallback) {
  57. let loginButtons = [{
  58. title: APP.translation.generateTranslationHTML("dialog.Ok"),
  59. value: true
  60. }];
  61. let finishedButtons = [{
  62. title: APP.translation.generateTranslationHTML('dialog.retry'),
  63. value: 'retry'
  64. }];
  65. // show "cancel" button only if cancelCallback provided
  66. if (cancelCallback) {
  67. loginButtons.push(cancelButton());
  68. finishedButtons.push(cancelButton());
  69. }
  70. const states = {
  71. login: {
  72. titleKey: 'dialog.passwordRequired',
  73. html: getPasswordInputHtml(),
  74. buttons: loginButtons,
  75. focus: ':input:first',
  76. submit: function (e, v, m, f) {
  77. e.preventDefault();
  78. if (v) {
  79. let jid = f.username;
  80. let password = f.password;
  81. if (jid && password) {
  82. connDialog.goToState('connecting');
  83. successCallback(toJid(jid), password);
  84. }
  85. } else {
  86. // User cancelled
  87. cancelCallback();
  88. }
  89. }
  90. },
  91. connecting: {
  92. titleKey: 'dialog.connecting',
  93. html: '<div id="connectionStatus"></div>',
  94. buttons: [],
  95. defaultButton: 0
  96. },
  97. finished: {
  98. titleKey: 'dialog.error',
  99. html: '<div id="errorMessage"></div>',
  100. buttons: finishedButtons,
  101. defaultButton: 0,
  102. submit: function (e, v) {
  103. e.preventDefault();
  104. if (v === 'retry') {
  105. connDialog.goToState('login');
  106. } else {
  107. // User cancelled
  108. cancelCallback();
  109. }
  110. }
  111. }
  112. };
  113. var connDialog = APP.UI.messageHandler.openDialogWithStates(
  114. states, { persistent: true, closeText: '' }, null
  115. );
  116. /**
  117. * Displays error message in 'finished' state which allows either to cancel
  118. * or retry.
  119. * @param messageKey the key to the message to be displayed.
  120. * @param options the options to the error message (optional)
  121. */
  122. this.displayError = function (messageKey, options) {
  123. let finishedState = connDialog.getState('finished');
  124. let errorMessageElem = finishedState.find('#errorMessage');
  125. errorMessageElem.attr("data-i18n", messageKey);
  126. APP.translation.translateElement($(errorMessageElem), options);
  127. connDialog.goToState('finished');
  128. };
  129. /**
  130. * Show message as connection status.
  131. * @param {string} messageKey the key to the message
  132. */
  133. this.displayConnectionStatus = function (messageKey) {
  134. let connectingState = connDialog.getState('connecting');
  135. let connectionStatus = connectingState.find('#connectionStatus');
  136. connectionStatus.attr("data-i18n", messageKey);
  137. APP.translation.translateElement($(connectionStatus));
  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 = APP.UI.messageHandler.openCenteredPopup(
  166. url, 910, 660,
  167. // On closed
  168. callback
  169. );
  170. if (!dialog) {
  171. APP.UI.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 msg = APP.translation.generateTranslationHTML(
  185. "dialog.WaitForHostMsg", {room: roomName}
  186. );
  187. var buttonTxt = APP.translation.generateTranslationHTML(
  188. "dialog.IamHost"
  189. );
  190. var buttons = [{title: buttonTxt, value: "authNow"}];
  191. return APP.UI.messageHandler.openDialog(
  192. "dialog.WaitingForHost",
  193. msg,
  194. true,
  195. buttons,
  196. function (e, submitValue) {
  197. // Do not close the dialog yet
  198. e.preventDefault();
  199. // Open login popup
  200. if (submitValue === 'authNow') {
  201. onAuthNow();
  202. }
  203. }
  204. );
  205. }
  206. };