From fc9f6ed1772d55c302e75d1c29e7951d5a4693c7 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 25 Jun 2015 16:02:03 -0700
Subject: [PATCH] possibility of infinite reference loops in composite
 definitions is gone

---
 js/lattice/CompositeEditorLattice.js | 13 +++++++++++++
 js/lattice/Lattice.js                |  1 -
 js/materials/DMAMaterials.js         | 16 +++++++++-------
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/js/lattice/CompositeEditorLattice.js b/js/lattice/CompositeEditorLattice.js
index 9cba622d..4356d6c8 100644
--- a/js/lattice/CompositeEditorLattice.js
+++ b/js/lattice/CompositeEditorLattice.js
@@ -20,7 +20,20 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
         }),
 
         __initialize: function(options, callback){
+
             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){
+                        materials.setToDefaultMaterial(true);
+                    }
+                }
+            }
+
+
+
             if (!options.color || options.color == "") this.set("color",  makeRandomColor(), {silent:true});
 
             if (callback) callback(this);
diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js
index f36df0bc..06cd6f96 100644
--- a/js/lattice/Lattice.js
+++ b/js/lattice/Lattice.js
@@ -208,7 +208,6 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
                     var json = cell.toJSON();
                     json.index = cell.getIndex();
                     self.makeCellForLatticeType(json, function(newCell){
-                        console.log(newCell);
                         self.sparseCells[x][y][z] = newCell;
                         cell.destroy();
                     });
diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js
index e8539bb5..debed48f 100644
--- a/js/materials/DMAMaterials.js
+++ b/js/materials/DMAMaterials.js
@@ -16,11 +16,11 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
     _.extend(listener, Backbone.Events);
 
     listener.listenTo(appState, "change:realisticColorScheme", changeColorScheme);
-    listener.listenTo(appState, "change:materialClass", loadMaterialClass);
-    listener.listenTo(lattice, "change:connectionType cellType", loadMaterialClass);
+    listener.listenTo(appState, "change:materialClass", function(){setToDefaultMaterial()});//pass no params
+    listener.listenTo(lattice, "change:connectionType cellType", function(){setToDefaultMaterial()});
     listener.listenTo(appState, "change:materialType", setMaterialDefaults);
 
-    loadMaterialClass();
+    setToDefaultMaterial();
 
 
 
@@ -61,7 +61,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
         }
         delete materialsList[id];//todo check if being used first
         var deleted = true;
-        if (deleted) loadMaterialClass();//set to defaults
+        if (deleted) setToDefaultMaterial();
         return deleted;
     }
 
@@ -139,12 +139,13 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
 
 
 
-    function loadMaterialClass(){
+    function setToDefaultMaterial(triggerEvent){
         var materialClass = appState.get("materialClass");
         var newDefaultType = _.keys(plist.allMaterials[materialClass])[0];
         if (!materialsList[newDefaultType]) _.extend(materialsList, parseClassFromDefinitions(plist.allMaterials[materialClass]));
         if (!materialsList[newDefaultType]) console.warn("material type " + newDefaultType + "  not in definition for " + materialClass);
-        appState.set("materialType", newDefaultType, {silent:true});//set to default silently
+        if (triggerEvent === undefined) triggerEvent = false;
+        appState.set("materialType", newDefaultType, {silent:!triggerEvent});
     }
 
     function parseClassFromDefinitions(definitions){
@@ -210,6 +211,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
         setMaterial: setMaterial,
         getCompositeKeys: getCompositeKeys,
         getVaildAvailableCompositeKeys: getVaildAvailableCompositeKeys,
-        getChildCellTypes:getChildCellTypes
+        getChildCellTypes:getChildCellTypes,
+        setToDefaultMaterial: setToDefaultMaterial
     };
 });
\ No newline at end of file
-- 
GitLab