public/js/quartz.js in quartz_flow-0.0.1 vs public/js/quartz.js in quartz_flow-0.0.2

- old
+ new

@@ -6,17 +6,18 @@ quartzModule.config(function($routeProvider) { $routeProvider. when('/', {controller: TorrentTableCtrl, templateUrl:'/torrent_table'}). when('/details/:torrent', {controller: TorrentDetailsCtrl, templateUrl:'/torrent_detail'}). when('/config', {controller: ConfigCtrl, templateUrl:'/config'}). + when('/show_list', {controller: ShowListCtrl, templateUrl:'/show_list'}). otherwise({redirectTo:'/'}); }); /* Set up function that keeps retrieving torrent data from server on a timer. We store the data in the rootScope which means it's available in all scopes. */ -quartzModule.run(function($rootScope, $timeout, $http, $window) { +quartzModule.run(function($rootScope, $timeout, $http) { $rootScope.alerts = {}; $rootScope.deleteRootscopeError = function(err){ delete $rootScope.alerts[err]; } @@ -30,16 +31,30 @@ } return rc; }; +}); + +/* Controller for the torrent table view */ +function TorrentTableCtrl($scope, $rootScope, $timeout, $http, $window) { + $scope.errors = []; + $scope.destroyed = false; + + $scope.$on("$destroy", function(e){ + console.log("Destroy called for TorrentTableCtrl"); + $scope.destroyed = true; + }); + console.log("TorrentTableCtrl called"); + // Load the list of torrent data every 1 second. var refresh = function() { // http://code.angularjs.org/1.0.8/docs/api/ng.$http var msg = "Server is unreachable."; - $http.get("/torrent_data", {'timeout': 3000}). + var fields = ["recommendedName", "dataLength", "state", "infoHash", "downloadRate", "uploadRate","percentComplete","timeLeft","paused"] + $http.get("/torrent_data", {'timeout': 3000, "params": {"fields" : fields } }). success(function(data,status,headers,config){ $rootScope.torrents = data; updateTableTorrentData($rootScope); $rootScope.deleteRootscopeError(msg); }). @@ -53,29 +68,27 @@ } else { $rootScope.alerts[data] = 1; } }); - $timeout(refresh, 1000); + if ( ! $scope.destroyed ){ + $timeout(refresh, 1000); + } } refresh(); -}); -/* Controller for the torrent table view */ -function TorrentTableCtrl($scope, $rootScope, $timeout, $http) { - $scope.errors = []; - - var checkIframeMessages = function() { while( iframeUploadResultMessages.length > 0 ) { var msg = iframeUploadResultMessages.shift(); if ( msg != "@@success" ){ $scope.errors.push(msg); } } - $timeout(checkIframeMessages, 1000); + if ( ! $scope.destroyed ){ + $timeout(checkIframeMessages, 1000); + } } $timeout(checkIframeMessages, 1000); $scope.deleteError = function(err){ genericDeleteError($scope, err); @@ -89,13 +102,15 @@ success(function(data,status,headers,config){ $scope.dailyUsage = data.dailyUsage; $scope.monthlyUsage = data.monthlyUsage; }) - $timeout(getUsage, 2000); + if ( ! $scope.destroyed ){ + $timeout(getUsage, 2000); + } } - $timeout(getUsage, 2000); + getUsage(); $scope.getTimes = function(n){ var result = []; for(var i = 0; i < n; i++){ result.push(i); @@ -215,17 +230,83 @@ } /* Controller for the torrent details view */ -function TorrentDetailsCtrl($scope, $routeParams, $http) { - $scope.torrent = $scope.torrentsForTable[$routeParams.torrent] +function TorrentDetailsCtrl($scope, $rootScope, $timeout, $routeParams, $http, $window) { + $scope.destroyed = false; + $scope.errors = []; + $scope.torrent = null; + $scope.$on("$destroy", function(e){ + console.log("Destroy called for TorrentDetailsCtrl"); + $scope.destroyed = true; + }); + + $scope.stateForDisplay = function(){ + var torrent = $scope.torrent; + if ( ! torrent ){ + return "unknown"; + } + + var result = torrent.state; + + if ( torrent.paused ){ + result = result + " (paused)"; + } + + return result; + } + + // Load the list of torrent data every 1 second. + var refresh = function() { + // http://code.angularjs.org/1.0.8/docs/api/ng.$http + + var msg = "Server is unreachable."; + $http.get("/torrent_data", {'timeout': 3000, "params": {"where" : {"infoHash" : $routeParams.torrent} } }). + success(function(data,status,headers,config){ + if ( ! $scope.torrent ){ + $scope.torrent = data[$routeParams.torrent] + } + else { + updateTorrentData(data[$routeParams.torrent], $scope.torrent); + } + $rootScope.deleteRootscopeError(msg); + }). + error(function(data,status,headers,config){ + $scope.torrent = null + if ( status == 0 ){ + $rootScope.alerts[msg] = 1; + } else if (data == "Authentication required" ) { + $window.location.href = '/login'; + } else { + $rootScope.alerts[data] = 1; + } + }); + + if ( ! $scope.destroyed ){ + $timeout(refresh, 1000); + } + } + refresh(); + $scope.deleteError = function(err){ genericDeleteError($scope, err); } + $scope.applyChange = function(propertyName){ + var json = {"infoHash": $scope.torrent.infoHash}; + json[propertyName] = $scope.torrent[propertyName]; + $http.post("/change_torrent", json). + success(function(data,status,headers,config){ + console.log("huzzah, changing setting succeeded"); + }). + error(function(data,status,headers,config){ + $scope.errors.push(data); + }); + } + $scope.applyDownloadRateLimit = function(){ $http.post("/change_torrent", {"infoHash": $scope.torrent.infoHash, "downloadRateLimit" : $scope.torrent.downloadRateLimit}). success(function(data,status,headers,config){ console.log("huzzah, changing setting succeeded"); }). @@ -236,10 +317,11 @@ } /* Controller for the config view */ function ConfigCtrl($scope, $timeout, $http) { + $scope.errors = []; $scope.deleteError = function(err){ genericDeleteError($scope, err); } $http.get("/global_settings"). @@ -267,10 +349,11 @@ $scope.errors = []; $scope.login = null; $scope.password = null; $scope.doLogin = function(){ + var msg = "Server is unreachable."; $http.post("/login", {"login": $scope.login, "password" : $scope.password}). success(function(data,status,headers,config){ $window.location.href = '/'; }). error(function(data,status,headers,config){ @@ -292,14 +375,18 @@ $scope.deleteError = function(err){ genericDeleteError($scope, err); } } -var torrentPropsNotToUpdate = { 'downloadRateLimit': 1 }; +function ShowListCtrl($scope) { +} /* Helper used to update the $scope's list of torrent data shown in the table - from the full data retrieved from the server */ + from the full data retrieved from the server. One of the useful effects of this + function is that the table model is only changed if the data from the server is + different. This means AngularJs won't need to keep updating the view with the same + data every time. */ function updateTableTorrentData($scope) { if ( ! $scope.torrents ) return; if ( ! $scope.torrentsForTable ) { @@ -314,16 +401,11 @@ $scope.torrentsForTable[key] = clone(torrent); } else { // Update entry if needed existing = $scope.torrentsForTable[key]; - for (var prop in torrent) { - if (torrent.hasOwnProperty(prop)) { - if (existing[prop] != torrent[prop] && !torrentPropsNotToUpdate[prop]) - existing[prop] = torrent[prop]; - } - } + updateTorrentData(torrent, existing); } } } var toDelete = []; @@ -338,9 +420,23 @@ } // Set up the current page updatePages($scope); } + +var torrentPropsNotToUpdate = { 'downloadRateLimit': 1, 'uploadRateLimit': 1, 'ratio' : 1, 'uploadDuration' : 1 }; + +/* srcTorrent should be a JSON object representing torrent data retrieved from the server. +*/ +function updateTorrentData(srcTorrent, dstTorrent){ + for (var prop in srcTorrent) { + if (srcTorrent.hasOwnProperty(prop)) { + if (dstTorrent[prop] != srcTorrent[prop] && !torrentPropsNotToUpdate[prop]) + dstTorrent[prop] = srcTorrent[prop]; + } + } +} + function updatePages($scope) { // Set up the current page var pageSize = 5; if( ! $scope.currentPageIndex ) {