From 2e2b18eaa502c0190df75a011e8edfadbe73ae26 Mon Sep 17 00:00:00 2001
From: Henry <andrew.k.henry@nasa.gov>
Date: Thu, 19 Nov 2015 10:33:44 -0800
Subject: [PATCH] [Edit Mode Prototype] Create button initiates edit-mode
 immediately #286

---
 platform/commonUI/browse/bundle.json          |  3 +-
 .../browse/src/creation/CreateAction.js       | 72 +++++++------------
 .../src/creation/CreateActionProvider.js      | 14 ++--
 .../commonUI/edit/src/actions/SaveAction.js   |  6 +-
 4 files changed, 37 insertions(+), 58 deletions(-)

diff --git a/platform/commonUI/browse/bundle.json b/platform/commonUI/browse/bundle.json
index 9ab7bd179c..ccf4234ddd 100644
--- a/platform/commonUI/browse/bundle.json
+++ b/platform/commonUI/browse/bundle.json
@@ -150,7 +150,8 @@
                 "provides": "actionService",
                 "type": "provider",
                 "implementation": "creation/CreateActionProvider.js",
-                "depends": [ "typeService", "dialogService", "creationService", "policyService" ]
+                "depends": [ "$injector", "$q", "typeService",
+                    "navigationService"]
             },
             {
                 "key": "CreationService",
diff --git a/platform/commonUI/browse/src/creation/CreateAction.js b/platform/commonUI/browse/src/creation/CreateAction.js
index 984b26cfe5..c829731f7c 100644
--- a/platform/commonUI/browse/src/creation/CreateAction.js
+++ b/platform/commonUI/browse/src/creation/CreateAction.js
@@ -25,8 +25,10 @@
  * Module defining CreateAction. Created by vwoeltje on 11/10/14.
  */
 define(
-    ['./CreateWizard'],
-    function (CreateWizard) {
+    ['./CreateWizard',
+    'uuid',
+    '../../../edit/src/objects/EditableDomainObject'],
+    function (CreateWizard, uuid, EditableDomainObject) {
         "use strict";
 
         /**
@@ -45,13 +47,11 @@ define(
          *        override this)
          * @param {ActionContext} context the context in which the
          *        action is being performed
-         * @param {DialogService} dialogService the dialog service
-         *        to use when requesting user input
-         * @param {CreationService} creationService the creation service,
-         *        which handles the actual instantiation and persistence
-         *        of the newly-created domain object
+         * @param {NavigationService} navigationService the navigation service,
+         *        which handles changes in navigation. It allows the object
+         *        being browsed/edited to be set.
          */
-        function CreateAction(type, parent, context, dialogService, creationService, policyService) {
+        function CreateAction(type, parent, context, $injector, $q, navigationService) {
             this.metadata = {
                 key: 'create',
                 glyph: type.getGlyph(),
@@ -63,9 +63,8 @@ define(
 
             this.type = type;
             this.parent = parent;
-            this.policyService = policyService;
-            this.dialogService = dialogService;
-            this.creationService = creationService;
+            this.navigationService = navigationService;
+            this.$q = $q;
         }
 
         /**
@@ -73,45 +72,24 @@ define(
          * This will prompt for user input first.
          */
         CreateAction.prototype.perform = function () {
-            /*
-             Overview of steps in object creation:
+            var newModel = this.type.getInitialModel(),
+                parentObject = this.navigationService.getNavigation(),
+                newObject,
+                editableObject;
 
-             1. Show dialog
-             a. Prepare dialog contents
-             b. Invoke dialogService
-             2. Create new object in persistence service
-             a. Generate UUID
-             b. Store model
-             3. Mutate destination container
-             a. Get mutation capability
-             b. Add new id to composition
-             4. Persist destination container
-             a. ...use persistence capability.
-             */
+            newModel.type = this.type.getKey();
+            newObject = parentObject.useCapability('instantiation', newModel);
+            editableObject = new EditableDomainObject(newObject, this.$q);
+            editableObject.setOriginalObject(parentObject);
+            editableObject.useCapability('mutation', function(model){
+                model.location = parentObject.getId();
+            });
 
-            // The wizard will handle creating the form model based
-            // on the type...
-            var wizard =
-                new CreateWizard(this.type, this.parent, this.policyService),
-                self = this;
-
-            // Create and persist the new object, based on user
-            // input.
-            function persistResult(formValue) {
-                var parent = wizard.getLocation(formValue),
-                    newModel = wizard.createModel(formValue);
-                return self.creationService.createObject(newModel, parent);
+            if (newObject.hasCapability('composition') && this.type.getKey()!=='folder') {
+                this.navigationService.setNavigation(editableObject);
+            } else {
+                return editableObject.getCapability('action').perform('save');
             }
-
-            function doNothing() {
-                // Create cancelled, do nothing
-                return false;
-            }
-
-            return this.dialogService.getUserInput(
-                wizard.getFormStructure(),
-                wizard.getInitialFormValue()
-            ).then(persistResult, doNothing);
         };
 
 
diff --git a/platform/commonUI/browse/src/creation/CreateActionProvider.js b/platform/commonUI/browse/src/creation/CreateActionProvider.js
index 4ca2bce59f..9be8052ecf 100644
--- a/platform/commonUI/browse/src/creation/CreateActionProvider.js
+++ b/platform/commonUI/browse/src/creation/CreateActionProvider.js
@@ -46,11 +46,11 @@ define(
          *        introduced in this bundle), responsible for handling actual
          *        object creation.
          */
-        function CreateActionProvider(typeService, dialogService, creationService, policyService) {
+        function CreateActionProvider($injector, $q, typeService, navigationService) {
             this.typeService = typeService;
-            this.dialogService = dialogService;
-            this.creationService = creationService;
-            this.policyService = policyService;
+            this.navigationService = navigationService;
+            this.$injector = $injector;
+            this.$q = $q;
         }
 
         CreateActionProvider.prototype.getActions = function (actionContext) {
@@ -75,9 +75,9 @@ define(
                     type,
                     destination,
                     context,
-                    self.dialogService,
-                    self.creationService,
-                    self.policyService
+                    self.$injector,
+                    self.$q,
+                    self.navigationService
                 );
             });
         };
diff --git a/platform/commonUI/edit/src/actions/SaveAction.js b/platform/commonUI/edit/src/actions/SaveAction.js
index cbe60dec2b..e484be4922 100644
--- a/platform/commonUI/edit/src/actions/SaveAction.js
+++ b/platform/commonUI/edit/src/actions/SaveAction.js
@@ -133,7 +133,7 @@ define(
 
             function persistObject(object){
 
-                return (object.hasCapability('editor') && object.getCapability('editor').save() || object.getCapability('persistence').persist())
+                return (object.hasCapability('editor') && object.getCapability('editor').save(true) || object.getCapability('persistence').persist())
                     .then(resolveWith(object));
                 /*
                 if (object.hasCapability('editor')){
@@ -165,9 +165,9 @@ define(
             // during editing.
             function doSave() {
                 //WARNING: HACK
-                //This is a new 'virtual panel' that has not been persisted
+                //This is a new 'virtual object' that has not been persisted
                 // yet.
-                if (domainObject.getModel().type === 'telemetry.panel' && !domainObject.getModel().persisted){
+                if (!domainObject.getModel().persisted){
                     return getParent(domainObject)
                             .then(doWizardSave)
                             .then(persistObject)