Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LoginDialog.js 7.9KB

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