From 6090fdb7f55a5e85b02517772b13e0b9ed89f083 Mon Sep 17 00:00:00 2001 From: Luke Hritsko Date: Mon, 12 Jan 2015 17:27:35 -0500 Subject: [PATCH] Made quick-ng-repeat work with jshint --- quick-ng-repeat.js | 58 +++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/quick-ng-repeat.js b/quick-ng-repeat.js index 10108e4..136ef2f 100644 --- a/quick-ng-repeat.js +++ b/quick-ng-repeat.js @@ -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; @@ -56,7 +56,7 @@ angular.module('QuickList').directive('quickNgRepeat', } uid.unshift('0'); return uid.join(''); - }; + } function isArrayLike(obj) { if (obj == null || isWindow(obj)) { @@ -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 { @@ -99,7 +99,7 @@ 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); @@ -107,10 +107,10 @@ angular.module('QuickList').directive('quickNgRepeat', } else { trackByIdArrayFn = function(key, value) { return hashKey(value); - } + }; trackByIdObjFn = function(key) { return key; - } + }; } match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/); @@ -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); @@ -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++) { @@ -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(); @@ -259,4 +269,4 @@ angular.module('QuickList').directive('quickNgRepeat', }; }] -); +); \ No newline at end of file