/** * Created by Mai on 2017/3/17. */ var idTree = { createNew: function (setting) { var _setting = { id: 'id', pid: 'pid', nid: 'nid', rootId: -1, autoUpdate: false }; var _eventType = { editedData: 'editedData' }; var tools = { findNode: function (nodes, check) { for (var i = 0; i < nodes.length; i++) { if (check(nodes[i])) { return nodes[i]; } } return null; }, reSortNodes: function (nodes, recursive) { var temp = [], first; var findFirstNode = function (nodes) { return tools.findNode(nodes, function (node) { return node.preSibling === null; }); }; var moveNode = function (node, orgArray, newArray, newIndex) { var next; orgArray.splice(orgArray.indexOf(node), 1); newArray.splice(newIndex, 0, node); if (node.getNextSiblingID() !== -1) { next = node.nextSibling; if (next && (orgArray.indexOf(next) >= 0)) { moveNode(next, orgArray, newArray, newIndex + 1); } } }; if (nodes.length === 0) { return nodes; } if (recursive) { nodes.forEach(function (node) { node.children = tools.reSortNodes(node.children, recursive); }); } while (nodes.length > 0) { first = findFirstNode(nodes); first = first ? first : nodes[0]; moveNode(first, nodes, temp, temp.length); } nodes = null; tools.reSiblingNodes(temp); return temp; }, reSiblingNodes: function (nodes) { var i; for (i = 0; i < nodes.length; i++) { nodes[i].preSibling = (i === 0) ? null : nodes[i - 1]; nodes[i].nextSibling = (i === nodes.length - 1) ? null : nodes[i + 1]; } }, // 在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.setNextSibling(next); } else if (next) { next.preSibling = null; } if (arguments.length === 4) { children.splice(iIndex, count); } else { children.splice(iIndex, children.length - iIndex); } }, // 在parent.children/tree.roots中增加nodes, 位置从index开始 addNodes: function (tree, parent, nodes, iIndex) { var children = parent ? parent.children : tree.roots; var pre, next, i; if (nodes.length === 0) { return; } if (arguments.length === 4) { pre = (iIndex <= 0 || iIndex > children.length) ? null : children[iIndex-1]; if(pre==null){ next = iIndex==0?children[0]:null; }else { next = pre.nextSibling; } } else if (arguments.length === 3) { pre = children.length === 0 ? null : children[children.length - 1]; next = null; } if (pre) { pre.setNextSibling(nodes[0]); } else { nodes[0].preSibling = null; } 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].setParent(parent ? parent : null); } }, 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); }, 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)}); } } }; var Node = function (tree, data) { // 以下的属性,本单元外均不可直接修改 this.tree = tree; this.data = data; this.children = []; this.parent = null; this.nextSibling = null; this.preSibling = null; this.expanded = true; this.visible = true; this.visible = true; }; Node.prototype.getID = function () { return this.data[this.tree.setting.id]; }; Node.prototype.getParentID = function () { return this.parent ? this.parent.getID() : -1; }; Node.prototype.getNextSiblingID = function () { 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 (nextSibling) { nextSibling.preSibling = this; } 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]; }; 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; }; Node.prototype.isFirst = function () { if (this.parent) { return this.parent.children.indexOf(this) === 0 ? true : false; } else { return this.tree.roots.indexOf(this) === 0 ? true : false; } }; Node.prototype.isLast = function () { if (this.parent) { return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false; } else { return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false; } }; 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) { iCount += this.children.length; this.children.forEach(function (child) { iCount += child.posterityCount(); }); } return iCount; /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) { return x.posterityCount() + y.posterityCount(); }) + node.children.count;*/ }; Node.prototype.setExpanded = function (expanded) { var setNodesVisible = function (nodes, visible) { nodes.forEach(function (node) { node.visible = visible; setNodesVisible(node.children, visible && node.expanded); }) }; this.expanded = expanded; setNodesVisible(this.children, expanded); }; Node.prototype.setExpandedNoRecur = function (expanded) { this.expanded = expanded; this.visible = expanded && this.visible; for(let node of this.children){ node.visible = expanded && node.visible; } }; /*Node.prototype.vis = function () { return this.parent ? this.parent.vis() && this.parent.expanded() : true; };*/ Node.prototype.serialNo = function () { return this.tree.items.indexOf(this); }; Node.prototype.addChild = function (node) { var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1]; node.parent = this; if (preSibling) { preSibling.nextSibling = node; } node.preSibling = preSibling; this.children.push(node); }; Node.prototype.removeChild = function (node) { var preSibling = node.preSibling, nextSibling = node.nextSibling; if (preSibling) { preSibling.nextSibling = nextSibling; } if (nextSibling) { nextSibling.preSibling = preSibling; } this.children.splice(node.siblingIndex, 1); }; Node.prototype.canUpLevel = function () { return this.parent ? true : false; }; 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); } if (this.lastChild() && this.nextSibling) { tools.addUpdateDataForNextSibling(data, this.lastChild(), this.getNextSiblingID()); } 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.upLevel = function () { 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)); // Orginal Parent remove node and nextSiblings tools.removeNodes(this.tree, this.parent, iIndex); // New Parent add node tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1); if (!this.expanded) { this.setExpanded(true); } result.success = true; } 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); var newParent = this.preSibling; if (this.canDownLevel()) { tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1); tools.addNodes(this.tree, this.preSibling, [this]); if (!newParent.expanded) { newParent.setExpanded(true); } success = true; } 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.setNextSibling(this); } else { this.preSibling = null; } orgPre.setNextSibling(this.nextSibling); this.setNextSibling(orgPre); belongArray.splice(iIndex, 1); belongArray.splice(iIndex - 1, 0, this); tools.sortTreeItems(this.tree); success = true; } 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.setNextSibling(orgNext); } else if (orgNext) { orgNext.preSibling = null; } this.setNextSibling(orgNext.nextSibling); orgNext.setNextSibling(this); belongArray.splice(iIndex, 1); belongArray.splice(iIndex + 1, 0, this); tools.sortTreeItems(this.tree); success = true; } return success; }; // 节点所属固定ID Node.prototype.getFlag = function() { if (!this.data || !this.data.flags || !this.data.flags[0] || ! this.data.flags[0].flag) { return 0; } return this.data.flags[0].flag; }; // 节点所属固定ID Node.prototype.belongToFlag = function () { let node = this; while (node) { if (node.data && node.data.flags && node.data.flags[0] && node.data.flags[0].flag) { return node.data.flags[0].flag; } node = node.parent; } return null; }; // 节点是否属于某些固定ID,会一直向上找,直到找到或到达顶层 Node.prototype.isBelongToFlags = function (flags) { let node = this; while (node) { const flag = node.getFlag(); if (flags.includes(flag)) { return true; } node = node.parent; } return false; } // 获取节点所有后代节点 Node.prototype.getPosterity = function() { let posterity = []; getNodes(this.children); return posterity; function getNodes(nodes) { for (let node of nodes) { posterity.push(node); if (node.children.length > 0){ getNodes(node.children); } } } }; var Tree = function (setting) { this.nodes = {}; this.roots = []; this.items = []; this.setting = setting; this.prefix = 'id_'; this.selected = null; this.event = {}; 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) { if (arguments.length === 0) { return maxID; } else { maxID = Math.max(maxID, ID); } }; })(); Tree.prototype.rangeNodeID = (function () { var rangeID = -1; return function (ID) { if (arguments.length === 0) { return rangeID; } else { rangeID = Math.max(rangeID, ID); } } })(); Tree.prototype.newNodeID = function () { if (this.rangeNodeID() == -1) { return this.maxNodeID() + 1; } else { if (this.maxNodeID() < this.rangeNodeID()) { return this.maxNodeID() + 1; } else { return -1; } } /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) { return -1; } else { return this.maxNodeID() + 1; }*/ }; Tree.prototype.clearNodes = function () { this.nodes = {}; this.roots = []; this.items = []; }; Tree.prototype.loadDatas = function (datas) { var prefix = this.prefix, i, node, parent, next, that = this; this.nodes = {}; this.roots = []; this.items = []; // prepare index datas.forEach(function (data) { var node = new Node(that, data); that.nodes[prefix + data[that.setting.id]] = node; that.maxNodeID(data[that.setting.id]); }); // set parent by pid, set nextSibling by nid datas.forEach(function (data) { node = that.nodes[prefix + data[that.setting.id]]; if (data[that.setting.pid] == that.setting.rootId) { that.roots.push(node); } else { parent = that.nodes[prefix + data[that.setting.pid]]; if (parent) { node.parent = parent; parent.children.push(node); } } if (data[that.setting.nid] !== that.setting.rootId) { next = that.nodes[prefix + data[that.setting.nid]]; if (next) { node.nextSibling = next; next.preSibling = node; } } }) // sort by nid this.roots = tools.reSortNodes(this.roots, true); tools.sortTreeItems(this); }; Tree.prototype.firstNode = function () { return this.roots.length === 0 ? null : this.roots[0]; }; Tree.prototype.findNode = function (id) { return this.nodes[this.prefix + id]; }; Tree.prototype.count = function () { var iCount = 0; if (this.roots.length !== 0) { iCount += this.roots.length; this.roots.forEach(function (node) { iCount += node.posterityCount(); }); } return iCount; }; 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] = parent ? parent.getID() : this.setting.rootId; data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId; 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; }; Tree.prototype.insertByID = function (newID, parentID, nextSiblingID) { var 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) { data = {}; data[this.setting.id] = newID; data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId; data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId; 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); } return node; }; Tree.prototype.getInsertData = function (parentID, nextSiblingID, uid = null) { var data = []; var newID = uid ? uuid.v1() : 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, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)}); if (nextSibling && nextSibling.preSibling) { tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID); } else if (parent && parent.children.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.insertByData = function (data, parentID, nextSiblingID, uid = null, resort = true) { 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; if (resort) { tools.sortTreeItems(this); } if(!uid){ this.maxNodeID( data[this.setting.id]); } return node; } }; // 一次性插入多个连续的同层节点 Tree.prototype.multiInsert = function (datas, preID) { const newNodes = []; for (const data of datas) { this.nodes[this.prefix + data.ID] = new Node(this, data.ID); this.nodes[this.prefix + data.ID]['data'] = data; newNodes.push(this.nodes[this.prefix + data.ID]); } const fisrtNode = this.nodes[this.prefix + datas[0].ID]; const firstPre = this.nodes[this.prefix + preID]; if (firstPre) { firstPre.nextSibling = fisrtNode; firstPre.data.NextSiblingID = fisrtNode.getID(); } const parent = this.nodes[this.prefix + datas[0].ParentID] || null; const parentChildren = parent ? parent.children : this.roots; let baseIndex = firstPre ? parentChildren.indexOf(firstPre) + 1 : 0; datas.forEach(data => { const node = this.nodes[this.prefix + data.ID]; node.parent = parent; parentChildren.splice(baseIndex++, 0, node); const next = this.nodes[this.prefix + data.NextSiblingID] || null; node.nextSibling = next; if (next) { next.preSibling = node; } }); this.roots = tools.reSortNodes(this.roots, true); tools.sortTreeItems(this); return newNodes; } // 插入某一完整片段到某节点的子项中 Tree.prototype.insertByDatas = function (datas) { for(let data of datas){ this.nodes[this.prefix + data.ID] = new Node(this, data.ID); this.nodes[this.prefix + data.ID]['data'] = data; } for(let data of datas){ let node = this.nodes[this.prefix + data.ID]; let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID]; node.parent = parent; if(!parent){ this.roots.push(node); } else { parent.children.push(node); } let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID]; node.nextSibling = next; if(next){ next.preSibling = node; } } //resort this.roots = tools.reSortNodes(this.roots, true); tools.sortTreeItems(this); }; Tree.prototype.delete = function (node) { var success = false; success=this.cascadeRemove(node); tools.sortTreeItems(this); return success; }; Tree.prototype.m_delete=function(nodes){ for(let node of nodes){ this.cascadeRemove(node); } tools.sortTreeItems(this); return true; }; Tree.prototype.cascadeRemove = function (node){ 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) { deleteIdIndex([node]); //delete this.nodes[this.prefix + node.getID()]; if (node.preSibling) { node.preSibling.setNextSibling(node.nextSibling); } else if (node.nextSibling) { node.nextSibling.preSibling = null; } if (node.parent) { node.parent.children.splice(node.siblingIndex(), 1); } else { this.roots.splice(node.siblingIndex(), 1); } success = true; } return success; }; Tree.prototype.singleDelete = function (node) {//删除本身不删除子项 let that = this; let success = false; delete that.nodes[that.prefix + node.getID()];//删除本身 if (node.parent) {//从父项的子节点中移除 node.parent.children.splice(node.siblingIndex(), 1); } else { this.roots.splice(node.siblingIndex(), 1); } if(node.preSibling) node.preSibling.setNextSibling(node.nextSibling);//后兄弟设置为前兄弟的后兄弟 if(node.children.length>0){ if(node.preSibling){//子项变成前兄弟的子项 for(let c of node.children){ node.preSibling.addChild(c); } }else if(node.nextSibling){//没有前兄弟,有后兄弟 let oldChild = node.parent.children; node.parent.children = []; for(let c of node.children){ node.parent.addChild(c); } for(let oc of oldChild){ node.parent.addChild(oc); } }else {//都没有的情况 for(let c of node.children){ node.parent.addChild(c); } } tools.sortTreeItems(this); success = true; } 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.setRootExpanded = function(nodes, expanded){ for(let node of nodes){ if(node.children.length > 0){ node.setExpanded(expanded); this.setRootExpanded(node.children, expanded); } } }; Tree.prototype.getExpState = function (nodes) { let sessionExpanded = []; function getStat(items){ for(let item of items){ sessionExpanded.push(item.expanded ? 1 : 0); } } getStat(nodes); let expState = sessionExpanded.join(''); return expState; }; //节点根据展开收起列表'010101'展开收起 Tree.prototype.setExpandedByState = function (nodes, expState) { let expStateArr = expState.split(''); for(let i = 0; i < nodes.length; i++){ let expanded = expStateArr[i] == 1 ? true : false; if(nodes[i].expanded === expanded){ continue; } nodes[i].setExpanded(expanded); } }; /*Tree.prototype.editedData = function (field, id, newText) { var node = this.findNode(id), result = {allow: false, nodes: []}; if (this.event[this.eventType.editedData]) { return this.event[this.eventType.editedData](field, node.data); } else { node.data[field] = newText; result.allow = true; return result; } };*/ Tree.prototype.bind = function (eventName, eventFun) { this.event[eventName] = eventFun; }; Tree.prototype.getNodeByID = function (ID) { let node = this.nodes[this.prefix+ID]; return node; }; //检查树结构数据有没问题 Tree.prototype.check = function (roots) { return isValid(roots); function isValid(nodes) { for (let node of nodes) { if (node.data.ParentID != -1 && (!node.parent || node.parent.data.ID !== node.data.ParentID)) { console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`); return false; } if (node.data.ParentID == -1 && node.parent) { console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`); return false; } if (node.data.NextSiblingID != -1 && (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) { console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`); return false; } if (node.data.NextSiblingID == -1 && node.nextSibling) { console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`); return false; } let sameDepthNodes = node.parent ? node.parent.children : roots, nodeIdx = sameDepthNodes.indexOf(node), nextIdx = sameDepthNodes.indexOf(node.nextSibling); if (nodeIdx != -1 && nextIdx != -1 && nodeIdx > nextIdx) { console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`); return false; } // nextSibling跟parent children的下一节点对应不上 if (nodeIdx != -1 && (nodeIdx === sameDepthNodes.length - 1 && nextIdx != -1) || (nodeIdx !== sameDepthNodes.length - 1 && nodeIdx + 1 !== nextIdx)) { console.log(`${node.serialNo() + 1}:${node.data.name} nextSibling与树显示的下一节点对应不上`); return false; } if (node.children.length) { let v = isValid(node.children); if (!v) { return false; } } } return true; } }; return new Tree(setting); }, updateType: {update: 'update', new: 'new', delete: 'delete'} };