app.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. /*jslint browser: true, continue: true, plusplus: true */
  5. /*global $: false, angular: false */
  6. 'use strict';
  7. var syncthing = angular.module('syncthing', ['pascalprecht.translate']);
  8. var urlbase = 'rest';
  9. syncthing.config(function ($httpProvider, $translateProvider) {
  10. $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token';
  11. $httpProvider.defaults.xsrfCookieName = 'CSRF-Token';
  12. $translateProvider.useStaticFilesLoader({
  13. prefix: 'lang-',
  14. suffix: '.json'
  15. });
  16. $translateProvider.preferredLanguage('en');
  17. });
  18. syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $location) {
  19. var prevDate = 0;
  20. var getOK = true;
  21. var restarting = false;
  22. $scope.connections = {};
  23. $scope.config = {};
  24. $scope.myID = '';
  25. $scope.nodes = [];
  26. $scope.configInSync = true;
  27. $scope.protocolChanged = false;
  28. $scope.errors = [];
  29. $scope.seenError = '';
  30. $scope.model = {};
  31. $scope.repos = {};
  32. $scope.reportData = {};
  33. $scope.reportPreview = false;
  34. $scope.upgradeInfo = {};
  35. $scope.$on("$locationChangeSuccess", function () {
  36. var lang = $location.search().lang;
  37. if (lang) {
  38. $translate.use(lang);
  39. }
  40. });
  41. $scope.needActions = {
  42. 'rm': 'Del',
  43. 'rmdir': 'Del (dir)',
  44. 'sync': 'Sync',
  45. 'touch': 'Update',
  46. }
  47. $scope.needIcons = {
  48. 'rm': 'remove',
  49. 'rmdir': 'remove',
  50. 'sync': 'download',
  51. 'touch': 'asterisk',
  52. }
  53. function getSucceeded() {
  54. if (!getOK) {
  55. $scope.init();
  56. $('#networkError').modal('hide');
  57. getOK = true;
  58. }
  59. if (restarting) {
  60. $scope.init();
  61. $('#restarting').modal('hide');
  62. $('#shutdown').modal('hide');
  63. restarting = false;
  64. }
  65. }
  66. function getFailed() {
  67. if (restarting) {
  68. return;
  69. }
  70. if (getOK) {
  71. $('#networkError').modal({backdrop: 'static', keyboard: false});
  72. getOK = false;
  73. }
  74. }
  75. $scope.refresh = function () {
  76. $http.get(urlbase + '/system').success(function (data) {
  77. getSucceeded();
  78. $scope.system = data;
  79. }).error(function () {
  80. getFailed();
  81. });
  82. Object.keys($scope.repos).forEach(function (id) {
  83. if (typeof $scope.model[id] === 'undefined') {
  84. // Never fetched before
  85. $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
  86. $scope.model[id] = data;
  87. });
  88. } else {
  89. $http.get(urlbase + '/model/version?repo=' + encodeURIComponent(id)).success(function (data) {
  90. if (data.version > $scope.model[id].version) {
  91. $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
  92. $scope.model[id] = data;
  93. });
  94. }
  95. });
  96. }
  97. });
  98. $http.get(urlbase + '/connections').success(function (data) {
  99. var now = Date.now(),
  100. td = (now - prevDate) / 1000,
  101. id;
  102. prevDate = now;
  103. for (id in data) {
  104. if (!data.hasOwnProperty(id)) {
  105. continue;
  106. }
  107. try {
  108. data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
  109. data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
  110. } catch (e) {
  111. data[id].inbps = 0;
  112. data[id].outbps = 0;
  113. }
  114. }
  115. $scope.connections = data;
  116. });
  117. $http.get(urlbase + '/errors').success(function (data) {
  118. $scope.errors = data;
  119. });
  120. };
  121. $scope.repoStatus = function (repo) {
  122. if (typeof $scope.model[repo] === 'undefined') {
  123. return 'Unknown';
  124. }
  125. if ($scope.model[repo].invalid !== '') {
  126. return 'Stopped';
  127. }
  128. var state = '' + $scope.model[repo].state;
  129. state = state[0].toUpperCase() + state.substr(1);
  130. if (state == "Syncing" || state == "Idle") {
  131. state += " (" + $scope.syncPercentage(repo) + "%)";
  132. }
  133. return state;
  134. };
  135. $scope.repoClass = function (repo) {
  136. if (typeof $scope.model[repo] === 'undefined') {
  137. return 'info';
  138. }
  139. if ($scope.model[repo].invalid !== '') {
  140. return 'danger';
  141. }
  142. var state = '' + $scope.model[repo].state;
  143. if (state == 'idle') {
  144. return 'success';
  145. }
  146. if (state == 'syncing') {
  147. return 'primary';
  148. }
  149. return 'info';
  150. };
  151. $scope.syncPercentage = function (repo) {
  152. if (typeof $scope.model[repo] === 'undefined') {
  153. return 100;
  154. }
  155. if ($scope.model[repo].globalBytes === 0) {
  156. return 100;
  157. }
  158. var pct = 100 * $scope.model[repo].inSyncBytes / $scope.model[repo].globalBytes;
  159. return Math.floor(pct);
  160. };
  161. $scope.nodeStatus = function (nodeCfg) {
  162. var conn = $scope.connections[nodeCfg.NodeID];
  163. if (conn) {
  164. if (conn.Completion === 100) {
  165. return 'Up to Date';
  166. } else {
  167. return 'Syncing (' + conn.Completion + '%)';
  168. }
  169. }
  170. return 'Disconnected';
  171. };
  172. $scope.nodeIcon = function (nodeCfg) {
  173. var conn = $scope.connections[nodeCfg.NodeID];
  174. if (conn) {
  175. if (conn.Completion === 100) {
  176. return 'ok';
  177. } else {
  178. return 'refresh';
  179. }
  180. }
  181. return 'minus';
  182. };
  183. $scope.nodeClass = function (nodeCfg) {
  184. var conn = $scope.connections[nodeCfg.NodeID];
  185. if (conn) {
  186. if (conn.Completion === 100) {
  187. return 'success';
  188. } else {
  189. return 'primary';
  190. }
  191. }
  192. return 'info';
  193. };
  194. $scope.nodeAddr = function (nodeCfg) {
  195. var conn = $scope.connections[nodeCfg.NodeID];
  196. if (conn) {
  197. return conn.Address;
  198. }
  199. return '?';
  200. };
  201. $scope.nodeCompletion = function (nodeCfg) {
  202. var conn = $scope.connections[nodeCfg.NodeID];
  203. if (conn) {
  204. return conn.Completion + '%';
  205. }
  206. return '';
  207. };
  208. $scope.nodeVer = function (nodeCfg) {
  209. if (nodeCfg.NodeID === $scope.myID) {
  210. return $scope.version;
  211. }
  212. var conn = $scope.connections[nodeCfg.NodeID];
  213. if (conn) {
  214. return conn.ClientVersion;
  215. }
  216. return '?';
  217. };
  218. $scope.findNode = function (nodeID) {
  219. var matches = $scope.nodes.filter(function (n) { return n.NodeID == nodeID; });
  220. if (matches.length != 1) {
  221. return undefined;
  222. }
  223. return matches[0];
  224. };
  225. $scope.nodeName = function (nodeCfg) {
  226. if (typeof nodeCfg === 'undefined') {
  227. return "";
  228. }
  229. if (nodeCfg.Name) {
  230. return nodeCfg.Name;
  231. }
  232. return nodeCfg.NodeID.substr(0, 6);
  233. };
  234. $scope.thisNodeName = function () {
  235. var node = $scope.thisNode();
  236. if (typeof node === 'undefined') {
  237. return "(unknown node)";
  238. }
  239. if (node.Name) {
  240. return node.Name;
  241. }
  242. return node.NodeID.substr(0, 6);
  243. };
  244. $scope.editSettings = function () {
  245. // Make a working copy
  246. $scope.tmpOptions = angular.copy($scope.config.Options);
  247. $scope.tmpOptions.UREnabled = ($scope.tmpOptions.URAccepted > 0);
  248. $scope.tmpGUI = angular.copy($scope.config.GUI);
  249. $('#settings').modal({backdrop: 'static', keyboard: true});
  250. };
  251. $scope.saveConfig = function() {
  252. var cfg = JSON.stringify($scope.config);
  253. var opts = {headers: {'Content-Type': 'application/json'}};
  254. $http.post(urlbase + '/config', cfg, opts).success(function () {
  255. $http.get(urlbase + '/config/sync').success(function (data) {
  256. $scope.configInSync = data.configInSync;
  257. });
  258. });
  259. };
  260. $scope.saveSettings = function () {
  261. // Make sure something changed
  262. var changed = !angular.equals($scope.config.Options, $scope.tmpOptions) ||
  263. !angular.equals($scope.config.GUI, $scope.tmpGUI);
  264. if (changed) {
  265. // Check if usage reporting has been enabled or disabled
  266. if ($scope.tmpOptions.UREnabled && $scope.tmpOptions.URAccepted <= 0) {
  267. $scope.tmpOptions.URAccepted = 1000;
  268. } else if (!$scope.tmpOptions.UREnabled && $scope.tmpOptions.URAccepted > 0){
  269. $scope.tmpOptions.URAccepted = -1;
  270. }
  271. // Check if protocol will need to be changed on restart
  272. if($scope.config.GUI.UseTLS !== $scope.tmpGUI.UseTLS){
  273. $scope.protocolChanged = true;
  274. }
  275. // Apply new settings locally
  276. $scope.config.Options = angular.copy($scope.tmpOptions);
  277. $scope.config.GUI = angular.copy($scope.tmpGUI);
  278. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  279. $scope.saveConfig();
  280. }
  281. $('#settings').modal("hide");
  282. };
  283. $scope.restart = function () {
  284. restarting = true;
  285. $scope.restartingTitle = "Restarting"
  286. $scope.restartingBody = "Syncthing is restarting."
  287. $('#restarting').modal({backdrop: 'static', keyboard: false});
  288. $http.post(urlbase + '/restart');
  289. $scope.configInSync = true;
  290. // Switch webpage protocol if needed
  291. if($scope.protocolChanged){
  292. var protocol = 'http';
  293. if($scope.config.GUI.UseTLS){
  294. protocol = 'https';
  295. }
  296. setTimeout(function(){
  297. window.location.protocol = protocol;
  298. }, 1000);
  299. $scope.protocolChanged = false;
  300. }
  301. };
  302. $scope.upgrade = function () {
  303. $scope.restartingTitle = "Upgrading"
  304. $scope.restartingBody = "Syncthing is upgrading."
  305. $('#restarting').modal({backdrop: 'static', keyboard: false});
  306. $http.post(urlbase + '/upgrade').success(function () {
  307. restarting = true;
  308. $scope.restartingBody = "Syncthing is restarting into the new version."
  309. }).error(function () {
  310. $('#restarting').modal('hide');
  311. });
  312. };
  313. $scope.shutdown = function () {
  314. restarting = true;
  315. $http.post(urlbase + '/shutdown').success(function () {
  316. $('#shutdown').modal({backdrop: 'static', keyboard: false});
  317. });
  318. $scope.configInSync = true;
  319. };
  320. $scope.editNode = function (nodeCfg) {
  321. $scope.currentNode = $.extend({}, nodeCfg);
  322. $scope.editingExisting = true;
  323. $scope.editingSelf = (nodeCfg.NodeID == $scope.myID);
  324. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  325. $scope.nodeEditor.$setPristine();
  326. $('#editNode').modal({backdrop: 'static', keyboard: true});
  327. };
  328. $scope.idNode = function () {
  329. $('#idqr').modal('show');
  330. };
  331. $scope.addNode = function () {
  332. $scope.currentNode = {AddressesStr: 'dynamic'};
  333. $scope.editingExisting = false;
  334. $scope.editingSelf = false;
  335. $scope.nodeEditor.$setPristine();
  336. $('#editNode').modal({backdrop: 'static', keyboard: true});
  337. };
  338. $scope.deleteNode = function () {
  339. $('#editNode').modal('hide');
  340. if (!$scope.editingExisting) {
  341. return;
  342. }
  343. $scope.nodes = $scope.nodes.filter(function (n) {
  344. return n.NodeID !== $scope.currentNode.NodeID;
  345. });
  346. $scope.config.Nodes = $scope.nodes;
  347. for (var id in $scope.repos) {
  348. $scope.repos[id].Nodes = $scope.repos[id].Nodes.filter(function (n) {
  349. return n.NodeID !== $scope.currentNode.NodeID;
  350. });
  351. }
  352. $scope.saveConfig();
  353. };
  354. $scope.saveNode = function () {
  355. var nodeCfg, done, i;
  356. $('#editNode').modal('hide');
  357. nodeCfg = $scope.currentNode;
  358. nodeCfg.NodeID = nodeCfg.NodeID.replace(/ /g, '').replace(/-/g, '').toLowerCase().trim();
  359. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  360. done = false;
  361. for (i = 0; i < $scope.nodes.length; i++) {
  362. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  363. $scope.nodes[i] = nodeCfg;
  364. done = true;
  365. break;
  366. }
  367. }
  368. if (!done) {
  369. $scope.nodes.push(nodeCfg);
  370. }
  371. $scope.nodes.sort(nodeCompare);
  372. $scope.config.Nodes = $scope.nodes;
  373. $scope.saveConfig();
  374. };
  375. $scope.otherNodes = function () {
  376. return $scope.nodes.filter(function (n){
  377. return n.NodeID !== $scope.myID;
  378. });
  379. };
  380. $scope.thisNode = function () {
  381. var i, n;
  382. for (i = 0; i < $scope.nodes.length; i++) {
  383. n = $scope.nodes[i];
  384. if (n.NodeID === $scope.myID) {
  385. return n;
  386. }
  387. }
  388. };
  389. $scope.allNodes = function () {
  390. var nodes = $scope.otherNodes();
  391. nodes.push($scope.thisNode());
  392. return nodes;
  393. };
  394. $scope.errorList = function () {
  395. return $scope.errors.filter(function (e) {
  396. return e.Time > $scope.seenError;
  397. });
  398. };
  399. $scope.clearErrors = function () {
  400. $scope.seenError = $scope.errors[$scope.errors.length - 1].Time;
  401. $http.post(urlbase + '/error/clear');
  402. };
  403. $scope.friendlyNodes = function (str) {
  404. for (var i = 0; i < $scope.nodes.length; i++) {
  405. var cfg = $scope.nodes[i];
  406. str = str.replace(cfg.NodeID, $scope.nodeName(cfg));
  407. }
  408. return str;
  409. };
  410. $scope.repoList = function () {
  411. return repoList($scope.repos);
  412. };
  413. $scope.editRepo = function (nodeCfg) {
  414. $scope.currentRepo = angular.copy(nodeCfg);
  415. $scope.currentRepo.selectedNodes = {};
  416. $scope.currentRepo.Nodes.forEach(function (n) {
  417. $scope.currentRepo.selectedNodes[n.NodeID] = true;
  418. });
  419. if ($scope.currentRepo.Versioning && $scope.currentRepo.Versioning.Type === "simple") {
  420. $scope.currentRepo.simpleFileVersioning = true;
  421. $scope.currentRepo.simpleKeep = +$scope.currentRepo.Versioning.Params.keep;
  422. }
  423. $scope.currentRepo.simpleKeep = $scope.currentRepo.simpleKeep || 5;
  424. $scope.editingExisting = true;
  425. $scope.repoEditor.$setPristine();
  426. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  427. };
  428. $scope.addRepo = function () {
  429. $scope.currentRepo = {selectedNodes: {}};
  430. $scope.editingExisting = false;
  431. $scope.repoEditor.$setPristine();
  432. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  433. };
  434. $scope.saveRepo = function () {
  435. var repoCfg, done, i;
  436. $('#editRepo').modal('hide');
  437. repoCfg = $scope.currentRepo;
  438. repoCfg.Nodes = [];
  439. repoCfg.selectedNodes[$scope.myID] = true;
  440. for (var nodeID in repoCfg.selectedNodes) {
  441. if (repoCfg.selectedNodes[nodeID] === true) {
  442. repoCfg.Nodes.push({NodeID: nodeID});
  443. }
  444. }
  445. delete repoCfg.selectedNodes;
  446. if (repoCfg.simpleFileVersioning) {
  447. repoCfg.Versioning = {
  448. 'Type': 'simple',
  449. 'Params': {
  450. 'keep': '' + repoCfg.simpleKeep,
  451. }
  452. };
  453. delete repoCfg.simpleFileVersioning;
  454. delete repoCfg.simpleKeep;
  455. } else {
  456. delete repoCfg.Versioning;
  457. }
  458. $scope.repos[repoCfg.ID] = repoCfg;
  459. $scope.config.Repositories = repoList($scope.repos);
  460. $scope.saveConfig();
  461. };
  462. $scope.sharesRepo = function(repoCfg) {
  463. var names = [];
  464. repoCfg.Nodes.forEach(function (node) {
  465. names.push($scope.nodeName($scope.findNode(node.NodeID)));
  466. });
  467. names.sort();
  468. return names.join(", ");
  469. };
  470. $scope.deleteRepo = function () {
  471. $('#editRepo').modal('hide');
  472. if (!$scope.editingExisting) {
  473. return;
  474. }
  475. delete $scope.repos[$scope.currentRepo.ID];
  476. $scope.config.Repositories = repoList($scope.repos);
  477. $scope.saveConfig();
  478. };
  479. $scope.setAPIKey = function (cfg) {
  480. cfg.APIKey = randomString(30, 32);
  481. };
  482. $scope.init = function() {
  483. $http.get(urlbase + '/version').success(function (data) {
  484. $scope.version = data;
  485. });
  486. $http.get(urlbase + '/system').success(function (data) {
  487. $scope.system = data;
  488. $scope.myID = data.myID;
  489. });
  490. $http.get(urlbase + '/config').success(function (data) {
  491. $scope.config = data;
  492. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  493. $scope.nodes = $scope.config.Nodes;
  494. $scope.nodes.sort(nodeCompare);
  495. $scope.repos = repoMap($scope.config.Repositories);
  496. $scope.refresh();
  497. if ($scope.config.Options.URAccepted == 0) {
  498. // If usage reporting has been neither accepted nor declined,
  499. // we want to ask the user to make a choice. But we don't want
  500. // to bug them during initial setup, so we set a cookie with
  501. // the time of the first visit. When that cookie is present
  502. // and the time is more than four hours ago, we ask the
  503. // question.
  504. var firstVisit = document.cookie.replace(/(?:(?:^|.*;\s*)firstVisit\s*\=\s*([^;]*).*$)|^.*$/, "$1");
  505. if (!firstVisit) {
  506. document.cookie = "firstVisit=" + Date.now() + ";max-age=" + 30*24*3600;
  507. } else {
  508. if (+firstVisit < Date.now() - 4*3600*1000){
  509. $('#ur').modal({backdrop: 'static', keyboard: false});
  510. }
  511. }
  512. }
  513. });
  514. $http.get(urlbase + '/config/sync').success(function (data) {
  515. $scope.configInSync = data.configInSync;
  516. });
  517. $http.get(urlbase + '/report').success(function (data) {
  518. $scope.reportData = data;
  519. });
  520. $http.get(urlbase + '/upgrade').success(function (data) {
  521. $scope.upgradeInfo = data;
  522. }).error(function () {
  523. $scope.upgradeInfo = {};
  524. });
  525. };
  526. $scope.acceptUR = function () {
  527. $scope.config.Options.URAccepted = 1000; // Larger than the largest existing report version
  528. $scope.saveConfig();
  529. $('#ur').modal('hide');
  530. };
  531. $scope.declineUR = function () {
  532. $scope.config.Options.URAccepted = -1;
  533. $scope.saveConfig();
  534. $('#ur').modal('hide');
  535. };
  536. $scope.showNeed = function (repo) {
  537. $scope.neededLoaded = false;
  538. $('#needed').modal({backdrop: 'static', keyboard: true});
  539. $http.get(urlbase + "/need?repo=" + encodeURIComponent(repo)).success(function (data) {
  540. $scope.needed = data;
  541. $scope.neededLoaded = true;
  542. });
  543. };
  544. $scope.needAction = function (file) {
  545. var fDelete = 4096;
  546. var fDirectory = 16384;
  547. if ((file.Flags & (fDelete+fDirectory)) === fDelete+fDirectory) {
  548. return 'rmdir';
  549. } else if ((file.Flags & fDelete) === fDelete) {
  550. return 'rm';
  551. } else if ((file.Flags & fDirectory) === fDirectory) {
  552. return 'touch';
  553. } else {
  554. return 'sync';
  555. }
  556. };
  557. $scope.override = function (repo) {
  558. $http.post(urlbase + "/model/override?repo=" + encodeURIComponent(repo)).success(function () {
  559. $scope.refresh();
  560. });
  561. };
  562. $scope.about = function () {
  563. $('#about').modal('show');
  564. };
  565. $scope.init();
  566. setInterval($scope.refresh, 10000);
  567. });
  568. function nodeCompare(a, b) {
  569. if (typeof a.Name !== 'undefined' && typeof b.Name !== 'undefined') {
  570. if (a.Name < b.Name)
  571. return -1;
  572. return a.Name > b.Name;
  573. }
  574. if (a.NodeID < b.NodeID) {
  575. return -1;
  576. }
  577. return a.NodeID > b.NodeID;
  578. }
  579. function repoCompare(a, b) {
  580. if (a.Directory < b.Directory) {
  581. return -1;
  582. }
  583. return a.Directory > b.Directory;
  584. }
  585. function repoMap(l) {
  586. var m = {};
  587. l.forEach(function (r) {
  588. m[r.ID] = r;
  589. });
  590. return m;
  591. }
  592. function repoList(m) {
  593. var l = [];
  594. for (var id in m) {
  595. l.push(m[id]);
  596. }
  597. l.sort(repoCompare);
  598. return l;
  599. }
  600. function decimals(val, num) {
  601. var digits, decs;
  602. if (val === 0) {
  603. return 0;
  604. }
  605. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  606. decs = Math.max(0, num - digits);
  607. return decs;
  608. }
  609. function randomString(len, bits)
  610. {
  611. bits = bits || 36;
  612. var outStr = "", newStr;
  613. while (outStr.length < len)
  614. {
  615. newStr = Math.random().toString(bits).slice(2);
  616. outStr += newStr.slice(0, Math.min(newStr.length, (len - outStr.length)));
  617. }
  618. return outStr.toLowerCase();
  619. }
  620. syncthing.filter('natural', function () {
  621. return function (input, valid) {
  622. return input.toFixed(decimals(input, valid));
  623. };
  624. });
  625. syncthing.filter('binary', function () {
  626. return function (input) {
  627. if (input === undefined) {
  628. return '0 ';
  629. }
  630. if (input > 1024 * 1024 * 1024) {
  631. input /= 1024 * 1024 * 1024;
  632. return input.toFixed(decimals(input, 2)) + ' Gi';
  633. }
  634. if (input > 1024 * 1024) {
  635. input /= 1024 * 1024;
  636. return input.toFixed(decimals(input, 2)) + ' Mi';
  637. }
  638. if (input > 1024) {
  639. input /= 1024;
  640. return input.toFixed(decimals(input, 2)) + ' Ki';
  641. }
  642. return Math.round(input) + ' ';
  643. };
  644. });
  645. syncthing.filter('metric', function () {
  646. return function (input) {
  647. if (input === undefined) {
  648. return '0 ';
  649. }
  650. if (input > 1000 * 1000 * 1000) {
  651. input /= 1000 * 1000 * 1000;
  652. return input.toFixed(decimals(input, 2)) + ' G';
  653. }
  654. if (input > 1000 * 1000) {
  655. input /= 1000 * 1000;
  656. return input.toFixed(decimals(input, 2)) + ' M';
  657. }
  658. if (input > 1000) {
  659. input /= 1000;
  660. return input.toFixed(decimals(input, 2)) + ' k';
  661. }
  662. return Math.round(input) + ' ';
  663. };
  664. });
  665. syncthing.filter('short', function () {
  666. return function (input) {
  667. return input.substr(0, 6);
  668. };
  669. });
  670. syncthing.filter('alwaysNumber', function () {
  671. return function (input) {
  672. if (input === undefined) {
  673. return 0;
  674. }
  675. return input;
  676. };
  677. });
  678. syncthing.filter('shortPath', function () {
  679. return function (input) {
  680. if (input === undefined)
  681. return "";
  682. var parts = input.split(/[\/\\]/);
  683. if (!parts || parts.length <= 3) {
  684. return input;
  685. }
  686. return ".../" + parts.slice(parts.length-2).join("/");
  687. };
  688. });
  689. syncthing.filter('basename', function () {
  690. return function (input) {
  691. if (input === undefined)
  692. return "";
  693. var parts = input.split(/[\/\\]/);
  694. if (!parts || parts.length < 1) {
  695. return input;
  696. }
  697. return parts[parts.length-1];
  698. };
  699. });
  700. syncthing.filter('clean', function () {
  701. return function (input) {
  702. return encodeURIComponent(input).replace(/%/g, '');
  703. };
  704. });
  705. syncthing.directive('optionEditor', function () {
  706. return {
  707. restrict: 'C',
  708. replace: true,
  709. transclude: true,
  710. scope: {
  711. setting: '=setting',
  712. },
  713. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  714. };
  715. });
  716. syncthing.directive('uniqueRepo', function() {
  717. return {
  718. require: 'ngModel',
  719. link: function(scope, elm, attrs, ctrl) {
  720. ctrl.$parsers.unshift(function(viewValue) {
  721. if (scope.editingExisting) {
  722. // we shouldn't validate
  723. ctrl.$setValidity('uniqueRepo', true);
  724. } else if (scope.repos[viewValue]) {
  725. // the repo exists already
  726. ctrl.$setValidity('uniqueRepo', false);
  727. } else {
  728. // the repo is unique
  729. ctrl.$setValidity('uniqueRepo', true);
  730. }
  731. return viewValue;
  732. });
  733. }
  734. };
  735. });
  736. syncthing.directive('validNodeid', function($http) {
  737. return {
  738. require: 'ngModel',
  739. link: function(scope, elm, attrs, ctrl) {
  740. ctrl.$parsers.unshift(function(viewValue) {
  741. if (scope.editingExisting) {
  742. // we shouldn't validate
  743. ctrl.$setValidity('validNodeid', true);
  744. } else {
  745. $http.get(urlbase + '/nodeid?id='+viewValue).success(function (resp) {
  746. if (resp.error) {
  747. ctrl.$setValidity('validNodeid', false);
  748. } else {
  749. ctrl.$setValidity('validNodeid', true);
  750. }
  751. });
  752. }
  753. return viewValue;
  754. });
  755. }
  756. };
  757. });
  758. syncthing.directive('modal', function () {
  759. return {
  760. restrict: 'E',
  761. templateUrl: 'modal.html',
  762. replace: true,
  763. transclude: true,
  764. scope: {
  765. title: '@',
  766. status: '@',
  767. icon: '@',
  768. close: '@',
  769. large: '@',
  770. },
  771. }
  772. });