From 9db282addd90b9f0016614459d2851fa7a5951f9 Mon Sep 17 00:00:00 2001 From: hamza zia Date: Tue, 22 Jan 2013 12:53:30 +0500 Subject: [PATCH] filters landed in angular --- angular.html | 43 ++++++++++++++++++++++------------------- js/ctrls/download.js | 32 +++++++++--------------------- js/ctrls/modal.js | 1 - js/directives/dgraph.js | 4 ++-- js/filters/bytes.js | 34 ++++++++++++++++++++++++++++++++ js/services/utils.js | 20 ------------------- 6 files changed, 68 insertions(+), 66 deletions(-) create mode 100644 js/filters/bytes.js diff --git a/angular.html b/angular.html index 5d08155..b3ee0b6 100644 --- a/angular.html +++ b/angular.html @@ -35,6 +35,8 @@ + + @@ -151,9 +153,11 @@
+ + @@ -178,32 +182,32 @@ @@ -241,26 +245,26 @@
  • Status: {{download.status}}
  • GID: {{download.gid}}
  • Dir: {{download.dir}}
  • -
  • Size: {{download.totalLength}}
  • -
  • Downloaded: {{download.completedLength}}
  • +
  • Size: {{download.totalLength | blength}}
  • +
  • Downloaded: {{download.completedLength | blength}}
  • Num of Pieces: {{download.numPieces}}
  • -
  • Piece Length: {{download.pieceLength}}
  • -
  • ETA: {{download.eta}}
  • -
  • Down Speed: {{download.downloadSpeed}}
  • +
  • Piece Length: {{download.pieceLength | blength}}
  • +
  • ETA: {{download.eta | time}}
  • +
  • Down Speed: {{download.downloadSpeed | bspeed}}
  • -
  • Upload Speed: {{download.uploadSpeed}}
  • -
  • Upload Length: {{download.uploadLength}}
  • +
  • Upload Speed: {{download.uploadSpeed | bspeed}}
  • +
  • Upload Length: {{download.uploadLength | blength}}
  • Connections: {{download.connections}}
  • Download Files

    -
    +
    @@ -274,7 +278,6 @@
    -
    diff --git a/js/ctrls/download.js b/js/ctrls/download.js index 117ad4a..cc6bf59 100644 --- a/js/ctrls/download.js +++ b/js/ctrls/download.js @@ -41,37 +41,23 @@ function(scope, rpc, utils) { scope.getCtx = function(d, ctx) { ctx = ctx || {}; - ctx.status = d.status; - ctx.bitfield = d.bitfield; - ctx.gid = d.gid; - ctx.numPieces = d.numPieces; - ctx.connections = d.connections; - ctx.dir = d.dir.replace(/\\/g, '/'); + _.each([ + 'totalLength', 'completedLength', 'uploadLength', + 'pieceLength', 'downloadSpeed', 'uploadSpeed', + 'status', 'gid', 'bitfield', 'numPieces', 'connections' + ], function(e) { + ctx[e] = d[e]; + }); + ctx.dir = d.dir.replace(/\\/g, '/'); ctx.files = _.map(d.files, function(e) { - e.length = utils.changeLength(e.length, "B"); e.path = e.path.replace(/\\/g, '/').replace(ctx.dir, '.'); return e; }); - _.each(['downloadSpeed', 'uploadSpeed'], function(e) { - ctx[e] = utils.changeLength(d[e], 'B/s'); - }); - _.each([ - 'totalLength', /*'remainingLength',*/ 'completedLength', - 'uploadLength', 'pieceLength' - ], function(e) { - ctx[e] = utils.changeLength(d[e], 'B'); - }); - // raw data for graphs - ctx.dspeed = d.downloadSpeed; - ctx.uspeed = d.uploadSpeed; - - ctx.eta = utils.changeTime( - (d.totalLength-d.completedLength) / d.downloadSpeed - ); + ctx.eta = (d.totalLength-d.completedLength) / d.downloadSpeed; var percentage = (d.completedLength / d.totalLength)*100; percentage = percentage.toFixed(2); diff --git a/js/ctrls/modal.js b/js/ctrls/modal.js index 84fc87a..95aedac 100644 --- a/js/ctrls/modal.js +++ b/js/ctrls/modal.js @@ -1,4 +1,3 @@ - app.controller('ModalCtrl', ['$scope', function(scope) { }]); diff --git a/js/directives/dgraph.js b/js/directives/dgraph.js index 5b3feed..b024a27 100644 --- a/js/directives/dgraph.js +++ b/js/directives/dgraph.js @@ -1,5 +1,5 @@ -app.directive('dgraph', ['$', '$utils', function($, utils) { +app.directive('dgraph', ['$', '$filter', function($, filter) { return function(scope, elem, attrs) { @@ -30,7 +30,7 @@ app.directive('dgraph', ['$', '$utils', function($, utils) { }, yaxis: { tickFormatter: function(val, axis) { - return utils.changeLength(val, "B/s"); + return filter('bspeed')(val); } , min: 0 diff --git a/js/filters/bytes.js b/js/filters/bytes.js new file mode 100644 index 0000000..37ac14c --- /dev/null +++ b/js/filters/bytes.js @@ -0,0 +1,34 @@ +app.filter('changeLength', function() { + return function(len, pref) { + len = parseFloat(len); + if(len <= (1<<10)) return len.toFixed(1) + " " + pref; + else if(len <= (1<<20)) return (len/(1<<10)).toFixed(1) + " K" + pref; + else if(len <= (1<<30)) return (len/(1<<20)).toFixed(1) + " M" + pref; + else return (len/(1<<30)).toFixed(1) + " G" + pref; + }; +}); + +app.filter('blength', ['$filter', function(filter) { + return function(len) { + return filter('changeLength')(len, 'B'); + }; +}]); + +app.filter('bspeed', ['$filter', function(filter) { + return function(speed) { + return filter('changeLength')(speed, 'B/s'); + }; +}]); + +app.filter('time', function() { + return function(time) { + time = parseFloat(time); + if (isNaN(time) || !isFinite(time)) return " infinite"; + if (!time) return " infinite"; + if (time < 60) return time.toFixed(2) + " s"; + else if (time < 60*60) return (time/60).toFixed(2) + " min"; + else if (time < 60*60*24) return (time/(60*60)).toFixed(2) + " hours"; + else return (time/(60*60*24)).toFixed(2) + " days!"; + }; +}); + diff --git a/js/services/utils.js b/js/services/utils.js index 525f92e..9d3f4e0 100644 --- a/js/services/utils.js +++ b/js/services/utils.js @@ -38,26 +38,6 @@ app.factory('$utils', function() { return dest; }, - // change time units - changeTime: function(time) { - time = parseFloat(time); - if (isNaN(time) || !isFinite(time)) return " infinite"; - if (!time) return " infinite"; - if (time < 60) return time.toFixed(2) + " s"; - else if (time < 60*60) return (time/60).toFixed(2) + " min"; - else if (time < 60*60*24) return (time/(60*60)).toFixed(2) + " hours"; - else return (time/(60*60*24)).toFixed(2) + " days!"; - }, - - // change length units - changeLength: function(len, pref) { - len = parseFloat(len); - if(len <= (1<<10)) return len.toFixed(1) + " " + pref; - else if(len <= (1<<20)) return (len/(1<<10)).toFixed(1) + " K" + pref; - else if(len <= (1<<30)) return (len/(1<<20)).toFixed(1) + " M" + pref; - else return (len/(1<<30)).toFixed(1) + " G" + pref; - }, - // get download chunks from aria2 bitfield getChunksFromHex: function(bitfield, numOfPieces) { var chunks = [], len = 0, numPieces = parseInt(numOfPieces);