app.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Copyright © 2016-2020 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 {hasValue} from './helpers';
  26. import './partials/messenger';
  27. import './partials/welcome';
  28. import './services';
  29. import {LogService} from './services/log';
  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', config.VERSION)
  61. .value('PROTOCOL_VERSION', 2)
  62. // Configuration object
  63. .constant('CONFIG', config)
  64. // Set cache bust parameter
  65. .constant('CACHE_BUST', `v=${config.VERSION}`)
  66. // Constants to be used by controllers
  67. .constant('BROWSER_MIN_VERSIONS', {
  68. FF: 60,
  69. CHROME: 65,
  70. OPERA: 52,
  71. SAFARI: 11,
  72. EDGE: 79,
  73. })
  74. // Set default route
  75. .config(['$urlRouterProvider', ($urlRouterProvider) => {
  76. $urlRouterProvider.otherwise('/welcome');
  77. }])
  78. // Configure i18n / l10n
  79. .config(['$translateProvider', ($translateProvider: ng.translate.ITranslateProvider) => {
  80. $translateProvider.useSanitizeValueStrategy('sanitizeParameters');
  81. $translateProvider.useMessageFormatInterpolation();
  82. $translateProvider
  83. .useStaticFilesLoader({
  84. prefix: 'i18n/',
  85. suffix: '.json',
  86. })
  87. .uniformLanguageTag('java')
  88. .registerAvailableLanguageKeys(['cs', 'de', 'en', 'es', 'fr', 'nl', 'pl', 'sk', 'uk', 'zh'], {
  89. 'cs_*': 'cs',
  90. 'de_*': 'de',
  91. 'en_*': 'en',
  92. 'es_*': 'es',
  93. 'fr_*': 'fr',
  94. 'nl_*': 'nl',
  95. 'pl_*': 'pl',
  96. 'sk_*': 'sk',
  97. 'uk_*': 'uk',
  98. 'zh_*': 'zh',
  99. })
  100. .determinePreferredLanguage()
  101. .fallbackLanguage('en');
  102. }])
  103. // Configure theme
  104. .config(['$mdThemingProvider', ($mdThemingProvider: ng.material.IThemingProvider) => {
  105. $mdThemingProvider.definePalette('threema', {
  106. 50: 'f2faf5',
  107. 100: 'e6f6eb',
  108. 200: 'cdedd9',
  109. 300: 'b4e4c5',
  110. 400: '9bdbb2',
  111. 500: '82d29f',
  112. 600: '69ca8c',
  113. 700: '50c078',
  114. 800: '37b865',
  115. 900: '1eae52',
  116. A100: '05a63f',
  117. A200: '048432',
  118. A400: '03732b',
  119. A700: '036325',
  120. contrastDefaultColor: 'light',
  121. contrastDarkColors: ['50', '100', '200', '300', '400', '500', '600'],
  122. });
  123. $mdThemingProvider.definePalette('threemawork', {
  124. 50: '#f2f9ff',
  125. 100: 'e5f4ff',
  126. 200: 'cceaff',
  127. 300: 'b2dfff',
  128. 400: '99d5ff',
  129. 500: '7fcaff',
  130. 600: '66c0ff',
  131. 700: '4cb5ff',
  132. 800: '33abff',
  133. 900: '19a0ff',
  134. A100: '0096ff',
  135. A200: '0086e5',
  136. A400: '0068b2',
  137. A700: '004a7f',
  138. contrastDefaultColor: 'light',
  139. contrastDarkColors: ['50', '100', '200', '300', '400', '500', '600'],
  140. });
  141. $mdThemingProvider.theme('threema')
  142. .primaryPalette('grey', {default: '800'})
  143. .accentPalette('threema', {default: 'A100'});
  144. $mdThemingProvider.theme('threemawork')
  145. .primaryPalette('grey', {default: '800'})
  146. .accentPalette('threemawork', {default: 'A100'});
  147. $mdThemingProvider.alwaysWatchTheme(true);
  148. }])
  149. // Optimizations: https://docs.angularjs.org/guide/production
  150. .config(['$compileProvider', ($compileProvider) => {
  151. // Disable debug info for improved performance
  152. // TODO: Somehow breaks overflow: scroll on chat window.
  153. // Comment for now.
  154. // $compileProvider.debugInfoEnabled(false);
  155. }])
  156. // Add cache busting parameter to some HTTP requests
  157. .config(['$httpProvider', ($httpProvider: ng.IHttpProvider) => {
  158. $httpProvider.interceptors.push(['CACHE_BUST', (CACHE_BUST: string) => {
  159. return {
  160. request: (conf) => {
  161. if (conf.url.indexOf('partials/') !== -1 ||
  162. conf.url.indexOf('directives/') !== -1 ||
  163. conf.url.indexOf('components/') !== -1 ||
  164. conf.url.indexOf('i18n/') !== -1) {
  165. const separator = conf.url.indexOf('?') === -1 ? '?' : '&';
  166. conf.url = conf.url + separator + CACHE_BUST;
  167. }
  168. return conf;
  169. },
  170. };
  171. }]);
  172. }])
  173. .factory('$exceptionHandler', ['LogService', function(logService: LogService) {
  174. const logger = logService.getLogger('UncaughtException');
  175. return function myExceptionHandler(e: any, cause: any) {
  176. if (!hasValue(e)) {
  177. // Fun fact: `throw null` is prefectly valid
  178. logger.error(`Unhandled exception (ng): ${e}`);
  179. return;
  180. }
  181. const data: any[] = [];
  182. data.push('Unhandled exception (ng):');
  183. if (e.message && e.name) {
  184. // Firefox does not include the exception name in the message, Chrome does.
  185. data.push(e.message.includes(e.name) ? `${e.message}\n` : `${e.name}: ${e.message}\n`);
  186. }
  187. data.push(e.stack ? e.stack : e);
  188. if (cause) {
  189. data.push('\nCaused by:\n');
  190. data.push(cause.stack ? cause.stack : cause);
  191. }
  192. logger.error(...data);
  193. };
  194. }])
  195. ;