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

LoginDialog.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* global $, APP, config*/
  2. var XMPP = require('../../xmpp/xmpp');
  3. var Moderator = require('../../xmpp/moderator');
  4. //FIXME: use LoginDialog to add retries to XMPP.connect method used when
  5. // anonymous domain is not enabled
  6. /**
  7. * Creates new <tt>Dialog</tt> instance.
  8. * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt> called
  9. * when we either fail to connect or succeed(check Strophe.Status).
  10. * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to Jicofo
  11. * in order to create session-id after the connection is established.
  12. * @constructor
  13. */
  14. function Dialog(callback, obtainSession) {
  15. var self = this;
  16. var stop = false;
  17. var connection = APP.xmpp.createConnection();
  18. var message = '<h2 data-i18n="dialog.passwordRequired">';
  19. message += APP.translation.translateString("dialog.passwordRequired");
  20. message += '</h2>' +
  21. '<input name="username" type="text" ' +
  22. 'placeholder="user@domain.net" autofocus>' +
  23. '<input name="password" ' +
  24. 'type="password" data-i18n="[placeholder]dialog.userPassword"' +
  25. ' placeholder="user password">';
  26. var okButton = APP.translation.generateTranslatonHTML("dialog.Ok");
  27. var cancelButton = APP.translation.generateTranslatonHTML("dialog.Cancel");
  28. var states = {
  29. login: {
  30. html: message,
  31. buttons: [
  32. { title: okButton, value: true},
  33. { title: cancelButton, value: false}
  34. ],
  35. focus: ':input:first',
  36. submit: function (e, v, m, f) {
  37. e.preventDefault();
  38. if (v) {
  39. var jid = f.username;
  40. var password = f.password;
  41. if (jid && password) {
  42. stop = false;
  43. connection.reset();
  44. connDialog.goToState('connecting');
  45. connection.connect(jid, password, stateHandler);
  46. }
  47. } else {
  48. // User cancelled
  49. stop = true;
  50. callback();
  51. }
  52. }
  53. },
  54. connecting: {
  55. title: APP.translation.translateString('dialog.connecting'),
  56. html: '<div id="connectionStatus"></div>',
  57. buttons: [],
  58. defaultButton: 0
  59. },
  60. finished: {
  61. title: APP.translation.translateString('dialog.error'),
  62. html: '<div id="errorMessage"></div>',
  63. buttons: [
  64. {
  65. title: APP.translation.translateString('dialog.retry'),
  66. value: 'retry'
  67. },
  68. {
  69. title: APP.translation.translateString('dialog.Cancel'),
  70. value: 'cancel'
  71. },
  72. ],
  73. defaultButton: 0,
  74. submit: function (e, v, m, f) {
  75. e.preventDefault();
  76. if (v === 'retry')
  77. connDialog.goToState('login');
  78. else
  79. callback();
  80. }
  81. }
  82. };
  83. var connDialog
  84. = APP.UI.messageHandler.openDialogWithStates(states,
  85. { persistent: true, closeText: '' }, null);
  86. var stateHandler = function (status, message) {
  87. if (stop) {
  88. return;
  89. }
  90. var translateKey = "connection." + XMPP.getStatusString(status);
  91. var statusStr = APP.translation.translateString(translateKey);
  92. // Display current state
  93. var connectionStatus =
  94. connDialog.getState('connecting').find('#connectionStatus');
  95. connectionStatus.text(statusStr);
  96. switch (status) {
  97. case XMPP.Status.CONNECTED:
  98. stop = true;
  99. if (!obtainSession) {
  100. callback(connection, status);
  101. return;
  102. }
  103. // Obtaining session-id status
  104. connectionStatus.text(
  105. APP.translation.translateString(
  106. 'connection.FETCH_SESSION_ID'));
  107. // Authenticate with Jicofo and obtain session-id
  108. var roomName = APP.UI.generateRoomName();
  109. // Jicofo will return new session-id when connected
  110. // from authenticated domain
  111. connection.sendIQ(
  112. Moderator.createConferenceIq(roomName),
  113. function (result) {
  114. connectionStatus.text(
  115. APP.translation.translateString(
  116. 'connection.GOT_SESSION_ID'));
  117. stop = true;
  118. // Parse session-id
  119. Moderator.parseSessionId(result);
  120. callback(connection, status);
  121. },
  122. function (error) {
  123. console.error("Auth on the fly failed", error);
  124. stop = true;
  125. var errorMsg =
  126. APP.translation.translateString(
  127. 'connection.GET_SESSION_ID_ERROR') +
  128. $(error).find('>error').attr('code');
  129. self.displayError(errorMsg);
  130. connection.disconnect();
  131. });
  132. break;
  133. case XMPP.Status.AUTHFAIL:
  134. case XMPP.Status.CONNFAIL:
  135. case XMPP.Status.DISCONNECTED:
  136. stop = true;
  137. callback(connection, status);
  138. var errorMessage = statusStr;
  139. if (message)
  140. {
  141. errorMessage += ': ' + message;
  142. }
  143. self.displayError(errorMessage);
  144. break;
  145. default:
  146. break;
  147. }
  148. };
  149. /**
  150. * Displays error message in 'finished' state which allows either to cancel
  151. * or retry.
  152. * @param message the final message to be displayed.
  153. */
  154. this.displayError = function (message) {
  155. var finishedState = connDialog.getState('finished');
  156. var errorMessageElem = finishedState.find('#errorMessage');
  157. errorMessageElem.text(message);
  158. connDialog.goToState('finished');
  159. };
  160. /**
  161. * Closes LoginDialog.
  162. */
  163. this.close = function () {
  164. stop = true;
  165. connDialog.close();
  166. };
  167. }
  168. var LoginDialog = {
  169. /**
  170. * Displays login prompt used to establish new XMPP connection. Given
  171. * <tt>callback(Strophe.Connection, Strophe.Status)</tt> function will be
  172. * called when we connect successfully(status === CONNECTED) or when we fail
  173. * to do so. On connection failure program can call Dialog.close() method in
  174. * order to cancel or do nothing to let the user retry.
  175. * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt>
  176. * called when we either fail to connect or succeed(check
  177. * Strophe.Status).
  178. * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to
  179. * Jicofo in order to create session-id after the connection is
  180. * established.
  181. * @returns {Dialog}
  182. */
  183. show: function (callback, obtainSession) {
  184. return new Dialog(callback, obtainSession);
  185. }
  186. };
  187. module.exports = LoginDialog;