mirror of
https://github.com/nasa/openmct.git
synced 2025-05-31 14:40:48 +00:00
Merge pull request #272 from nasa/open120
[Actions] Avoid suppression of context menus
This commit is contained in:
commit
4ed35cddde
@ -97,7 +97,7 @@
|
|||||||
"provides": "actionService",
|
"provides": "actionService",
|
||||||
"type": "provider",
|
"type": "provider",
|
||||||
"implementation": "actions/ActionProvider.js",
|
"implementation": "actions/ActionProvider.js",
|
||||||
"depends": [ "actions[]" ]
|
"depends": [ "actions[]", "$log" ]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"provides": "actionService",
|
"provides": "actionService",
|
||||||
|
@ -39,9 +39,11 @@ define(
|
|||||||
* @imeplements {ActionService}
|
* @imeplements {ActionService}
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function ActionProvider(actions) {
|
function ActionProvider(actions, $log) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
this.$log = $log;
|
||||||
|
|
||||||
// Build up look-up tables
|
// Build up look-up tables
|
||||||
this.actions = actions;
|
this.actions = actions;
|
||||||
this.actionsByKey = {};
|
this.actionsByKey = {};
|
||||||
@ -74,6 +76,7 @@ define(
|
|||||||
var context = (actionContext || {}),
|
var context = (actionContext || {}),
|
||||||
category = context.category,
|
category = context.category,
|
||||||
key = context.key,
|
key = context.key,
|
||||||
|
$log = this.$log,
|
||||||
candidates;
|
candidates;
|
||||||
|
|
||||||
// Instantiate an action; invokes the constructor and
|
// Instantiate an action; invokes the constructor and
|
||||||
@ -103,12 +106,31 @@ define(
|
|||||||
// appliesTo method of given actions (if defined), and
|
// appliesTo method of given actions (if defined), and
|
||||||
// instantiate those applicable actions.
|
// instantiate those applicable actions.
|
||||||
function createIfApplicable(actions, context) {
|
function createIfApplicable(actions, context) {
|
||||||
return (actions || []).filter(function (Action) {
|
function isApplicable(Action) {
|
||||||
return Action.appliesTo ?
|
return Action.appliesTo ? Action.appliesTo(context) : true;
|
||||||
Action.appliesTo(context) : true;
|
}
|
||||||
}).map(function (Action) {
|
|
||||||
|
function instantiate(Action) {
|
||||||
|
try {
|
||||||
return instantiateAction(Action, context);
|
return instantiateAction(Action, context);
|
||||||
});
|
} catch (e) {
|
||||||
|
$log.error([
|
||||||
|
"Could not instantiate action",
|
||||||
|
Action.key,
|
||||||
|
"due to:",
|
||||||
|
e.message
|
||||||
|
].join(" "));
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDefined(action) {
|
||||||
|
return action !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (actions || []).filter(isApplicable)
|
||||||
|
.map(instantiate)
|
||||||
|
.filter(isDefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Match actions to the provided context by comparing "key"
|
// Match actions to the provided context by comparing "key"
|
||||||
|
@ -30,7 +30,8 @@ define(
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
describe("The action provider", function () {
|
describe("The action provider", function () {
|
||||||
var actions,
|
var mockLog,
|
||||||
|
actions,
|
||||||
actionProvider;
|
actionProvider;
|
||||||
|
|
||||||
function SimpleAction() {
|
function SimpleAction() {
|
||||||
@ -62,6 +63,10 @@ define(
|
|||||||
MetadataAction.key = "metadata";
|
MetadataAction.key = "metadata";
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
mockLog = jasmine.createSpyObj(
|
||||||
|
'$log',
|
||||||
|
['error', 'warn', 'info', 'debug']
|
||||||
|
);
|
||||||
actions = [
|
actions = [
|
||||||
SimpleAction,
|
SimpleAction,
|
||||||
CategorizedAction,
|
CategorizedAction,
|
||||||
@ -137,6 +142,42 @@ define(
|
|||||||
expect(provided[0].getMetadata()).toEqual("custom metadata");
|
expect(provided[0].getMetadata()).toEqual("custom metadata");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("when actions throw errors during instantiation", function () {
|
||||||
|
var errorText,
|
||||||
|
provided;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
errorText = "some error text";
|
||||||
|
|
||||||
|
function BadAction() {
|
||||||
|
throw new Error(errorText);
|
||||||
|
}
|
||||||
|
|
||||||
|
provided = new ActionProvider(
|
||||||
|
[ SimpleAction, BadAction ],
|
||||||
|
mockLog
|
||||||
|
).getActions();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs an error", function () {
|
||||||
|
expect(mockLog.error)
|
||||||
|
.toHaveBeenCalledWith(jasmine.any(String));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports the error's message", function () {
|
||||||
|
expect(
|
||||||
|
mockLog.error.mostRecentCall.args[0].indexOf(errorText)
|
||||||
|
).not.toEqual(-1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still provides valid actions", function () {
|
||||||
|
expect(provided.length).toEqual(1);
|
||||||
|
expect(provided[0].perform()).toEqual("simple");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
@ -122,6 +122,14 @@ define(
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AbstractComposeAction.appliesTo = function (context) {
|
||||||
|
var applicableObject =
|
||||||
|
context.selectedObject || context.domainObject;
|
||||||
|
|
||||||
|
return !!(applicableObject &&
|
||||||
|
applicableObject.hasCapability('context'));
|
||||||
|
};
|
||||||
|
|
||||||
return AbstractComposeAction;
|
return AbstractComposeAction;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -131,6 +131,9 @@ define(
|
|||||||
return AbstractComposeAction.prototype.perform.call(this)
|
return AbstractComposeAction.prototype.perform.call(this)
|
||||||
.then(success, error, notification);
|
.then(success, error, notification);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CopyAction.appliesTo = AbstractComposeAction.appliesTo;
|
||||||
|
|
||||||
return CopyAction;
|
return CopyAction;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -35,14 +35,15 @@ define(
|
|||||||
* @memberof platform/entanglement
|
* @memberof platform/entanglement
|
||||||
*/
|
*/
|
||||||
function LinkAction(locationService, linkService, context) {
|
function LinkAction(locationService, linkService, context) {
|
||||||
return new AbstractComposeAction(
|
AbstractComposeAction.apply(
|
||||||
locationService,
|
this,
|
||||||
linkService,
|
[locationService, linkService, context, "Link"]
|
||||||
context,
|
|
||||||
"Link"
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LinkAction.prototype = Object.create(AbstractComposeAction.prototype);
|
||||||
|
LinkAction.appliesTo = AbstractComposeAction.appliesTo;
|
||||||
|
|
||||||
return LinkAction;
|
return LinkAction;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -35,14 +35,16 @@ define(
|
|||||||
* @memberof platform/entanglement
|
* @memberof platform/entanglement
|
||||||
*/
|
*/
|
||||||
function MoveAction(locationService, moveService, context) {
|
function MoveAction(locationService, moveService, context) {
|
||||||
return new AbstractComposeAction(
|
AbstractComposeAction.apply(
|
||||||
locationService,
|
this,
|
||||||
moveService,
|
[locationService, moveService, context, "Move"]
|
||||||
context,
|
|
||||||
"Move"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MoveAction.prototype = Object.create(AbstractComposeAction.prototype);
|
||||||
|
MoveAction.appliesTo = AbstractComposeAction.appliesTo;
|
||||||
|
|
||||||
return MoveAction;
|
return MoveAction;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -94,6 +94,28 @@ define(
|
|||||||
composeService = new MockCopyService();
|
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 () {
|
describe("with context from context-action", function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user