diff --git a/platform/commonUI/browse/bundle.js b/platform/commonUI/browse/bundle.js index 73b3f44de7..8a694c57a7 100644 --- a/platform/commonUI/browse/bundle.js +++ b/platform/commonUI/browse/bundle.js @@ -309,9 +309,7 @@ define([ "type": "provider", "implementation": CreateActionProvider, "depends": [ - "$q", "typeService", - "navigationService", "policyService" ] }, diff --git a/platform/commonUI/browse/src/creation/CreateAction.js b/platform/commonUI/browse/src/creation/CreateAction.js index 2e04a0b07e..6ea0d513a5 100644 --- a/platform/commonUI/browse/src/creation/CreateAction.js +++ b/platform/commonUI/browse/src/creation/CreateAction.js @@ -43,11 +43,8 @@ define( * override this) * @param {ActionContext} context the context in which the * action is being performed - * @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, $q, navigationService) { + function CreateAction(type, parent, context) { this.metadata = { key: 'create', glyph: type.getGlyph(), @@ -56,24 +53,8 @@ define( description: type.getDescription(), context: context }; - this.type = type; this.parent = parent; - this.navigationService = navigationService; - this.$q = $q; - } - - // Get a count of views which are not flagged as non-editable. - function countEditableViews(domainObject) { - var views = domainObject && domainObject.useCapability('view'), - count = 0; - - // A view is editable unless explicitly flagged as not - (views || []).forEach(function (view) { - count += (view.editable !== false) ? 1 : 0; - }); - - return count; } /** @@ -82,7 +63,6 @@ define( */ CreateAction.prototype.perform = function () { var newModel = this.type.getInitialModel(), - parentObject = this.navigationService.getNavigation(), newObject, editAction, editorCapability; @@ -96,16 +76,18 @@ define( } newModel.type = this.type.getKey(); - newModel.location = parentObject.getId(); - newObject = parentObject.useCapability('instantiation', newModel); + newModel.location = this.parent.getId(); + newObject = this.parent.useCapability('instantiation', newModel); editorCapability = newObject.hasCapability('editor') && newObject.getCapability("editor"); editAction = newObject.getCapability("action").getActions("edit")[0]; + //If an edit action is available, perform it if (editAction) { - return editAction.perform("edit"); + return editAction.perform(); } else if (editorCapability) { + //otherwise, use the save action editorCapability.edit(); - return newObject.useCapability("action").perform("save").then(onSave, onCancel); + return newObject.getCapability("action").perform("save").then(onSave, onCancel); } }; diff --git a/platform/commonUI/browse/src/creation/CreateActionProvider.js b/platform/commonUI/browse/src/creation/CreateActionProvider.js index 82f57343da..5039149104 100644 --- a/platform/commonUI/browse/src/creation/CreateActionProvider.js +++ b/platform/commonUI/browse/src/creation/CreateActionProvider.js @@ -44,10 +44,8 @@ define( * introduced in this bundle), responsible for handling actual * object creation. */ - function CreateActionProvider($q, typeService, navigationService, policyService) { + function CreateActionProvider(typeService, policyService) { this.typeService = typeService; - this.navigationService = navigationService; - this.$q = $q; this.policyService = policyService; } @@ -72,9 +70,7 @@ define( return new CreateAction( type, destination, - context, - self.$q, - self.navigationService + context ); }); }; diff --git a/platform/commonUI/browse/test/creation/CreateActionProviderSpec.js b/platform/commonUI/browse/test/creation/CreateActionProviderSpec.js index fbfc4a1140..d8aefe17fa 100644 --- a/platform/commonUI/browse/test/creation/CreateActionProviderSpec.js +++ b/platform/commonUI/browse/test/creation/CreateActionProviderSpec.js @@ -29,13 +29,10 @@ define( describe("The create action provider", function () { var mockTypeService, - mockDialogService, - mockNavigationService, mockPolicyService, mockCreationPolicy, mockPolicyMap = {}, mockTypes, - mockQ, provider; function createMockType(name) { @@ -61,14 +58,6 @@ define( "typeService", ["listTypes"] ); - mockDialogService = jasmine.createSpyObj( - "dialogService", - ["getUserInput"] - ); - mockNavigationService = jasmine.createSpyObj( - "navigationService", - ["setNavigation"] - ); mockPolicyService = jasmine.createSpyObj( "policyService", ["allow"] @@ -91,9 +80,7 @@ define( mockTypeService.listTypes.andReturn(mockTypes); provider = new CreateActionProvider( - mockQ, mockTypeService, - mockNavigationService, mockPolicyService ); }); diff --git a/platform/commonUI/browse/test/creation/CreateActionSpec.js b/platform/commonUI/browse/test/creation/CreateActionSpec.js index 5fa89512e0..b37339fa40 100644 --- a/platform/commonUI/browse/test/creation/CreateActionSpec.js +++ b/platform/commonUI/browse/test/creation/CreateActionSpec.js @@ -31,8 +31,10 @@ define( var mockType, mockParent, mockContext, - mockDialogService, - mockCreationService, + mockDomainObject, + capabilities = {}, + mockEditAction, + mockSaveAction, action; function mockPromise(value) { @@ -60,20 +62,61 @@ define( [ "getId", "getModel", - "getCapability" + "getCapability", + "useCapability" ] ); + mockDomainObject = jasmine.createSpyObj( + "domainObject", + [ + "getId", + "getModel", + "getCapability", + "hasCapability", + "useCapability" + ] + ); + mockDomainObject.hasCapability.andCallFake(function (name) { + return !!capabilities[name]; + }); + mockDomainObject.getCapability.andCallFake(function (name) { + return capabilities[name]; + }); + mockSaveAction = jasmine.createSpyObj( + "saveAction", + [ + "perform" + ] + ); + + capabilities.action = jasmine.createSpyObj( + "actionCapability", + [ + "getActions", + "perform" + ] + ); + + capabilities.editor = jasmine.createSpyObj( + "editorCapability", + [ + "edit", + "save", + "cancel" + ] + ); + + mockEditAction = jasmine.createSpyObj( + "editAction", + [ + "perform" + ] + ); + mockContext = { domainObject: mockParent }; - mockDialogService = jasmine.createSpyObj( - "dialogService", - ["getUserInput"] - ); - mockCreationService = jasmine.createSpyObj( - "creationService", - ["createObject"] - ); + mockParent.useCapability.andReturn(mockDomainObject); mockType.getKey.andReturn("test"); mockType.getGlyph.andReturn("T"); @@ -82,14 +125,10 @@ define( mockType.getProperties.andReturn([]); mockType.getInitialModel.andReturn({}); - mockDialogService.getUserInput.andReturn(mockPromise({})); - action = new CreateAction( mockType, mockParent, - mockContext, - mockDialogService, - mockCreationService + mockContext ); }); @@ -101,28 +140,49 @@ define( expect(metadata.glyph).toEqual("T"); }); - //TODO: Disabled for NEM Beta - xit("invokes the creation service when performed", function () { - action.perform(); - expect(mockCreationService.createObject).toHaveBeenCalledWith( - { type: "test" }, - mockParent - ); - }); - - //TODO: Disabled for NEM Beta - xit("does not create an object if the user cancels", function () { - mockDialogService.getUserInput.andReturn({ - then: function (callback, fail) { - fail(); - } + describe("the perform function", function () { + beforeEach(function () { + capabilities.action.getActions.andReturn([mockEditAction]); }); - action.perform(); + it("uses the instantiation capability when performed", function () { + action.perform(); + expect(mockParent.useCapability).toHaveBeenCalledWith("instantiation", jasmine.any(Object)); + }); - expect(mockCreationService.createObject) - .not.toHaveBeenCalled(); + it("uses the edit action if available", function () { + action.perform(); + expect(mockEditAction.perform).toHaveBeenCalled(); + }); + it("uses the save action if object does not have an edit action" + + " available", function () { + capabilities.action.getActions.andReturn([]); + capabilities.action.perform.andReturn(mockPromise(undefined)); + action.perform(); + expect(capabilities.action.perform).toHaveBeenCalledWith("save"); + }); + + describe("uses to editor capability", function () { + var promise = jasmine.createSpyObj("promise", ["then"]); + beforeEach(function () { + capabilities.action.getActions.andReturn([]); + capabilities.action.perform.andReturn(promise); + }); + + it("to save the edit if user saves dialog", function () { + action.perform(); + expect(promise.then).toHaveBeenCalled(); + promise.then.mostRecentCall.args[0](); + expect(capabilities.editor.save).toHaveBeenCalled(); + }); + + it("to cancel the edit if user cancels dialog", function () { + action.perform(); + promise.then.mostRecentCall.args[1](); + expect(capabilities.editor.cancel).toHaveBeenCalled(); + }); + }); }); });