Browse Source

cacheTree: preSibling Bug

MaiXinRong 8 years ago
parent
commit
3e072dbda7
1 changed files with 38 additions and 26 deletions
  1. 38 26
      web/building_saas/main/js/models/cache_tree.js

+ 38 - 26
web/building_saas/main/js/models/cache_tree.js

@@ -54,16 +54,15 @@ var cacheTree = {
                     nodes[i].nextSibling = (i === nodes.length - 1) ? null : nodes[i + 1];
                 }
             },
-            // 在nodes中,从iIndex(包括)开始全部移除
+            // 锟斤拷nodes锟叫o拷锟斤拷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;
-                }
-                if (next) {
-                    next.preSibling = pre;
+                    pre.setNextSibling(next);
+                } else if (next) {
+                    next.preSibling = null;
                 }
                 if (arguments.length === 4) {
                     children.splice(iIndex, count);
@@ -71,7 +70,7 @@ var cacheTree = {
                     children.splice(iIndex, children.length - iIndex);
                 }
             },
-            // 在nodes中增加addNodes, 位置从index开
+            // 锟斤拷nodes锟斤拷锟斤拷锟斤拷addNodes, 位锟矫达拷index锟斤拷
             addNodes: function (tree, parent, nodes, iIndex) {
                 var children = parent ? parent.children : tree.roots;
                 var pre, next, i;
@@ -84,13 +83,11 @@ var cacheTree = {
                     next = null;
                 }
                 if (pre) {
-                    pre.nextSibling = nodes[0];
-                }
-                nodes[0].preSibling = pre;
-                if (next) {
-                    next.preSibling = nodes[nodes.length - 1];
+                    pre.setNextSibling(nodes[0]);
+                } else {
+                    nodes[0].preSibling = null;
                 }
-                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]);
@@ -104,7 +101,7 @@ var cacheTree = {
 
         var Node = function (tree, id) {
             var ID = id;
-            // 以下的属性,本单元外均不可直接修改
+            // 锟斤拷锟铰碉拷锟斤拷锟皆o拷锟斤拷锟斤拷元锟斤拷锟斤拷锟斤拷锟斤拷直锟斤拷锟睫革拷
             this.tree = tree;
             this.children = [];
 
@@ -129,6 +126,16 @@ var cacheTree = {
             return this.nextSibling ? this.nextSibling.getID() : -1;
         };
 
+        Node.prototype.setParent = function (parent) {
+            this.parent = parent;
+        };
+        Node.prototype.setNextSibling = function (nextSibling) {
+            this.nextSibling = nextSibling;
+            if (nextSibling) {
+                nextSibling.preSibling = this;
+            }
+        }
+
         Node.prototype.firstChild = function () {
             return this.children.length === 0 ? null : this.children[0];
         };
@@ -248,10 +255,13 @@ var cacheTree = {
             var success = false;
             var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
             if (this.canUpMove()) {
-                orgPre.nextSibling = this.nextSibling;
-                this.preSibling = orgPre.preSibling;
-                orgPre.preSibling = this;
-                this.nextSibling = orgPre;
+                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);
                 this.tree.sortTreeItems();
@@ -263,10 +273,13 @@ var cacheTree = {
             var success = false;
             var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
             if (this.canDownMove()) {
-                orgNext.preSibling = this.preSibling;
-                this.nextSibling = orgNext.nextSibling;
-                orgNext.nextSibling = this;
-                this.preSibling = orgNext;
+                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);
                 this.tree.sortTreeItems();
@@ -353,10 +366,9 @@ var cacheTree = {
             if (node) {
                 delete this.nodes[this.prefix + node.getID()];
                 if (node.preSibling) {
-                    node.preSibling.nextSibling = node.nextSibling;
-                }
-                if (node.nextSibling) {
-                    node.nextSibling.preSibling = 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);
@@ -371,4 +383,4 @@ var cacheTree = {
 
         return new Tree(owner);
     }
-};
+};