app.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*jslint browser: true, continue: true, plusplus: true */
  2. /*global $: false, angular: false */
  3. 'use strict';
  4. var syncthing = angular.module('syncthing', []);
  5. syncthing.controller('SyncthingCtrl', function ($scope, $http) {
  6. var prevDate = 0,
  7. modelGetOK = true;
  8. $scope.connections = {};
  9. $scope.config = {};
  10. $scope.myID = '';
  11. $scope.nodes = [];
  12. $scope.configInSync = true;
  13. // Strings before bools look better
  14. $scope.settings = [
  15. {id: 'ListenStr', descr: 'Sync Protocol Listen Addresses', type: 'text', restart: true},
  16. {id: 'GUIAddress', descr: 'GUI Listen Address', type: 'text', restart: true},
  17. {id: 'MaxSendKbps', descr: 'Outgoing Rate Limit (KBps)', type: 'number', restart: true},
  18. {id: 'RescanIntervalS', descr: 'Rescan Interval (s)', type: 'number', restart: true},
  19. {id: 'ReconnectIntervalS', descr: 'Reconnect Interval (s)', type: 'number', restart: true},
  20. {id: 'ParallelRequests', descr: 'Max Outstanding Requests', type: 'number', restart: true},
  21. {id: 'MaxChangeKbps', descr: 'Max File Change Rate (KBps)', type: 'number', restart: true},
  22. {id: 'ReadOnly', descr: 'Read Only', type: 'bool', restart: true},
  23. {id: 'AllowDelete', descr: 'Allow Delete', type: 'bool', restart: true},
  24. {id: 'FollowSymlinks', descr: 'Follow Symlinks', type: 'bool', restart: true},
  25. {id: 'GlobalAnnEnabled', descr: 'Global Announce', type: 'bool', restart: true},
  26. {id: 'LocalAnnEnabled', descr: 'Local Announce', type: 'bool', restart: true},
  27. ];
  28. function modelGetSucceeded() {
  29. if (!modelGetOK) {
  30. $('#networkError').modal('hide');
  31. modelGetOK = true;
  32. }
  33. }
  34. function modelGetFailed() {
  35. if (modelGetOK) {
  36. $('#networkError').modal({backdrop: 'static', keyboard: false});
  37. modelGetOK = false;
  38. }
  39. }
  40. function nodeCompare(a, b) {
  41. if (a.NodeID === $scope.myID) {
  42. return -1;
  43. }
  44. if (b.NodeID === $scope.myID) {
  45. return 1;
  46. }
  47. if (a.NodeID < b.NodeID) {
  48. return -1;
  49. }
  50. return a.NodeID > b.NodeID;
  51. }
  52. $http.get('/rest/version').success(function (data) {
  53. $scope.version = data;
  54. });
  55. $http.get('/rest/system').success(function (data) {
  56. $scope.system = data;
  57. $scope.myID = data.myID;
  58. $http.get('/rest/config').success(function (data) {
  59. $scope.config = data;
  60. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  61. var nodes = $scope.config.Repositories[0].Nodes;
  62. nodes.sort(nodeCompare);
  63. $scope.nodes = nodes;
  64. });
  65. $http.get('/rest/config/sync').success(function (data) {
  66. $scope.configInSync = data.configInSync;
  67. });
  68. });
  69. $scope.refresh = function () {
  70. $http.get('/rest/system').success(function (data) {
  71. $scope.system = data;
  72. });
  73. $http.get('/rest/model').success(function (data) {
  74. $scope.model = data;
  75. modelGetSucceeded();
  76. }).error(function () {
  77. modelGetFailed();
  78. });
  79. $http.get('/rest/connections').success(function (data) {
  80. var now = Date.now(),
  81. td = (now - prevDate) / 1000,
  82. id;
  83. prevDate = now;
  84. $scope.inbps = 0;
  85. $scope.outbps = 0;
  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. $scope.inbps += data[id].inbps;
  98. $scope.outbps += data[id].outbps;
  99. }
  100. $scope.connections = data;
  101. });
  102. $http.get('/rest/need').success(function (data) {
  103. var i, name;
  104. for (i = 0; i < data.length; i++) {
  105. name = data[i].Name.split('/');
  106. data[i].ShortName = name[name.length - 1];
  107. }
  108. data.sort(function (a, b) {
  109. if (a.ShortName < b.ShortName) {
  110. return -1;
  111. }
  112. if (a.ShortName > b.ShortName) {
  113. return 1;
  114. }
  115. return 0;
  116. });
  117. $scope.need = data;
  118. });
  119. };
  120. $scope.nodeIcon = function (nodeCfg) {
  121. if ($scope.connections[nodeCfg.NodeID]) {
  122. return 'ok';
  123. }
  124. return 'minus';
  125. };
  126. $scope.nodeStatus = function (nodeCfg) {
  127. if ($scope.connections[nodeCfg.NodeID]) {
  128. return 'Connected';
  129. }
  130. return 'Disconnected';
  131. };
  132. $scope.nodeIcon = function (nodeCfg) {
  133. if ($scope.connections[nodeCfg.NodeID]) {
  134. return 'ok';
  135. }
  136. return 'minus';
  137. };
  138. $scope.nodeClass = function (nodeCfg) {
  139. var conn = $scope.connections[nodeCfg.NodeID];
  140. if (conn) {
  141. return 'success';
  142. }
  143. return 'info';
  144. };
  145. $scope.nodeAddr = function (nodeCfg) {
  146. var conn = $scope.connections[nodeCfg.NodeID];
  147. if (conn) {
  148. return conn.Address;
  149. }
  150. return '(unknown address)';
  151. };
  152. $scope.nodeVer = function (nodeCfg) {
  153. if (nodeCfg.NodeID === $scope.myID) {
  154. return $scope.version;
  155. }
  156. var conn = $scope.connections[nodeCfg.NodeID];
  157. if (conn) {
  158. return conn.ClientVersion;
  159. }
  160. return '(unknown version)';
  161. };
  162. $scope.nodeName = function (nodeCfg) {
  163. if (nodeCfg.Name) {
  164. return nodeCfg.Name;
  165. }
  166. return nodeCfg.NodeID.substr(0, 6);
  167. };
  168. $scope.saveSettings = function () {
  169. $scope.configInSync = false;
  170. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  171. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  172. $('#settingsTable').collapse('hide');
  173. };
  174. $scope.restart = function () {
  175. $http.post('/rest/restart');
  176. $scope.configInSync = true;
  177. };
  178. $scope.editNode = function (nodeCfg) {
  179. $scope.currentNode = nodeCfg;
  180. $scope.editingExisting = true;
  181. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  182. $('#editNode').modal({backdrop: 'static', keyboard: false});
  183. };
  184. $scope.addNode = function () {
  185. $scope.currentNode = {NodeID: '', AddressesStr: 'dynamic'};
  186. $scope.editingExisting = false;
  187. $('#editNode').modal({backdrop: 'static', keyboard: false});
  188. };
  189. $scope.deleteNode = function () {
  190. var newNodes = [], i;
  191. $('#editNode').modal('hide');
  192. if (!$scope.editingExisting) {
  193. return;
  194. }
  195. for (i = 0; i < $scope.nodes.length; i++) {
  196. if ($scope.nodes[i].NodeID !== $scope.currentNode.NodeID) {
  197. newNodes.push($scope.nodes[i]);
  198. }
  199. }
  200. $scope.nodes = newNodes;
  201. $scope.config.Repositories[0].Nodes = newNodes;
  202. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  203. };
  204. $scope.saveNode = function () {
  205. var nodeCfg, done, i;
  206. $scope.configInSync = false;
  207. $('#editNode').modal('hide');
  208. nodeCfg = $scope.currentNode;
  209. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  210. done = false;
  211. for (i = 0; i < $scope.nodes.length; i++) {
  212. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  213. $scope.nodes[i] = nodeCfg;
  214. done = true;
  215. break;
  216. }
  217. }
  218. if (!done) {
  219. $scope.nodes.push(nodeCfg);
  220. }
  221. $scope.nodes.sort(nodeCompare);
  222. $scope.config.Repositories[0].Nodes = $scope.nodes;
  223. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  224. };
  225. $scope.otherNodes = function () {
  226. var nodes = [], i, n;
  227. for (i = 0; i < $scope.nodes.length; i++) {
  228. n = $scope.nodes[i];
  229. if (n.NodeID !== $scope.myID) {
  230. nodes.push(n);
  231. }
  232. }
  233. return nodes;
  234. };
  235. $scope.thisNode = function () {
  236. var i, n;
  237. for (i = 0; i < $scope.nodes.length; i++) {
  238. n = $scope.nodes[i];
  239. if (n.NodeID === $scope.myID) {
  240. return [n];
  241. }
  242. }
  243. };
  244. $scope.refresh();
  245. setInterval($scope.refresh, 10000);
  246. });
  247. function decimals(val, num) {
  248. var digits, decs;
  249. if (val === 0) {
  250. return 0;
  251. }
  252. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  253. decs = Math.max(0, num - digits);
  254. return decs;
  255. }
  256. syncthing.filter('natural', function () {
  257. return function (input, valid) {
  258. return input.toFixed(decimals(input, valid));
  259. };
  260. });
  261. syncthing.filter('binary', function () {
  262. return function (input) {
  263. if (input === undefined) {
  264. return '0 ';
  265. }
  266. if (input > 1024 * 1024 * 1024) {
  267. input /= 1024 * 1024 * 1024;
  268. return input.toFixed(decimals(input, 2)) + ' Gi';
  269. }
  270. if (input > 1024 * 1024) {
  271. input /= 1024 * 1024;
  272. return input.toFixed(decimals(input, 2)) + ' Mi';
  273. }
  274. if (input > 1024) {
  275. input /= 1024;
  276. return input.toFixed(decimals(input, 2)) + ' Ki';
  277. }
  278. return Math.round(input) + ' ';
  279. };
  280. });
  281. syncthing.filter('metric', function () {
  282. return function (input) {
  283. if (input === undefined) {
  284. return '0 ';
  285. }
  286. if (input > 1000 * 1000 * 1000) {
  287. input /= 1000 * 1000 * 1000;
  288. return input.toFixed(decimals(input, 2)) + ' G';
  289. }
  290. if (input > 1000 * 1000) {
  291. input /= 1000 * 1000;
  292. return input.toFixed(decimals(input, 2)) + ' M';
  293. }
  294. if (input > 1000) {
  295. input /= 1000;
  296. return input.toFixed(decimals(input, 2)) + ' k';
  297. }
  298. return Math.round(input) + ' ';
  299. };
  300. });
  301. syncthing.filter('short', function () {
  302. return function (input) {
  303. return input.substr(0, 6);
  304. };
  305. });
  306. syncthing.filter('alwaysNumber', function () {
  307. return function (input) {
  308. if (input === undefined) {
  309. return 0;
  310. }
  311. return input;
  312. };
  313. });
  314. syncthing.directive('optionEditor', function () {
  315. return {
  316. restrict: 'C',
  317. replace: true,
  318. transclude: true,
  319. scope: {
  320. setting: '=setting',
  321. },
  322. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  323. };
  324. });