Skip to content

Commit

Permalink
fixed dupes error #101
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLiu committed Apr 12, 2014
1 parent fb179e3 commit 6020363
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 90 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "angular-ui-tree",
"version": "2.0.8",
"version": "2.0.9",
"homepage": "https://github.com/JimLiu/angular-ui-tree",
"authors": [
"Jim Liu <https://github.com/JimLiu>",
Expand Down
66 changes: 38 additions & 28 deletions demo/dist/angular-ui-tree.js
@@ -1,5 +1,5 @@
/**
* @license Angular UI Tree v2.0.8
* @license Angular UI Tree v2.0.9
* (c) 2010-2014. https://github.com/JimLiu/angular-ui-tree
* License: MIT
*/
Expand Down Expand Up @@ -74,7 +74,7 @@
nodesScope: node.$parentNodesScope,
},
index: node.index(),
siblings: node.$parentNodesScope.$nodes.slice(0),
siblings: node.siblings().slice(0),
parent: node.$parentNodesScope,

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

var collapseOrExpand = function(scope, collapsed) {
for (var i = 0; i < scope.$nodes.length; i++) {
collapsed ? scope.$nodes[i].collapse() : scope.$nodes[i].expand();
var subScope = scope.$nodes[i].$childNodesScope;
var nodes = scope.childNodes();
for (var i = 0; i < nodes.length; i++) {
collapsed ? nodes[i].collapse() : nodes[i].expand();
var subScope = nodes[i].$childNodesScope;
if (subScope) {
collapseOrExpand(subScope, collapsed);
}
Expand All @@ -328,31 +329,28 @@

angular.module('ui.tree')

.controller('TreeNodesController', ['$scope', '$element', 'treeConfig',
function ($scope, $element, treeConfig) {
.controller('TreeNodesController', ['$scope', '$element', '$timeout', 'treeConfig',
function ($scope, $element, $timeout, treeConfig) {
this.scope = $scope;

$scope.$element = $element;
$scope.$modelValue = null;
$scope.$nodes = []; // sub nodes
$scope.$nodeScope = null; // the scope of node which the nodes belongs to
$scope.$treeScope = null;
$scope.$type = 'uiTreeNodes';
$scope.$nodesMap = {};

$scope.nodrop = false;
$scope.maxDepth = 0;

$scope.initSubNode = function(subNode) {
$scope.$nodes.splice(subNode.index(), 0, subNode);
$timeout(function() {
$scope.$nodesMap[subNode.$modelValue.$$hashKey] = subNode;
});
};

$scope.reinitNodes = function() {
var nodes = $scope.$nodes.splice(0);
$scope.$nodes = [];
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
$scope.$nodes.splice(node.index(), 0, node);
}
$scope.destroySubNode = function(subNode) {
$scope.$nodesMap[subNode.$modelValue.$$hashKey] = null;
};

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

$scope.hasChild = function() {
return $scope.$nodes.length > 0;
return $scope.$modelValue.length > 0;
};

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

$scope.removeNode = function(node) {
var index = $scope.$nodes.indexOf(node);
var index = $scope.$modelValue.indexOf(node.$modelValue);
if (index > -1) {
$scope.safeApply(function() {
$scope.$modelValue.splice(index, 1)[0];
$scope.$nodes.splice(index, 1)[0];
});
return node;
}
Expand All @@ -396,6 +393,13 @@
});
};

$scope.childNodes = function() {
var nodes = [];
for (var i = 0; i < $scope.$modelValue.length; i++) {
nodes.push($scope.$nodesMap[$scope.$modelValue[i].$$hashKey]);
}
return nodes;
};

$scope.depth = function() {
if ($scope.$nodeScope) {
Expand Down Expand Up @@ -433,7 +437,7 @@
$scope.$treeScope = null; // uiTree scope
$scope.$handleScope = null; // it's handle scope
$scope.$type = 'uiTreeNode';
$scope.$$apply = false; //
$scope.$$apply = false; //

$scope.collapsed = false;

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

$element.on('$destroy', function() {
treeNodesCtrl.scope.destroySubNode($scope); // destroy sub nodes
});
};

Expand Down Expand Up @@ -478,7 +483,7 @@
};

$scope.siblings = function() {
return $scope.$parentNodesScope.$nodes;
return $scope.$parentNodesScope.childNodes();
};

$scope.childNodesCount = function() {
Expand All @@ -490,7 +495,7 @@
};

$scope.childNodes = function() {
return $scope.$childNodesScope ? $scope.$childNodesScope.$nodes : null;
return $scope.$childNodesScope ? $scope.$childNodesScope.childNodes() : null;
};

$scope.accept = function(sourceNode, destIndex) {
Expand Down Expand Up @@ -524,8 +529,9 @@
var subDepth = 0;
var countSubDepth = function(scope) {
var count = 0;
for (var i = 0; i < scope.$nodes.length; i++) {
var childNodes = scope.$nodes[i].$childNodesScope;
var nodes = scope.childNodes();
for (var i = 0; i < nodes.length; i++) {
var childNodes = nodes[i].$childNodesScope;
if (childNodes) {
count = 1;
countSubDepth(childNodes);
Expand Down Expand Up @@ -690,11 +696,15 @@
scope.$treeScope = treeCtrl.scope;

if (ngModel) {
scope.$watch(attrs.ngModel, function() {
ngModel.$render = function() {
scope.$modelValue = ngModel.$modelValue;
scope.reinitNodes(); // we have to keep syncing with $nodes array
}, true);
};
}
/*
scope.$watch(attrs.ngModel, function() {
scope.$modelValue = ngModel.$modelValue;
}, true);
*/

scope.$watch(function() {
return scope.$eval(attrs.maxDepth);
Expand Down Expand Up @@ -923,7 +933,7 @@
treeScope = targetNode;
if (targetNode.$nodesScope.accept(scope, 0)) {
targetNode.place(placeElm);
dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.$nodes, 0);
dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.childNodes(), 0);
}
} else if (targetNode.dragEnabled()){ // drag enabled
targetElm = targetNode.$element; // Get the element of ui-tree-node
Expand Down
4 changes: 2 additions & 2 deletions demo/dist/angular-ui-tree.min.js

Large diffs are not rendered by default.

66 changes: 38 additions & 28 deletions dist/angular-ui-tree.js
@@ -1,5 +1,5 @@
/**
* @license Angular UI Tree v2.0.8
* @license Angular UI Tree v2.0.9
* (c) 2010-2014. https://github.com/JimLiu/angular-ui-tree
* License: MIT
*/
Expand Down Expand Up @@ -74,7 +74,7 @@
nodesScope: node.$parentNodesScope,
},
index: node.index(),
siblings: node.$parentNodesScope.$nodes.slice(0),
siblings: node.siblings().slice(0),
parent: node.$parentNodesScope,

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

var collapseOrExpand = function(scope, collapsed) {
for (var i = 0; i < scope.$nodes.length; i++) {
collapsed ? scope.$nodes[i].collapse() : scope.$nodes[i].expand();
var subScope = scope.$nodes[i].$childNodesScope;
var nodes = scope.childNodes();
for (var i = 0; i < nodes.length; i++) {
collapsed ? nodes[i].collapse() : nodes[i].expand();
var subScope = nodes[i].$childNodesScope;
if (subScope) {
collapseOrExpand(subScope, collapsed);
}
Expand All @@ -328,31 +329,28 @@

angular.module('ui.tree')

.controller('TreeNodesController', ['$scope', '$element', 'treeConfig',
function ($scope, $element, treeConfig) {
.controller('TreeNodesController', ['$scope', '$element', '$timeout', 'treeConfig',
function ($scope, $element, $timeout, treeConfig) {
this.scope = $scope;

$scope.$element = $element;
$scope.$modelValue = null;
$scope.$nodes = []; // sub nodes
$scope.$nodeScope = null; // the scope of node which the nodes belongs to
$scope.$treeScope = null;
$scope.$type = 'uiTreeNodes';
$scope.$nodesMap = {};

$scope.nodrop = false;
$scope.maxDepth = 0;

$scope.initSubNode = function(subNode) {
$scope.$nodes.splice(subNode.index(), 0, subNode);
$timeout(function() {
$scope.$nodesMap[subNode.$modelValue.$$hashKey] = subNode;
});
};

$scope.reinitNodes = function() {
var nodes = $scope.$nodes.splice(0);
$scope.$nodes = [];
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
$scope.$nodes.splice(node.index(), 0, node);
}
$scope.destroySubNode = function(subNode) {
$scope.$nodesMap[subNode.$modelValue.$$hashKey] = null;
};

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

$scope.hasChild = function() {
return $scope.$nodes.length > 0;
return $scope.$modelValue.length > 0;
};

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

$scope.removeNode = function(node) {
var index = $scope.$nodes.indexOf(node);
var index = $scope.$modelValue.indexOf(node.$modelValue);
if (index > -1) {
$scope.safeApply(function() {
$scope.$modelValue.splice(index, 1)[0];
$scope.$nodes.splice(index, 1)[0];
});
return node;
}
Expand All @@ -396,6 +393,13 @@
});
};

$scope.childNodes = function() {
var nodes = [];
for (var i = 0; i < $scope.$modelValue.length; i++) {
nodes.push($scope.$nodesMap[$scope.$modelValue[i].$$hashKey]);
}
return nodes;
};

$scope.depth = function() {
if ($scope.$nodeScope) {
Expand Down Expand Up @@ -433,7 +437,7 @@
$scope.$treeScope = null; // uiTree scope
$scope.$handleScope = null; // it's handle scope
$scope.$type = 'uiTreeNode';
$scope.$$apply = false; //
$scope.$$apply = false; //

$scope.collapsed = false;

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

$element.on('$destroy', function() {
treeNodesCtrl.scope.destroySubNode($scope); // destroy sub nodes
});
};

Expand Down Expand Up @@ -478,7 +483,7 @@
};

$scope.siblings = function() {
return $scope.$parentNodesScope.$nodes;
return $scope.$parentNodesScope.childNodes();
};

$scope.childNodesCount = function() {
Expand All @@ -490,7 +495,7 @@
};

$scope.childNodes = function() {
return $scope.$childNodesScope ? $scope.$childNodesScope.$nodes : null;
return $scope.$childNodesScope ? $scope.$childNodesScope.childNodes() : null;
};

$scope.accept = function(sourceNode, destIndex) {
Expand Down Expand Up @@ -524,8 +529,9 @@
var subDepth = 0;
var countSubDepth = function(scope) {
var count = 0;
for (var i = 0; i < scope.$nodes.length; i++) {
var childNodes = scope.$nodes[i].$childNodesScope;
var nodes = scope.childNodes();
for (var i = 0; i < nodes.length; i++) {
var childNodes = nodes[i].$childNodesScope;
if (childNodes) {
count = 1;
countSubDepth(childNodes);
Expand Down Expand Up @@ -690,11 +696,15 @@
scope.$treeScope = treeCtrl.scope;

if (ngModel) {
scope.$watch(attrs.ngModel, function() {
ngModel.$render = function() {
scope.$modelValue = ngModel.$modelValue;
scope.reinitNodes(); // we have to keep syncing with $nodes array
}, true);
};
}
/*
scope.$watch(attrs.ngModel, function() {
scope.$modelValue = ngModel.$modelValue;
}, true);
*/

scope.$watch(function() {
return scope.$eval(attrs.maxDepth);
Expand Down Expand Up @@ -923,7 +933,7 @@
treeScope = targetNode;
if (targetNode.$nodesScope.accept(scope, 0)) {
targetNode.place(placeElm);
dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.$nodes, 0);
dragInfo.moveTo(targetNode.$nodesScope, targetNode.$nodesScope.childNodes(), 0);
}
} else if (targetNode.dragEnabled()){ // drag enabled
targetElm = targetNode.$element; // Get the element of ui-tree-node
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-ui-tree.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "Angular-NestedSortable",
"version": "2.0.8",
"version": "2.0.9",
"dependencies": {
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.8.0",
Expand Down

0 comments on commit 6020363

Please sign in to comment.