Skip to content

Commit 6020363

Browse files
committed
fixed dupes error angular-ui-tree#101
1 parent fb179e3 commit 6020363

File tree

13 files changed

+120
-90
lines changed

13 files changed

+120
-90
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-ui-tree",
3-
"version": "2.0.8",
3+
"version": "2.0.9",
44
"homepage": "https://github.com/JimLiu/angular-ui-tree",
55
"authors": [
66
"Jim Liu <https://github.com/JimLiu>",

demo/dist/angular-ui-tree.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @license Angular UI Tree v2.0.8
2+
* @license Angular UI Tree v2.0.9
33
* (c) 2010-2014. https://github.com/JimLiu/angular-ui-tree
44
* License: MIT
55
*/
@@ -74,7 +74,7 @@
7474
nodesScope: node.$parentNodesScope,
7575
},
7676
index: node.index(),
77-
siblings: node.$parentNodesScope.$nodes.slice(0),
77+
siblings: node.siblings().slice(0),
7878
parent: node.$parentNodesScope,
7979

8080
moveTo: function(parent, siblings, index) { // Move the node to a new position
@@ -302,9 +302,10 @@
302302
};
303303

304304
var collapseOrExpand = function(scope, collapsed) {
305-
for (var i = 0; i < scope.$nodes.length; i++) {
306-
collapsed ? scope.$nodes[i].collapse() : scope.$nodes[i].expand();
307-
var subScope = scope.$nodes[i].$childNodesScope;
305+
var nodes = scope.childNodes();
306+
for (var i = 0; i < nodes.length; i++) {
307+
collapsed ? nodes[i].collapse() : nodes[i].expand();
308+
var subScope = nodes[i].$childNodesScope;
308309
if (subScope) {
309310
collapseOrExpand(subScope, collapsed);
310311
}
@@ -328,31 +329,28 @@
328329

329330
angular.module('ui.tree')
330331

331-
.controller('TreeNodesController', ['$scope', '$element', 'treeConfig',
332-
function ($scope, $element, treeConfig) {
332+
.controller('TreeNodesController', ['$scope', '$element', '$timeout', 'treeConfig',
333+
function ($scope, $element, $timeout, treeConfig) {
333334
this.scope = $scope;
334335

335336
$scope.$element = $element;
336337
$scope.$modelValue = null;
337-
$scope.$nodes = []; // sub nodes
338338
$scope.$nodeScope = null; // the scope of node which the nodes belongs to
339339
$scope.$treeScope = null;
340340
$scope.$type = 'uiTreeNodes';
341+
$scope.$nodesMap = {};
341342

342343
$scope.nodrop = false;
343344
$scope.maxDepth = 0;
344345

345346
$scope.initSubNode = function(subNode) {
346-
$scope.$nodes.splice(subNode.index(), 0, subNode);
347+
$timeout(function() {
348+
$scope.$nodesMap[subNode.$modelValue.$$hashKey] = subNode;
349+
});
347350
};
348351

349-
$scope.reinitNodes = function() {
350-
var nodes = $scope.$nodes.splice(0);
351-
$scope.$nodes = [];
352-
for (var i = 0; i < nodes.length; i++) {
353-
var node = nodes[i];
354-
$scope.$nodes.splice(node.index(), 0, node);
355-
}
352+
$scope.destroySubNode = function(subNode) {
353+
$scope.$nodesMap[subNode.$modelValue.$$hashKey] = null;
356354
};
357355

358356
$scope.accept = function(sourceNode, destIndex) {
@@ -364,7 +362,7 @@
364362
};
365363

366364
$scope.hasChild = function() {
367-
return $scope.$nodes.length > 0;
365+
return $scope.$modelValue.length > 0;
368366
};
369367

370368
$scope.safeApply = function(fn) {
@@ -379,11 +377,10 @@
379377
};
380378

381379
$scope.removeNode = function(node) {
382-
var index = $scope.$nodes.indexOf(node);
380+
var index = $scope.$modelValue.indexOf(node.$modelValue);
383381
if (index > -1) {
384382
$scope.safeApply(function() {
385383
$scope.$modelValue.splice(index, 1)[0];
386-
$scope.$nodes.splice(index, 1)[0];
387384
});
388385
return node;
389386
}
@@ -396,6 +393,13 @@
396393
});
397394
};
398395

396+
$scope.childNodes = function() {
397+
var nodes = [];
398+
for (var i = 0; i < $scope.$modelValue.length; i++) {
399+
nodes.push($scope.$nodesMap[$scope.$modelValue[i].$$hashKey]);
400+
}
401+
return nodes;
402+
};
399403

400404
$scope.depth = function() {
401405
if ($scope.$nodeScope) {
@@ -433,7 +437,7 @@
433437
$scope.$treeScope = null; // uiTree scope
434438
$scope.$handleScope = null; // it's handle scope
435439
$scope.$type = 'uiTreeNode';
436-
$scope.$$apply = false; //
440+
$scope.$$apply = false; //
437441

438442
$scope.collapsed = false;
439443

@@ -449,6 +453,7 @@
449453
treeNodesCtrl.scope.initSubNode($scope); // init sub nodes
450454

451455
$element.on('$destroy', function() {
456+
treeNodesCtrl.scope.destroySubNode($scope); // destroy sub nodes
452457
});
453458
};
454459

@@ -478,7 +483,7 @@
478483
};
479484

480485
$scope.siblings = function() {
481-
return $scope.$parentNodesScope.$nodes;
486+
return $scope.$parentNodesScope.childNodes();
482487
};
483488

484489
$scope.childNodesCount = function() {
@@ -490,7 +495,7 @@
490495
};
491496

492497
$scope.childNodes = function() {
493-
return $scope.$childNodesScope ? $scope.$childNodesScope.$nodes : null;
498+
return $scope.$childNodesScope ? $scope.$childNodesScope.childNodes() : null;
494499
};
495500

496501
$scope.accept = function(sourceNode, destIndex) {
@@ -524,8 +529,9 @@
524529
var subDepth = 0;
525530
var countSubDepth = function(scope) {
526531
var count = 0;
527-
for (var i = 0; i < scope.$nodes.length; i++) {
528-
var childNodes = scope.$nodes[i].$childNodesScope;
532+
var nodes = scope.childNodes();
533+
for (var i = 0; i < nodes.length; i++) {
534+
var childNodes = nodes[i].$childNodesScope;
529535
if (childNodes) {
530536
count = 1;
531537
countSubDepth(childNodes);
@@ -690,11 +696,15 @@
690696
scope.$treeScope = treeCtrl.scope;
691697

692698
if (ngModel) {
693-
scope.$watch(attrs.ngModel, function() {
699+
ngModel.$render = function() {
694700
scope.$modelValue = ngModel.$modelValue;
695-
scope.reinitNodes(); // we have to keep syncing with $nodes array
696-
}, true);
701+
};
697702
}
703+
/*
704+
scope.$watch(attrs.ngModel, function() {
705+
scope.$modelValue = ngModel.$modelValue;
706+
}, true);
707+
*/
698708

699709
scope.$watch(function() {
700710
return scope.$eval(attrs.maxDepth);
@@ -923,7 +933,7 @@
923933
treeScope = targetNode;
924934
if (targetNode.$nodesScope.accept(scope, 0)) {
925935
targetNode.place(placeElm);
926-
dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.$nodes, 0);
936+
dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.childNodes(), 0);
927937
}
928938
} else if (targetNode.dragEnabled()){ // drag enabled
929939
targetElm = targetNode.$element; // Get the element of ui-tree-node

demo/dist/angular-ui-tree.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-ui-tree.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @license Angular UI Tree v2.0.8
2+
* @license Angular UI Tree v2.0.9
33
* (c) 2010-2014. https://github.com/JimLiu/angular-ui-tree
44
* License: MIT
55
*/
@@ -74,7 +74,7 @@
7474
nodesScope: node.$parentNodesScope,
7575
},
7676
index: node.index(),
77-
siblings: node.$parentNodesScope.$nodes.slice(0),
77+
siblings: node.siblings().slice(0),
7878
parent: node.$parentNodesScope,
7979

8080
moveTo: function(parent, siblings, index) { // Move the node to a new position
@@ -302,9 +302,10 @@
302302
};
303303

304304
var collapseOrExpand = function(scope, collapsed) {
305-
for (var i = 0; i < scope.$nodes.length; i++) {
306-
collapsed ? scope.$nodes[i].collapse() : scope.$nodes[i].expand();
307-
var subScope = scope.$nodes[i].$childNodesScope;
305+
var nodes = scope.childNodes();
306+
for (var i = 0; i < nodes.length; i++) {
307+
collapsed ? nodes[i].collapse() : nodes[i].expand();
308+
var subScope = nodes[i].$childNodesScope;
308309
if (subScope) {
309310
collapseOrExpand(subScope, collapsed);
310311
}
@@ -328,31 +329,28 @@
328329

329330
angular.module('ui.tree')
330331

331-
.controller('TreeNodesController', ['$scope', '$element', 'treeConfig',
332-
function ($scope, $element, treeConfig) {
332+
.controller('TreeNodesController', ['$scope', '$element', '$timeout', 'treeConfig',
333+
function ($scope, $element, $timeout, treeConfig) {
333334
this.scope = $scope;
334335

335336
$scope.$element = $element;
336337
$scope.$modelValue = null;
337-
$scope.$nodes = []; // sub nodes
338338
$scope.$nodeScope = null; // the scope of node which the nodes belongs to
339339
$scope.$treeScope = null;
340340
$scope.$type = 'uiTreeNodes';
341+
$scope.$nodesMap = {};
341342

342343
$scope.nodrop = false;
343344
$scope.maxDepth = 0;
344345

345346
$scope.initSubNode = function(subNode) {
346-
$scope.$nodes.splice(subNode.index(), 0, subNode);
347+
$timeout(function() {
348+
$scope.$nodesMap[subNode.$modelValue.$$hashKey] = subNode;
349+
});
347350
};
348351

349-
$scope.reinitNodes = function() {
350-
var nodes = $scope.$nodes.splice(0);
351-
$scope.$nodes = [];
352-
for (var i = 0; i < nodes.length; i++) {
353-
var node = nodes[i];
354-
$scope.$nodes.splice(node.index(), 0, node);
355-
}
352+
$scope.destroySubNode = function(subNode) {
353+
$scope.$nodesMap[subNode.$modelValue.$$hashKey] = null;
356354
};
357355

358356
$scope.accept = function(sourceNode, destIndex) {
@@ -364,7 +362,7 @@
364362
};
365363

366364
$scope.hasChild = function() {
367-
return $scope.$nodes.length > 0;
365+
return $scope.$modelValue.length > 0;
368366
};
369367

370368
$scope.safeApply = function(fn) {
@@ -379,11 +377,10 @@
379377
};
380378

381379
$scope.removeNode = function(node) {
382-
var index = $scope.$nodes.indexOf(node);
380+
var index = $scope.$modelValue.indexOf(node.$modelValue);
383381
if (index > -1) {
384382
$scope.safeApply(function() {
385383
$scope.$modelValue.splice(index, 1)[0];
386-
$scope.$nodes.splice(index, 1)[0];
387384
});
388385
return node;
389386
}
@@ -396,6 +393,13 @@
396393
});
397394
};
398395

396+
$scope.childNodes = function() {
397+
var nodes = [];
398+
for (var i = 0; i < $scope.$modelValue.length; i++) {
399+
nodes.push($scope.$nodesMap[$scope.$modelValue[i].$$hashKey]);
400+
}
401+
return nodes;
402+
};
399403

400404
$scope.depth = function() {
401405
if ($scope.$nodeScope) {
@@ -433,7 +437,7 @@
433437
$scope.$treeScope = null; // uiTree scope
434438
$scope.$handleScope = null; // it's handle scope
435439
$scope.$type = 'uiTreeNode';
436-
$scope.$$apply = false; //
440+
$scope.$$apply = false; //
437441

438442
$scope.collapsed = false;
439443

@@ -449,6 +453,7 @@
449453
treeNodesCtrl.scope.initSubNode($scope); // init sub nodes
450454

451455
$element.on('$destroy', function() {
456+
treeNodesCtrl.scope.destroySubNode($scope); // destroy sub nodes
452457
});
453458
};
454459

@@ -478,7 +483,7 @@
478483
};
479484

480485
$scope.siblings = function() {
481-
return $scope.$parentNodesScope.$nodes;
486+
return $scope.$parentNodesScope.childNodes();
482487
};
483488

484489
$scope.childNodesCount = function() {
@@ -490,7 +495,7 @@
490495
};
491496

492497
$scope.childNodes = function() {
493-
return $scope.$childNodesScope ? $scope.$childNodesScope.$nodes : null;
498+
return $scope.$childNodesScope ? $scope.$childNodesScope.childNodes() : null;
494499
};
495500

496501
$scope.accept = function(sourceNode, destIndex) {
@@ -524,8 +529,9 @@
524529
var subDepth = 0;
525530
var countSubDepth = function(scope) {
526531
var count = 0;
527-
for (var i = 0; i < scope.$nodes.length; i++) {
528-
var childNodes = scope.$nodes[i].$childNodesScope;
532+
var nodes = scope.childNodes();
533+
for (var i = 0; i < nodes.length; i++) {
534+
var childNodes = nodes[i].$childNodesScope;
529535
if (childNodes) {
530536
count = 1;
531537
countSubDepth(childNodes);
@@ -690,11 +696,15 @@
690696
scope.$treeScope = treeCtrl.scope;
691697

692698
if (ngModel) {
693-
scope.$watch(attrs.ngModel, function() {
699+
ngModel.$render = function() {
694700
scope.$modelValue = ngModel.$modelValue;
695-
scope.reinitNodes(); // we have to keep syncing with $nodes array
696-
}, true);
701+
};
697702
}
703+
/*
704+
scope.$watch(attrs.ngModel, function() {
705+
scope.$modelValue = ngModel.$modelValue;
706+
}, true);
707+
*/
698708

699709
scope.$watch(function() {
700710
return scope.$eval(attrs.maxDepth);
@@ -923,7 +933,7 @@
923933
treeScope = targetNode;
924934
if (targetNode.$nodesScope.accept(scope, 0)) {
925935
targetNode.place(placeElm);
926-
dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.$nodes, 0);
936+
dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.childNodes(), 0);
927937
}
928938
} else if (targetNode.dragEnabled()){ // drag enabled
929939
targetElm = targetNode.$element; // Get the element of ui-tree-node

dist/angular-ui-tree.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Angular-NestedSortable",
3-
"version": "2.0.8",
3+
"version": "2.0.9",
44
"dependencies": {
55
"grunt": "~0.4.2",
66
"grunt-contrib-jshint": "~0.8.0",

0 commit comments

Comments
 (0)