|
@@ -3,9 +3,6 @@
|
|
|
*/
|
|
|
var cacheTree = {
|
|
|
createNew: function (owner) {
|
|
|
- var _eventType = {
|
|
|
- afterSelectedChanged: 'afterSelectedChanged'
|
|
|
- };
|
|
|
var tools = {
|
|
|
findNode: function (nodes, check) {
|
|
|
for (var i = 0; i < nodes.length; i++) {
|
|
@@ -102,17 +99,6 @@ var cacheTree = {
|
|
|
}
|
|
|
nodes[i].parent = parent;
|
|
|
}
|
|
|
- },
|
|
|
- sortTreeItems: function (tree) {
|
|
|
- var addItems = function (items) {
|
|
|
- var i;
|
|
|
- for (i = 0; i < items.length; i++) {
|
|
|
- tree.items.push(items[i]);
|
|
|
- addItems(items[i].children);
|
|
|
- }
|
|
|
- };
|
|
|
- tree.items.splice(0, tree.items.length);
|
|
|
- addItems(tree.roots);
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -129,7 +115,7 @@ var cacheTree = {
|
|
|
this.expanded = true;
|
|
|
this.visible = true;
|
|
|
|
|
|
- this.nodeType = null;
|
|
|
+ this.sourceType = null;
|
|
|
this.source = null;
|
|
|
|
|
|
this.getID = function () {
|
|
@@ -212,14 +198,14 @@ var cacheTree = {
|
|
|
};
|
|
|
|
|
|
Node.prototype.canUpLevel = function () {
|
|
|
- if (this.nodeType === this.tree.nodeType.bills) {
|
|
|
+ if (this.sourceType === this.tree.owner.Bills.getSourceType()) {
|
|
|
return this.parent ? true : false;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
};
|
|
|
Node.prototype.canDownLevel = function () {
|
|
|
- return this.nodeType === this.tree.nodeType.bills ? !this.isFirst() : false;
|
|
|
+ return this.sourceType === this.tree.owner.Bills.getSourceType() ? !this.isFirst() : false;
|
|
|
};
|
|
|
Node.prototype.canUpMove = function () {
|
|
|
return !this.isFirst();
|
|
@@ -268,7 +254,7 @@ var cacheTree = {
|
|
|
this.nextSibling = orgPre;
|
|
|
belongArray.splice(iIndex, 1);
|
|
|
belongArray.splice(iIndex - 1, 0, this);
|
|
|
- tools.sortTreeItems(this.tree);
|
|
|
+ this.tree.sortTreeItems();
|
|
|
success = true;
|
|
|
}
|
|
|
return success;
|
|
@@ -283,22 +269,20 @@ var cacheTree = {
|
|
|
this.preSibling = orgNext;
|
|
|
belongArray.splice(iIndex, 1);
|
|
|
belongArray.splice(iIndex + 1, 0, this);
|
|
|
- tools.sortTreeItems(this.tree);
|
|
|
+ this.tree.sortTreeItems();
|
|
|
success = true;
|
|
|
}
|
|
|
return success;
|
|
|
};
|
|
|
|
|
|
var Tree = function (owner) {
|
|
|
+ this.owner = owner;
|
|
|
this.nodes = {};
|
|
|
this.roots = [];
|
|
|
this.items = [];
|
|
|
this.prefix = 'id_';
|
|
|
this.selected = null;
|
|
|
|
|
|
- this.nodeType = {bills: 'bills', ration: 'ration', zc: 'zhuCai'};
|
|
|
- this.masterField = {ration: 'BillsID'};
|
|
|
-
|
|
|
var _MaxID = 0;
|
|
|
this.newNodeID = function (id) {
|
|
|
if (arguments.length === 0) {
|
|
@@ -315,44 +299,29 @@ var cacheTree = {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- Tree.prototype.createNewNode = function () {
|
|
|
- var node = new Node(this, this.newNodeID());
|
|
|
- this.nodes[this.prefix + node.getID()] = node;
|
|
|
- return node;
|
|
|
+ Tree.prototype.addNode = function (parent, nextSibling) {
|
|
|
+ var newNode = new Node(this, this.newNodeID());
|
|
|
+ this.nodes[this.prefix + newNode.getID()] = newNode;
|
|
|
+ if (nextSibling) {
|
|
|
+ tools.addNodes(this, parent, [newNode], nextSibling.siblingIndex());
|
|
|
+ } else {
|
|
|
+ tools.addNodes(this, parent, [newNode]);
|
|
|
+ }
|
|
|
+ return newNode;
|
|
|
};
|
|
|
-
|
|
|
- Tree.prototype.loadDatas = function (idTree, rations) {
|
|
|
+ Tree.prototype.sortTreeItems = function () {
|
|
|
var that = this;
|
|
|
- var loadRationNode = function (rations, cacheNode) {
|
|
|
- var newNode;
|
|
|
- rations.forEach(function (ration) {
|
|
|
- if (ration[that.masterField.ration] && ration[that.masterField.ration] === cacheNode.source.getID()) {
|
|
|
- newNode = that.createNewNode();
|
|
|
- newNode.source = ration;
|
|
|
- newNode.nodeType = that.nodeType.ration;
|
|
|
- newNode.data = ration;
|
|
|
- tools.addNodes(that, cacheNode, [newNode]);
|
|
|
- }
|
|
|
- });
|
|
|
- };
|
|
|
- var loadIdTreeNode = function (nodes, parent) {
|
|
|
- var newNode, i;
|
|
|
+ var addItems = function (nodes) {
|
|
|
+ var i;
|
|
|
for (i = 0; i < nodes.length; i++) {
|
|
|
- newNode = that.createNewNode();
|
|
|
- newNode.source = nodes[i];
|
|
|
- newNode.nodeType = that.nodeType.bills;
|
|
|
- newNode.data = nodes[i].data;
|
|
|
- newNode.parent = parent;
|
|
|
- tools.addNodes(that, parent, [newNode]);
|
|
|
- if (nodes[i].children.length === 0) {
|
|
|
- loadRationNode(rations, newNode);
|
|
|
- };
|
|
|
- loadIdTreeNode(nodes[i].children, newNode);
|
|
|
+ that.items.push(nodes[i]);
|
|
|
+ addItems(nodes[i].children);
|
|
|
}
|
|
|
};
|
|
|
- loadIdTreeNode(idTree.roots, null);
|
|
|
- tools.sortTreeItems(this);
|
|
|
+ this.items.splice(0, this.items.length);
|
|
|
+ addItems(this.roots);
|
|
|
};
|
|
|
+
|
|
|
Tree.prototype.firstNode = function () {
|
|
|
return this.roots.length === 0 ? null : this.roots[0];
|
|
|
};
|
|
@@ -371,25 +340,13 @@ var cacheTree = {
|
|
|
};
|
|
|
|
|
|
Tree.prototype.insert = function (parentID, nextSiblingID) {
|
|
|
- var newID = this.newNodeID(), node = null, data = {};
|
|
|
- var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
|
|
|
- var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
|
|
|
- if (newID !== -1) {
|
|
|
- data = {};
|
|
|
- data[this.setting.id] = newID;
|
|
|
- data[this.setting.pid] = parentID;
|
|
|
- data[this.setting.nid] = nextSiblingID;
|
|
|
- node = new Node(this, data);
|
|
|
- if (nextSibling) {
|
|
|
- tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
|
|
|
- } else {
|
|
|
- tools.addNodes(this, parent, [node]);
|
|
|
- }
|
|
|
- this.nodes[this.prefix + newID] = node;
|
|
|
- tools.sortTreeItems(this);
|
|
|
- this.maxNodeID(newID);
|
|
|
- }
|
|
|
- return node;
|
|
|
+ var parent = !parentID || parentID === -1 ? null : this.nodes[this.prefix + parentID];
|
|
|
+ var nextSibling = !nextSiblingID || nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
|
|
|
+
|
|
|
+ var newNode = this.addNode(parent, nextSibling);
|
|
|
+ this.sortTreeItems();
|
|
|
+
|
|
|
+ return newNode;
|
|
|
};
|
|
|
Tree.prototype.delete = function (node) {
|
|
|
var success = false;
|
|
@@ -406,16 +363,12 @@ var cacheTree = {
|
|
|
} else {
|
|
|
this.roots.splice(node.siblingIndex(), 1);
|
|
|
}
|
|
|
- tools.sortTreeItems(this);
|
|
|
+ this.sortTreeItems();
|
|
|
success = true;
|
|
|
}
|
|
|
return success;
|
|
|
};
|
|
|
|
|
|
- Tree.prototype.editedData = function (field, id, newText) {
|
|
|
- var node = this.findNode(id);
|
|
|
- }
|
|
|
-
|
|
|
return new Tree(owner);
|
|
|
}
|
|
|
};
|