app.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. }
  259. };
  260. $scope.shutdown = function () {
  261. restarting = true;
  262. $http.post(urlbase + '/shutdown').success(function () {
  263. $('#shutdown').modal({backdrop: 'static', keyboard: false});
  264. });
  265. $scope.configInSync = true;
  266. };
  267. $scope.editNode = function (nodeCfg) {
  268. $scope.currentNode = $.extend({}, nodeCfg);
  269. $scope.editingExisting = true;
  270. $scope.editingSelf = (nodeCfg.NodeID == $scope.myID);
  271. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  272. $scope.nodeEditor.$setPristine();
  273. $('#editNode').modal({backdrop: 'static', keyboard: true});
  274. };
  275. $scope.idNode = function () {
  276. $('#idqr').modal('show');
  277. };
  278. $scope.addNode = function () {
  279. $scope.currentNode = {AddressesStr: 'dynamic'};
  280. $scope.editingExisting = false;
  281. $scope.editingSelf = false;
  282. $scope.nodeEditor.$setPristine();
  283. $('#editNode').modal({backdrop: 'static', keyboard: true});
  284. };
  285. $scope.deleteNode = function () {
  286. $('#editNode').modal('hide');
  287. if (!$scope.editingExisting) {
  288. return;
  289. }
  290. $scope.nodes = $scope.nodes.filter(function (n) {
  291. return n.NodeID !== $scope.currentNode.NodeID;
  292. });
  293. $scope.config.Nodes = $scope.nodes;
  294. for (var id in $scope.repos) {
  295. $scope.repos[id].Nodes = $scope.repos[id].Nodes.filter(function (n) {
  296. return n.NodeID !== $scope.currentNode.NodeID;
  297. });
  298. }
  299. $scope.configInSync = false;
  300. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  301. };
  302. $scope.saveNode = function () {
  303. var nodeCfg, done, i;
  304. $scope.configInSync = false;
  305. $('#editNode').modal('hide');
  306. nodeCfg = $scope.currentNode;
  307. nodeCfg.NodeID = nodeCfg.NodeID.replace(/ /g, '').replace(/-/g, '').toUpperCase().trim();
  308. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  309. done = false;
  310. for (i = 0; i < $scope.nodes.length; i++) {
  311. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  312. $scope.nodes[i] = nodeCfg;
  313. done = true;
  314. break;
  315. }
  316. }
  317. if (!done) {
  318. $scope.nodes.push(nodeCfg);
  319. }
  320. $scope.nodes.sort(nodeCompare);
  321. $scope.config.Nodes = $scope.nodes;
  322. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  323. };
  324. $scope.otherNodes = function () {
  325. return $scope.nodes.filter(function (n){
  326. return n.NodeID !== $scope.myID;
  327. });
  328. };
  329. $scope.thisNode = function () {
  330. var i, n;
  331. for (i = 0; i < $scope.nodes.length; i++) {
  332. n = $scope.nodes[i];
  333. if (n.NodeID === $scope.myID) {
  334. return n;
  335. }
  336. }
  337. };
  338. $scope.allNodes = function () {
  339. var nodes = $scope.otherNodes();
  340. nodes.push($scope.thisNode());
  341. return nodes;
  342. };
  343. $scope.errorList = function () {
  344. return $scope.errors.filter(function (e) {
  345. return e.Time > $scope.seenError;
  346. });
  347. };
  348. $scope.clearErrors = function () {
  349. $scope.seenError = $scope.errors[$scope.errors.length - 1].Time;
  350. $http.post(urlbase + '/error/clear');
  351. };
  352. $scope.friendlyNodes = function (str) {
  353. for (var i = 0; i < $scope.nodes.length; i++) {
  354. var cfg = $scope.nodes[i];
  355. str = str.replace(cfg.NodeID, $scope.nodeName(cfg));
  356. }
  357. return str;
  358. };
  359. $scope.repoList = function () {
  360. return repoList($scope.repos);
  361. };
  362. $scope.editRepo = function (nodeCfg) {
  363. $scope.currentRepo = angular.copy(nodeCfg);
  364. $scope.currentRepo.selectedNodes = {};
  365. $scope.currentRepo.Nodes.forEach(function (n) {
  366. $scope.currentRepo.selectedNodes[n.NodeID] = true;
  367. });
  368. if ($scope.currentRepo.Versioning && $scope.currentRepo.Versioning.Type === "simple") {
  369. $scope.currentRepo.simpleFileVersioning = true;
  370. $scope.currentRepo.simpleKeep = +$scope.currentRepo.Versioning.Params.keep;
  371. }
  372. $scope.currentRepo.simpleKeep = $scope.currentRepo.simpleKeep || 5;
  373. $scope.editingExisting = true;
  374. $scope.repoEditor.$setPristine();
  375. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  376. };
  377. $scope.addRepo = function () {
  378. $scope.currentRepo = {selectedNodes: {}};
  379. $scope.editingExisting = false;
  380. $scope.repoEditor.$setPristine();
  381. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  382. };
  383. $scope.saveRepo = function () {
  384. var repoCfg, done, i;
  385. $scope.configInSync = false;
  386. $('#editRepo').modal('hide');
  387. repoCfg = $scope.currentRepo;
  388. repoCfg.Nodes = [];
  389. repoCfg.selectedNodes[$scope.myID] = true;
  390. for (var nodeID in repoCfg.selectedNodes) {
  391. if (repoCfg.selectedNodes[nodeID] === true) {
  392. repoCfg.Nodes.push({NodeID: nodeID});
  393. }
  394. }
  395. delete repoCfg.selectedNodes;
  396. if (repoCfg.simpleFileVersioning) {
  397. repoCfg.Versioning = {
  398. 'Type': 'simple',
  399. 'Params': {
  400. 'keep': '' + repoCfg.simpleKeep,
  401. }
  402. };
  403. delete repoCfg.simpleFileVersioning;
  404. delete repoCfg.simpleKeep;
  405. } else {
  406. delete repoCfg.Versioning;
  407. }
  408. $scope.repos[repoCfg.ID] = repoCfg;
  409. $scope.config.Repositories = repoList($scope.repos);
  410. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  411. };
  412. $scope.sharesRepo = function(repoCfg) {
  413. var names = [];
  414. repoCfg.Nodes.forEach(function (node) {
  415. names.push($scope.nodeName($scope.findNode(node.NodeID)));
  416. });
  417. names.sort();
  418. return names.join(", ");
  419. };
  420. $scope.deleteRepo = function () {
  421. $('#editRepo').modal('hide');
  422. if (!$scope.editingExisting) {
  423. return;
  424. }
  425. delete $scope.repos[$scope.currentRepo.ID];
  426. $scope.config.Repositories = repoList($scope.repos);
  427. $scope.configInSync = false;
  428. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  429. };
  430. $scope.init = function() {
  431. $http.get(urlbase + '/version').success(function (data) {
  432. $scope.version = data;
  433. });
  434. $http.get(urlbase + '/system').success(function (data) {
  435. $scope.system = data;
  436. $scope.myID = data.myID;
  437. });
  438. $http.get(urlbase + '/config').success(function (data) {
  439. $scope.config = data;
  440. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  441. $scope.nodes = $scope.config.Nodes;
  442. $scope.nodes.sort(nodeCompare);
  443. $scope.repos = repoMap($scope.config.Repositories);
  444. $scope.refresh();
  445. });
  446. $http.get(urlbase + '/config/sync').success(function (data) {
  447. $scope.configInSync = data.configInSync;
  448. });
  449. };
  450. $scope.init();
  451. setInterval($scope.refresh, 10000);
  452. });
  453. function nodeCompare(a, b) {
  454. if (typeof a.Name !== 'undefined' && typeof b.Name !== 'undefined') {
  455. if (a.Name < b.Name)
  456. return -1;
  457. return a.Name > b.Name;
  458. }
  459. if (a.NodeID < b.NodeID) {
  460. return -1;
  461. }
  462. return a.NodeID > b.NodeID;
  463. }
  464. function repoCompare(a, b) {
  465. if (a.Directory < b.Directory) {
  466. return -1;
  467. }
  468. return a.Directory > b.Directory;
  469. }
  470. function repoMap(l) {
  471. var m = {};
  472. l.forEach(function (r) {
  473. m[r.ID] = r;
  474. });
  475. return m;
  476. }
  477. function repoList(m) {
  478. var l = [];
  479. for (var id in m) {
  480. l.push(m[id]);
  481. }
  482. l.sort(repoCompare);
  483. return l;
  484. }
  485. function decimals(val, num) {
  486. var digits, decs;
  487. if (val === 0) {
  488. return 0;
  489. }
  490. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  491. decs = Math.max(0, num - digits);
  492. return decs;
  493. }
  494. syncthing.filter('natural', function () {
  495. return function (input, valid) {
  496. return input.toFixed(decimals(input, valid));
  497. };
  498. });
  499. syncthing.filter('binary', function () {
  500. return function (input) {
  501. if (input === undefined) {
  502. return '0 ';
  503. }
  504. if (input > 1024 * 1024 * 1024) {
  505. input /= 1024 * 1024 * 1024;
  506. return input.toFixed(decimals(input, 2)) + ' Gi';
  507. }
  508. if (input > 1024 * 1024) {
  509. input /= 1024 * 1024;
  510. return input.toFixed(decimals(input, 2)) + ' Mi';
  511. }
  512. if (input > 1024) {
  513. input /= 1024;
  514. return input.toFixed(decimals(input, 2)) + ' Ki';
  515. }
  516. return Math.round(input) + ' ';
  517. };
  518. });
  519. syncthing.filter('metric', function () {
  520. return function (input) {
  521. if (input === undefined) {
  522. return '0 ';
  523. }
  524. if (input > 1000 * 1000 * 1000) {
  525. input /= 1000 * 1000 * 1000;
  526. return input.toFixed(decimals(input, 2)) + ' G';
  527. }
  528. if (input > 1000 * 1000) {
  529. input /= 1000 * 1000;
  530. return input.toFixed(decimals(input, 2)) + ' M';
  531. }
  532. if (input > 1000) {
  533. input /= 1000;
  534. return input.toFixed(decimals(input, 2)) + ' k';
  535. }
  536. return Math.round(input) + ' ';
  537. };
  538. });
  539. syncthing.filter('short', function () {
  540. return function (input) {
  541. return input.substr(0, 6);
  542. };
  543. });
  544. syncthing.filter('alwaysNumber', function () {
  545. return function (input) {
  546. if (input === undefined) {
  547. return 0;
  548. }
  549. return input;
  550. };
  551. });
  552. syncthing.filter('chunkID', function () {
  553. return function (input) {
  554. if (input === undefined)
  555. return "";
  556. var parts = input.match(/.{1,6}/g);
  557. if (!parts)
  558. return "";
  559. return parts.join('-');
  560. };
  561. });
  562. syncthing.filter('shortPath', function () {
  563. return function (input) {
  564. if (input === undefined)
  565. return "";
  566. var parts = input.split(/[\/\\]/);
  567. if (!parts || parts.length <= 3) {
  568. return input;
  569. }
  570. return ".../" + parts.slice(parts.length-2).join("/");
  571. };
  572. });
  573. syncthing.filter('clean', function () {
  574. return function (input) {
  575. return encodeURIComponent(input).replace(/%/g, '');
  576. };
  577. });
  578. syncthing.directive('optionEditor', function () {
  579. return {
  580. restrict: 'C',
  581. replace: true,
  582. transclude: true,
  583. scope: {
  584. setting: '=setting',
  585. },
  586. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  587. };
  588. });
  589. syncthing.directive('uniqueRepo', function() {
  590. return {
  591. require: 'ngModel',
  592. link: function(scope, elm, attrs, ctrl) {
  593. ctrl.$parsers.unshift(function(viewValue) {
  594. if (scope.editingExisting) {
  595. // we shouldn't validate
  596. ctrl.$setValidity('uniqueRepo', true);
  597. } else if (scope.repos[viewValue]) {
  598. // the repo exists already
  599. ctrl.$setValidity('uniqueRepo', false);
  600. } else {
  601. // the repo is unique
  602. ctrl.$setValidity('uniqueRepo', true);
  603. }
  604. return viewValue;
  605. });
  606. }
  607. };
  608. });
  609. syncthing.directive('validNodeid', function() {
  610. return {
  611. require: 'ngModel',
  612. link: function(scope, elm, attrs, ctrl) {
  613. ctrl.$parsers.unshift(function(viewValue) {
  614. if (scope.editingExisting) {
  615. // we shouldn't validate
  616. ctrl.$setValidity('validNodeid', true);
  617. } else {
  618. var cleaned = viewValue.replace(/ /g, '').replace(/-/g, '').toUpperCase().trim();
  619. if (cleaned.match(/^[A-Z2-7]{52}$/)) {
  620. ctrl.$setValidity('validNodeid', true);
  621. } else {
  622. ctrl.$setValidity('validNodeid', false);
  623. }
  624. }
  625. return viewValue;
  626. });
  627. }
  628. };
  629. });