Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit 2b7d6a8

Browse files
committed
Multiple fixes for list view update and default template
1 parent 84aef09 commit 2b7d6a8

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

angular.tree.js

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
}
5757
}
5858

59-
return itemTemplate || angular.element('<li class="ng-tree-node"><div>{{item}}</div></li>');
59+
return itemTemplate || angular.element('<li class="ng-tree-node"><div>{{item}}</div><ul></ul></li>');
6060
}
6161

6262
function findParentListItem (target) {
@@ -183,50 +183,44 @@
183183
} else if (index === 0) {
184184
listElem.prepend(itemElem);
185185
} else {
186-
listElem.eq(index - 1).after(itemElem);
186+
listElem.children().eq(index - 1).after(itemElem);
187187
}
188188
}
189189

190190
function loadTree (scope, tree, listElem, listWatch) {
191-
scope.$watch(listWatch, function (newList, oldList, scope) {
191+
scope.$watch(listWatch, function (newList, oldList) {
192192
if (typeof newList === 'undefined' || newList === null || newList.length === 0) {
193193
listElem.children().remove();
194194
return;
195195
}
196196

197-
// Remove list DOM elements that do not exist in the new list data
198-
(function() {
199-
var listChildren = listElem.children();
197+
angular.forEach(newList, function (item, itemIndex) {
198+
var listChildElems = listElem.children();
200199

201-
angular.forEach(listChildren, function (child) {
202-
if (newList.indexOf(tree.getItem(child.scope())) < 0) {
203-
child.remove();
204-
}
205-
});
206-
})();
200+
if (itemIndex >= listChildElems.length) {
201+
addListItem(scope, tree, listElem, item, -1);
202+
return;
203+
}
207204

208-
(function () {
209-
angular.forEach(newList, function (item, index) {
210-
var listChildren = listElem.children(); // TODO don't do this ever loop iteration
205+
for (var childElemIndex = itemIndex; childElemIndex < listChildElems.length; childElemIndex += 1) {
206+
var childElem = angular.element(listChildElems[childElemIndex]);
207+
var childItem = tree.getItem(childElem.scope());
211208

212-
if (index >= listChildren.length) {
213-
addListItem(scope, tree, listElem, item, -1);
214-
return;
209+
if (childItem === item) {
210+
break;
215211
}
212+
}
216213

217-
for (var i = index; i < listChildren.length; i += 1) {
218-
if (tree.getItem(angular.element(listChildren[i]).scope()) === item) {
219-
break;
220-
}
221-
}
214+
if (childElemIndex >= listChildElems.length) {
215+
addListItem(scope, tree, listElem, item, itemIndex);
216+
} else if (childElemIndex !== itemIndex) {
217+
insertListItem(listElem, listChildElems[childElemIndex], itemIndex);
218+
}
219+
});
222220

223-
if (i >= listChildren.length) {
224-
addListItem(scope, tree, listElem, item, index);
225-
} else if (i !== index) {
226-
insertListItem(listElem, listChildren[i], index);
227-
}
228-
});
229-
})();
221+
while (listElem.children().length > newList.length) {
222+
listElem.children().eq(newList.length).remove();
223+
}
230224
}, true);
231225
}
232226

0 commit comments

Comments
 (0)