Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions quick-ng-repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ angular.module('QuickList').directive('quickNgRepeat',
}

return objType + ':' + key;
};
}

function isWindow(obj) {
return obj && obj.document && obj.location && obj.alert && obj.setInterval;
};
}

function nextUid() {
var index = uid.length;
Expand All @@ -56,7 +56,7 @@ angular.module('QuickList').directive('quickNgRepeat',
}
uid.unshift('0');
return uid.join('');
};
}

function isArrayLike(obj) {
if (obj == null || isWindow(obj)) {
Expand All @@ -72,7 +72,7 @@ angular.module('QuickList').directive('quickNgRepeat',
return angular.isArray(obj) || !angular.isFunction(obj) && (
length === 0 || typeof length === "number" && length > 0 && (length - 1) in obj
);
};
}


return {
Expand All @@ -99,18 +99,18 @@ angular.module('QuickList').directive('quickNgRepeat',
trackByExpGetter = $parse(trackByExp);
trackByIdFn = function(key, value, index) {
// assign key, value, and $index to the locals so that they can be used in hash functions
if (keyIdentifier) hashFnLocals[keyIdentifier] = key;
if (keyIdentifier) {hashFnLocals[keyIdentifier] = key;}
hashFnLocals[valueIdentifier] = value;
hashFnLocals.$index = index;
return trackByExpGetter($scope, hashFnLocals);
};
} else {
trackByIdArrayFn = function(key, value) {
return hashKey(value);
}
};
trackByIdObjFn = function(key) {
return key;
}
};
}

match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);
Expand Down Expand Up @@ -166,20 +166,23 @@ angular.module('QuickList').directive('quickNgRepeat',

// locate existing items
length = nextBlockOrder.length = collectionKeys.length;

var lastBlockMapFn = function(block) {
if (block && block.startNode) {lastBlockMap[block.id] = block;}
};

for(index = 0; index < length; index++) {
key = (collection === collectionKeys) ? index : collectionKeys[index];
value = collection[key];
trackById = trackByIdFn(key, value, index);
if(lastBlockMap.hasOwnProperty(trackById)) {
block = lastBlockMap[trackById]
block = lastBlockMap[trackById];
delete lastBlockMap[trackById];
nextBlockMap[trackById] = block;
nextBlockOrder[index] = block;
} else if (nextBlockMap.hasOwnProperty(trackById)) {
// restore lastBlockMap
angular.forEach(nextBlockOrder, function(block) {
if (block && block.startNode) lastBlockMap[block.id] = block;
});
angular.forEach(nextBlockOrder, lastBlockMapFn);
// This is a duplicate and we need to throw an error
throw ngRepeatMinErr('dupes', "Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}",
expression, trackById);
Expand All @@ -190,15 +193,30 @@ angular.module('QuickList').directive('quickNgRepeat',
}
}

var setNgRemovedTrue = function setNgRemovedTrue(element) {
element[NG_REMOVED] = true;
};

// remove existing items
for (key in lastBlockMap) {
if (lastBlockMap.hasOwnProperty(key)) {
block = lastBlockMap[key];
$animate.leave(block.elements);
angular.forEach(block.elements, function(element) { element[NG_REMOVED] = true});
angular.forEach(block.elements, setNgRemovedTrue);
block.scope.$destroy();
}
}


var registerBlock = function registerBlock(clone) {
$animate.enter(clone, null, angular.element(previousNode));
previousNode = clone;
block.scope = childScope;
block.startNode = clone[0];
block.elements = clone;
block.endNode = clone[clone.length - 1];
nextBlockMap[block.id] = block;
};

// we are not using forEach for perf reasons (trying to avoid #call)
for (index = 0, length = collectionKeys.length; index < length; index++) {
Expand Down Expand Up @@ -229,23 +247,15 @@ angular.module('QuickList').directive('quickNgRepeat',
}

childScope[valueIdentifier] = value;
if (keyIdentifier) childScope[keyIdentifier] = key;
if (keyIdentifier) {childScope[keyIdentifier] = key;}
childScope.$index = index;
childScope.$first = (index === 0);
childScope.$last = (index === (arrayLength - 1));
childScope.$middle = !(childScope.$first || childScope.$last);
childScope.$odd = !(childScope.$even = index%2==0);
childScope.$odd = !(childScope.$even = index%2 === 0);

if (!block.startNode) {
linker(childScope, function(clone) {
$animate.enter(clone, null, angular.element(previousNode));
previousNode = clone;
block.scope = childScope;
block.startNode = clone[0];
block.elements = clone;
block.endNode = clone[clone.length - 1];
nextBlockMap[block.id] = block;
});
linker(childScope, registerBlock);

if ($rootScope.$$phase !== '$digest' && childScope.$$phase !== '$digest'){
childScope.$digest();
Expand All @@ -259,4 +269,4 @@ angular.module('QuickList').directive('quickNgRepeat',
};
}]

);
);