2016-06-11 14:53:04 +02:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('ariaNg').directive('ngPieceMap', ['aria2TaskService', function (aria2TaskService) {
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
template: '<div class="piece-map"></div>',
|
|
|
|
replace: true,
|
|
|
|
scope: {
|
|
|
|
bitField: '=',
|
|
|
|
pieceCount: '='
|
|
|
|
},
|
|
|
|
link: function (scope, element) {
|
2016-06-24 17:25:11 +02:00
|
|
|
var pieces = [];
|
|
|
|
var currentPieceStatus = [];
|
|
|
|
|
2016-06-11 14:53:04 +02:00
|
|
|
var redraw = function () {
|
2016-06-24 17:25:11 +02:00
|
|
|
currentPieceStatus = aria2TaskService.getPieceStatus(scope.bitField, scope.pieceCount);
|
|
|
|
pieces.length = 0;
|
2016-06-11 14:53:04 +02:00
|
|
|
element.empty();
|
|
|
|
|
2016-06-24 17:25:11 +02:00
|
|
|
var pieceCount = Math.max(1, currentPieceStatus.length);
|
2016-07-04 16:59:07 +02:00
|
|
|
|
2016-06-23 16:48:35 +02:00
|
|
|
for (var i = 0; i < pieceCount; i++) {
|
2016-06-24 17:25:11 +02:00
|
|
|
var piece = angular.element('<div class="piece' + (currentPieceStatus[i] ? ' piece-completed' : '') + '"></div>');
|
|
|
|
pieces.push(piece);
|
|
|
|
element.append(piece);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var refresh = function () {
|
|
|
|
var newPieceStatus = aria2TaskService.getPieceStatus(scope.bitField, scope.pieceCount);
|
|
|
|
|
2016-08-01 19:26:10 +02:00
|
|
|
if (!currentPieceStatus || !newPieceStatus || currentPieceStatus.length !== newPieceStatus.length || newPieceStatus.length !== pieces.length) {
|
2016-06-24 17:25:11 +02:00
|
|
|
redraw();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < pieces.length; i++) {
|
2016-08-01 19:26:10 +02:00
|
|
|
if (currentPieceStatus[i] !== newPieceStatus[i]) {
|
2016-06-24 17:25:11 +02:00
|
|
|
if (newPieceStatus[i]) {
|
|
|
|
pieces[i].addClass('piece-completed');
|
|
|
|
} else {
|
|
|
|
pieces[i].removeClass('piece-completed');
|
|
|
|
}
|
|
|
|
}
|
2016-06-11 14:53:04 +02:00
|
|
|
}
|
2016-06-24 17:25:11 +02:00
|
|
|
|
|
|
|
currentPieceStatus = newPieceStatus;
|
2016-06-11 14:53:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
scope.$watch('bitField', function () {
|
2016-06-24 17:25:11 +02:00
|
|
|
refresh();
|
2016-06-11 14:53:04 +02:00
|
|
|
});
|
|
|
|
|
2016-07-04 16:59:07 +02:00
|
|
|
scope.$watch('pieceCount', function () {
|
2016-06-11 14:53:04 +02:00
|
|
|
redraw();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|
2016-08-01 16:49:16 +02:00
|
|
|
}());
|