app.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. // Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
  2. // Use of this source code is governed by an MIT-style license that can be
  3. // 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. // Strings before bools look better
  28. $scope.settings = [
  29. {id: 'ListenStr', descr: 'Sync Protocol Listen Addresses', type: 'text', restart: true},
  30. {id: 'MaxSendKbps', descr: 'Outgoing Rate Limit (KiB/s)', type: 'number', restart: true},
  31. {id: 'RescanIntervalS', descr: 'Rescan Interval (s)', type: 'number', restart: true},
  32. {id: 'ReconnectIntervalS', descr: 'Reconnect Interval (s)', type: 'number', restart: true},
  33. {id: 'ParallelRequests', descr: 'Max Outstanding Requests', type: 'number', restart: true},
  34. {id: 'MaxChangeKbps', descr: 'Max File Change Rate (KiB/s)', type: 'number', restart: true},
  35. {id: 'GlobalAnnEnabled', descr: 'Global Discovery', type: 'bool', restart: true},
  36. {id: 'LocalAnnEnabled', descr: 'Local Discovery', type: 'bool', restart: true},
  37. {id: 'LocalAnnPort', descr: 'Local Discovery Port', type: 'number', restart: true},
  38. {id: 'StartBrowser', descr: 'Start Browser', type: 'bool'},
  39. {id: 'UPnPEnabled', descr: 'Enable UPnP', type: 'bool'},
  40. ];
  41. $scope.guiSettings = [
  42. {id: 'Address', descr: 'GUI Listen Addresses', type: 'text', restart: true},
  43. {id: 'User', descr: 'GUI Authentication User', type: 'text', restart: true},
  44. {id: 'Password', descr: 'GUI Authentication Password', type: 'password', restart: true},
  45. {id: 'UseTLS', descr: 'Use HTTPS for GUI', type: 'bool', restart: true},
  46. {id: 'APIKey', descr: 'API Key', type: 'apikey'},
  47. ];
  48. function getSucceeded() {
  49. if (!getOK) {
  50. $scope.init();
  51. $('#networkError').modal('hide');
  52. getOK = true;
  53. }
  54. if (restarting) {
  55. $scope.init();
  56. $('#restarting').modal('hide');
  57. $('#shutdown').modal('hide');
  58. restarting = false;
  59. }
  60. }
  61. function getFailed() {
  62. if (restarting) {
  63. return;
  64. }
  65. if (getOK) {
  66. $('#networkError').modal({backdrop: 'static', keyboard: false});
  67. getOK = false;
  68. }
  69. }
  70. $scope.refresh = function () {
  71. $http.get(urlbase + '/system').success(function (data) {
  72. getSucceeded();
  73. $scope.system = data;
  74. }).error(function () {
  75. getFailed();
  76. });
  77. Object.keys($scope.repos).forEach(function (id) {
  78. $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
  79. $scope.model[id] = data;
  80. });
  81. });
  82. $http.get(urlbase + '/connections').success(function (data) {
  83. var now = Date.now(),
  84. td = (now - prevDate) / 1000,
  85. id;
  86. prevDate = now;
  87. for (id in data) {
  88. if (!data.hasOwnProperty(id)) {
  89. continue;
  90. }
  91. try {
  92. data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
  93. data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
  94. } catch (e) {
  95. data[id].inbps = 0;
  96. data[id].outbps = 0;
  97. }
  98. }
  99. $scope.connections = data;
  100. });
  101. $http.get(urlbase + '/errors').success(function (data) {
  102. $scope.errors = data;
  103. });
  104. };
  105. $scope.repoStatus = function (repo) {
  106. if (typeof $scope.model[repo] === 'undefined') {
  107. return 'Unknown';
  108. }
  109. if ($scope.model[repo].invalid !== '') {
  110. return 'Stopped';
  111. }
  112. var state = '' + $scope.model[repo].state;
  113. state = state[0].toUpperCase() + state.substr(1);
  114. if (state == "Syncing" || state == "Idle") {
  115. state += " (" + $scope.syncPercentage(repo) + "%)";
  116. }
  117. return state;
  118. };
  119. $scope.repoClass = function (repo) {
  120. if (typeof $scope.model[repo] === 'undefined') {
  121. return 'info';
  122. }
  123. if ($scope.model[repo].invalid !== '') {
  124. return 'danger';
  125. }
  126. var state = '' + $scope.model[repo].state;
  127. if (state == 'idle') {
  128. return 'success';
  129. }
  130. if (state == 'syncing') {
  131. return 'primary';
  132. }
  133. return 'info';
  134. };
  135. $scope.syncPercentage = function (repo) {
  136. if (typeof $scope.model[repo] === 'undefined') {
  137. return 100;
  138. }
  139. if ($scope.model[repo].globalBytes === 0) {
  140. return 100;
  141. }
  142. var pct = 100 * $scope.model[repo].inSyncBytes / $scope.model[repo].globalBytes;
  143. return Math.floor(pct);
  144. };
  145. $scope.nodeStatus = function (nodeCfg) {
  146. var conn = $scope.connections[nodeCfg.NodeID];
  147. if (conn) {
  148. if (conn.Completion === 100) {
  149. return 'Up to Date';
  150. } else {
  151. return 'Syncing (' + conn.Completion + '%)';
  152. }
  153. }
  154. return 'Disconnected';
  155. };
  156. $scope.nodeIcon = function (nodeCfg) {
  157. var conn = $scope.connections[nodeCfg.NodeID];
  158. if (conn) {
  159. if (conn.Completion === 100) {
  160. return 'ok';
  161. } else {
  162. return 'refresh';
  163. }
  164. }
  165. return 'minus';
  166. };
  167. $scope.nodeClass = function (nodeCfg) {
  168. var conn = $scope.connections[nodeCfg.NodeID];
  169. if (conn) {
  170. if (conn.Completion === 100) {
  171. return 'success';
  172. } else {
  173. return 'primary';
  174. }
  175. }
  176. return 'info';
  177. };
  178. $scope.nodeAddr = function (nodeCfg) {
  179. var conn = $scope.connections[nodeCfg.NodeID];
  180. if (conn) {
  181. return conn.Address;
  182. }
  183. return '?';
  184. };
  185. $scope.nodeCompletion = function (nodeCfg) {
  186. var conn = $scope.connections[nodeCfg.NodeID];
  187. if (conn) {
  188. return conn.Completion + '%';
  189. }
  190. return '';
  191. };
  192. $scope.nodeVer = function (nodeCfg) {
  193. if (nodeCfg.NodeID === $scope.myID) {
  194. return $scope.version;
  195. }
  196. var conn = $scope.connections[nodeCfg.NodeID];
  197. if (conn) {
  198. return conn.ClientVersion;
  199. }
  200. return '?';
  201. };
  202. $scope.findNode = function (nodeID) {
  203. var matches = $scope.nodes.filter(function (n) { return n.NodeID == nodeID; });
  204. if (matches.length != 1) {
  205. return undefined;
  206. }
  207. return matches[0];
  208. };
  209. $scope.nodeName = function (nodeCfg) {
  210. if (typeof nodeCfg === 'undefined') {
  211. return "";
  212. }
  213. if (nodeCfg.Name) {
  214. return nodeCfg.Name;
  215. }
  216. return nodeCfg.NodeID.substr(0, 6);
  217. };
  218. $scope.thisNodeName = function () {
  219. var node = $scope.thisNode();
  220. if (typeof node === 'undefined') {
  221. return "(unknown node)";
  222. }
  223. if (node.Name) {
  224. return node.Name;
  225. }
  226. return node.NodeID.substr(0, 6);
  227. };
  228. $scope.editSettings = function () {
  229. // Make a working copy
  230. $scope.config.workingOptions = angular.copy($scope.config.Options);
  231. $scope.config.workingGUI = angular.copy($scope.config.GUI);
  232. $('#settings').modal({backdrop: 'static', keyboard: true});
  233. };
  234. $scope.saveSettings = function () {
  235. // Make sure something changed
  236. var changed = ! angular.equals($scope.config.Options, $scope.config.workingOptions) ||
  237. ! angular.equals($scope.config.GUI, $scope.config.workingGUI);
  238. if(changed){
  239. // see if protocol will need to be changed on restart
  240. if($scope.config.GUI.UseTLS !== $scope.config.workingGUI.UseTLS){
  241. $scope.protocolChanged = true;
  242. }
  243. // Apply new settings locally
  244. $scope.config.Options = angular.copy($scope.config.workingOptions);
  245. $scope.config.GUI = angular.copy($scope.config.workingGUI);
  246. $scope.configInSync = false;
  247. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  248. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  249. }
  250. $('#settings').modal("hide");
  251. };
  252. $scope.restart = function () {
  253. restarting = true;
  254. $('#restarting').modal({backdrop: 'static', keyboard: false});
  255. $http.post(urlbase + '/restart');
  256. $scope.configInSync = true;
  257. // Switch webpage protocol if needed
  258. if($scope.protocolChanged){
  259. var protocol = 'http';
  260. if($scope.config.GUI.UseTLS){
  261. protocol = 'https';
  262. }
  263. setTimeout(function(){
  264. window.location.protocol = protocol;
  265. }, 1000);
  266. $scope.protocolChanged = false;
  267. }
  268. };
  269. $scope.shutdown = function () {
  270. restarting = true;
  271. $http.post(urlbase + '/shutdown').success(function () {
  272. $('#shutdown').modal({backdrop: 'static', keyboard: false});
  273. });
  274. $scope.configInSync = true;
  275. };
  276. $scope.editNode = function (nodeCfg) {
  277. $scope.currentNode = $.extend({}, nodeCfg);
  278. $scope.editingExisting = true;
  279. $scope.editingSelf = (nodeCfg.NodeID == $scope.myID);
  280. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  281. $scope.nodeEditor.$setPristine();
  282. $('#editNode').modal({backdrop: 'static', keyboard: true});
  283. };
  284. $scope.idNode = function () {
  285. $('#idqr').modal('show');
  286. };
  287. $scope.addNode = function () {
  288. $scope.currentNode = {AddressesStr: 'dynamic'};
  289. $scope.editingExisting = false;
  290. $scope.editingSelf = false;
  291. $scope.nodeEditor.$setPristine();
  292. $('#editNode').modal({backdrop: 'static', keyboard: true});
  293. };
  294. $scope.deleteNode = function () {
  295. $('#editNode').modal('hide');
  296. if (!$scope.editingExisting) {
  297. return;
  298. }
  299. $scope.nodes = $scope.nodes.filter(function (n) {
  300. return n.NodeID !== $scope.currentNode.NodeID;
  301. });
  302. $scope.config.Nodes = $scope.nodes;
  303. for (var id in $scope.repos) {
  304. $scope.repos[id].Nodes = $scope.repos[id].Nodes.filter(function (n) {
  305. return n.NodeID !== $scope.currentNode.NodeID;
  306. });
  307. }
  308. $scope.configInSync = false;
  309. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  310. };
  311. $scope.saveNode = function () {
  312. var nodeCfg, done, i;
  313. $scope.configInSync = false;
  314. $('#editNode').modal('hide');
  315. nodeCfg = $scope.currentNode;
  316. nodeCfg.NodeID = nodeCfg.NodeID.replace(/ /g, '').replace(/-/g, '').toUpperCase().trim();
  317. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  318. done = false;
  319. for (i = 0; i < $scope.nodes.length; i++) {
  320. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  321. $scope.nodes[i] = nodeCfg;
  322. done = true;
  323. break;
  324. }
  325. }
  326. if (!done) {
  327. $scope.nodes.push(nodeCfg);
  328. }
  329. $scope.nodes.sort(nodeCompare);
  330. $scope.config.Nodes = $scope.nodes;
  331. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  332. };
  333. $scope.otherNodes = function () {
  334. return $scope.nodes.filter(function (n){
  335. return n.NodeID !== $scope.myID;
  336. });
  337. };
  338. $scope.thisNode = function () {
  339. var i, n;
  340. for (i = 0; i < $scope.nodes.length; i++) {
  341. n = $scope.nodes[i];
  342. if (n.NodeID === $scope.myID) {
  343. return n;
  344. }
  345. }
  346. };
  347. $scope.allNodes = function () {
  348. var nodes = $scope.otherNodes();
  349. nodes.push($scope.thisNode());
  350. return nodes;
  351. };
  352. $scope.errorList = function () {
  353. return $scope.errors.filter(function (e) {
  354. return e.Time > $scope.seenError;
  355. });
  356. };
  357. $scope.clearErrors = function () {
  358. $scope.seenError = $scope.errors[$scope.errors.length - 1].Time;
  359. $http.post(urlbase + '/error/clear');
  360. };
  361. $scope.friendlyNodes = function (str) {
  362. for (var i = 0; i < $scope.nodes.length; i++) {
  363. var cfg = $scope.nodes[i];
  364. str = str.replace(cfg.NodeID, $scope.nodeName(cfg));
  365. }
  366. return str;
  367. };
  368. $scope.repoList = function () {
  369. return repoList($scope.repos);
  370. };
  371. $scope.editRepo = function (nodeCfg) {
  372. $scope.currentRepo = angular.copy(nodeCfg);
  373. $scope.currentRepo.selectedNodes = {};
  374. $scope.currentRepo.Nodes.forEach(function (n) {
  375. $scope.currentRepo.selectedNodes[n.NodeID] = true;
  376. });
  377. if ($scope.currentRepo.Versioning && $scope.currentRepo.Versioning.Type === "simple") {
  378. $scope.currentRepo.simpleFileVersioning = true;
  379. $scope.currentRepo.simpleKeep = +$scope.currentRepo.Versioning.Params.keep;
  380. }
  381. $scope.currentRepo.simpleKeep = $scope.currentRepo.simpleKeep || 5;
  382. $scope.editingExisting = true;
  383. $scope.repoEditor.$setPristine();
  384. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  385. };
  386. $scope.addRepo = function () {
  387. $scope.currentRepo = {selectedNodes: {}};
  388. $scope.editingExisting = false;
  389. $scope.repoEditor.$setPristine();
  390. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  391. };
  392. $scope.saveRepo = function () {
  393. var repoCfg, done, i;
  394. $scope.configInSync = false;
  395. $('#editRepo').modal('hide');
  396. repoCfg = $scope.currentRepo;
  397. repoCfg.Nodes = [];
  398. repoCfg.selectedNodes[$scope.myID] = true;
  399. for (var nodeID in repoCfg.selectedNodes) {
  400. if (repoCfg.selectedNodes[nodeID] === true) {
  401. repoCfg.Nodes.push({NodeID: nodeID});
  402. }
  403. }
  404. delete repoCfg.selectedNodes;
  405. if (repoCfg.simpleFileVersioning) {
  406. repoCfg.Versioning = {
  407. 'Type': 'simple',
  408. 'Params': {
  409. 'keep': '' + repoCfg.simpleKeep,
  410. }
  411. };
  412. delete repoCfg.simpleFileVersioning;
  413. delete repoCfg.simpleKeep;
  414. } else {
  415. delete repoCfg.Versioning;
  416. }
  417. $scope.repos[repoCfg.ID] = repoCfg;
  418. $scope.config.Repositories = repoList($scope.repos);
  419. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  420. };
  421. $scope.sharesRepo = function(repoCfg) {
  422. var names = [];
  423. repoCfg.Nodes.forEach(function (node) {
  424. names.push($scope.nodeName($scope.findNode(node.NodeID)));
  425. });
  426. names.sort();
  427. return names.join(", ");
  428. };
  429. $scope.deleteRepo = function () {
  430. $('#editRepo').modal('hide');
  431. if (!$scope.editingExisting) {
  432. return;
  433. }
  434. delete $scope.repos[$scope.currentRepo.ID];
  435. $scope.config.Repositories = repoList($scope.repos);
  436. $scope.configInSync = false;
  437. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  438. };
  439. $scope.setAPIKey = function (cfg) {
  440. cfg.APIKey = randomString(30, 32);
  441. };
  442. $scope.init = function() {
  443. $http.get(urlbase + '/version').success(function (data) {
  444. $scope.version = data;
  445. });
  446. $http.get(urlbase + '/system').success(function (data) {
  447. $scope.system = data;
  448. $scope.myID = data.myID;
  449. });
  450. $http.get(urlbase + '/config').success(function (data) {
  451. $scope.config = data;
  452. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  453. $scope.nodes = $scope.config.Nodes;
  454. $scope.nodes.sort(nodeCompare);
  455. $scope.repos = repoMap($scope.config.Repositories);
  456. $scope.refresh();
  457. });
  458. $http.get(urlbase + '/config/sync').success(function (data) {
  459. $scope.configInSync = data.configInSync;
  460. });
  461. };
  462. $scope.init();
  463. setInterval($scope.refresh, 10000);
  464. });
  465. function nodeCompare(a, b) {
  466. if (typeof a.Name !== 'undefined' && typeof b.Name !== 'undefined') {
  467. if (a.Name < b.Name)
  468. return -1;
  469. return a.Name > b.Name;
  470. }
  471. if (a.NodeID < b.NodeID) {
  472. return -1;
  473. }
  474. return a.NodeID > b.NodeID;
  475. }
  476. function repoCompare(a, b) {
  477. if (a.Directory < b.Directory) {
  478. return -1;
  479. }
  480. return a.Directory > b.Directory;
  481. }
  482. function repoMap(l) {
  483. var m = {};
  484. l.forEach(function (r) {
  485. m[r.ID] = r;
  486. });
  487. return m;
  488. }
  489. function repoList(m) {
  490. var l = [];
  491. for (var id in m) {
  492. l.push(m[id]);
  493. }
  494. l.sort(repoCompare);
  495. return l;
  496. }
  497. function decimals(val, num) {
  498. var digits, decs;
  499. if (val === 0) {
  500. return 0;
  501. }
  502. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  503. decs = Math.max(0, num - digits);
  504. return decs;
  505. }
  506. function randomString(len, bits)
  507. {
  508. bits = bits || 36;
  509. var outStr = "", newStr;
  510. while (outStr.length < len)
  511. {
  512. newStr = Math.random().toString(bits).slice(2);
  513. outStr += newStr.slice(0, Math.min(newStr.length, (len - outStr.length)));
  514. }
  515. return outStr.toUpperCase();
  516. }
  517. syncthing.filter('natural', function () {
  518. return function (input, valid) {
  519. return input.toFixed(decimals(input, valid));
  520. };
  521. });
  522. syncthing.filter('binary', function () {
  523. return function (input) {
  524. if (input === undefined) {
  525. return '0 ';
  526. }
  527. if (input > 1024 * 1024 * 1024) {
  528. input /= 1024 * 1024 * 1024;
  529. return input.toFixed(decimals(input, 2)) + ' Gi';
  530. }
  531. if (input > 1024 * 1024) {
  532. input /= 1024 * 1024;
  533. return input.toFixed(decimals(input, 2)) + ' Mi';
  534. }
  535. if (input > 1024) {
  536. input /= 1024;
  537. return input.toFixed(decimals(input, 2)) + ' Ki';
  538. }
  539. return Math.round(input) + ' ';
  540. };
  541. });
  542. syncthing.filter('metric', function () {
  543. return function (input) {
  544. if (input === undefined) {
  545. return '0 ';
  546. }
  547. if (input > 1000 * 1000 * 1000) {
  548. input /= 1000 * 1000 * 1000;
  549. return input.toFixed(decimals(input, 2)) + ' G';
  550. }
  551. if (input > 1000 * 1000) {
  552. input /= 1000 * 1000;
  553. return input.toFixed(decimals(input, 2)) + ' M';
  554. }
  555. if (input > 1000) {
  556. input /= 1000;
  557. return input.toFixed(decimals(input, 2)) + ' k';
  558. }
  559. return Math.round(input) + ' ';
  560. };
  561. });
  562. syncthing.filter('short', function () {
  563. return function (input) {
  564. return input.substr(0, 6);
  565. };
  566. });
  567. syncthing.filter('alwaysNumber', function () {
  568. return function (input) {
  569. if (input === undefined) {
  570. return 0;
  571. }
  572. return input;
  573. };
  574. });
  575. syncthing.filter('chunkID', function () {
  576. return function (input) {
  577. if (input === undefined)
  578. return "";
  579. var parts = input.match(/.{1,6}/g);
  580. if (!parts)
  581. return "";
  582. return parts.join('-');
  583. };
  584. });
  585. syncthing.filter('shortPath', function () {
  586. return function (input) {
  587. if (input === undefined)
  588. return "";
  589. var parts = input.split(/[\/\\]/);
  590. if (!parts || parts.length <= 3) {
  591. return input;
  592. }
  593. return ".../" + parts.slice(parts.length-2).join("/");
  594. };
  595. });
  596. syncthing.filter('clean', function () {
  597. return function (input) {
  598. return encodeURIComponent(input).replace(/%/g, '');
  599. };
  600. });
  601. syncthing.directive('optionEditor', function () {
  602. return {
  603. restrict: 'C',
  604. replace: true,
  605. transclude: true,
  606. scope: {
  607. setting: '=setting',
  608. },
  609. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  610. };
  611. });
  612. syncthing.directive('uniqueRepo', function() {
  613. return {
  614. require: 'ngModel',
  615. link: function(scope, elm, attrs, ctrl) {
  616. ctrl.$parsers.unshift(function(viewValue) {
  617. if (scope.editingExisting) {
  618. // we shouldn't validate
  619. ctrl.$setValidity('uniqueRepo', true);
  620. } else if (scope.repos[viewValue]) {
  621. // the repo exists already
  622. ctrl.$setValidity('uniqueRepo', false);
  623. } else {
  624. // the repo is unique
  625. ctrl.$setValidity('uniqueRepo', true);
  626. }
  627. return viewValue;
  628. });
  629. }
  630. };
  631. });
  632. syncthing.directive('validNodeid', function() {
  633. return {
  634. require: 'ngModel',
  635. link: function(scope, elm, attrs, ctrl) {
  636. ctrl.$parsers.unshift(function(viewValue) {
  637. if (scope.editingExisting) {
  638. // we shouldn't validate
  639. ctrl.$setValidity('validNodeid', true);
  640. } else {
  641. var cleaned = viewValue.replace(/ /g, '').replace(/-/g, '').toUpperCase().trim();
  642. if (cleaned.match(/^[A-Z2-7]{52}$/)) {
  643. ctrl.$setValidity('validNodeid', true);
  644. } else {
  645. ctrl.$setValidity('validNodeid', false);
  646. }
  647. }
  648. return viewValue;
  649. });
  650. }
  651. };
  652. });