From 2866574dc05e12bcf00c10e79e2ceb2340520ada Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Mon, 9 Nov 2015 16:28:04 -0800 Subject: [PATCH] [Actions] Define applicability Define applicability of Move/Copy/Link using appliesTo, to avoid errors being thrown due to lack of context during instantiation. Addresses immediate cause of nasa/openmctweb#120. --- .../src/actions/AbstractComposeAction.js | 8 +++++++ .../entanglement/src/actions/CopyAction.js | 11 ++++++---- .../entanglement/src/actions/LinkAction.js | 11 +++++----- .../entanglement/src/actions/MoveAction.js | 12 +++++----- .../test/actions/AbstractComposeActionSpec.js | 22 +++++++++++++++++++ 5 files changed, 50 insertions(+), 14 deletions(-) diff --git a/platform/entanglement/src/actions/AbstractComposeAction.js b/platform/entanglement/src/actions/AbstractComposeAction.js index f68391adc9..ef56c952b3 100644 --- a/platform/entanglement/src/actions/AbstractComposeAction.js +++ b/platform/entanglement/src/actions/AbstractComposeAction.js @@ -122,6 +122,14 @@ define( }); }; + AbstractComposeAction.appliesTo = function (context) { + var applicableObject = + context.selectedObject || context.domainObject; + + return !!(applicableObject && + applicableObject.hasCapability('context')); + }; + return AbstractComposeAction; } ); diff --git a/platform/entanglement/src/actions/CopyAction.js b/platform/entanglement/src/actions/CopyAction.js index cdcefeb935..161db1ce27 100644 --- a/platform/entanglement/src/actions/CopyAction.js +++ b/platform/entanglement/src/actions/CopyAction.js @@ -34,7 +34,7 @@ define( * @constructor * @memberof platform/entanglement */ - function CopyAction($log, locationService, copyService, dialogService, + function CopyAction($log, locationService, copyService, dialogService, notificationService, context) { this.dialog = undefined; this.notification = undefined; @@ -42,7 +42,7 @@ define( this.notificationService = notificationService; this.$log = $log; //Extend the behaviour of the Abstract Compose Action - AbstractComposeAction.call(this, locationService, copyService, + AbstractComposeAction.call(this, locationService, copyService, context, "Duplicate", "to a location"); } @@ -87,8 +87,8 @@ define( }; /** - * Executes the CopyAction. The CopyAction uses the default behaviour of - * the AbstractComposeAction, but extends it to support notification + * Executes the CopyAction. The CopyAction uses the default behaviour of + * the AbstractComposeAction, but extends it to support notification * updates of progress on copy. */ CopyAction.prototype.perform = function() { @@ -131,6 +131,9 @@ define( return AbstractComposeAction.prototype.perform.call(this) .then(success, error, notification); }; + + CopyAction.appliesTo = AbstractComposeAction.appliesTo; + return CopyAction; } ); diff --git a/platform/entanglement/src/actions/LinkAction.js b/platform/entanglement/src/actions/LinkAction.js index c791310886..f34e91f156 100644 --- a/platform/entanglement/src/actions/LinkAction.js +++ b/platform/entanglement/src/actions/LinkAction.js @@ -35,14 +35,15 @@ define( * @memberof platform/entanglement */ function LinkAction(locationService, linkService, context) { - return new AbstractComposeAction( - locationService, - linkService, - context, - "Link" + AbstractComposeAction.apply( + this, + [locationService, linkService, context, "Link"] ); } + LinkAction.prototype = Object.create(AbstractComposeAction.prototype); + LinkAction.appliesTo = AbstractComposeAction.appliesTo; + return LinkAction; } ); diff --git a/platform/entanglement/src/actions/MoveAction.js b/platform/entanglement/src/actions/MoveAction.js index 4fdd4b59df..1d090ce313 100644 --- a/platform/entanglement/src/actions/MoveAction.js +++ b/platform/entanglement/src/actions/MoveAction.js @@ -35,14 +35,16 @@ define( * @memberof platform/entanglement */ function MoveAction(locationService, moveService, context) { - return new AbstractComposeAction( - locationService, - moveService, - context, - "Move" + AbstractComposeAction.apply( + this, + [locationService, moveService, context, "Move"] ); + } + MoveAction.prototype = Object.create(AbstractComposeAction.prototype); + MoveAction.appliesTo = AbstractComposeAction.appliesTo; + return MoveAction; } ); diff --git a/platform/entanglement/test/actions/AbstractComposeActionSpec.js b/platform/entanglement/test/actions/AbstractComposeActionSpec.js index 5be0604ec3..6a0ddd7ebd 100644 --- a/platform/entanglement/test/actions/AbstractComposeActionSpec.js +++ b/platform/entanglement/test/actions/AbstractComposeActionSpec.js @@ -94,6 +94,28 @@ define( composeService = new MockCopyService(); }); + it("are only applicable to domain objects with a context", function () { + var noContextObject = domainObjectFactory({ + name: 'selectedObject', + model: { name: 'selectedObject' }, + capabilities: {} + }); + + expect(AbstractComposeAction.appliesTo({ + selectedObject: selectedObject + })).toBe(true); + expect(AbstractComposeAction.appliesTo({ + domainObject: selectedObject + })).toBe(true); + + expect(AbstractComposeAction.appliesTo({ + selectedObject: noContextObject + })).toBe(false); + expect(AbstractComposeAction.appliesTo({ + domainObject: noContextObject + })).toBe(false); + }); + describe("with context from context-action", function () { beforeEach(function () {