|
@@ -7,7 +7,8 @@ var idTree = {
|
|
|
id: 'id',
|
|
|
pid: 'pid',
|
|
|
nid: 'nid',
|
|
|
- rootId: -1
|
|
|
+ rootId: -1,
|
|
|
+ autoUpdate: false
|
|
|
};
|
|
|
|
|
|
var _eventType = {
|
|
@@ -65,13 +66,14 @@ var idTree = {
|
|
|
nodes[i].nextSibling = (i === nodes.length - 1) ? null : nodes[i + 1];
|
|
|
}
|
|
|
},
|
|
|
- // 在nodes中,从iIndex(包括)开始全部移除
|
|
|
+ // 在nodes中,从iIndex(包括)开始全部移除
|
|
|
removeNodes: function (tree, parent, iIndex, count) {
|
|
|
var children = parent ? parent.children : tree.roots;
|
|
|
var pre = (iIndex < 0 || iIndex >= children.length) ? null : children[iIndex].preSibling;
|
|
|
var next = (pre && iIndex + count - 1 < children.length) ? children[iIndex + count] : null;
|
|
|
if (pre) {
|
|
|
- pre.nextSibling = next;
|
|
|
+ pre.setNextSibling(next);
|
|
|
+ //pre.nextSibling = next;
|
|
|
}
|
|
|
if (next) {
|
|
|
next.preSibling = pre;
|
|
@@ -82,7 +84,7 @@ var idTree = {
|
|
|
children.splice(iIndex, children.length - iIndex);
|
|
|
}
|
|
|
},
|
|
|
- // 在nodes中增加addNodes, 位置从index开始
|
|
|
+ // 在parent.children/tree.roots中增加nodes, 位置从index开始
|
|
|
addNodes: function (tree, parent, nodes, iIndex) {
|
|
|
var children = parent ? parent.children : tree.roots;
|
|
|
var pre, next, i;
|
|
@@ -95,20 +97,20 @@ var idTree = {
|
|
|
next = null;
|
|
|
}
|
|
|
if (pre) {
|
|
|
- pre.nextSibling = nodes[0];
|
|
|
+ pre.setNextSibling(nodes[0]);
|
|
|
}
|
|
|
nodes[0].preSibling = pre;
|
|
|
if (next) {
|
|
|
next.preSibling = nodes[nodes.length - 1];
|
|
|
}
|
|
|
- nodes[nodes.length - 1].nextSibling = next;
|
|
|
+ nodes[nodes.length - 1].setNextSibling(next);
|
|
|
for (i = 0; i < nodes.length; i++) {
|
|
|
if (arguments.length === 4) {
|
|
|
children.splice(iIndex + i, 0, nodes[i]);
|
|
|
} else if (arguments.length === 3) {
|
|
|
children.push(nodes[i]);
|
|
|
}
|
|
|
- nodes[i].parent = parent ? parent : null;
|
|
|
+ nodes[i].setParent(parent ? parent : null);
|
|
|
}
|
|
|
},
|
|
|
sortTreeItems: function (tree) {
|
|
@@ -135,7 +137,7 @@ var idTree = {
|
|
|
};
|
|
|
|
|
|
var Node = function (tree, data) {
|
|
|
- // 以下的属性,本单元外均不可直接修改
|
|
|
+ // 以下的属性,本单元外均不可直接修改
|
|
|
this.tree = tree;
|
|
|
this.data = data;
|
|
|
this.children = [];
|
|
@@ -160,6 +162,19 @@ var idTree = {
|
|
|
return this.nextSibling ? this.nextSibling.getID() : -1;
|
|
|
};
|
|
|
|
|
|
+ Node.prototype.setParent = function (parent) {
|
|
|
+ this.parent = parent;
|
|
|
+ if (this.tree.setting.autoUpdate) {
|
|
|
+ this.data[this.tree.setting.pid] = this.getParentID();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ Node.prototype.setNextSibling = function (nextSibling) {
|
|
|
+ this.nextSibling = nextSibling;
|
|
|
+ if (this.tree.setting.autoUpdate) {
|
|
|
+ this.data[this.tree.setting.nid] = this.getNextSiblingID();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
Node.prototype.firstChild = function () {
|
|
|
return this.children.length === 0 ? null : this.children[0];
|
|
|
};
|
|
@@ -320,12 +335,12 @@ var idTree = {
|
|
|
var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
|
|
|
if (this.canUpMove()) {
|
|
|
if (orgPre.preSibling) {
|
|
|
- orgPre.preSibling.nextSibling = this;
|
|
|
+ orgPre.preSibling.setNextSibling(this);
|
|
|
}
|
|
|
- orgPre.nextSibling = this.nextSibling;
|
|
|
+ orgPre.seNextSibling(this.nextSibling);
|
|
|
this.preSibling = orgPre.preSibling;
|
|
|
orgPre.preSibling = this;
|
|
|
- this.nextSibling = orgPre;
|
|
|
+ this.setNextSibling(orgPre);
|
|
|
belongArray.splice(iIndex, 1);
|
|
|
belongArray.splice(iIndex - 1, 0, this);
|
|
|
tools.sortTreeItems(this.tree);
|
|
@@ -353,11 +368,11 @@ var idTree = {
|
|
|
var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
|
|
|
if (this.canDownMove()) {
|
|
|
if (this.preSibling) {
|
|
|
- this.preSibling.nextSibling = orgNext;
|
|
|
+ this.preSibling.setNextSibling(orgNext);
|
|
|
}
|
|
|
orgNext.preSibling = this.preSibling;
|
|
|
- this.nextSibling = orgNext.nextSibling;
|
|
|
- orgNext.nextSibling = this;
|
|
|
+ this.setNextSibling(orgNext.nextSibling);
|
|
|
+ orgNext.setNextSibling(this);
|
|
|
this.preSibling = orgNext;
|
|
|
belongArray.splice(iIndex, 1);
|
|
|
belongArray.splice(iIndex + 1, 0, this);
|
|
@@ -512,6 +527,25 @@ var idTree = {
|
|
|
}
|
|
|
return data;
|
|
|
};
|
|
|
+ Tree.prototype.insertByData = function (data, parentID, nextSiblingID) {
|
|
|
+ var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
|
|
|
+ var nextSibling = nextSiblingID === -1 ? null : this.nodes[this.prefix + nextSiblingID];
|
|
|
+ var node = this.nodes[this.prefix + data[this.setting.id]];
|
|
|
+ if (node) {
|
|
|
+ return node;
|
|
|
+ } else {
|
|
|
+ node = new Node(this, data);
|
|
|
+ if (nextSibling) {
|
|
|
+ tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
|
|
|
+ } else {
|
|
|
+ tools.addNodes(this, parent, [node]);
|
|
|
+ }
|
|
|
+ this.nodes[this.prefix + data[this.setting.id]] = node;
|
|
|
+ tools.sortTreeItems(this);
|
|
|
+ this.maxNodeID( data[this.setting.id]);
|
|
|
+ return node;
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
Tree.prototype.delete = function (node) {
|
|
|
var success = false, that = this;
|
|
@@ -525,7 +559,7 @@ var idTree = {
|
|
|
deleteIdIndex([node]);
|
|
|
//delete this.nodes[this.prefix + node.getID()];
|
|
|
if (node.preSibling) {
|
|
|
- node.preSibling.nextSibling = node.nextSibling;
|
|
|
+ node.preSibling.setNextSibling(node.nextSibling);
|
|
|
}
|
|
|
if (node.nextSibling) {
|
|
|
node.nextSibling.preSibling = node.preSibling;
|