You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* global $, config, getRoomName */
  2. /* application specific logic */
  3. import "babel-polyfill";
  4. import "jquery";
  5. import "jquery-contextmenu";
  6. import "jquery-ui";
  7. import "strophe";
  8. import "strophe-disco";
  9. import "strophe-caps";
  10. import "jQuery-Impromptu";
  11. import "autosize";
  12. import 'aui';
  13. import 'aui-experimental';
  14. import 'aui-css';
  15. import 'aui-experimental-css';
  16. window.toastr = require("toastr");
  17. import URLProcessor from "./modules/config/URLProcessor";
  18. import RoomnameGenerator from './modules/util/RoomnameGenerator';
  19. import UI from "./modules/UI/UI";
  20. import settings from "./modules/settings/Settings";
  21. import conference from './conference';
  22. import ConferenceUrl from './modules/URL/ConferenceUrl';
  23. import API from './modules/API/API';
  24. import UIEvents from './service/UI/UIEvents';
  25. import getTokenData from "./modules/tokendata/TokenData";
  26. import translation from "./modules/translation/translation";
  27. /**
  28. * Tries to push history state with the following parameters:
  29. * 'VideoChat', `Room: ${roomName}`, URL. If fail, prints the error and returns
  30. * it.
  31. */
  32. function pushHistoryState(roomName, URL) {
  33. try {
  34. window.history.pushState(
  35. 'VideoChat', `Room: ${roomName}`, URL
  36. );
  37. } catch (e) {
  38. console.warn("Push history state failed with parameters:",
  39. 'VideoChat', `Room: ${roomName}`, URL, e);
  40. return e;
  41. }
  42. return null;
  43. }
  44. /**
  45. * Replaces current history state(replaces the URL displayed by the browser).
  46. * @param {string} newUrl the URL string which is to be displayed by the browser
  47. * to the user.
  48. */
  49. function replaceHistoryState (newUrl) {
  50. if (window.history
  51. && typeof window.history.replaceState === 'function') {
  52. window.history.replaceState({}, document.title, newUrl);
  53. }
  54. }
  55. /**
  56. * Builds and returns the room name.
  57. */
  58. function buildRoomName () {
  59. let roomName = getRoomName();
  60. if(!roomName) {
  61. let word = RoomnameGenerator.generateRoomWithoutSeparator();
  62. roomName = word.toLowerCase();
  63. let historyURL = window.location.href + word;
  64. //Trying to push state with current URL + roomName
  65. pushHistoryState(word, historyURL);
  66. }
  67. return roomName;
  68. }
  69. const APP = {
  70. // Used by do_external_connect.js if we receive the attach data after
  71. // connect was already executed. status property can be "initialized",
  72. // "ready" or "connecting". We are interested in "ready" status only which
  73. // means that connect was executed but we have to wait for the attach data.
  74. // In status "ready" handler property will be set to a function that will
  75. // finish the connect process when the attach data or error is received.
  76. connect: {
  77. status: "initialized",
  78. handler: null
  79. },
  80. // Used for automated performance tests
  81. connectionTimes: {
  82. "index.loaded": window.indexLoadedTime
  83. },
  84. UI,
  85. settings,
  86. conference,
  87. translation,
  88. /**
  89. * After the APP has been initialized provides utility methods for dealing
  90. * with the conference room URL(address).
  91. * @type ConferenceUrl
  92. */
  93. ConferenceUrl : null,
  94. connection: null,
  95. API,
  96. init () {
  97. this.keyboardshortcut =
  98. require("./modules/keyboardshortcut/keyboardshortcut");
  99. this.configFetch = require("./modules/config/HttpConfigFetch");
  100. this.tokenData = getTokenData();
  101. }
  102. };
  103. /**
  104. * If JWT token data it will be used for local user settings
  105. */
  106. function setTokenData() {
  107. let localUser = APP.tokenData.caller;
  108. if(localUser) {
  109. APP.settings.setEmail((localUser.getEmail() || "").trim(), true);
  110. APP.settings.setAvatarUrl((localUser.getAvatarUrl() || "").trim());
  111. APP.settings.setDisplayName((localUser.getName() || "").trim(), true);
  112. }
  113. }
  114. function init() {
  115. setTokenData();
  116. // Initialize the conference URL handler
  117. APP.ConferenceUrl = new ConferenceUrl(window.location);
  118. // Clean up the URL displayed by the browser
  119. replaceHistoryState(APP.ConferenceUrl.getInviteUrl());
  120. var isUIReady = APP.UI.start();
  121. if (isUIReady) {
  122. APP.conference.init({roomName: buildRoomName()}).then(function () {
  123. APP.UI.initConference();
  124. APP.UI.addListener(UIEvents.LANG_CHANGED, function (language) {
  125. APP.translation.setLanguage(language);
  126. APP.settings.setLanguage(language);
  127. });
  128. APP.keyboardshortcut.init();
  129. }).catch(function (err) {
  130. APP.UI.hideRingOverLay();
  131. APP.API.notifyConferenceLeft(APP.conference.roomName);
  132. console.error(err);
  133. });
  134. }
  135. }
  136. /**
  137. * If we have an HTTP endpoint for getting config.json configured we're going to
  138. * read it and override properties from config.js and interfaceConfig.js.
  139. * If there is no endpoint we'll just continue with initialization.
  140. * Keep in mind that if the endpoint has been configured and we fail to obtain
  141. * the config for any reason then the conference won't start and error message
  142. * will be displayed to the user.
  143. */
  144. function obtainConfigAndInit() {
  145. let roomName = APP.conference.roomName;
  146. if (config.configLocation) {
  147. APP.configFetch.obtainConfig(
  148. config.configLocation, roomName,
  149. // Get config result callback
  150. function(success, error) {
  151. if (success) {
  152. var now = APP.connectionTimes["configuration.fetched"] =
  153. window.performance.now();
  154. console.log("(TIME) configuration fetched:\t", now);
  155. init();
  156. } else {
  157. // Show obtain config error,
  158. // pass the error object for report
  159. APP.UI.messageHandler.openReportDialog(
  160. null, "dialog.connectError", error);
  161. }
  162. });
  163. } else {
  164. require("./modules/config/BoshAddressChoice").chooseAddress(
  165. config, roomName);
  166. init();
  167. }
  168. }
  169. $(document).ready(function () {
  170. var now = APP.connectionTimes["document.ready"] = window.performance.now();
  171. console.log("(TIME) document ready:\t", now);
  172. URLProcessor.setConfigParametersFromUrl();
  173. APP.init();
  174. APP.translation.init(settings.getLanguage());
  175. APP.API.init(APP.tokenData.externalAPISettings);
  176. obtainConfigAndInit();
  177. });
  178. $(window).bind('beforeunload', function () {
  179. APP.API.dispose();
  180. });
  181. module.exports = APP;