app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 https://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', 'ngSanitize',
  11. 'syncthing.core'
  12. ]);
  13. var urlbase = 'rest';
  14. syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvider) {
  15. var deviceIDShort = metadata.deviceID.substr(0, 5);
  16. $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token-' + deviceIDShort;
  17. $httpProvider.defaults.xsrfCookieName = 'CSRF-Token-' + deviceIDShort;
  18. // language and localisation
  19. $translateProvider.useSanitizeValueStrategy('escape');
  20. $translateProvider.useStaticFilesLoader({
  21. prefix: 'assets/lang/lang-',
  22. suffix: '.json'
  23. });
  24. LocaleServiceProvider.setAvailableLocales(validLangs);
  25. LocaleServiceProvider.setDefaultLocale('en');
  26. });
  27. // @TODO: extract global level functions into separate service(s)
  28. function deviceCompare(a, b) {
  29. if (typeof a.name !== 'undefined' && typeof b.name !== 'undefined') {
  30. if (a.name < b.name)
  31. return -1;
  32. return a.name > b.name;
  33. }
  34. if (a.deviceID < b.deviceID) {
  35. return -1;
  36. }
  37. return a.deviceID > b.deviceID;
  38. }
  39. function folderCompare(a, b) {
  40. var labelA = a.id;
  41. if (typeof a.label !== 'undefined' && a.label !== null && a.label.length > 0) {
  42. labelA = a.label;
  43. }
  44. var labelB = b.id;
  45. if (typeof b.label !== 'undefined' && b.label !== null && b.label.length > 0) {
  46. labelB = b.label;
  47. }
  48. if (labelA < labelB) {
  49. return -1;
  50. }
  51. return labelA > labelB;
  52. }
  53. function folderMap(l) {
  54. var m = {};
  55. l.forEach(function (r) {
  56. m[r.id] = r;
  57. });
  58. return m;
  59. }
  60. function folderList(m) {
  61. var l = [];
  62. for (var id in m) {
  63. l.push(m[id]);
  64. }
  65. l.sort(folderCompare);
  66. return l;
  67. }
  68. function decimals(val, num) {
  69. var digits, decs;
  70. if (val === 0) {
  71. return 0;
  72. }
  73. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  74. decs = Math.max(0, num - digits);
  75. return decs;
  76. }
  77. function isEmptyObject(obj) {
  78. var name;
  79. for (name in obj) {
  80. return false;
  81. }
  82. return true;
  83. }
  84. function debounce(func, wait) {
  85. var timeout, args, context, timestamp, result, again;
  86. var later = function () {
  87. var last = Date.now() - timestamp;
  88. if (last < wait) {
  89. timeout = setTimeout(later, wait - last);
  90. } else {
  91. timeout = null;
  92. if (again) {
  93. again = false;
  94. result = func.apply(context, args);
  95. context = args = null;
  96. }
  97. }
  98. };
  99. return function () {
  100. context = this;
  101. args = arguments;
  102. timestamp = Date.now();
  103. if (!timeout) {
  104. timeout = setTimeout(later, wait);
  105. result = func.apply(context, args);
  106. context = args = null;
  107. } else {
  108. again = true;
  109. }
  110. return result;
  111. };
  112. }