From 682c100d4b35df558f90b3dd58f9036bf4e97aa5 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 27 Jan 2015 00:40:32 -0500
Subject: [PATCH] clear all cells

---
 js/menus/LatticeMenuView.js |  9 ++++++++-
 js/models/lattice.js        |  9 ++++++---
 js/models/threeModel.js     | 21 +++++++++++----------
 js/threeViews/threeView.js  |  1 +
 4 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/js/menus/LatticeMenuView.js b/js/menus/LatticeMenuView.js
index 30ee3467..61fd323b 100644
--- a/js/menus/LatticeMenuView.js
+++ b/js/menus/LatticeMenuView.js
@@ -8,6 +8,8 @@ LatticeMenuView = Backbone.View.extend({
     el: "#menuContent",
 
     events: {
+        "click #latticeMenuClearCells":                "_clearCells"
+
     },
 
     initialize: function(){
@@ -16,6 +18,11 @@ LatticeMenuView = Backbone.View.extend({
         this.listenTo(this.model, "change", this.render);
     },
 
+    _clearCells: function(e){
+        e.preventDefault();
+        this.model.clearCells();
+    },
+
     _formatData: function(){
         var formattedCellType = "Octagon";
         var formattedConnectionType = "Face-Connected";
@@ -32,7 +39,7 @@ LatticeMenuView = Backbone.View.extend({
         Scale:&nbsp;&nbsp;<%= scale %><br/>\
         Column Separation:<br/>\
         NumCells:&nbsp;&nbsp;<%= numCells %><br/><br/>\
-        <a href="#" class=" btn btn-block btn-lg btn-default">Clear All Cells</a><br/>\
+        <a href="#" id="latticeMenuClearCells" class=" btn btn-block btn-lg btn-default">Clear All Cells</a><br/>\
         ')
 
 });
\ No newline at end of file
diff --git a/js/models/lattice.js b/js/models/lattice.js
index fef1ebfd..8466fd0f 100644
--- a/js/models/lattice.js
+++ b/js/models/lattice.js
@@ -22,14 +22,16 @@ Lattice = Backbone.Model.extend({
     },
 
     addCell: function(position){
-        new Cell(position);
+        this.get("cells").push(new Cell(position));
         this.set("numCells", this.get("numCells")+1);
         window.three.render();
     },
 
     removeCell: function(object){
-        window.three.sceneRemove(object);
+        var cells = this.get("cells");
+        cells.splice(cells.indexOf(object), 1);
         this.set("numCells", this.get("numCells")-1);
+        window.three.sceneRemove(object);
         window.three.render();
     },
 
@@ -38,7 +40,8 @@ Lattice = Backbone.Model.extend({
             cell.remove();
         });
         this.set("cells", []);
-        this.set("numCells, 0");
+        this.set("numCells", 0);
+        window.three.render();
     }
 
 });
\ No newline at end of file
diff --git a/js/models/threeModel.js b/js/models/threeModel.js
index ff5a5e8d..a4ccba9c 100644
--- a/js/models/threeModel.js
+++ b/js/models/threeModel.js
@@ -54,7 +54,7 @@ function ThreeModel(){
 
     function sceneRemove(object){
         var objectToRemove = object;
-        if (object.parent !== THREE.Scene) {
+        if (object.parent.type != "Scene") {
             objectToRemove = object.parent;
         }
         scene.remove(objectToRemove);
@@ -66,15 +66,16 @@ function ThreeModel(){
         renderer.render(scene, camera);
     }
 
-    function clearAll(){x
-        var children = scene.children;
-        for (var i=children.length;i>=0;i--){
-            var object = children[i];
-            if (object instanceof THREE.Mesh){// && object != this.fillGeometry.get("mesh")
-                scene.remove(object);
-            }
-        }
-        render();
+    function clearAll(){
+//        var children = objects.slice(0);
+//        for (var i=children.length;i>=0;i--){
+//            var object = children[i];
+//            if (!(object instanceof THREE.Mesh)){// && object != this.fillGeometry.get("mesh")
+//                scene.remove(object);
+//                objects.splice(objects.indexOf(object), 1);
+//            }
+//        }
+//        render();
     }
 
     return {//return public properties/methods
diff --git a/js/threeViews/threeView.js b/js/threeViews/threeView.js
index 1d9673cc..f9cc4b4f 100644
--- a/js/threeViews/threeView.js
+++ b/js/threeViews/threeView.js
@@ -75,6 +75,7 @@ ThreeView = Backbone.View.extend({
                 this.controls.enabled = !state;
                 break;
             case 32://space bar
+                e.preventDefault();
                 this.deleteMode = state;
                 this.controls.enabled = !state;
                 break;
-- 
GitLab