app.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Copyright © 2016-2018 Threema GmbH (https://threema.ch/).
  3. *
  4. * This file is part of Threema Web.
  5. *
  6. * Threema Web is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. import 'regenerator-runtime/runtime';
  20. import {AsyncEvent} from 'ts-events';
  21. import './components';
  22. import config from './config';
  23. import './controllers';
  24. import './directives';
  25. import './filters';
  26. import './partials/messenger';
  27. import './partials/welcome';
  28. import './services';
  29. import {BrowserService} from './services/browser';
  30. import './threema/container';
  31. // Configure asynchronous events
  32. AsyncEvent.setScheduler(function(callback) {
  33. // Replace the default setImmediate() call by a setTimeout(, 0) call
  34. setTimeout(callback, 0);
  35. });
  36. // Create app module and set dependencies
  37. angular.module('3ema', [
  38. // Angular
  39. 'ngAnimate',
  40. 'ngSanitize',
  41. 'ngAria',
  42. // 3rd party
  43. 'ui.router',
  44. 'angular-inview',
  45. 'monospaced.qrcode',
  46. 'luegg.directives',
  47. 'pascalprecht.translate',
  48. 'ngMaterial',
  49. // Own
  50. '3ema.filters',
  51. '3ema.components',
  52. '3ema.directives',
  53. '3ema.container',
  54. '3ema.services',
  55. '3ema.controllers',
  56. '3ema.welcome',
  57. '3ema.messenger',
  58. ])
  59. // Set versions
  60. .value('VERSION', '[[VERSION]]')
  61. .value('PROTOCOL_VERSION', 2)
  62. // Configuration object
  63. .constant('CONFIG', config)
  64. // Set cache bust parameter
  65. .constant('CACHE_BUST', 'v=[[VERSION]]')
  66. // Constants to be used by controllers
  67. .constant('BROWSER_MIN_VERSIONS', {
  68. FF: 50,
  69. CHROME: 45,
  70. OPERA: 32,
  71. SAFARI: 11,
  72. })
  73. // Set default route
  74. .config(['$urlRouterProvider', ($urlRouterProvider) => {
  75. $urlRouterProvider.otherwise('/welcome');
  76. }])
  77. // Configure i18n / l10n
  78. .config(['$translateProvider', ($translateProvider: ng.translate.ITranslateProvider) => {
  79. $translateProvider.useSanitizeValueStrategy('sanitizeParameters');
  80. $translateProvider.useMessageFormatInterpolation();
  81. $translateProvider
  82. .useStaticFilesLoader({
  83. prefix: 'i18n/',
  84. suffix: '.json',
  85. })
  86. .uniformLanguageTag('java')
  87. .registerAvailableLanguageKeys(['cs', 'de', 'en', 'fr', 'zh'], {
  88. 'cs_*': 'cs',
  89. 'de_*': 'de',
  90. 'en_*': 'en',
  91. 'fr_*': 'fr',
  92. 'zh_*': 'zh',
  93. })
  94. .determinePreferredLanguage()
  95. .fallbackLanguage('en');
  96. }])
  97. // Configure theme
  98. .config(['$mdThemingProvider', ($mdThemingProvider) => {
  99. $mdThemingProvider.theme('default')
  100. .primaryPalette('grey', {
  101. default: '800',
  102. })
  103. .accentPalette('teal', {
  104. default: '500',
  105. });
  106. }])
  107. // Optimizations: https://docs.angularjs.org/guide/production
  108. .config(['$compileProvider', ($compileProvider) => {
  109. // Disable debug info for improved performance
  110. // TODO: Somehow breaks overflow: scroll on chat window.
  111. // Comment for now.
  112. // $compileProvider.debugInfoEnabled(false);
  113. }])
  114. // Add cache busting parameter to some HTTP requests
  115. .config(['$httpProvider', ($httpProvider: ng.IHttpProvider) => {
  116. $httpProvider.interceptors.push(['CACHE_BUST', (CACHE_BUST: string) => {
  117. return {
  118. request: (conf) => {
  119. if (conf.url.indexOf('partials/') !== -1 ||
  120. conf.url.indexOf('directives/') !== -1 ||
  121. conf.url.indexOf('components/') !== -1 ||
  122. conf.url.indexOf('i18n/') !== -1) {
  123. const separator = conf.url.indexOf('?') === -1 ? '?' : '&';
  124. conf.url = conf.url + separator + CACHE_BUST;
  125. }
  126. return conf;
  127. },
  128. };
  129. }]);
  130. }])
  131. .run([
  132. '$log', 'CONFIG', 'BrowserService',
  133. function($log: ng.ILogService, CONFIG: threema.Config, browserService: BrowserService) {
  134. // For Safari (when in DEBUG mode), monkey-patch $log to show timestamps.
  135. if (!(CONFIG.DEBUG && browserService.getBrowser().isSafari(false))) {
  136. return;
  137. }
  138. const oldLog = $log.log;
  139. const oldInfo = $log.info;
  140. const oldWarn = $log.warn;
  141. const oldDebug = $log.debug;
  142. const oldError = $log.error;
  143. function enhanceLogging(wrapped) {
  144. return function(data) {
  145. const now = new Date();
  146. const currentDate = `[${now.toISOString()}.${now.getMilliseconds()}]`;
  147. wrapped.apply(this, [currentDate, ...arguments]);
  148. };
  149. }
  150. $log.log = enhanceLogging(oldLog);
  151. $log.info = enhanceLogging(oldInfo);
  152. $log.warn = enhanceLogging(oldWarn);
  153. $log.debug = enhanceLogging(oldDebug);
  154. $log.error = enhanceLogging(oldError);
  155. },
  156. ])
  157. ;