|
@@ -121,6 +121,16 @@ var idTree = {
|
|
|
};
|
|
|
tree.items.splice(0, tree.items.length);
|
|
|
addItems(tree.roots);
|
|
|
+ },
|
|
|
+ addUpdateDataForParent: function (datas, nodes, pid) {
|
|
|
+ nodes.forEach(function (node) {
|
|
|
+ datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), pid, node.getNextSiblingID())});
|
|
|
+ });
|
|
|
+ },
|
|
|
+ addUpdateDataForNextSibling: function (datas, node, nid) {
|
|
|
+ if (node) {
|
|
|
+ datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), node.getParentID(), nid)});
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -138,9 +148,6 @@ var idTree = {
|
|
|
this.visible = true;
|
|
|
|
|
|
this.visible = true;
|
|
|
-
|
|
|
- this.isUpdate = false;
|
|
|
- this.isNew = false;
|
|
|
};
|
|
|
|
|
|
Node.prototype.getID = function () {
|
|
@@ -156,6 +163,10 @@ var idTree = {
|
|
|
Node.prototype.firstChild = function () {
|
|
|
return this.children.length === 0 ? null : this.children[0];
|
|
|
};
|
|
|
+ Node.prototype.lastChild = function () {
|
|
|
+ return this.children.length === 0 ? null : this.children[this.children.length - 1];
|
|
|
+ };
|
|
|
+
|
|
|
Node.prototype.depth = function () {
|
|
|
return this.parent ? this.parent.depth() + 1 : 0;
|
|
|
};
|
|
@@ -175,7 +186,7 @@ var idTree = {
|
|
|
};
|
|
|
Node.prototype.siblingIndex = function () {
|
|
|
return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
|
|
|
- }
|
|
|
+ };
|
|
|
Node.prototype.posterityCount = function () {
|
|
|
var iCount = 0;
|
|
|
if (this.children.length !== 0) {
|
|
@@ -230,19 +241,23 @@ var idTree = {
|
|
|
Node.prototype.canUpLevel = function () {
|
|
|
return this.parent ? true : false;
|
|
|
};
|
|
|
- Node.prototype.canDownLevel = function () {
|
|
|
- return !this.isFirst();
|
|
|
- };
|
|
|
- Node.prototype.canUpMove = function () {
|
|
|
- return !this.isFirst();
|
|
|
+ Node.prototype.getUpLevelData = function () {
|
|
|
+ var data = [];
|
|
|
+ if (this.canUpLevel()) {
|
|
|
+ if (!this.isLast()) {
|
|
|
+ tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
|
|
|
+ }
|
|
|
+ if (this.preSibling) {
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
|
|
|
+ }
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
|
|
|
+ data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID())});
|
|
|
+ }
|
|
|
+ return data;
|
|
|
};
|
|
|
- Node.prototype.canDownMove = function () {
|
|
|
- return !this.isLast();
|
|
|
- }
|
|
|
-
|
|
|
Node.prototype.upLevel = function () {
|
|
|
- var success = false,
|
|
|
- iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
|
|
|
+ var result = {success: false, updateDatas: []};
|
|
|
+ var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
|
|
|
if (this.canUpLevel) {
|
|
|
// NextSiblings become child
|
|
|
tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
|
|
@@ -253,9 +268,24 @@ var idTree = {
|
|
|
if (!this.expanded) {
|
|
|
this.setExpanded(true);
|
|
|
}
|
|
|
- success = true;
|
|
|
+ result.success = true;
|
|
|
}
|
|
|
- return success;
|
|
|
+ return result;
|
|
|
+ };
|
|
|
+
|
|
|
+ Node.prototype.canDownLevel = function () {
|
|
|
+ return !this.isFirst();
|
|
|
+ };
|
|
|
+ Node.prototype.getDownLevelData = function () {
|
|
|
+ var data = [];
|
|
|
+ if (this.canDownLevel()) {
|
|
|
+ if (this.preSibling.children.length !== 0) {
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
|
|
|
+ }
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
|
|
|
+ data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
|
|
|
+ }
|
|
|
+ return data;
|
|
|
};
|
|
|
Node.prototype.downLevel = function () {
|
|
|
var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
|
|
@@ -270,10 +300,28 @@ var idTree = {
|
|
|
}
|
|
|
return success;
|
|
|
};
|
|
|
+
|
|
|
+ Node.prototype.canUpMove = function () {
|
|
|
+ return !this.isFirst();
|
|
|
+ };
|
|
|
+ Node.prototype.getUpMoveData = function () {
|
|
|
+ var data = [];
|
|
|
+ if (this.canUpMove()) {
|
|
|
+ if (this.preSibling.preSibling) {
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
|
|
|
+ }
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
|
|
|
+ tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ };
|
|
|
Node.prototype.upMove = function () {
|
|
|
var success = false;
|
|
|
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.nextSibling = this.nextSibling;
|
|
|
this.preSibling = orgPre.preSibling;
|
|
|
orgPre.preSibling = this;
|
|
@@ -285,10 +333,28 @@ var idTree = {
|
|
|
}
|
|
|
return success;
|
|
|
};
|
|
|
+
|
|
|
+ Node.prototype.canDownMove = function () {
|
|
|
+ return !this.isLast();
|
|
|
+ };
|
|
|
+ Node.prototype.getDownMoveData = function () {
|
|
|
+ var data = [];
|
|
|
+ if (this.canDownMove()) {
|
|
|
+ if (this.preSibling) {
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
|
|
|
+ }
|
|
|
+ tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ };
|
|
|
Node.prototype.downMove = function () {
|
|
|
var success = false;
|
|
|
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;
|
|
|
+ }
|
|
|
orgNext.preSibling = this.preSibling;
|
|
|
this.nextSibling = orgNext.nextSibling;
|
|
|
orgNext.nextSibling = this;
|
|
@@ -299,7 +365,7 @@ var idTree = {
|
|
|
success = true;
|
|
|
}
|
|
|
return success;
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
var Tree = function (setting) {
|
|
|
this.nodes = {};
|
|
@@ -313,6 +379,14 @@ var idTree = {
|
|
|
this.eventType = _eventType;
|
|
|
};
|
|
|
|
|
|
+ Tree.prototype.getDataTemplate = function (id, pid, nid) {
|
|
|
+ var data = {};
|
|
|
+ data[this.setting.id] = id;
|
|
|
+ data[this.setting.pid] = pid;
|
|
|
+ data[this.setting.nid] = nid;
|
|
|
+ return data;
|
|
|
+ };
|
|
|
+
|
|
|
Tree.prototype.maxNodeID = (function () {
|
|
|
var maxID = 0;
|
|
|
return function (ID) {
|
|
@@ -420,10 +494,36 @@ var idTree = {
|
|
|
}
|
|
|
return node;
|
|
|
};
|
|
|
+ Tree.prototype.getInsertData = function (parentID, nextSiblingID) {
|
|
|
+ var data = [];
|
|
|
+ var newID = this.newNodeID();
|
|
|
+ var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
|
|
|
+ var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
|
|
|
+ if (newID !== -1) {
|
|
|
+ data.push({type: 'new', data: this.getDataTemplate(newID, parentID, nextSiblingID)});
|
|
|
+
|
|
|
+ if (nextSibling && nextSibling.preSibling) {
|
|
|
+ tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
|
|
|
+ } else if (parent && parent.childrent.length !== 0) {
|
|
|
+ tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
|
|
|
+ } else if (!parent && this.roots.length !== 0) {
|
|
|
+ tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ };
|
|
|
+
|
|
|
Tree.prototype.delete = function (node) {
|
|
|
- var success = false;
|
|
|
+ var success = false, that = this;
|
|
|
+ var deleteIdIndex = function (nodes) {
|
|
|
+ nodes.forEach(function (node) {
|
|
|
+ delete that.nodes[that.prefix + node.getID()];
|
|
|
+ deleteIdIndex(node.children);
|
|
|
+ })
|
|
|
+ }
|
|
|
if (node) {
|
|
|
- delete this.nodes[this.prefix + node.getID()];
|
|
|
+ deleteIdIndex([node]);
|
|
|
+ //delete this.nodes[this.prefix + node.getID()];
|
|
|
if (node.preSibling) {
|
|
|
node.preSibling.nextSibling = node.nextSibling;
|
|
|
}
|
|
@@ -440,6 +540,24 @@ var idTree = {
|
|
|
}
|
|
|
return success;
|
|
|
};
|
|
|
+ Tree.prototype.getDeleteData = function (node) {
|
|
|
+ var data = [];
|
|
|
+ var addUpdateDataForDelete = function (datas, nodes) {
|
|
|
+ nodes.forEach(function (node) {
|
|
|
+ var delData = {};
|
|
|
+ delData[node.tree.setting.id] = node.getID();
|
|
|
+ datas.push({type: 'delete', data: delData});
|
|
|
+ addUpdateDataForDelete(datas, node.children);
|
|
|
+ })
|
|
|
+ };
|
|
|
+ if (node) {
|
|
|
+ addUpdateDataForDelete(data, [node]);
|
|
|
+ if (node.preSibling) {
|
|
|
+ tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
|
|
|
/*Tree.prototype.editedData = function (field, id, newText) {
|
|
|
var node = this.findNode(id), result = {allow: false, nodes: []};
|