Browse Source

fix:项目管理树建立代码,树结构数据没错,但是建立出来有问题

vian 4 năm trước cách đây
mục cha
commit
59b0e3fe2f
2 tập tin đã thay đổi với 94 bổ sung4 xóa
  1. 19 1
      web/building_saas/pm/js/pm_newMain.js
  2. 75 3
      web/building_saas/pm/js/pm_tree.js

+ 19 - 1
web/building_saas/pm/js/pm_newMain.js

@@ -61,7 +61,8 @@ const STATE = {
     addingProject: false,
     addingFolder: false,
     deleting: false,
-    importing: false
+    importing: false,
+    moving: false
 };
 const projTreeObj = {
     tree: null,
@@ -560,11 +561,16 @@ const projTreeObj = {
             }
             projTreeObj.moveTo(selected, null, parent, next, null, action);
             $.bootstrapLoading.end();
+            STATE.moving = false;
             projTreeObj.emitTreeChange();
         });
     },
     //升级后选中节点的后兄弟节点不成为其子节点,因为有层级类型限制(相当于选中节点移动到父项后成为其后兄弟)
     upLevel: function () {
+        if (STATE.moving) {
+            return;
+        }
+        STATE.moving = true;
         let selected = projTreeObj.tree.selected,
             parent = selected.parent.parent,
             next = selected.parent.nextSibling,
@@ -581,6 +587,10 @@ const projTreeObj = {
         this.doAfterTreeOpr({selected, parent, next, projectMap});
     },
     downLevel: function () {
+        if (STATE.moving) {
+            return;
+        }
+        STATE.moving = true;
         let selected = projTreeObj.tree.selected,
             parent = null,
             next = null,
@@ -603,6 +613,10 @@ const projTreeObj = {
         this.doAfterTreeOpr({selected, parent, next, projectMap}, 'downLevel');
     },
     upMove: function () {
+        if (STATE.moving) {
+            return;
+        }
+        STATE.moving = true;
         let selected = projTreeObj.tree.selected,
             parent = selected.parent,
             next = selected.preSibling(),
@@ -620,6 +634,10 @@ const projTreeObj = {
         this.doAfterTreeOpr({selected, parent, next, projectMap});
     },
     downMove: function () {
+        if (STATE.moving) {
+            return;
+        }
+        STATE.moving = true;
         let selected = projTreeObj.tree.selected,
             parent = selected.parent,
             next = selected.nextSibling.nextSibling,

+ 75 - 3
web/building_saas/pm/js/pm_tree.js

@@ -267,7 +267,79 @@ const pmTree = {
                 }, []);
             };
 
-            Tree.prototype.loadData = function (arrData) {
+            function sortChildren(nodes) {
+                const IDMap = {};
+                const nextMap = {};
+                let firstNode = null;
+                const newList = [];
+                for (const node of nodes) {
+                    // 递规排序
+                    if (node.children && node.children.length > 0) {
+                        node.children = sortChildren(node.children);
+                    }
+                    IDMap[node.data.ID] = node;
+                    if (node.data.NextSiblingID != -1) {
+                        nextMap[node.data.NextSiblingID] = node;
+                    }
+                }
+                for (const node of nodes) {
+                    if (!nextMap[node.data.ID]) { //如果在下一节点映射没找到,则是第一个节点
+                        firstNode = node;
+                        break;
+                    }
+                }
+                if (firstNode) {
+                    newList.push(firstNode);
+                    delete IDMap[firstNode.data.ID];
+                    setNext(firstNode, newList);
+                }
+                // 容错处理,如果链断了的情况,直接添加到后面
+                for (const key in IDMap) {
+                    if (IDMap[key]) {
+                        newList.push(IDMap[key]);
+                    }
+                }
+                return newList;
+
+                function setNext(node, array) {
+                    if (node.data.NextSiblingID != -1) {
+                        const next = IDMap[node.data.NextSiblingID];
+                        node.nextSibling = next || null;
+                        if (next) {
+                            array.push(next);
+                            delete IDMap[next.data.ID];
+                            setNext(next, array);
+                        }
+                    }
+                }
+            }
+
+            Tree.prototype.loadData = function (treeData) {
+                this.sourceData = treeData;
+                // 建立节点及索引
+                const nodes = [];
+                const parentMap = {};
+                const IDMap = {};
+                treeData.forEach(nodeData => {
+                    const node = new Node(this, nodeData);
+                    this.maxNodeId(node.id());
+                    IDMap[node.data.ID] = node;
+                    (parentMap[node.data.ParentID] || (parentMap[node.data.ParentID] = [])).push(node);
+                    nodes.push(node);
+                });
+                // 过滤出各节点的children、设置节点parent
+                nodes.forEach(node => {
+                    node.children = parentMap[node.data.ID] || [];
+                    node.parent = IDMap[node.data.ParentID] || this._root;
+                });
+                const roots = parentMap['-1'];
+                // 将各节点的children进行排序,并设置nextSibling
+                this._root.children = sortChildren(roots);
+                // 生成tree.items
+                sortTreeItems(this);
+            };
+
+/*             Tree.prototype.loadData = function (arrData) {
                 this.sourceData = arrData;
                 let i, that = this;
                 let nodesIndex = {};
@@ -331,7 +403,7 @@ const pmTree = {
                 }
                 //set items
                 sortTreeItems(this);
-            };
+            }; */
 
             Tree.prototype.addNodeData = function (data, parent, nextSibling, nodeState = null) {
                 this.sourceData.push(data);
@@ -409,7 +481,7 @@ const pmTree = {
                 function isValid(nodes) {
                     for (let node of nodes) {
                         if (node.data.ParentID !== -1 &&
-                            (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
+                            (!node.parent || !node.parent.data || node.parent.data.ID !== node.data.ParentID)) {
                             console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
                             return false;
                         }