From 652d31ea8c56fe9570e788dc6f8460a338cff4bf Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Mon, 21 Sep 2015 20:37:45 -0400
Subject: [PATCH] menu init with object

---
 js/menus/MSetupMenuView.js  |  2 +-
 js/menus/MenuParentView.js  | 16 +++++++-
 js/menus/MenuWrapperView.js | 80 ++++++++++++++++++++++++++++++++-----
 js/plists/PList.js          |  4 +-
 4 files changed, 86 insertions(+), 16 deletions(-)

diff --git a/js/menus/MSetupMenuView.js b/js/menus/MSetupMenuView.js
index 7b08c3ba..53df0700 100644
--- a/js/menus/MSetupMenuView.js
+++ b/js/menus/MSetupMenuView.js
@@ -21,7 +21,7 @@ define(['jquery', 'underscore', 'menuParent', 'lattice', 'plist', 'text!mSetupMe
         },
 
         _makeTemplateJSON: function(){
-            return _.extend(this.model.toJSON(), plist. lattice.toJSON());
+            return _.extend(this.model.toJSON(), plist, lattice.toJSON());
         },
 
         template: _.template(template)
diff --git a/js/menus/MenuParentView.js b/js/menus/MenuParentView.js
index 45ebe547..e801b3d4 100644
--- a/js/menus/MenuParentView.js
+++ b/js/menus/MenuParentView.js
@@ -9,17 +9,29 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone){
 
         el: "#menuContent",
 
-        initialize: function(){
+        initialize: function(options){
 
             _.bindAll(this, "render");
 
-            if (this._initialize) this._initialize();//call subclass
+            if (this._initialize) this._initialize(options);//call subclass
         },
 
         getPropertyOwner: function($target){
             return null;//override in subclasses
         },
 
+        deleteExitMenu: function(e, callback){
+            callback();
+        },
+
+        cancelExitMenu: function(e, callback){
+            callback();
+        },
+
+        saveExitMenu: function(e, callback){
+            callback();
+        },
+
         render: function(){
             if (this.model.changedAttributes()["currentNav"]) return;
             if ($("input[type=text]").is(":focus")) return;
diff --git a/js/menus/MenuWrapperView.js b/js/menus/MenuWrapperView.js
index 0c6a8bff..3c8efc89 100644
--- a/js/menus/MenuWrapperView.js
+++ b/js/menus/MenuWrapperView.js
@@ -22,13 +22,20 @@ define(['jquery', 'underscore', 'plist', 'backbone', 'lattice', 'appState', 'tex
 
         initialize: function(){
 
+            this.currentNav = null;
+            this.currentTab = null;
+
             _.bindAll(this, "render", "_updateCurrentTab", "_setVisibility", "_hide", "_show", "_onKeyUp");
             $(document).bind('keyup', {}, this._onKeyUp);
 
             //bind events
-            this.listenTo(this.model, "change:currentNav", this.render);
+            this.listenTo(this.model, "change:currentNav", function(){
+                if (this.currentNav == this.model.get("currentNav")) return;
+                this.render();
+            });
             this.listenTo(lattice, "change:cellType change:connectionType change:latticeType", this._populateAndShow);
             this.listenTo(this.model, "change:currentTab", function(){
+                if (this.currentTab == this.model.get("currentTab")) return;
                 if (!this.model.changedAttributes() || this.model.changedAttributes()["currentNav"]) return;
                 this._updateCurrentTab();
             });
@@ -226,6 +233,38 @@ define(['jquery', 'underscore', 'plist', 'backbone', 'lattice', 'appState', 'tex
 
 
 
+        _deleteExitMenu: function(e){
+            e.preventDefault();
+            var nextNav = this._getNextNav();
+            this.menu.deleteExitMenu(e, function(){
+                appState.set("currentNav", nextNav);
+            });
+        },
+
+        _cancelExitMenu: function(e){
+            e.preventDefault();
+            var nextNav = this._getNextNav();
+            this.menu.cancelExitMenu(e, function(){
+                appState.set("currentNav", nextNav);
+            });
+        },
+
+        _saveExitMenu: function(e){
+            e.preventDefault();
+            var nextNav = this._getNextNav();
+            this.menu.saveExitMenu(e, function(){
+                appState.set("currentNav", nextNav);
+            });
+        },
+
+        _getNextNav: function(){
+            var parentNav = plist.allMenus[this.model.get("currentNav")].parent;
+            if (parentNav === undefined) console.warn("no parent nav found, unable to exit menu");
+            return parentNav || appState.get("currentNav");
+        },
+
+
+
 
         _tabWasSelected: function(e){
             e.preventDefault();
@@ -233,8 +272,25 @@ define(['jquery', 'underscore', 'plist', 'backbone', 'lattice', 'appState', 'tex
             this.model.set("currentTab", tabName);
         },
 
-        _updateCurrentTab: function(){
+        initTabWithObject: function(object, tab, nav){
+            //bypass appstate event listening
+            this.model.set("currentTab", tab, {silent:true});
+            if (nav === undefined || nav == this.model.get("currentNav")) this._updateCurrentTab(object);
+            else {
+                this.model.set("currentNav", nav, {silent:true});
+                this.render(object);
+            }
+            this.model.trigger("change:currentTab change:currentNav");
+        },
+
+        _updateCurrentTab: function(object){
             var tabName = this.model.get("currentTab");
+            this.currentTab = tabName;//todo
+            this._selectTab(tabName);
+            this._renderTab(tabName, object);
+        },
+
+        _selectTab: function(tabName){
             _.each($(".menuWrapperTab"), function(tab){
                 var $tab = $(tab);
                 if ($tab.data('name') == tabName){
@@ -243,7 +299,6 @@ define(['jquery', 'underscore', 'plist', 'backbone', 'lattice', 'appState', 'tex
                     $tab.removeClass("active");
                 }
             });
-            this._renderTab(tabName);
         },
 
         _softRenderTab: function(){
@@ -251,29 +306,32 @@ define(['jquery', 'underscore', 'plist', 'backbone', 'lattice', 'appState', 'tex
             else console.warn("no menu found");
         },
 
-        _renderTab: function(tabName){
+        _renderTab: function(tabName, object){
             if (!tabName || !_.isString(tabName)) tabName = this.model.get("currentTab");
 
             if (this.menu) this.menu.destroy();
             var self = this;
-            require([tabName + "Menu"], function(MenuView){
-                self.menu = new MenuView({model:self.model});
+            require(["menus/" + tabName.charAt(0).toUpperCase() + tabName.slice(1) + "MenuView"], function(MenuView){
+                var data = {model:self.model};
+                if (object) data.myObject = object;
+                self.menu = new MenuView(data);
                 self.menu.render();
             });
         },
 
-        render: function(){
+        render: function(object){
+            this.currentNav = this.model.get("currentNav");
             var self = this;
             this._hide(function(){
                 $("#menuContent").html("");//clear current menu
-                self._populateAndShow();
-                self.model.trigger("change:currentTab");//this was updated silently before todo need this?
+                self._populateAndShow(object);
+
             }, true);
         },
 
-        _populateAndShow: function(){
+        _populateAndShow: function(object){
             $("#menuHeader").html(this.template(_.extend(this.model.toJSON(), lattice.toJSON(), plist)));
-            this._updateCurrentTab();
+            this._updateCurrentTab(object);
             this._show();
         },
 
diff --git a/js/plists/PList.js b/js/plists/PList.js
index 845d2cff..b55c4598 100644
--- a/js/plists/PList.js
+++ b/js/plists/PList.js
@@ -66,7 +66,7 @@ define(['three'], function(THREE){
             navOptimize:{
                 name: "Optimize",
                 tabs:{
-                    optimize:"Optimize"
+                    optimization:"Optimize"
                 }
             },
             navAssemble:{
@@ -76,7 +76,7 @@ define(['three'], function(THREE){
                     assemblerSetup: "Setup",
                     cam: "Globals",
     //                editCamOutput: "Edit",
-                    animate:"Preview"
+                    animation:"Preview"
                 }
             },
             navDNAAssemble:{
-- 
GitLab