app.js 22 KB

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