app.js 22 KB

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