|
56 | 56 | } |
57 | 57 | } |
58 | 58 |
|
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>'); |
60 | 60 | } |
61 | 61 |
|
62 | 62 | function findParentListItem (target) { |
|
183 | 183 | } else if (index === 0) { |
184 | 184 | listElem.prepend(itemElem); |
185 | 185 | } else { |
186 | | - listElem.eq(index - 1).after(itemElem); |
| 186 | + listElem.children().eq(index - 1).after(itemElem); |
187 | 187 | } |
188 | 188 | } |
189 | 189 |
|
190 | 190 | function loadTree (scope, tree, listElem, listWatch) { |
191 | | - scope.$watch(listWatch, function (newList, oldList, scope) { |
| 191 | + scope.$watch(listWatch, function (newList, oldList) { |
192 | 192 | if (typeof newList === 'undefined' || newList === null || newList.length === 0) { |
193 | 193 | listElem.children().remove(); |
194 | 194 | return; |
195 | 195 | } |
196 | 196 |
|
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(); |
200 | 199 |
|
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 | + } |
207 | 204 |
|
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()); |
211 | 208 |
|
212 | | - if (index >= listChildren.length) { |
213 | | - addListItem(scope, tree, listElem, item, -1); |
214 | | - return; |
| 209 | + if (childItem === item) { |
| 210 | + break; |
215 | 211 | } |
| 212 | + } |
216 | 213 |
|
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 | + }); |
222 | 220 |
|
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 | + } |
230 | 224 | }, true); |
231 | 225 | } |
232 | 226 |
|
|
0 commit comments