(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) {
                var redraw = function () {
                    var pieces = aria2TaskService.getPieceStatus(scope.bitField, scope.pieceCount);
                    element.empty();

                    var pieceCount = Math.max(1, pieces.length);

                    for (var i = 0; i < pieceCount; i++) {
                        element.append('<div class="piece' + (pieces[i] ? ' piece-completed' : '') + '"></div>');
                    }
                };

                scope.$watch('bitField', function () {
                    redraw();
                });

                scope.$watch('pieceNumber', function () {
                    redraw();
                });
            }
        };
    }]);
})();

@@ -398,24 +398,103 @@
  element) %7B%0A
+                var pieces = %5B%5D;%0A                var currentPieceStatus = %5B%5D;%0A%0A
             
@@ -540,33 +540,41 @@
             
-var piece
+currentPieceStatu
 s = aria2Tas
@@ -632,16 +632,55 @@
 Count);%0A
+                    pieces.length = 0;%0A
         
@@ -762,24 +762,56 @@
 (1, 
-pieces.length);%0A
+currentPieceStatus.length);%0A                    
 %0A   
@@ -894,22 +894,35 @@
     
+var piece = angular.
 element
-.append
 ('%3Cd
@@ -941,21 +941,33 @@
 ece' + (
-piece
+currentPieceStatu
 s%5Bi%5D ? '
@@ -1017,33 +1017,1055 @@
                 
-%7D
+    pieces.push(piece);%0A                        element.append(piece);%0A                    %7D%0A                %7D;%0A%0A                var refresh = function () %7B%0A                    var newPieceStatus = aria2TaskService.getPieceStatus(scope.bitField, scope.pieceCount);%0A%0A                    if (!currentPieceStatus %7C%7C !newPieceStatus %7C%7C currentPieceStatus.length != newPieceStatus.length %7C%7C newPieceStatus.length != pieces.length) %7B%0A                        redraw();%0A                        return;%0A                    %7D%0A%0A                    for (var i = 0; i %3C pieces.length; i++) %7B%0A                        if (currentPieceStatus%5Bi%5D != newPieceStatus%5Bi%5D) %7B%0A                            if (newPieceStatus%5Bi%5D) %7B%0A                                pieces%5Bi%5D.addClass('piece-completed');%0A                            %7D else %7B%0A                                pieces%5Bi%5D.removeClass('piece-completed');%0A                            %7D%0A                        %7D%0A                    %7D%0A%0A                    currentPieceStatus = newPieceStatus;
 %0A               
@@ -2138,36 +2138,37 @@
               re
-draw
+fresh
 ();%0A            

improve piece map performance