From fe6dd0bd01d3289644b1834f0614e9f3ba5801c8 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 25 Sep 2015 00:51:53 -0400
Subject: [PATCH] check id name for compoiste validation in one place

---
 js/cells/supercells/DMASuperCell.js  |  6 ++--
 js/lattice/CompositeEditorLattice.js |  6 ++--
 js/lattice/LatticeBase.js            |  6 ++--
 js/materials/DMAMaterials.js         | 19 +++++++++---
 js/models/AppState.js                | 46 ++++++++++++++++------------
 5 files changed, 49 insertions(+), 34 deletions(-)

diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js
index 2baea810..06b1eb0a 100644
--- a/js/cells/supercells/DMASuperCell.js
+++ b/js/cells/supercells/DMASuperCell.js
@@ -4,8 +4,8 @@
 
 
 
-define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
-    function(_, THREE, three, lattice, appState, DMACell){
+define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell', 'materials'],
+    function(_, THREE, three, lattice, appState, DMACell, materials){
 
     function DMASuperCell(json, superCell){//supercells might have supercells
 
@@ -64,7 +64,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
 
     DMASuperCell.prototype._makeSubCellForIndex = function(json, callback){
         var subclassFile = lattice.getCellSubclassFile();
-        if (json.materialID && json.materialID.substr(0,5) == "super") subclassFile = "compositeCell";
+        if (json.materialID && materials.isComposite(json.materialID)) subclassFile = "compositeCell";
         var self = this;
         require([subclassFile], function(CellSubclass){
             var cell = new CellSubclass(json, self);
diff --git a/js/lattice/CompositeEditorLattice.js b/js/lattice/CompositeEditorLattice.js
index 83bd8f57..d4783e73 100644
--- a/js/lattice/CompositeEditorLattice.js
+++ b/js/lattice/CompositeEditorLattice.js
@@ -25,9 +25,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             if (!options.id || options.id == "") this.set("id", "super" + this.cid);
             else {
                 //change material type to allowed type
-                var currentMaterial = appState.get("materialType");
-                if (currentMaterial.substr(0,5) == "super"){
-                    if (materials.getVaildAvailableCompositeKeys(options.id).indexOf(currentMaterial) < 0){
+                var currentMaterialID = appState.get("materialType");
+                if (materials.isComposite(currentMaterialID)){
+                    if (materials.getVaildAvailableCompositeKeys(options.id).indexOf(currentMaterialID) < 0){
                         materials.setToDefaultMaterial(true);
                     }
                 }
diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js
index a48fefcb..555b8bf5 100644
--- a/js/lattice/LatticeBase.js
+++ b/js/lattice/LatticeBase.js
@@ -8,8 +8,8 @@
  */
 
 
-define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'threeModel'],
-    function(_, Backbone, appState, globals, plist, THREE, three){
+define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'threeModel', 'materials'],
+    function(_, Backbone, appState, globals, plist, THREE, three, materials){
 
     return Backbone.Model.extend({
 
@@ -95,7 +95,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
 
         makeCellForLatticeType: function(json, callback){
             var subclassFile = this.getCellSubclassFile();
-            if (json.materialID && json.materialID.substr(0,5) == "super") subclassFile = "compositeCell";
+            if (json.materialID && materials.isComposite(json.materialID)) subclassFile = "compositeCell";
             require([subclassFile], function(CellSubclass){
                 var cell = new CellSubclass(json);
                 if (callback) callback(cell);
diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js
index a1ae8b90..07371187 100644
--- a/js/materials/DMAMaterials.js
+++ b/js/materials/DMAMaterials.js
@@ -107,12 +107,18 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
 
 
 
-
-
+    function isComposite(id){
+        return id.substr(0,5) == "super";
+//
+//        var material = getMaterialForId(id);
+//        if (material) return material.isComposite();
+//        console.warn("no material found with id = " + id);
+//        return false;
+    }
 
     function getCompositeKeys(){
-        return _.filter(_.keys(materialsList), function(key){
-            return key.substr(0,5) == "super";
+        return _.filter(materialsList, function(material){
+            return material.isComposite();
         });
     }
 
@@ -138,7 +144,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
         var children = [];
         loopCells(cells, function(cell){
             if (!cell) return;
-            var isComposite = cell.materialID.substr(0,5) == "super";
+            var isComposite = isComposite(cell.materialID);
             if ((elementaryTypes && !isComposite) || (!elementaryTypes && isComposite)) children.push(cell.materialID);
             if (isComposite){
                 if (elementaryTypes && materialsList[cell.materialID].elementaryChildren) {
@@ -249,6 +255,9 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
         setMaterial: setMaterial,
         deleteMaterial: deleteMaterial,
         getMaterialForId: getMaterialForId,
+        isComposite: isComposite,
+
+
         getCompositeKeys: getCompositeKeys,
         getVaildAvailableCompositeKeys: getVaildAvailableCompositeKeys,
         getChildCellTypes:getChildCellTypes,
diff --git a/js/models/AppState.js b/js/models/AppState.js
index af3ed3ba..b72126a8 100644
--- a/js/models/AppState.js
+++ b/js/models/AppState.js
@@ -5,7 +5,8 @@
 //a class to store global app state, model for navbar and menu wrapper
 //never deallocated
 
-define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'], function(_, Backbone, three, THREE, plist, globals){
+define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'],
+    function(_, Backbone, three, THREE, plist, globals){
 
     var AppState = Backbone.Model.extend({
 
@@ -82,11 +83,11 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'], fu
         },
 
 
-        ///////////////////////////////////////////////////////////////////////////////
-        /////////////////////EVENTS////////////////////////////////////////////////////
-        ///////////////////////////////////////////////////////////////////////////////
 
 
+
+        //events
+
         _tabChanged: function(){
             var currentTab = this.get("currentTab");
             if (currentTab != "animate") this.set("stockSimulationPlaying", false);
@@ -128,21 +129,26 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'], fu
         },
 
         _materialTypeChanged: function(){
-            var materialType = this.get("materialType");
-            //verify that correct class is in sync
-            if (materialType.substr(0,5) != "super") {
-                if (this.previous("materialType").substr(0,5) != "super") return;
-                //re init highlighter
-                require([this.lattice.getHighlighterFile()], function(HighlighterClass){
-                    globals.highlighter = new HighlighterClass();
-                });
-                return;
-            }
 
-            //composite material
-            require(['superCellHighlighter'], function(SuperCellHighlighter){
-                globals.highlighter = new SuperCellHighlighter();
+            var self = this;
+            require(['materials'], function(materials){
+                var materialType = this.get("materialType");
+                //verify that correct class is in sync
+                if (materials.isComposite(materialType)) {
+                    if (materials.isComposite(self.previous("materialType"))) return;
+                    //re init highlighter
+                    require([self.lattice.getHighlighterFile()], function(HighlighterClass){
+                        globals.highlighter = new HighlighterClass();
+                    });
+                    return;
+                }
+
+                //composite material
+                require(['superCellHighlighter'], function(SuperCellHighlighter){
+                    globals.highlighter = new SuperCellHighlighter();
+                });
             });
+
         },
 
         _gikLengthChanged: function(){
@@ -180,9 +186,9 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'], fu
         },
 
 
-        ///////////////////////////////////////////////////////////////////////////////
-        /////////////////////KEY BINDINGS//////////////////////////////////////////////
-        ///////////////////////////////////////////////////////////////////////////////
+
+
+        //key bindings
 
         _handleKeyStroke: function(e){//receives keyup and keydown
 
-- 
GitLab