app.js 26 KB

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