2013-01-21 15:07:55 +01:00
|
|
|
var draw = function(canvas, chunks, fillStyle) {
|
|
|
|
chunks = chunks || [];
|
|
|
|
if (!canvas.getContext) {
|
|
|
|
console.log('use chunk bar on an canvas implementation!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
ctx.fillStyle = fillStyle || "#149BDF";
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
var x = 0,
|
|
|
|
width = canvas.width,
|
|
|
|
height = canvas.height;
|
|
|
|
chunks.forEach(function(c) {
|
|
|
|
var dx = c.ratio * width;
|
|
|
|
if (c.show)
|
|
|
|
ctx.fillRect(x, 0, dx, height);
|
|
|
|
x += dx;
|
|
|
|
});
|
|
|
|
};
|
2013-01-28 15:18:21 +01:00
|
|
|
// put chunkbar and bitfield attributes in a canvas element
|
|
|
|
// to use the directive, draw is optional and canvas is
|
|
|
|
// only drawn when it is true if given
|
2013-01-29 13:47:40 +01:00
|
|
|
angular
|
|
|
|
.module('webui.directives.chunkbar', ['webui.services.utils'])
|
|
|
|
.directive('chunkbar', ['$utils', function(utils) {
|
2013-01-21 15:07:55 +01:00
|
|
|
return function(scope, elem, attrs) {
|
2013-01-28 15:18:21 +01:00
|
|
|
var bitfield = "", pieces = 0, canDraw = true;
|
2013-01-21 15:07:55 +01:00
|
|
|
var update = function() {
|
2013-01-28 15:18:21 +01:00
|
|
|
if (canDraw)
|
|
|
|
draw(elem[0], utils.getChunksFromHex(bitfield, pieces), attrs.fillStyle);
|
2013-01-21 15:07:55 +01:00
|
|
|
};
|
|
|
|
scope.$watch(attrs.bitfield, function(bf) {
|
|
|
|
bitfield = bf;
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
scope.$watch(attrs.pieces, function(p) {
|
|
|
|
pieces = p;
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
|
2013-01-28 15:18:21 +01:00
|
|
|
if (attrs.draw) {
|
|
|
|
scope.$watch(attrs.draw, function(val) {
|
|
|
|
canDraw = val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-21 15:07:55 +01:00
|
|
|
update();
|
|
|
|
};
|
|
|
|
}]);
|