app.js 22 KB

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