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

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