pieceBar.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').directive('ngPieceBar', ['aria2TaskService', function (aria2TaskService) {
  4. return {
  5. restrict: 'E',
  6. template: '<canvas class="piece-bar progress"></canvas>',
  7. replace: true,
  8. scope: {
  9. bitField: '=',
  10. pieceCount: '=',
  11. color: '@'
  12. },
  13. link: function (scope, element) {
  14. var redraw = function () {
  15. var canvas = element[0];
  16. var combinedPieces = aria2TaskService.getCombinedPieces(scope.bitField, scope.pieceCount);
  17. var context = canvas.getContext('2d');
  18. context.fillStyle = scope.color || '#000';
  19. context.clearRect(0, 0, canvas.width, canvas.height);
  20. var posX = 0;
  21. var width = canvas.width;
  22. var height = canvas.height;
  23. for (var i = 0; i < combinedPieces.length; i++) {
  24. var piece = combinedPieces[i];
  25. var pieceWidth = piece.count / scope.pieceCount * width;
  26. if (piece.isCompleted) {
  27. context.fillRect(posX, 0, pieceWidth, height);
  28. }
  29. posX += pieceWidth;
  30. }
  31. };
  32. scope.$watch('bitField', function () {
  33. redraw();
  34. });
  35. scope.$watch('pieceNumber', function () {
  36. redraw();
  37. });
  38. }
  39. };
  40. }]);
  41. }());