From aaef075365bd3a847a8fd5031d2c345475c34c10 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 24 Feb 2015 15:14:43 -0500
Subject: [PATCH] remove all window calls, replace with dmaGlobals

---
 js/fea/DmaCell.js              |  6 +++---
 js/fea/DmaNode.js              |  2 +-
 js/fea/DmaPart.js              |  6 +++---
 js/main.js                     | 14 ++++++++------
 js/menus/LatticeMenuView.js    |  2 +-
 js/models/BasePlane.js         | 24 ++++++++++++------------
 js/models/FillGeometry.js      |  8 ++++----
 js/models/Lattice.js           | 18 +++++++++---------
 js/models/extrudeVisualizer.js |  6 +++---
 js/threeViews/Highlighter.js   | 10 +++++-----
 js/threeViews/ThreeView.js     |  8 ++++----
 11 files changed, 53 insertions(+), 51 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index ef565e17..08d23a57 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -13,8 +13,8 @@ function DMACell(indices, scale, lattice) {
     this.parts = this._initParts(indices.z);
     this.updateForScale(scale);
 
-    window.three.sceneAdd(this.cellMesh, "cell");
-    this.drawForMode(window.appState.get("cellMode"));
+    dmaGlobals.three.sceneAdd(this.cellMesh, "cell");
+    this.drawForMode(dmaGlobals.appState.get("cellMode"));
 }
 
 DMACell.prototype.removePart = function(index){
@@ -78,7 +78,7 @@ DMACell.prototype.getIndex = function(){
 
 DMACell.prototype.destroy = function(){
     if (this.cellMesh) {
-        window.three.sceneRemove(this.cellMesh, "cell");
+        dmaGlobals.three.sceneRemove(this.cellMesh, "cell");
         this.cellMesh.myParent = null;
 //            this.cellMesh.dispose();
 //            geometry.dispose();
diff --git a/js/fea/DmaNode.js b/js/fea/DmaNode.js
index 83f2057f..b20b8bb9 100644
--- a/js/fea/DmaNode.js
+++ b/js/fea/DmaNode.js
@@ -18,7 +18,7 @@ BeamNode.prototype.render = function(){
     geometry.applyMatrix( new THREE.Matrix4().makeTranslation(this.x, this.y, this.z) );
     var mesh = new THREE.Mesh(geometry);
 
-//    window.three.sceneAdd(mesh);
+//    dmaGlobals.three.sceneAdd(mesh);
 };
 
 
diff --git a/js/fea/DmaPart.js b/js/fea/DmaPart.js
index 34e02bfa..c7af2da0 100644
--- a/js/fea/DmaPart.js
+++ b/js/fea/DmaPart.js
@@ -16,7 +16,7 @@
         if (this.mesh) console.warn("part mesh already in scene");
         this.mesh = this._makeMeshForType(this.type);
         this.updateForScale(this.parentCell.getScale(), this.parentCell.getPosition());
-        window.three.sceneAdd(this.mesh, "part");
+        dmaGlobals.three.sceneAdd(this.mesh, "part");
     };
 
     DMAPart.prototype.updateForScale = function(scale, position){
@@ -53,13 +53,13 @@
     DMAPart.prototype.removeFromCell = function(){//send message back to parent cell to destroy this
         if (this.parentCell) {
             this.parentCell.removePart(this.type);
-            window.three.render();
+            dmaGlobals.three.render();
         } else console.warn("part has no parent cell");
     };
 
     DMAPart.prototype.destroy = function(){
         if (this.mesh) {
-            window.three.sceneRemove(this.mesh, "part");
+            dmaGlobals.three.sceneRemove(this.mesh, "part");
             this.mesh.myPart = null;
 //            this.mesh.dispose();
 //            geometry.dispose();
diff --git a/js/main.js b/js/main.js
index 6e90f8d9..3e6e6889 100644
--- a/js/main.js
+++ b/js/main.js
@@ -7,22 +7,24 @@ $(function(){
 
     //setup persistent global variables
 
+    dmaGlobals = {};
+
     //init web workers
     window.workers = persistentWorkers(8);
 
     //init threeJS and geometry models
-    window.three = new ThreeModel();
+    dmaGlobals.three = new ThreeModel();
 
-    window.lattice = new Lattice();
-    window.appState = new AppState({lattice:window.lattice});
+    dmaGlobals.lattice = new Lattice();
+    dmaGlobals.appState = new AppState({lattice:dmaGlobals.lattice});
 
 
 
     //ui
-    new NavBar({model:appState});
+    new NavBar({model:dmaGlobals.appState});
 
     //threeJS View
-    new ThreeView({model:window.three, appState:appState});
+    new ThreeView({model:dmaGlobals.three, appState:dmaGlobals.appState});
 
-    window.lattice.addCellAtIndex({x:0,y:0,z:0});//add a cell
+    dmaGlobals.lattice.addCellAtIndex({x:0,y:0,z:0});//add a cell
 });
diff --git a/js/menus/LatticeMenuView.js b/js/menus/LatticeMenuView.js
index 04dc173d..47cefba1 100644
--- a/js/menus/LatticeMenuView.js
+++ b/js/menus/LatticeMenuView.js
@@ -43,7 +43,7 @@ LatticeMenuView = Backbone.View.extend({
 //        this.lattice.set("scale", $(e.target)[0].value);
         this.lattice.previewScaleChange(scale);//does not trigger lattice change event - no rerendering of ui
         $("#latticeScale").val(scale);
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     _changeScaleSlider: function(e){
diff --git a/js/models/BasePlane.js b/js/models/BasePlane.js
index f88fa498..c52308ad 100644
--- a/js/models/BasePlane.js
+++ b/js/models/BasePlane.js
@@ -27,9 +27,9 @@ BasePlane = Backbone.Model.extend({
 
         var self = this;
         _.each(this.get("mesh"), function(mesh){
-            window.three.sceneAdd(mesh, self._checkIsHighlightable(mesh));
+            dmaGlobals.three.sceneAdd(mesh, self._checkIsHighlightable(mesh));
         });
-        window.three.render();
+        dmaGlobals.three.render();
 
     },
 
@@ -52,7 +52,7 @@ BasePlane = Backbone.Model.extend({
     },
 
     getIndex: function(face){
-        return window.lattice.getIndexForPosition(face.geometry.vertices[0]);
+        return dmaGlobals.lattice.getIndexForPosition(face.geometry.vertices[0]);
     },
 
     //subclasses handle getHighlighterVertices
@@ -65,9 +65,9 @@ BasePlane = Backbone.Model.extend({
         var self = this;
         _.each(this.get("mesh"), function(mesh){
             if (mesh.myParent) mesh.myParent = null;
-            window.three.sceneRemove(mesh, self._checkIsHighlightable(mesh));
+            dmaGlobals.three.sceneRemove(mesh, self._checkIsHighlightable(mesh));
         });
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     destroy: function(){
@@ -128,11 +128,11 @@ OctaBasePlane = BasePlane.extend({
 
     _renderZIndexChange: function(){
         var zIndex = this.get("zIndex");
-        var scale = this.get("mesh")[0].scale.z;
+        var scale = dmaGlobals.lattice.get("scale");
         _.each(this.get("mesh"), function(mesh){
             mesh.position.set(0, 0, zIndex*scale*2/Math.sqrt(6));
         });
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     _calcOctaFaceVertices: function(colSep){
@@ -177,7 +177,7 @@ OctaBasePlane = BasePlane.extend({
         var mesh = this.get("mesh")[0];
         var vertices = mesh.geometry.vertices;
         var newVertices = [vertices[face.a].clone(), vertices[face.b].clone(), vertices[face.c].clone()];
-        var scale = mesh.scale.x;
+        var scale = dmaGlobals.lattice.get("scale");
         var position = (new THREE.Vector3()).setFromMatrixPosition(mesh.matrixWorld);
         _.each(newVertices, function(vertex){//apply scale
             vertex.multiplyScalar(scale);
@@ -225,18 +225,18 @@ SquareBasePlane = BasePlane.extend({
 
     _renderZIndexChange: function(){
         var zIndex = this.get("zIndex");
-        var scale = this.get("mesh")[0].scale.z;
+        var scale = dmaGlobals.lattice.get("scale");
         _.each(this.get("mesh"), function(mesh){
             mesh.position.set(0, 0, zIndex*scale);
         });
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     getHighlighterVertices: function(face, position){
         //the vertices don't include the position transformation applied to cell.  Add these to create highlighter vertices
-        var index = window.lattice.getIndexForPosition(position);
+        var index = dmaGlobals.lattice.getIndexForPosition(position);
         index.z += 1;
-        var scale = this.get("mesh")[0].scale.x;
+        var scale = dmaGlobals.lattice.get("scale");
         var vertices = [];
         vertices.push(new THREE.Vector3(index.x*scale, index.y*scale, index.z*scale));
         vertices.push(new THREE.Vector3((index.x+1)*scale, index.y*scale, index.z*scale));
diff --git a/js/models/FillGeometry.js b/js/models/FillGeometry.js
index 0aad2f7d..1b20f674 100644
--- a/js/models/FillGeometry.js
+++ b/js/models/FillGeometry.js
@@ -43,8 +43,8 @@ FillGeometry = Backbone.Model.extend({
         var mesh = new THREE.Mesh(this.get("geometry"), this.get("material"));
         this.makeBoundingBoxHelper(mesh);
         this.set({mesh: mesh});
-        window.three.sceneAdd(mesh, null);
-        window.three.render();
+        dmaGlobals.three.sceneAdd(mesh, null);
+        dmaGlobals.three.render();
 
         //send new geometry out to workers
 //            _.each(workers.allWorkers, function(worker){
@@ -74,9 +74,9 @@ FillGeometry = Backbone.Model.extend({
 
     remove: function(){
         if (!this.get("mesh")) return;
-        window.three.sceneRemove(this.get("mesh"), null);
+        dmaGlobals.three.sceneRemove(this.get("mesh"), null);
         this.set("mesh", null);
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     scale: function(scale){
diff --git a/js/models/Lattice.js b/js/models/Lattice.js
index c3705b85..70c12184 100644
--- a/js/models/Lattice.js
+++ b/js/models/Lattice.js
@@ -48,7 +48,7 @@ Lattice = Backbone.Model.extend({
                 }
             }
         }
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     addCellAtIndex: function(indices){
@@ -61,7 +61,7 @@ Lattice = Backbone.Model.extend({
         if (!cells[index.x][index.y][index.z]) {
             cells[index.x][index.y][index.z] = this._makeCellForLatticeType(indices, scale);
             this.set("numCells", this.get("numCells")+1);
-            window.three.render();
+            dmaGlobals.three.render();
         } else console.warn("already a cell there");
 
     },
@@ -85,7 +85,7 @@ Lattice = Backbone.Model.extend({
         //todo shrink cells matrix if needed
 
         this.set("numCells", this.get("numCells")-1);
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     clearCells: function(){
@@ -98,7 +98,7 @@ Lattice = Backbone.Model.extend({
         this.set("nodes", []);
         this.set("numCells", 0);
         if (this.get("basePlane")) this.get("basePlane").set("zIndex", 0);
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     ////////////////////////////////////////////////////////////////////////////////////
@@ -138,7 +138,7 @@ Lattice = Backbone.Model.extend({
             }
 
         }
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     _findIntersectionsInWindow: function(windowX, windowY, origin, allVertexPos){
@@ -264,7 +264,7 @@ Lattice = Backbone.Model.extend({
         this._iterCells(this.get("cells"), function(cell){
             if (cell && cell.drawForMode) cell.drawForMode(mode);
         });
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     _scaleDidChange: function(){
@@ -273,7 +273,7 @@ Lattice = Backbone.Model.extend({
         this._iterCells(this.get("cells"), function(cell){
             if (cell) cell.updateForScale(scale);
         });
-        window.three.render();
+        dmaGlobals.three.render();
     },
 
     previewScaleChange: function(scale){
@@ -348,7 +348,7 @@ Lattice = Backbone.Model.extend({
             this._iterCells(this.get("cells"), function(cell){
                 if (cell) cell.updateForScale(scale);
             });
-            window.three.render();
+            dmaGlobals.three.render();
         },
 
         getIndexForPosition: function(absPosition){
@@ -412,7 +412,7 @@ Lattice = Backbone.Model.extend({
             this._iterCells(this.get("cells"), function(cell){
                 if (cell) cell.updateForScale(scale);
             });
-            window.three.render();
+            dmaGlobals.three.render();
         },
 
         getIndexForPosition: function(absPosition){
diff --git a/js/models/extrudeVisualizer.js b/js/models/extrudeVisualizer.js
index 10cf1691..af363dba 100644
--- a/js/models/extrudeVisualizer.js
+++ b/js/models/extrudeVisualizer.js
@@ -18,10 +18,10 @@ function ExtrudeVisualizer(){
             mesh.position.x = profilePos.x;
             mesh.position.y = profilePos.y;
             mesh.position.z = profilePos.z;
-//            window.three.sceneAdd(mesh, false);
+//            dmaGlobals.three.sceneAdd(mesh, false);
             meshes.push(mesh);
         });
-        window.three.render();
+        dmaGlobals.three.render();
     }
 
     function makeHandle(){
@@ -36,7 +36,7 @@ function ExtrudeVisualizer(){
         _.each(meshes, function(mesh){
             mesh.scale.z = height;
         })
-        window.three.render();
+        dmaGlobals.three.render();
     }
 
     function renderIntoCells(){
diff --git a/js/threeViews/Highlighter.js b/js/threeViews/Highlighter.js
index c53d76b1..1ff9dd41 100644
--- a/js/threeViews/Highlighter.js
+++ b/js/threeViews/Highlighter.js
@@ -20,7 +20,7 @@ Highlighter = Backbone.View.extend({
                 vertexColors:THREE.FaceColors
             }));
 
-        window.three.sceneAdd(this.mesh, null);
+        dmaGlobals.three.sceneAdd(this.mesh, null);
         this.hide();
 
         //bind events
@@ -41,7 +41,7 @@ Highlighter = Backbone.View.extend({
     _setVisibility: function(visible, forceRender){
         if (forceRender || this.isVisible() != visible){
             this.mesh.visible = visible;
-            window.three.render();
+            dmaGlobals.three.render();
         }
         this.mesh.visible = visible;
     },
@@ -88,18 +88,18 @@ Highlighter = Backbone.View.extend({
 
         if (shouldAdd){
             if (!this.isVisible() || !this.highlightedObject) return;
-            window.lattice.addCellAtIndex(this._getNextCellPosition(this.highlightedObject.getIndex(this.mesh)));
+            dmaGlobals.lattice.addCellAtIndex(this._getNextCellPosition(this.highlightedObject.getIndex(this.mesh)));
         } else {
             if (!this.highlightedObject) return;
             if (!(this.highlightedObject instanceof DMACell)) return;
-            window.lattice.removeCell(this.highlightedObject);
+            dmaGlobals.lattice.removeCell(this.highlightedObject);
         }
         this.hide();
         this.highlightedObject = null;
     },
 
     destroy: function(){
-        window.three.sceneRemove(this.mesh, null);
+        dmaGlobals.three.sceneRemove(this.mesh, null);
         this.mesh = null;
         this.highlightedObject = null;
         this.stopListening();
diff --git a/js/threeViews/ThreeView.js b/js/threeViews/ThreeView.js
index e1533513..3fd1c6d1 100644
--- a/js/threeViews/ThreeView.js
+++ b/js/threeViews/ThreeView.js
@@ -30,7 +30,7 @@ ThreeView = Backbone.View.extend({
 
         //bind events
         this.listenTo(this.appState, "change:deleteMode change:extrudeMode change:shift", this._setControlsEnabled);
-        this.listenTo(window.lattice, "change:highlighter", this._saveHighlighter);
+        this.listenTo(dmaGlobals.lattice, "change:highlighter", this._saveHighlighter);
 
         this._saveHighlighter();//need a reference to the highlighter
 
@@ -125,14 +125,14 @@ ThreeView = Backbone.View.extend({
     ////////////////////////////////////////////////////////////////////////////////
 
     _saveHighlighter: function(){
-        this.highlighter = window.lattice.get("highlighter");
+        this.highlighter = dmaGlobals.lattice.get("highlighter");
     },
 
     _setNoPartIntersections: function(){
         if (this.currentIntersectedPart){
             this.currentIntersectedPart.unhighlight();
             this.currentIntersectedPart = null;
-            window.three.render();
+            dmaGlobals.three.render();
         }
     },
 
@@ -147,7 +147,7 @@ ThreeView = Backbone.View.extend({
             if (this.currentIntersectedPart) this.currentIntersectedPart.unhighlight();
             part.highlight();
             this.currentIntersectedPart = part;
-            window.three.render();
+            dmaGlobals.three.render();
         }
     }
 
-- 
GitLab