app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. /*jslint browser: true, continue: true, plusplus: true */
  7. /*global $: false, angular: false, console: false, validLangs: false */
  8. var syncthing = angular.module('syncthing', [
  9. 'angularUtils.directives.dirPagination',
  10. 'pascalprecht.translate',
  11. 'syncthing.core',
  12. 'syncthing.device',
  13. 'syncthing.folder',
  14. 'syncthing.settings',
  15. 'syncthing.transfer',
  16. 'syncthing.usagereport'
  17. ]);
  18. var urlbase = 'rest';
  19. syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvider) {
  20. $httpProvider.interceptors.push(function xHeadersResponseInterceptor() {
  21. var guiVersion = null;
  22. var deviceId = null;
  23. return {
  24. response: function onResponse(response) {
  25. var headers = response.headers();
  26. var responseVersion;
  27. var deviceIdShort;
  28. // angular template cache sends no headers
  29. if(Object.keys(headers).length === 0) {
  30. return response;
  31. }
  32. responseVersion = headers['x-syncthing-version'];
  33. if (!guiVersion) {
  34. guiVersion = responseVersion;
  35. } else if (guiVersion != responseVersion) {
  36. document.location.reload(true);
  37. }
  38. if (!deviceId) {
  39. deviceId = headers['x-syncthing-id'];
  40. if (deviceId) {
  41. deviceIdShort = deviceId.substring(0, 5);
  42. $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token-' + deviceIdShort;
  43. $httpProvider.defaults.xsrfCookieName = 'CSRF-Token-' + deviceIdShort;
  44. }
  45. }
  46. return response;
  47. }
  48. };
  49. });
  50. // language and localisation
  51. $translateProvider.useStaticFilesLoader({
  52. prefix: 'assets/lang/lang-',
  53. suffix: '.json'
  54. });
  55. LocaleServiceProvider.setAvailableLocales(validLangs);
  56. LocaleServiceProvider.setDefaultLocale('en');
  57. });
  58. // @TODO: extract global level functions into separate service(s)
  59. function deviceCompare(a, b) {
  60. if (typeof a.name !== 'undefined' && typeof b.name !== 'undefined') {
  61. if (a.name < b.name)
  62. return -1;
  63. return a.name > b.name;
  64. }
  65. if (a.deviceID < b.deviceID) {
  66. return -1;
  67. }
  68. return a.deviceID > b.deviceID;
  69. }
  70. function folderCompare(a, b) {
  71. if (a.id < b.id) {
  72. return -1;
  73. }
  74. return a.id > b.id;
  75. }
  76. function folderMap(l) {
  77. var m = {};
  78. l.forEach(function (r) {
  79. m[r.id] = r;
  80. });
  81. return m;
  82. }
  83. function folderList(m) {
  84. var l = [];
  85. for (var id in m) {
  86. l.push(m[id]);
  87. }
  88. l.sort(folderCompare);
  89. return l;
  90. }
  91. function decimals(val, num) {
  92. var digits, decs;
  93. if (val === 0) {
  94. return 0;
  95. }
  96. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  97. decs = Math.max(0, num - digits);
  98. return decs;
  99. }
  100. function randomString(len) {
  101. var i, result = '', chars = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-';
  102. for (i = 0; i < len; i++) {
  103. result += chars[Math.round(Math.random() * (chars.length - 1))];
  104. }
  105. return result;
  106. }
  107. function isEmptyObject(obj) {
  108. var name;
  109. for (name in obj) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. function debounce(func, wait) {
  115. var timeout, args, context, timestamp, result, again;
  116. var later = function () {
  117. var last = Date.now() - timestamp;
  118. if (last < wait) {
  119. timeout = setTimeout(later, wait - last);
  120. } else {
  121. timeout = null;
  122. if (again) {
  123. again = false;
  124. result = func.apply(context, args);
  125. context = args = null;
  126. }
  127. }
  128. };
  129. return function () {
  130. context = this;
  131. args = arguments;
  132. timestamp = Date.now();
  133. var callNow = !timeout;
  134. if (!timeout) {
  135. timeout = setTimeout(later, wait);
  136. result = func.apply(context, args);
  137. context = args = null;
  138. } else {
  139. again = true;
  140. }
  141. return result;
  142. };
  143. }