From bdb8a69e39dc6537f035a2737c237f20bf9f6911 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 12 Jun 2015 09:18:56 -0700
Subject: [PATCH] delete supercell

---
 js/cells/DMACell.js                 | 15 +++++++++++----
 js/cells/supercells/DMASuperCell.js |  7 +++++--
 js/highlighter/Highlighter.js       |  3 ++-
 js/lattice/LatticeBase.js           |  1 +
 4 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 1cc4227f..1b4989c5 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -153,6 +153,10 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
     };
 
     DMACell.prototype.setDeleteMode = function(state){
+        if (this.superCell) {
+            this.superCell.setDeleteMode(state);
+            return;
+        }
         var material;
         if (state) material = globals.materials.deleteMaterial;
         else  material = this.getMaterial();
@@ -162,6 +166,11 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
         three.render();
     };
 
+    DMACell.prototype.getParent = function(){
+        if (this.superCell) return this.superCell.getParent();
+        return this;
+    };
+
 
 
 
@@ -327,10 +336,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
         this.destroyParts();
         if (this.object3D) {
             if (this.superCell) this.superCell.removeChildren(this.object3D);
-            else if (this.index) {
-                three.sceneRemove(this.object3D);
-                if (!this.cells) lattice.getUItarget().removeHighlightableCell(this.object3D.children[0]);//remove mesh as highlightable object
-            }
+            else if (this.index) three.sceneRemove(this.object3D);
+            if (!this.cells) lattice.getUItarget().removeHighlightableCell(this.object3D.children[0]);//remove mesh as highlightable object
             this.object3D.myParent = null;
     //            this.object3D.dispose();
     //            geometry.dispose();
diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js
index 3cf1bb40..0d8c7fa0 100644
--- a/js/cells/supercells/DMASuperCell.js
+++ b/js/cells/supercells/DMASuperCell.js
@@ -98,10 +98,13 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
 
     DMASuperCell.prototype.destroy = function(){
         this._iterCells(function(cell){
-            if (cell) cell.destroy();
+            if (cell) {
+                cell.destroy();
+                cell = null;
+            }
         });
-        this.cells = null;
         DMACell.prototype.destroy.call(this);
+        this.cells = null;
     };
 
     DMASuperCell.prototype.destroyParts = function(){
diff --git a/js/highlighter/Highlighter.js b/js/highlighter/Highlighter.js
index c6595e33..884b5b52 100644
--- a/js/highlighter/Highlighter.js
+++ b/js/highlighter/Highlighter.js
@@ -86,6 +86,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', '
             var highlighted = intersection.object.parent;//cell mesh parent is object3d
             if (!highlighted.myParent) {//myParent is cell instance
                 console.warn("no parent for highlighted object");
+                console.warn(highlighted);
                 return;
             }
 
@@ -159,7 +160,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', '
             } else {
                 if (!this.highlightedObject) return;
                 if (!(this.highlightedObject instanceof DMACell)) return;
-                lattice.getUItarget().removeCell(this.highlightedObject);
+                lattice.getUItarget().removeCell(this.highlightedObject.getParent());
             }
             this.setNothingHighlighted();
         },
diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js
index 5b2c8e29..c7d38bb4 100644
--- a/js/lattice/LatticeBase.js
+++ b/js/lattice/LatticeBase.js
@@ -156,6 +156,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
     //    },
 
         removeCell: function(cell){
+
             if (!cell) return;
             var index = (new THREE.Vector3()).subVectors(cell.index, this.get("cellsMin"));
             cell.destroy();
-- 
GitLab