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

LoginDialog.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* global $, APP, config*/
  2. var messageHandler = require('../util/MessageHandler');
  3. //FIXME: use LoginDialog to add retries to XMPP.connect method used when
  4. // anonymous domain is not enabled
  5. function Dialog(successCallback, cancelCallback) {
  6. var message = '<h2 data-i18n="dialog.passwordRequired">';
  7. message += APP.translation.translateString("dialog.passwordRequired");
  8. message += '</h2>' +
  9. '<input name="username" type="text" ';
  10. if (config.hosts.authdomain) {
  11. message += 'placeholder="user identity" autofocus>';
  12. } else {
  13. message += 'placeholder="user@domain.net" autofocus>';
  14. }
  15. message += '<input name="password" ' +
  16. 'type="password" data-i18n="[placeholder]dialog.userPassword"' +
  17. ' placeholder="user password">';
  18. var okButton = APP.translation.generateTranslationHTML("dialog.Ok");
  19. var cancelButton = APP.translation.generateTranslationHTML("dialog.Cancel");
  20. var states = {
  21. login: {
  22. html: message,
  23. buttons: [
  24. { title: okButton, value: true},
  25. { title: cancelButton, value: false}
  26. ],
  27. focus: ':input:first',
  28. submit: function (e, v, m, f) {
  29. e.preventDefault();
  30. if (v) {
  31. var jid = f.username;
  32. var password = f.password;
  33. if (jid && password) {
  34. connDialog.goToState('connecting');
  35. successCallback(jid, password);
  36. }
  37. } else {
  38. // User cancelled
  39. cancelCallback();
  40. }
  41. }
  42. },
  43. connecting: {
  44. title: APP.translation.translateString('dialog.connecting'),
  45. html: '<div id="connectionStatus"></div>',
  46. buttons: [],
  47. defaultButton: 0
  48. },
  49. finished: {
  50. title: APP.translation.translateString('dialog.error'),
  51. html: '<div id="errorMessage"></div>',
  52. buttons: [
  53. {
  54. title: APP.translation.translateString('dialog.retry'),
  55. value: 'retry'
  56. },
  57. {
  58. title: APP.translation.translateString('dialog.Cancel'),
  59. value: 'cancel'
  60. },
  61. ],
  62. defaultButton: 0,
  63. submit: function (e, v, m, f) {
  64. e.preventDefault();
  65. if (v === 'retry') {
  66. connDialog.goToState('login');
  67. } else {
  68. cancelCallback();
  69. }
  70. }
  71. }
  72. };
  73. var connDialog = messageHandler.openDialogWithStates(
  74. states, { persistent: true, closeText: '' }, null
  75. );
  76. /**
  77. * Displays error message in 'finished' state which allows either to cancel
  78. * or retry.
  79. * @param message the final message to be displayed.
  80. */
  81. this.displayError = function (message) {
  82. var finishedState = connDialog.getState('finished');
  83. var errorMessageElem = finishedState.find('#errorMessage');
  84. errorMessageElem.text(message);
  85. connDialog.goToState('finished');
  86. };
  87. /**
  88. * Closes LoginDialog.
  89. */
  90. this.close = function () {
  91. connDialog.close();
  92. };
  93. }
  94. var LoginDialog = {
  95. /**
  96. * Displays login prompt used to establish new XMPP connection. Given
  97. * <tt>callback(Strophe.Connection, Strophe.Status)</tt> function will be
  98. * called when we connect successfully(status === CONNECTED) or when we fail
  99. * to do so. On connection failure program can call Dialog.close() method in
  100. * order to cancel or do nothing to let the user retry.
  101. * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt>
  102. * called when we either fail to connect or succeed(check
  103. * Strophe.Status).
  104. * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to
  105. * Jicofo in order to create session-id after the connection is
  106. * established.
  107. * @returns {Dialog}
  108. */
  109. show: function (successCallback, cancelCallback) {
  110. return new Dialog(successCallback, cancelCallback);
  111. },
  112. showExternalAuthDialog: function (url, callback) {
  113. var dialog = messageHandler.openCenteredPopup(
  114. url, 910, 660,
  115. // On closed
  116. callback
  117. );
  118. if (!dialog) {
  119. messageHandler.openMessageDialog(null, "dialog.popupError");
  120. }
  121. return dialog;
  122. },
  123. showAuthRequiredDialog: function (roomName, onAuthNow) {
  124. var title = APP.translation.generateTranslationHTML(
  125. "dialog.WaitingForHost"
  126. );
  127. var msg = APP.translation.generateTranslationHTML(
  128. "dialog.WaitForHostMsg", {room: roomName}
  129. );
  130. var buttonTxt = APP.translation.generateTranslationHTML(
  131. "dialog.IamHost"
  132. );
  133. var buttons = [{title: buttonTxt, value: "authNow"}];
  134. return APP.UI.messageHandler.openDialog(
  135. title,
  136. msg,
  137. true,
  138. buttons,
  139. function (onSubmitEvent, submitValue) {
  140. // Do not close the dialog yet
  141. onSubmitEvent.preventDefault();
  142. // Open login popup
  143. if (submitValue === 'authNow') {
  144. onAuthNow();
  145. }
  146. }
  147. );
  148. }
  149. };
  150. module.exports = LoginDialog;