diff --git a/platform/commonUI/edit/bundle.json b/platform/commonUI/edit/bundle.json index 2aed41c2ee..c216a9d877 100644 --- a/platform/commonUI/edit/bundle.json +++ b/platform/commonUI/edit/bundle.json @@ -59,7 +59,7 @@ "glyph": "Z", "name": "Remove", "description": "Remove this object from its containing object.", - "depends": [ "$q" ] + "depends": [ "$q", "navigationService" ] }, { "key": "save", diff --git a/platform/commonUI/edit/src/actions/RemoveAction.js b/platform/commonUI/edit/src/actions/RemoveAction.js index fbf47d22d9..e89ad3ed2d 100644 --- a/platform/commonUI/edit/src/actions/RemoveAction.js +++ b/platform/commonUI/edit/src/actions/RemoveAction.js @@ -40,8 +40,9 @@ define( * @constructor * @memberof module:editor/actions/remove-action */ - function RemoveAction($q, context) { - var object = (context || {}).domainObject; + function RemoveAction($q, navigationService, context) { + var object = (context || {}).domainObject, + ROOT_ID = "ROOT"; /** * Check whether an object ID matches the ID of the object being @@ -68,15 +69,39 @@ define( var persistence = domainObject.getCapability('persistence'); return persistence && persistence.persist(); } - + + // Checks current object and ascendants of current + // object with object being removed, if the current + // object or any in the current object's path is being removed, + // navigate back to parent of removed object. + function checkObjectNavigation(object, parentObject) { + // Traverse object starts at current location + var traverseObject = navigationService.getNavigation(); + + // Stop at ROOT of folder path + while(traverseObject.getId() !== ROOT_ID) { + // If traverse object is object being removed + // navigate to parent of removed object + if (traverseObject.getId() === object.getId()) { + navigationService.setNavigation(parentObject); + return; + } + // Traverses to parent + traverseObject = traverseObject.getCapability('context').getParent(); + } + } + /** * Remove the object from its parent, as identified by its context * capability. - * @param {ContextCapability} contextCapability the "context" capability - * of the domain object being removed. + * @param {object} domain object being removed contextCapability + gotten from the "context" capability of this object */ - function removeFromContext(contextCapability) { - var parent = contextCapability.getParent(); + function removeFromContext(object) { + var contextCapability = object.getCapability('context'), + parent = contextCapability.getParent(); + // Navigates through/ascendant if deleting current object + checkObjectNavigation(object, parent) $q.when( parent.useCapability('mutation', doMutate) ).then(function () { @@ -91,7 +116,7 @@ define( * fulfilled when the action has completed. */ perform: function () { - return $q.when(object.getCapability('context')) + return $q.when(object) .then(removeFromContext); } }; diff --git a/platform/commonUI/edit/test/actions/RemoveActionSpec.js b/platform/commonUI/edit/test/actions/RemoveActionSpec.js index d23542f0ee..072094ed1d 100644 --- a/platform/commonUI/edit/test/actions/RemoveActionSpec.js +++ b/platform/commonUI/edit/test/actions/RemoveActionSpec.js @@ -28,8 +28,10 @@ define( describe("The Remove action", function () { var mockQ, + mockNavigationService, mockDomainObject, mockParent, + mockGrandparent, mockContext, mockMutation, mockPersistence, @@ -55,6 +57,20 @@ define( [ "getId", "getCapability" ] ); mockQ = { when: mockPromise }; + mockGrandparent = { + getModel: function () { + return model; + }, + getCapability: function (k) { + return capabilities[k]; + }, + useCapability: function (k, v) { + return capabilities[k].invoke(v); + }, + getId: function () { + return "test"; + } + }; mockParent = { getModel: function () { return model; @@ -64,31 +80,44 @@ define( }, useCapability: function (k, v) { return capabilities[k].invoke(v); + }, + getParent: function () { + return mockGrandparent; } }; mockContext = jasmine.createSpyObj("context", [ "getParent" ]); mockMutation = jasmine.createSpyObj("mutation", [ "invoke" ]); mockPersistence = jasmine.createSpyObj("persistence", [ "persist" ]); mockType = jasmine.createSpyObj("type", [ "hasFeature" ]); - + mockNavigationService = jasmine.createSpyObj( + "navigationService", + [ + "getNavigation", + "setNavigation", + "addListener", + "removeListener" + ] + ); + mockNavigationService.getNavigation.andReturn(mockDomainObject); + + mockDomainObject.getId.andReturn("test"); mockDomainObject.getCapability.andReturn(mockContext); mockContext.getParent.andReturn(mockParent); mockType.hasFeature.andReturn(true); - capabilities = { mutation: mockMutation, persistence: mockPersistence, type: mockType }; model = { - composition: [ "a", "test", "b", "c" ] + composition: [ "a", "b", "test", "c" ] }; actionContext = { domainObject: mockDomainObject }; - action = new RemoveAction(mockQ, actionContext); + action = new RemoveAction(mockQ, mockNavigationService, actionContext); }); it("only applies to objects with parents", function () {