app.js 27 KB

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