| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- /*jslint browser: true, continue: true, plusplus: true */
- /*global $: false, angular: false */
- 'use strict';
- var syncthing = angular.module('syncthing', []);
- syncthing.controller('SyncthingCtrl', function ($scope, $http) {
- var prevDate = 0,
- modelGetOK = true;
- $scope.connections = {};
- $scope.config = {};
- $scope.myID = '';
- $scope.nodes = [];
- $scope.configInSync = true;
- // Strings before bools look better
- $scope.settings = [
- {id: 'ListenStr', descr: 'Sync Protocol Listen Addresses', type: 'text', restart: true},
- {id: 'GUIAddress', descr: 'GUI Listen Address', type: 'text', restart: true},
- {id: 'MaxSendKbps', descr: 'Outgoing Rate Limit (KBps)', type: 'number', restart: true},
- {id: 'RescanIntervalS', descr: 'Rescan Interval (s)', type: 'number', restart: true},
- {id: 'ReconnectIntervalS', descr: 'Reconnect Interval (s)', type: 'number', restart: true},
- {id: 'ParallelRequests', descr: 'Max Outstanding Requests', type: 'number', restart: true},
- {id: 'MaxChangeKbps', descr: 'Max File Change Rate (KBps)', type: 'number', restart: true},
- {id: 'ReadOnly', descr: 'Read Only', type: 'bool', restart: true},
- {id: 'AllowDelete', descr: 'Allow Delete', type: 'bool', restart: true},
- {id: 'FollowSymlinks', descr: 'Follow Symlinks', type: 'bool', restart: true},
- {id: 'GlobalAnnEnabled', descr: 'Global Announce', type: 'bool', restart: true},
- {id: 'LocalAnnEnabled', descr: 'Local Announce', type: 'bool', restart: true},
- ];
- function modelGetSucceeded() {
- if (!modelGetOK) {
- $('#networkError').modal('hide');
- modelGetOK = true;
- }
- }
- function modelGetFailed() {
- if (modelGetOK) {
- $('#networkError').modal({backdrop: 'static', keyboard: false});
- modelGetOK = false;
- }
- }
- function nodeCompare(a, b) {
- if (a.NodeID === $scope.myID) {
- return -1;
- }
- if (b.NodeID === $scope.myID) {
- return 1;
- }
- if (a.NodeID < b.NodeID) {
- return -1;
- }
- return a.NodeID > b.NodeID;
- }
- $http.get('/rest/version').success(function (data) {
- $scope.version = data;
- });
- $http.get('/rest/system').success(function (data) {
- $scope.system = data;
- $scope.myID = data.myID;
- $http.get('/rest/config').success(function (data) {
- $scope.config = data;
- $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
- var nodes = $scope.config.Repositories[0].Nodes;
- nodes.sort(nodeCompare);
- $scope.nodes = nodes;
- });
- $http.get('/rest/config/sync').success(function (data) {
- $scope.configInSync = data.configInSync;
- });
- });
- $scope.refresh = function () {
- $http.get('/rest/system').success(function (data) {
- $scope.system = data;
- });
- $http.get('/rest/model').success(function (data) {
- $scope.model = data;
- modelGetSucceeded();
- }).error(function () {
- modelGetFailed();
- });
- $http.get('/rest/connections').success(function (data) {
- var now = Date.now(),
- td = (now - prevDate) / 1000,
- id;
- prevDate = now;
- $scope.inbps = 0;
- $scope.outbps = 0;
- for (id in data) {
- if (!data.hasOwnProperty(id)) {
- continue;
- }
- try {
- data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
- data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
- } catch (e) {
- data[id].inbps = 0;
- data[id].outbps = 0;
- }
- $scope.inbps += data[id].inbps;
- $scope.outbps += data[id].outbps;
- }
- $scope.connections = data;
- });
- $http.get('/rest/need').success(function (data) {
- var i, name;
- for (i = 0; i < data.length; i++) {
- name = data[i].Name.split('/');
- data[i].ShortName = name[name.length - 1];
- }
- data.sort(function (a, b) {
- if (a.ShortName < b.ShortName) {
- return -1;
- }
- if (a.ShortName > b.ShortName) {
- return 1;
- }
- return 0;
- });
- $scope.need = data;
- });
- };
- $scope.nodeIcon = function (nodeCfg) {
- if ($scope.connections[nodeCfg.NodeID]) {
- return 'ok';
- }
- return 'minus';
- };
- $scope.nodeStatus = function (nodeCfg) {
- if ($scope.connections[nodeCfg.NodeID]) {
- return 'Connected';
- }
- return 'Disconnected';
- };
- $scope.nodeIcon = function (nodeCfg) {
- if ($scope.connections[nodeCfg.NodeID]) {
- return 'ok';
- }
- return 'minus';
- };
- $scope.nodeClass = function (nodeCfg) {
- var conn = $scope.connections[nodeCfg.NodeID];
- if (conn) {
- return 'success';
- }
- return 'info';
- };
- $scope.nodeAddr = function (nodeCfg) {
- var conn = $scope.connections[nodeCfg.NodeID];
- if (conn) {
- return conn.Address;
- }
- return '(unknown address)';
- };
- $scope.nodeVer = function (nodeCfg) {
- if (nodeCfg.NodeID === $scope.myID) {
- return $scope.version;
- }
- var conn = $scope.connections[nodeCfg.NodeID];
- if (conn) {
- return conn.ClientVersion;
- }
- return '(unknown version)';
- };
- $scope.nodeName = function (nodeCfg) {
- if (nodeCfg.Name) {
- return nodeCfg.Name;
- }
- return nodeCfg.NodeID.substr(0, 6);
- };
- $scope.saveSettings = function () {
- $scope.configInSync = false;
- $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
- $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
- $('#settingsTable').collapse('hide');
- };
- $scope.restart = function () {
- $http.post('/rest/restart');
- $scope.configInSync = true;
- };
- $scope.editNode = function (nodeCfg) {
- $scope.currentNode = nodeCfg;
- $scope.editingExisting = true;
- $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
- $('#editNode').modal({backdrop: 'static', keyboard: false});
- };
- $scope.addNode = function () {
- $scope.currentNode = {NodeID: '', AddressesStr: 'dynamic'};
- $scope.editingExisting = false;
- $('#editNode').modal({backdrop: 'static', keyboard: false});
- };
- $scope.deleteNode = function () {
- var newNodes = [], i;
- $('#editNode').modal('hide');
- if (!$scope.editingExisting) {
- return;
- }
- for (i = 0; i < $scope.nodes.length; i++) {
- if ($scope.nodes[i].NodeID !== $scope.currentNode.NodeID) {
- newNodes.push($scope.nodes[i]);
- }
- }
- $scope.nodes = newNodes;
- $scope.config.Repositories[0].Nodes = newNodes;
- $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
- };
- $scope.saveNode = function () {
- var nodeCfg, done, i;
- $scope.configInSync = false;
- $('#editNode').modal('hide');
- nodeCfg = $scope.currentNode;
- nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
- done = false;
- for (i = 0; i < $scope.nodes.length; i++) {
- if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
- $scope.nodes[i] = nodeCfg;
- done = true;
- break;
- }
- }
- if (!done) {
- $scope.nodes.push(nodeCfg);
- }
- $scope.nodes.sort(nodeCompare);
- $scope.config.Repositories[0].Nodes = $scope.nodes;
- $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
- };
- $scope.otherNodes = function () {
- var nodes = [], i, n;
- for (i = 0; i < $scope.nodes.length; i++) {
- n = $scope.nodes[i];
- if (n.NodeID !== $scope.myID) {
- nodes.push(n);
- }
- }
- return nodes;
- };
- $scope.thisNode = function () {
- var i, n;
- for (i = 0; i < $scope.nodes.length; i++) {
- n = $scope.nodes[i];
- if (n.NodeID === $scope.myID) {
- return [n];
- }
- }
- };
- $scope.refresh();
- setInterval($scope.refresh, 10000);
- });
- function decimals(val, num) {
- var digits, decs;
- if (val === 0) {
- return 0;
- }
- digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
- decs = Math.max(0, num - digits);
- return decs;
- }
- syncthing.filter('natural', function () {
- return function (input, valid) {
- return input.toFixed(decimals(input, valid));
- };
- });
- syncthing.filter('binary', function () {
- return function (input) {
- if (input === undefined) {
- return '0 ';
- }
- if (input > 1024 * 1024 * 1024) {
- input /= 1024 * 1024 * 1024;
- return input.toFixed(decimals(input, 2)) + ' Gi';
- }
- if (input > 1024 * 1024) {
- input /= 1024 * 1024;
- return input.toFixed(decimals(input, 2)) + ' Mi';
- }
- if (input > 1024) {
- input /= 1024;
- return input.toFixed(decimals(input, 2)) + ' Ki';
- }
- return Math.round(input) + ' ';
- };
- });
- syncthing.filter('metric', function () {
- return function (input) {
- if (input === undefined) {
- return '0 ';
- }
- if (input > 1000 * 1000 * 1000) {
- input /= 1000 * 1000 * 1000;
- return input.toFixed(decimals(input, 2)) + ' G';
- }
- if (input > 1000 * 1000) {
- input /= 1000 * 1000;
- return input.toFixed(decimals(input, 2)) + ' M';
- }
- if (input > 1000) {
- input /= 1000;
- return input.toFixed(decimals(input, 2)) + ' k';
- }
- return Math.round(input) + ' ';
- };
- });
- syncthing.filter('short', function () {
- return function (input) {
- return input.substr(0, 6);
- };
- });
- syncthing.filter('alwaysNumber', function () {
- return function (input) {
- if (input === undefined) {
- return 0;
- }
- return input;
- };
- });
- syncthing.directive('optionEditor', function () {
- return {
- restrict: 'C',
- replace: true,
- transclude: true,
- scope: {
- setting: '=setting',
- },
- template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
- };
- });
|