[Edit Mode] #635 Removed Edit-related concerns from ContextMenuGesture

This commit is contained in:
Henry 2016-03-22 20:17:23 -07:00
parent c591ade479
commit 7f108c3b24
10 changed files with 225 additions and 100 deletions

View File

@ -37,6 +37,7 @@ define([
"./src/policies/EditableLinkPolicy", "./src/policies/EditableLinkPolicy",
"./src/policies/EditableMovePolicy", "./src/policies/EditableMovePolicy",
"./src/policies/EditNavigationPolicy", "./src/policies/EditNavigationPolicy",
"./src/policies/EditContextualActionPolicy",
"./src/representers/EditRepresenter", "./src/representers/EditRepresenter",
"./src/representers/EditToolbarRepresenter", "./src/representers/EditToolbarRepresenter",
"text!./res/templates/library.html", "text!./res/templates/library.html",
@ -61,6 +62,7 @@ define([
EditableLinkPolicy, EditableLinkPolicy,
EditableMovePolicy, EditableMovePolicy,
EditNavigationPolicy, EditNavigationPolicy,
EditContextualActionPolicy,
EditRepresenter, EditRepresenter,
EditToolbarRepresenter, EditToolbarRepresenter,
libraryTemplate, libraryTemplate,
@ -191,6 +193,11 @@ define([
"category": "action", "category": "action",
"implementation": EditActionPolicy "implementation": EditActionPolicy
}, },
{
"category": "action",
"implementation": EditContextualActionPolicy,
"depends": ["navigationService"]
},
{ {
"category": "action", "category": "action",
"implementation": EditableMovePolicy "implementation": EditableMovePolicy

View File

@ -38,14 +38,6 @@ define(
this.policyService = policyService; this.policyService = policyService;
} }
function applicableView(key){
return ['plot', 'scrolling'].indexOf(key) >= 0;
}
function editableType(key){
return key === 'telemetry.panel';
}
/** /**
* Get a count of views which are not flagged as non-editable. * Get a count of views which are not flagged as non-editable.
* @private * @private
@ -65,7 +57,8 @@ define(
// A view is editable unless explicitly flagged as not // A view is editable unless explicitly flagged as not
(views || []).forEach(function (view) { (views || []).forEach(function (view) {
if (view.editable === true || (applicableView(view.key) && editableType(type.getKey()))) { if (view.editable === true ||
(view.key === 'plot' && type.getKey() === 'telemetry.panel')) {
count++; count++;
} }
}); });

View File

@ -0,0 +1,59 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define*/
define(
[],
function () {
"use strict";
/**
* Policy controlling whether the context menu is visible when
* objects are being edited
* @memberof platform/commonUI/edit
* @constructor
* @implements {Policy.<Action, ActionContext>}
*/
function EditContextualActionPolicy(navigationService) {
this.navigationService = navigationService;
this.blacklist = ["move", "copy", "link", "window", "follow"];
}
EditContextualActionPolicy.prototype.allow = function (action, context) {
var selectedObject = context.domainObject,
navigatedObject = this.navigationService.getNavigation(),
actionMetadata = action.getMetadata ? action.getMetadata() : {};
if (navigatedObject.hasCapability('editor')) {
if (!selectedObject.hasCapability('editor')){
return false;
} else {
return this.blacklist.indexOf(actionMetadata.key) === -1;
}
} else {
return true;
}
};
return EditContextualActionPolicy;
}
);

View File

@ -35,19 +35,43 @@ define(
mockDomainObject, mockDomainObject,
mockEditAction, mockEditAction,
mockPropertiesAction, mockPropertiesAction,
mockTypeCapability,
mockStatusCapability,
capabilities,
plotView,
policy; policy;
beforeEach(function () { beforeEach(function () {
mockDomainObject = jasmine.createSpyObj( mockDomainObject = jasmine.createSpyObj(
'domainObject', 'domainObject',
[ 'useCapability' ] [
'useCapability',
'hasCapability',
'getCapability'
]
); );
mockStatusCapability = jasmine.createSpyObj('statusCapability', ['get']);
mockStatusCapability.get.andReturn(false);
mockTypeCapability = jasmine.createSpyObj('type', ['getKey']);
capabilities = {
'status': mockStatusCapability,
'type': mockTypeCapability
};
mockEditAction = jasmine.createSpyObj('edit', ['getMetadata']); mockEditAction = jasmine.createSpyObj('edit', ['getMetadata']);
mockPropertiesAction = jasmine.createSpyObj('edit', ['getMetadata']); mockPropertiesAction = jasmine.createSpyObj('edit', ['getMetadata']);
mockDomainObject.getCapability.andCallFake(function(capability){
return capabilities[capability];
});
mockDomainObject.hasCapability.andCallFake(function(capability){
return !!capabilities[capability];
});
editableView = { editable: true }; editableView = { editable: true };
nonEditableView = { editable: false }; nonEditableView = { editable: false };
undefinedView = { someKey: "some value" }; undefinedView = { someKey: "some value" };
plotView = { key: "plot", editable: false };
testViews = []; testViews = [];
mockDomainObject.useCapability.andCallFake(function (c) { mockDomainObject.useCapability.andCallFake(function (c) {
@ -66,38 +90,53 @@ define(
policy = new EditActionPolicy(); policy = new EditActionPolicy();
}); });
//TODO: Disabled for NEM Beta it("allows the edit action when there are editable views", function () {
xit("allows the edit action when there are editable views", function () {
testViews = [ editableView ]; testViews = [ editableView ];
expect(policy.allow(mockEditAction, testContext)).toBeTruthy(); expect(policy.allow(mockEditAction, testContext)).toBe(true);
// No edit flag defined; should be treated as editable
testViews = [ undefinedView, undefinedView ];
expect(policy.allow(mockEditAction, testContext)).toBeTruthy();
}); });
//TODO: Disabled for NEM Beta it("allows the edit properties action when there are no editable views", function () {
xit("allows the edit properties action when there are no editable views", function () {
testViews = [ nonEditableView, nonEditableView ]; testViews = [ nonEditableView, nonEditableView ];
expect(policy.allow(mockPropertiesAction, testContext)).toBeTruthy(); expect(policy.allow(mockPropertiesAction, testContext)).toBe(true);
}); });
//TODO: Disabled for NEM Beta it("disallows the edit action when there are no editable views", function () {
xit("disallows the edit action when there are no editable views", function () {
testViews = [ nonEditableView, nonEditableView ]; testViews = [ nonEditableView, nonEditableView ];
expect(policy.allow(mockEditAction, testContext)).toBeFalsy(); expect(policy.allow(mockEditAction, testContext)).toBe(false);
}); });
//TODO: Disabled for NEM Beta it("disallows the edit properties action when there are" +
xit("disallows the edit properties action when there are" +
" editable views", function () { " editable views", function () {
testViews = [ editableView ]; testViews = [ editableView ];
expect(policy.allow(mockPropertiesAction, testContext)).toBeFalsy(); expect(policy.allow(mockPropertiesAction, testContext)).toBe(false);
}); });
it("disallows the edit action when object is already being" +
" edited", function () {
testViews = [ editableView ];
mockStatusCapability.get.andReturn(true);
expect(policy.allow(mockEditAction, testContext)).toBe(false);
});
it("allows editing of panels in plot view", function () {
testViews = [ plotView ];
mockTypeCapability.getKey.andReturn('telemetry.panel');
expect(policy.allow(mockEditAction, testContext)).toBe(true);
});
it("disallows editing of plot view when object not a panel type", function () {
testViews = [ plotView ];
mockTypeCapability.getKey.andReturn('something.else');
expect(policy.allow(mockEditAction, testContext)).toBe(false);
});
it("allows the edit properties outside of the 'view-control' category", function () { it("allows the edit properties outside of the 'view-control' category", function () {
testViews = [ nonEditableView ]; testViews = [ nonEditableView ];
testContext.category = "something-else"; testContext.category = "something-else";
expect(policy.allow(mockPropertiesAction, testContext)).toBeTruthy(); expect(policy.allow(mockPropertiesAction, testContext)).toBe(true);
}); });
}); });
} }

View File

@ -0,0 +1,76 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define,describe,it,expect,beforeEach,jasmine,xit,xdescribe*/
define(
["../../src/policies/EditContextualActionPolicy"],
function (EditContextualActionPolicy) {
"use strict";
describe("The Edit contextual action policy", function () {
var policy,
navigationService,
mockAction,
context,
navigatedObject,
mockDomainObject,
metadata;
beforeEach(function () {
navigatedObject = jasmine.createSpyObj("navigatedObject", ["hasCapability"]);
navigatedObject.hasCapability.andReturn(false);
mockDomainObject = jasmine.createSpyObj("domainObject", ["hasCapability"]);
mockDomainObject.hasCapability.andReturn(false);
navigationService = jasmine.createSpyObj("navigationService", ["getNavigation"]);
navigationService.getNavigation.andReturn(navigatedObject);
metadata = {key: "move"};
mockAction = jasmine.createSpyObj("action", ["getMetadata"]);
mockAction.getMetadata.andReturn(metadata);
context = {domainObject: mockDomainObject};
policy = new EditContextualActionPolicy(navigationService);
});
it('Allows all actions when navigated object not in edit mode', function() {
expect(policy.allow(mockAction, context)).toBe(true);
});
it('Allows no actions when navigated object in edit mode, but' +
' selected object not in edit mode ', function() {
navigatedObject.hasCapability.andReturn(true);
expect(policy.allow(mockAction, context)).toBe(false);
});
it('Disallows blacklisted actions when navigated object and' +
' selected object in edit mode', function() {
navigatedObject.hasCapability.andReturn(true);
mockDomainObject.hasCapability.andReturn(true);
expect(policy.allow(mockAction, context)).toBe(false);
});
});
}
);

View File

@ -28,7 +28,6 @@ define(
[], [],
function () { function () {
"use strict"; "use strict";
var DISALLOWED_ACTIONS = ["copy", "window", "follow"];
/** /**
* The ActionCapability allows applicable Actions to be retrieved and * The ActionCapability allows applicable Actions to be retrieved and
@ -55,37 +54,22 @@ define(
this.domainObject = domainObject; this.domainObject = domainObject;
} }
function isEditable(domainObject){
return domainObject.getCapability('status').get('editing');
}
function hasEditableAncestor(domainObject){
return domainObject.hasCapability('context') &&
domainObject
.getCapability('context')
.getPath()
.some(function isEditable (ancestor){
return ancestor.getCapability('status').get('editing');
});
}
/** /**
* Retrieve the actions applicable to the domain object in the given * Perform an action. This will find and perform the
* context. * first matching action available for the specified
* context or key.
* *
* @param {ActionContext|string} context the context in which * @param {ActionContext|string} context the context in which
* to assess the applicability of the available actions; this is * to perform the action; this is passed along to
* passed along to the action service to match against available * the action service to match against available
* actions. The "domainObject" field will automatically * actions. The "domainObject" field will automatically
* be populated with the domain object that exposed * be populated with the domain object that exposed
* this capability. If given as a string, this will * this capability. If given as a string, this will
* be taken as the "key" field to match against * be taken as the "key" field to match against
* specific actions. * specific actions.
* * @returns {Promise} the result of the action that was
* Additionally, this function will limit the actions * performed, or undefined if no matching action
* available for an object in Edit Mode * was found.
* @returns {Array<Action>} The actions applicable to this domain
* object in the given context
* @memberof platform/core.ActionCapability# * @memberof platform/core.ActionCapability#
*/ */
ActionCapability.prototype.getActions = function (context) { ActionCapability.prototype.getActions = function (context) {
@ -94,19 +78,11 @@ define(
// but additionally adds a domainObject field. // but additionally adds a domainObject field.
var baseContext = typeof context === 'string' ? var baseContext = typeof context === 'string' ?
{ key: context } : (context || {}), { key: context } : (context || {}),
actionContext = Object.create(baseContext), actionContext = Object.create(baseContext);
actions;
actionContext.domainObject = this.domainObject; actionContext.domainObject = this.domainObject;
actions = this.actionService.getActions(actionContext) || []; return this.actionService.getActions(actionContext);
if (isEditable(this.domainObject) || hasEditableAncestor(this.domainObject)){
return actions.filter(function(action){
return DISALLOWED_ACTIONS.indexOf(action.getMetadata().key) === -1;
});
} else {
return actions;
}
}; };
/** /**

View File

@ -19,8 +19,7 @@
* this source code distribution or the Licensing information page available * this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information. * at runtime from the About dialog for additional information.
*****************************************************************************/ *****************************************************************************/
/*global define,Promise,describe,xdescribe,it,expect,beforeEach,waitsFor, /*global define,Promise,describe,it,expect,beforeEach,waitsFor,jasmine*/
jasmine*/
/** /**
* ActionCapabilitySpec. Created by vwoeltje on 11/6/14. * ActionCapabilitySpec. Created by vwoeltje on 11/6/14.
@ -29,8 +28,8 @@ define(
["../../src/actions/ActionCapability"], ["../../src/actions/ActionCapability"],
function (ActionCapability) { function (ActionCapability) {
"use strict"; "use strict";
//TODO: Disabled for NEM beta
xdescribe("The action capability", function () { describe("The action capability", function () {
var mockQ, var mockQ,
mockAction, mockAction,
mockActionService, mockActionService,

View File

@ -99,9 +99,7 @@ define([
"implementation": ContextMenuGesture, "implementation": ContextMenuGesture,
"depends": [ "depends": [
"$timeout", "$timeout",
"$parse", "agentService"
"agentService",
"navigationService"
] ]
} }
], ],

View File

@ -41,32 +41,16 @@ define(
* in the context menu will be performed * in the context menu will be performed
* @implements {Gesture} * @implements {Gesture}
*/ */
function ContextMenuGesture($timeout, $parse, agentService, navigationService, element, domainObject) { function ContextMenuGesture($timeout, agentService, element, domainObject) {
var isPressing, var isPressing,
longTouchTime = 500, longTouchTime = 500;
parameters = element && element.attr('parameters') && $parse(element.attr('parameters'))();
function suppressMenu() {
return parameters
&& parameters.suppressMenuOnEdit
&& navigationService.getNavigation()
&& navigationService.getNavigation().hasCapability('editor');
}
function showMenu(event) { function showMenu(event) {
/** domainObject.getCapability('action').perform({
* Some menu items should have the context menu action key: 'menu',
* suppressed (eg. the navigation menu on the left) domainObject: domainObject,
*/ event: event
if (suppressMenu()){ });
return;
} else {
domainObject.getCapability('action').perform({
key: 'menu',
domainObject: domainObject,
event: event
});
}
} }
// When context menu event occurs, show object actions instead // When context menu event occurs, show object actions instead

View File

@ -30,16 +30,14 @@ define(
function (ContextMenuGesture) { function (ContextMenuGesture) {
"use strict"; "use strict";
var JQLITE_FUNCTIONS = [ "on", "off", "find", "append", "remove", "attr" ], var JQLITE_FUNCTIONS = [ "on", "off", "find", "append", "remove" ],
DOMAIN_OBJECT_METHODS = [ "getId", "getModel", "getCapability", "hasCapability", "useCapability"]; DOMAIN_OBJECT_METHODS = [ "getId", "getModel", "getCapability", "hasCapability", "useCapability"];
describe("The 'context menu' gesture", function () { describe("The 'context menu' gesture", function () {
var mockTimeout, var mockTimeout,
mockParse,
mockElement, mockElement,
mockAgentService, mockAgentService,
mockNavigationService,
mockDomainObject, mockDomainObject,
mockEvent, mockEvent,
mockTouchEvent, mockTouchEvent,
@ -53,7 +51,6 @@ define(
beforeEach(function () { beforeEach(function () {
mockTimeout = jasmine.createSpy("$timeout"); mockTimeout = jasmine.createSpy("$timeout");
mockParse = jasmine.createSpy("$parse");
mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS); mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS);
mockAgentService = jasmine.createSpyObj("agentService", ["isMobile"]); mockAgentService = jasmine.createSpyObj("agentService", ["isMobile"]);
mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS); mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS);
@ -62,17 +59,14 @@ define(
"action", "action",
[ "perform", "getActions" ] [ "perform", "getActions" ]
); );
mockActionContext = jasmine.createSpyObj(
"actionContext",
[ "" ]
);
mockActionContext = {domainObject: mockDomainObject, event: mockEvent}; mockActionContext = {domainObject: mockDomainObject, event: mockEvent};
mockDomainObject.getCapability.andReturn(mockContextMenuAction); mockDomainObject.getCapability.andReturn(mockContextMenuAction);
mockContextMenuAction.perform.andReturn(jasmine.any(Function)); mockContextMenuAction.perform.andReturn(jasmine.any(Function));
mockAgentService.isMobile.andReturn(false); mockAgentService.isMobile.andReturn(false);
gesture = new ContextMenuGesture(mockTimeout, mockParse, mockAgentService, mockNavigationService, mockElement, mockDomainObject);
gesture = new ContextMenuGesture(mockTimeout, mockAgentService, mockElement, mockDomainObject);
// Capture the contextmenu callback // Capture the contextmenu callback
fireGesture = mockElement.on.mostRecentCall.args[1]; fireGesture = mockElement.on.mostRecentCall.args[1];
@ -108,7 +102,7 @@ define(
mockAgentService.isMobile.andReturn(true); mockAgentService.isMobile.andReturn(true);
// Then create new (mobile) gesture // Then create new (mobile) gesture
gesture = new ContextMenuGesture(mockTimeout, mockParse, mockAgentService, mockNavigationService, mockElement, mockDomainObject); gesture = new ContextMenuGesture(mockTimeout, mockAgentService, mockElement, mockDomainObject);
// Set calls for the touchstart and touchend gestures // Set calls for the touchstart and touchend gestures
fireTouchStartGesture = mockElement.on.calls[1].args[1]; fireTouchStartGesture = mockElement.on.calls[1].args[1];