Skip to content

Commit

Permalink
refactor(ng-repeat): refactor fix for dart-archive#1015
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jun 4, 2014
1 parent e131da1 commit 747d5fa
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions lib/directive/ng_repeat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,24 @@ class NgRepeat {
_watch = _scope.watch(
_listExpr,
(CollectionChangeRecord changes, _) {
_onChange((changes is CollectionChangeRecord) ? changes : null);
if (changes is CollectionChangeRecord) {
_onCollectionChange(changes);
} else if (_rows != null) {
_rows.forEach((row) {
row.scope.destroy();
_viewPort.remove(row.view);
});
_rows = null;
}
},
collection: true,
formatters: formatters
);
}

// Computes and executes DOM changes when the item list changes
void _onChange(CollectionChangeRecord changes) {
final iterable = (changes == null) ? const [] : changes.iterable;
final int length = (changes == null) ? 0 : changes.length;
void _onCollectionChange(CollectionChangeRecord changes) {
final int length = changes.length;
final rows = new List<_Row>(length);
final changeFunctions = new List<Function>(length);
final removedIndexes = <int>[];
Expand Down Expand Up @@ -170,51 +177,43 @@ class NgRepeat {
_rows = new List<_Row>(length);
for (var i = 0; i < length; i++) {
changeFunctions[i] = (index, previousView) {
addRow(index, iterable.elementAt(i), previousView);
addRow(index, changes.iterable.elementAt(i), previousView);
};
}
} else {
if (changes == null) {
_rows.forEach((row) {
row.scope.destroy();
_viewPort.remove(row.view);
});
leftInDom.clear();
} else {
changes.forEachRemoval((CollectionChangeItem removal) {
var index = removal.previousIndex;
var row = _rows[index];
row.scope.destroy();
_viewPort.remove(row.view);
leftInDom.removeAt(domLength - 1 - index);
});
changes.forEachRemoval((CollectionChangeItem removal) {
var index = removal.previousIndex;
var row = _rows[index];
row.scope.destroy();
_viewPort.remove(row.view);
leftInDom.removeAt(domLength - 1 - index);
});

changes.forEachAddition((CollectionChangeItem addition) {
changeFunctions[addition.currentIndex] = (index, previousView) {
addRow(index, addition.item, previousView);
};
});
changes.forEachAddition((CollectionChangeItem addition) {
changeFunctions[addition.currentIndex] = (index, previousView) {
addRow(index, addition.item, previousView);
};
});

changes.forEachMove((CollectionChangeItem move) {
var previousIndex = move.previousIndex;
var value = move.item;
changeFunctions[move.currentIndex] = (index, previousView) {
var previousRow = _rows[previousIndex];
var childScope = previousRow.scope;
var childContext = _updateContext(childScope.context, index, length);
if (!identical(childScope.context[_valueIdentifier], value)) {
childContext[_valueIdentifier] = value;
}
rows[index] = _rows[previousIndex];
// Only move the DOM node when required
if (domIndex < 0 || leftInDom[domIndex] != previousIndex) {
_viewPort.move(previousRow.view, moveAfter: previousView);
leftInDom.remove(previousIndex);
}
domIndex--;
};
});
}
changes.forEachMove((CollectionChangeItem move) {
var previousIndex = move.previousIndex;
var value = move.item;
changeFunctions[move.currentIndex] = (index, previousView) {
var previousRow = _rows[previousIndex];
var childScope = previousRow.scope;
var childContext = _updateContext(childScope.context, index, length);
if (!identical(childScope.context[_valueIdentifier], value)) {
childContext[_valueIdentifier] = value;
}
rows[index] = _rows[previousIndex];
// Only move the DOM node when required
if (domIndex < 0 || leftInDom[domIndex] != previousIndex) {
_viewPort.move(previousRow.view, moveAfter: previousView);
leftInDom.remove(previousIndex);
}
domIndex--;
};
});
}

var previousView = null;
Expand Down

0 comments on commit 747d5fa

Please sign in to comment.