From 8c4616e95e89906d4f5017c6465ebb63e251d849 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Mon, 21 Sep 2015 23:17:37 -0400
Subject: [PATCH] composite editing

---
 js/lattice/Lattice.js                     |  25 ++----
 js/materials/DMAMaterials.js              |  17 +++-
 js/menus/CompositeMenuView.js             | 104 +++++++++++++---------
 js/menus/MaterialEditorMenuView.js        |  12 ++-
 js/menus/MaterialMenuView.js              |  31 +++----
 js/menus/MenuParentView.js                |   5 ++
 js/menus/templates/CompositeMenuView.html |   6 +-
 js/menus/templates/MaterialMenuView.html  |   2 +-
 8 files changed, 115 insertions(+), 87 deletions(-)

diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js
index 192be85e..2a174d16 100644
--- a/js/lattice/Lattice.js
+++ b/js/lattice/Lattice.js
@@ -194,7 +194,8 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
 
         _navChanged: function(){
             var currentNav = appState.get("currentNav");
-            if (currentNav != "navComposite" && this.compositeEditor && this.exitCompositeEditing) this.exitCompositeEditing();
+            if (currentNav != "navComposite" && this.compositeEditor) this.exitCompositeEditing();
+
             currentNav = plist.allMenus[currentNav].parent || currentNav;
             if (currentNav == "navSim" || currentNav == "navAssemble") this._parseSparseCell();
         },
@@ -209,26 +210,12 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             };
         },
 
-        setToCompositeMode: function(id, data){
-            var self = this;
-            require(['compositeEditorLattice'], function(CompositeEditorLattice){
-                self.hideCells();
-                if (self.compositeEditor) {
-                    console.warn("composite editor already allocated");
-                    self.compositeEditor.destroy();
-                }
-                self.compositeEditor = new CompositeEditorLattice(_.extend({id:id}, _.omit(data, "sparseCells")), null, function(_self){
-                    var cells = null;
-                    if (data) cells = data.sparseCells;
-                    _self._reloadCells(cells, self._getSubclassForLatticeType());
-                    appState.set("currentNav", "navComposite");
-                });
-
-            });
+        setToCompositeMode: function(compositeLattice){
+            this.compositeEditor = compositeLattice;
         },
 
         inCompositeMode: function(){
-            return this.compositeEditor != null;
+            return this.compositeEditor !== null && this.compositeEditor !== undefined;
         },
 
         _isSingltonLattice: function(){
@@ -246,7 +233,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
             return this;
         },
 
-        reinitAllCellsOfTypes: function(types){
+        reinitAllCellsOfTypes: function(types){//when material definition is changed
             this._loopCells(this.sparseCells, function(cell, x, y, z, self){
                 if (cell && cell.materialName.substr(0,5) == "super" && types.indexOf(cell.materialName) > -1){
                     //re-init cell;
diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js
index 7bac6704..9d0d99ad 100644
--- a/js/materials/DMAMaterials.js
+++ b/js/materials/DMAMaterials.js
@@ -13,6 +13,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
         }
     };
 
+
     var listener = {};
     _.extend(listener, Backbone.Events);
 
@@ -61,6 +62,10 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
     }
 
     function deleteMaterial(id){
+        if (materialsList[id] === undefined){
+            console.warn("this material was never saved");
+            return true;
+        }
         if (materialsList[id].noDelete) {
             console.warn("no delete flag on this material type");
             return false;
@@ -235,8 +240,16 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
 
 
 
+    var compositeID = 0;
+    var materialID = 0;
 
+    function getNextCompositeID(){
+        return "super" + compositeID++;
+    }
 
+    function getNextMaterialID(){
+        return "material" + materialID++;
+    }
 
 
     return {
@@ -248,6 +261,8 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
         getVaildAvailableCompositeKeys: getVaildAvailableCompositeKeys,
         getChildCellTypes:getChildCellTypes,
         setToDefaultMaterial: setToDefaultMaterial,
-        getDeleteMaterial: getDeleteMaterial
+        getDeleteMaterial: getDeleteMaterial,
+        getNextCompositeID: getNextCompositeID,
+        getNextMaterialID: getNextMaterialID
     };
 });
\ No newline at end of file
diff --git a/js/menus/CompositeMenuView.js b/js/menus/CompositeMenuView.js
index 66eceac2..8ae3da36 100644
--- a/js/menus/CompositeMenuView.js
+++ b/js/menus/CompositeMenuView.js
@@ -2,8 +2,8 @@
  * Created by aghassaei on 6/10/15.
  */
 
-define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'materials', 'text!compositeMenuTemplate', 'fileSaver'],
-    function($, _, MenuParentView, plist, lattice, globals, materials, template, fileSaver){
+define(['jquery', 'underscore', 'menuParent', 'compositeEditorLattice', 'plist', 'lattice', 'globals', 'materials', 'text!compositeMenuTemplate'],
+    function($, _, MenuParentView, CompositeEditorLattice, plist, lattice, globals, materials, template){
 
     var bounds;
 
@@ -11,36 +11,63 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma
 
         events: {
             "click #newRandomColor":                                  "_changeRandomColor",
-            "click #finishComposite":                                 "_finishComposite",
-            "click #saveComposite":                                   "_saveComposite",
-            "click #cancelComposite":                                 "_cancelComposite",
-            "click #deleteComposite":                                 "_deleteComposite"
+            "click #saveComposite":                                   "_saveCompositeToFile"
         },
 
-        _initialize: function(){
+        _initialize: function(options){
 
-            if (!lattice.compositeEditor) {
-                console.warn("no composite editor inited");
+            var id = options.myObject;
+            if (!id) {
+                console.warn("no composite editor id");
                 return;
             }
-            this.listenTo(lattice.compositeEditor, "change", function(){
-                if (lattice.compositeEditor.changedAttributes().numCells !== undefined) bounds = lattice.compositeEditor.calculateBoundingBox();
-                this.render();
+            var compositeData = {};
+            if (id == "fromLattice"){
+                id = materials.getNextCompositeID();
+                compositeData = lattice.getCompositeData();
+                lattice.clearCells();
+            } else if (options.myObject == "new"){
+                id = materials.getNextCompositeID();
+            } else {
+                compositeData = materials.getMaterialForId(id);
+            }
+
+            this.composite = this._setToCompositeMode(id, compositeData);
+
+            var self = this;
+            this.listenTo(this.composite, "change", function(){
+                if (this.composite.changedAttributes().numCells !== undefined) bounds = this.composite.calculateBoundingBox();
+                self.render();
             });
+            bounds = this.composite.calculateBoundingBox();
             this.listenTo(this.model, "change", this.render);
+        },
 
-            bounds = lattice.compositeEditor.calculateBoundingBox();
+        _setToCompositeMode: function(id, data){
+            lattice.hideCells();
+            if (lattice.inCompositeMode()) {
+                console.warn("composite editor already allocated");
+                lattice.exitCompositeEditing();
+            }
+            var compositeLattice = new CompositeEditorLattice(_.extend({id:id}, _.omit(data, "sparseCells")), null, function(_composite){
+                var cells = null;
+                if (data) cells = data.sparseCells;
+                _composite._reloadCells(cells, lattice._getSubclassForLatticeType());
+            });
+
+            lattice.setToCompositeMode(compositeLattice);
+            return compositeLattice;
         },
 
 
 
         _changeRandomColor: function(e){
             e.preventDefault();
-            lattice.compositeEditor._changeRandomColor();
+            this.composite._changeRandomColor();
         },
 
         getPropertyOwner: function($target){
-            if ($target.hasClass("compositeEditor")) return lattice.compositeEditor;
+            if ($target.hasClass("compositeEditor")) return this.composite;
             return null;
         },
 
@@ -49,49 +76,38 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma
             $target.css("border-color", hex);
         },
 
-        _finishComposite: function(e){
+        _saveCompositeToFile: function(e){
             e.preventDefault();
+            require(['fileSaver'], function(fileSaver){
+                fileSaver.saveMaterial(this.composite.get("id"), this.composite.toJSONForSave(bounds));
+            });
+        },
+
+        saveExitMenu: function(){
             this.stopListening();
-            if (!lattice.compositeEditor){
+            if (!this.composite){
                 console.warn("lattice not in composite mode for finish composite call");
-                this._exit();
-                return;
+                return false;
             }
-            lattice.compositeEditor.makeNewCompositeMaterial(bounds);
-            this._exit();
+            this.composite.makeNewCompositeMaterial(bounds);
+            return true;
         },
 
-        _saveComposite: function(e){
-            e.preventDefault();
-            fileSaver.saveMaterial(lattice.compositeEditor.get("id"), lattice.compositeEditor.toJSONForSave(bounds));
-        },
-
-        _cancelComposite: function(e){
-            e.preventDefault();
-            this._exit();
-        },
-
-        _deleteComposite: function(e){
-            e.preventDefault();
-            if (!lattice.compositeEditor){
+        deleteExitMenu: function(){
+            if (!this.composite){
                 console.warn("lattice not in composite mode for delete composite call");
-                this._exit();
-                return;
+                return true;
             }
-            var deleted = materials.setMaterial(lattice.compositeEditor.get("id"), null);
-            if (deleted) this._exit();
-        },
-
-        _exit: function(){
-            this.model.set("currentNav", "navDesign");
+            var deleted = materials.deleteMaterial(this.composite.get("id"));
+            return deleted;
         },
 
         _makeTemplateJSON: function(){
-            return _.extend(this.model.toJSON(), plist, globals, lattice.compositeEditor.toJSON(),
+            return _.extend(this.model.toJSON(), plist, globals, this.composite.toJSON(),
                 {
                     dimensions: bounds.max.clone().sub(bounds.min),
                     materials: materials.list,
-                    validCompositeMaterials: materials.getVaildAvailableCompositeKeys(lattice.compositeEditor.get("id"))
+                    validCompositeMaterials: materials.getVaildAvailableCompositeKeys(this.composite.get("id"))
                 });
         },
 
diff --git a/js/menus/MaterialEditorMenuView.js b/js/menus/MaterialEditorMenuView.js
index 7d7c6046..d7e0a224 100644
--- a/js/menus/MaterialEditorMenuView.js
+++ b/js/menus/MaterialEditorMenuView.js
@@ -16,20 +16,24 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materials', 'text!materi
         },
 
         _initialize: function(options){
-            //bind events
+
+            var id = options.myObject;
             if (!options.myObject) {
                 console.warn("no editing material id passed in");
                 this.model.set("currentNav", plist.allMenus.navMaterial.parent);
             }
-            this.materialID = options.myObject;
-            this.material = _.clone(materials.getMaterialForId(options.myObject));
-            if (!this.material) this.material = {
+            if (id == "new") id = materials.getNextMaterialID();
+            this.materialID = id;
+
+            var material = _.clone(materials.getMaterialForId(options.myObject));//todo do this in dmamaterials
+            if (!material) material = {
                 name: "Material " + materialNameIndex++,
                 color: '#000000',
                 altColor: '#000000',
                 noDelete: false,
                 properties: {}
             };
+            this.material = material;
 
         },
 
diff --git a/js/menus/MaterialMenuView.js b/js/menus/MaterialMenuView.js
index 3df456fe..c8cb6b91 100644
--- a/js/menus/MaterialMenuView.js
+++ b/js/menus/MaterialMenuView.js
@@ -5,12 +5,10 @@
 define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'materials', 'text!materialMenuTemplate'],
     function($, _, MenuParentView, plist, lattice, globals, materials, template){
 
-    var materialID = 0;
-
     return MenuParentView.extend({
 
         events: {
-            "click #navToCompositeMenu":                             "_navToCompositeMenu",
+            "click #newComposite":                                   "_newComposite",
             "click #compositeFromLattice":                           "_latticeToComposite",
             "click .editComposite":                                  "_editComposite",
             "click .editMaterial":                                   "_editMaterial",
@@ -23,16 +21,26 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma
             this.listenTo(this.model, "change", this.render);
         },
 
-        _navToCompositeMenu: function(e){
+        _newComposite: function(e){
             e.preventDefault();
-            lattice.setToCompositeMode();
+            this._openCompositeEditor("new");
         },
 
         _editComposite: function(e){
             e.stopPropagation();
             e.preventDefault();
-            var id = $(e.target).data("id");
-            lattice.setToCompositeMode(id,materials.getMaterialForId(id));
+            this._openCompositeEditor($(e.target).data("id"));
+        },
+
+        _latticeToComposite: function(e){
+            e.preventDefault();
+            this._openCompositeEditor("fromLattice");
+        },
+
+        _openCompositeEditor: function(compositeID){
+            require(['menuWrapper'], function(menuWrapper){
+                menuWrapper.initTabWithObject(compositeID, "composite", "navComposite")
+            });
         },
 
         _editMaterial: function(e){
@@ -43,8 +51,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma
 
         _newMaterial: function(e){
             e.preventDefault();
-            var id = "material" + this.cid + materialID++;//first create new material id
-            this._openMaterialEditor(id);
+            this._openMaterialEditor("new");
         },
 
         _openMaterialEditor: function(materialID){
@@ -53,12 +60,6 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma
             });
         },
 
-        _latticeToComposite: function(e){
-            lattice.setToCompositeMode(null, lattice.getCompositeData());
-            lattice.clearCells();
-            e.preventDefault();
-        },
-
         _makeTemplateJSON: function(){
             return _.extend(lattice.toJSON(), this.model.toJSON(), plist, globals, {inSimMode:false, materials:materials.list, compositeMaterialsKeys:materials.getCompositeKeys()});
         },
diff --git a/js/menus/MenuParentView.js b/js/menus/MenuParentView.js
index 135de2e9..19336de4 100644
--- a/js/menus/MenuParentView.js
+++ b/js/menus/MenuParentView.js
@@ -32,7 +32,12 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone){
             return true;
         },
 
+        shouldRender: function(){
+            return true;
+        },
+
         render: function(){
+            if (!this.shouldRender()) return;
             if (this.model.changedAttributes()["currentNav"]) return;
             if ($("input[type=text]").is(":focus")) return;
             if (this._preRender) this._preRender();
diff --git a/js/menus/templates/CompositeMenuView.html b/js/menus/templates/CompositeMenuView.html
index 11219b1d..eecf3cc0 100644
--- a/js/menus/templates/CompositeMenuView.html
+++ b/js/menus/templates/CompositeMenuView.html
@@ -34,7 +34,7 @@ Available Materials:<br/>
         <span><%= materials[key].name %></span></div>
     </label>
 <% }); %><br/>
-<a id="finishComposite" href="#" class="btn btn-block btn-lg btn-success">Save Composite</a><br/>
-<a id="deleteComposite" href="#" class="btn btn-halfWidth btn-lg btn-default"><span class="fui-trash"></span> Delete</a>
-<a id="cancelComposite" href="#" class="btn btn-halfWidth pull-right btn-lg btn-default">Cancel / Exit</a><br/><br/>
+<a id="saveExitMenu" href="#" class="btn btn-block btn-lg btn-success">Save Composite</a><br/>
+<a id="deleteExitMenu" href="#" class="btn btn-halfWidth btn-lg btn-default"><span class="fui-trash"></span> Delete</a>
+<a id="cancelExitMenu" href="#" class="btn btn-halfWidth pull-right btn-lg btn-default">Cancel / Exit</a><br/><br/>
 <a id="saveComposite" href="#" class="btn btn-block btn-lg btn-default">Save Composite To File</a><br/><br/>
diff --git a/js/menus/templates/MaterialMenuView.html b/js/menus/templates/MaterialMenuView.html
index a34ba638..3b3f1b80 100644
--- a/js/menus/templates/MaterialMenuView.html
+++ b/js/menus/templates/MaterialMenuView.html
@@ -52,7 +52,7 @@ Use realistic color scheme</label><br/>
                 <input data-property="superCellIndex" data-key="y" value="<%= superCellIndex.y %>" placeholder="Y" class="form-control intInput appState" type="text">&nbsp;
                 <input data-property="superCellIndex" data-key="z" value="<%= superCellIndex.z %>" placeholder="Z" class="form-control intInput appState" type="text"><br/><br/>
         <% } %>
-        <a id="navToCompositeMenu" href="#" class="btn btn-block btn-lg btn-default">+ Create New Composite Material</a><br/>
+        <a id="newComposite" href="#" class="btn btn-block btn-lg btn-default">+ Create New Composite Material</a><br/>
         <a id="compositeFromLattice" href="#" class="btn btn-block btn-lg btn-default">New Composite From Current Assembly</a><br/>
     <% } %>
 <% } %>
\ No newline at end of file
-- 
GitLab