app.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 {AsyncEvent} from 'ts-events';
  20. import './components';
  21. import config from './config';
  22. import './controllers';
  23. import './directives';
  24. import './filters';
  25. import './partials/messenger';
  26. import './partials/welcome';
  27. import './services';
  28. import {BrowserService} from './services/browser';
  29. import './threema/container';
  30. // Configure asynchronous events
  31. AsyncEvent.setScheduler(function(callback) {
  32. // Replace the default setImmediate() call by a setTimeout(, 0) call
  33. setTimeout(callback, 0);
  34. });
  35. // Create app module and set dependencies
  36. angular.module('3ema', [
  37. // Angular
  38. 'ngAnimate',
  39. 'ngSanitize',
  40. 'ngAria',
  41. // 3rd party
  42. 'ui.router',
  43. 'angular-inview',
  44. 'monospaced.qrcode',
  45. 'luegg.directives',
  46. 'pascalprecht.translate',
  47. 'ngMaterial',
  48. // Own
  49. '3ema.filters',
  50. '3ema.components',
  51. '3ema.directives',
  52. '3ema.container',
  53. '3ema.services',
  54. '3ema.controllers',
  55. '3ema.welcome',
  56. '3ema.messenger',
  57. ])
  58. // Set versions
  59. .value('VERSION', '[[VERSION]]')
  60. .value('PROTOCOL_VERSION', 2)
  61. // Configuration object
  62. .constant('CONFIG', config)
  63. // Set cache bust parameter
  64. .constant('CACHE_BUST', 'v=[[VERSION]]')
  65. // Constants to be used by controllers
  66. .constant('BROWSER_MIN_VERSIONS', {
  67. FF: 50,
  68. CHROME: 45,
  69. OPERA: 32,
  70. SAFARI: 10,
  71. })
  72. // Set default route
  73. .config(['$urlRouterProvider', ($urlRouterProvider) => {
  74. $urlRouterProvider.otherwise('/welcome');
  75. }])
  76. // Configure i18n / l10n
  77. .config(['$translateProvider', ($translateProvider: ng.translate.ITranslateProvider) => {
  78. $translateProvider.useSanitizeValueStrategy('sanitizeParameters');
  79. $translateProvider.useMessageFormatInterpolation();
  80. $translateProvider
  81. .useStaticFilesLoader({
  82. prefix: 'i18n/',
  83. suffix: '.json',
  84. })
  85. .uniformLanguageTag('java')
  86. .registerAvailableLanguageKeys(['en', 'de'], {
  87. 'en_*': 'en',
  88. 'de_*': 'de',
  89. })
  90. .determinePreferredLanguage()
  91. .fallbackLanguage('en');
  92. }])
  93. // Configure theme
  94. .config(['$mdThemingProvider', ($mdThemingProvider) => {
  95. $mdThemingProvider.theme('default')
  96. .primaryPalette('grey', {
  97. default: '800',
  98. })
  99. .accentPalette('teal', {
  100. default: '500',
  101. });
  102. }])
  103. // Optimizations: https://docs.angularjs.org/guide/production
  104. .config(['$compileProvider', ($compileProvider) => {
  105. // Disable debug info for improved performance
  106. // TODO: Somehow breaks overflow: scroll on chat window.
  107. // Comment for now.
  108. // $compileProvider.debugInfoEnabled(false);
  109. }])
  110. // Add cache busting parameter to some HTTP requests
  111. .config(['$httpProvider', ($httpProvider: ng.IHttpProvider) => {
  112. $httpProvider.interceptors.push(['CACHE_BUST', (CACHE_BUST: string) => {
  113. return {
  114. request: (conf) => {
  115. if (conf.url.indexOf('partials/') !== -1 ||
  116. conf.url.indexOf('directives/') !== -1 ||
  117. conf.url.indexOf('components/') !== -1 ||
  118. conf.url.indexOf('i18n/') !== -1) {
  119. const separator = conf.url.indexOf('?') === -1 ? '?' : '&';
  120. conf.url = conf.url + separator + CACHE_BUST;
  121. }
  122. return conf;
  123. },
  124. };
  125. }]);
  126. }])
  127. .run([
  128. '$log', 'CONFIG', 'BrowserService',
  129. function($log: ng.ILogService, CONFIG: threema.Config, browserService: BrowserService) {
  130. // For Safari (when in DEBUG mode), monkey-patch $log to show timestamps.
  131. if (!(CONFIG.DEBUG && browserService.getBrowser().isSafari(false))) {
  132. return;
  133. }
  134. const oldLog = $log.log;
  135. const oldInfo = $log.info;
  136. const oldWarn = $log.warn;
  137. const oldDebug = $log.debug;
  138. const oldError = $log.error;
  139. function enhanceLogging(wrapped) {
  140. return function(data) {
  141. const now = new Date();
  142. const currentDate = `[${now.toISOString()}.${now.getMilliseconds()}]`;
  143. wrapped.apply(this, [currentDate, ...arguments]);
  144. };
  145. }
  146. $log.log = enhanceLogging(oldLog);
  147. $log.info = enhanceLogging(oldInfo);
  148. $log.warn = enhanceLogging(oldWarn);
  149. $log.debug = enhanceLogging(oldDebug);
  150. $log.error = enhanceLogging(oldError);
  151. },
  152. ])
  153. ;