您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LoginDialog.js 7.1KB

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