Skip to content

Instantly share code, notes, and snippets.

@mikemilano
Created August 11, 2014 03:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemilano/70fc126b6d8175c505ff to your computer and use it in GitHub Desktop.
Save mikemilano/70fc126b6d8175c505ff to your computer and use it in GitHub Desktop.
Multi-download progress with AngularJS running in Node Webkit
angular.module('kalabox.dprogress', [])
.directive('dprogress', ['_', function (_) {
var bar_data = [];
function makeChart() {
var chart = d3.select('#chart')
.append("div").attr("class", "chart")
.selectAll('div')
.data(bar_data)
.enter()
.append("div")
.transition()
.style("width", function(d) { return d.progress.percent + "%"; })
.text(function(d) { return d.progress.percent + "%"; });
}
function updateChart() {
console.log(bar_data);
var chart = d3.selectAll('#chart');
bars = chart.selectAll('.chart div')
.data(bar_data)
.transition()
.duration(300)
.style("width", function(d) { return d.progress.percent + "%"; })
.text(function(d) { return d.progress.percent + "%"; });
}
return {
restrict: 'E',
scope: { data: "=ngModel" },
template: '<div id="chart"></div>',
link: function (scope, element, attrs) {
bar_data = scope.data;
makeChart();
scope.$watch('data', _.throttle(function(newVal) {
bar_data = newVal;
updateChart();
}, 500), true);
}
};
}]);
<h1>Kalabox Installer</h1>
<h2>Downloading Components</h2>
<dprogress ng-model="dependencies"></dprogress>
angular.module('kalabox.install', [])
.config(function ($routeProvider) {
$routeProvider.when('/install', {
templateUrl: 'modules/install/install.html',
controller: 'InstallCtrl'
});
})
.controller('InstallCtrl', ['$scope', '_', function($scope, _) {
$scope.dependencies = [
{
name: 'Virtual Box',
filename: 'virtualbox.dmg',
http_options: {
host: 'files.kalamuna.com',
path: '/virtualbox-macosx-4.3.6.dmg'
},
progress: {
downloaded: 0,
total: 0,
percent: 0
}
},
{
name: 'Boot2Docker',
filename: 'boot2docker.pkg',
http_options: {
host: 'files.kalamuna.com',
path: '/boot2docker-macosx-1.1.1.pkg'
},
progress: {
downloaded: 0,
total: 0,
percent: 0
}
}
];
var http = require('http');
var fs = require('fs');
var mbbytes = 1048576;
var download_dir = './downloads';
if (!fs.existsSync(download_dir)) {
fs.mkdir(download_dir);
}
angular.forEach($scope.dependencies, function(dep, key) {
var filename = dep.http_options.path.split('/').pop();
var file = fs.createWriteStream(download_dir + '/' + filename);
http.get(dep.http_options, function(res) {
var len = parseInt(res.headers['content-length'], 10);
//var body = "";
var cur = 0;
var total = len / mbbytes;
$scope.dependencies[key].progress.total = total.toFixed(2);
res.on('data', function(data) {
cur += data.length;
file.write(data);
$scope.$apply(function(){
$scope.dependencies[key].progress.current = (cur / mbbytes).toFixed(2);
$scope.dependencies[key].progress.percent = parseInt(100.0 * cur / len);
});
});
res.on('end', function(){
file.end();
console.log(filename + ' downloaded to ' + download_dir);
});
});
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment