Compare commits

...

8 Commits

3 changed files with 202 additions and 43 deletions

View File

@ -172,7 +172,7 @@ define([
* @memberof module:openmct.MCT#
* @name gestures
*/
this.gestures = new api.GestureAPI();
this.gestures = new api.GestureAPI(this, objectUtils);
/**
* An interface for interacting with domain objects and the domain

View File

@ -20,15 +20,27 @@
* at runtime from the About dialog for additional information.
*****************************************************************************/
define([], function () {
define([
'zepto',
'../../../platform/core/src/capabilities/ContextualDomainObject',
'../../selection/ContextManager',
'../../selection/Selection',
'../../selection/SelectGesture'
], function (
$,
ContextualDomainObject,
ContextManager,
Selection,
SelectGesture
) {
/**
* Allows support for common user actions to be attached to views.
* @interface GestureAPI
* @memberof module:openmct
*/
function GestureAPI(selectGesture, contextMenuGesture) {
this.selectGesture = selectGesture;
this.contextMenuGesture = contextMenuGesture;
function GestureAPI(openmct, objectUtils) {
this.openmct = openmct;
this.objectUtils = objectUtils;
}
/**
@ -44,7 +56,7 @@ define([], function () {
* @memberof module:openmct.GestureAPI#
*/
GestureAPI.prototype.selectable = function (htmlElement, item) {
return this.selectGesture.apply(htmlElement, item);
//TODO: implement selectable
};
@ -52,16 +64,73 @@ define([], function () {
* Designate an HTML element as having a context menu associated with
* the provided item.
*
//TODO: should this really be private?
* @private
* @param {HTMLElement} htmlElement the element to make selectable
* @param {*} item the object for which a context menu should appear
* @returns {Function} a function to remove this geture from this
* @param {HTMLElement} htmlElement the element containing the context menu
* @param {*} childObject the object for which a context menu should appear
* @param {*} parentObject the object which has the context required for
* showing the context menu
* @returns {Function} a function to remove this gesture from this
* HTML element.
* @method selectable
* @method contextMenu
* @memberof module:openmct.GestureAPI#
*/
GestureAPI.prototype.contextMenu = function (htmlElement, item) {
return this.contextMenuGesture.apply(htmlElement, item);
GestureAPI.prototype.contextMenu = function (htmlElement, childObject, parentObject) {
var gestureService = this.openmct.$injector.get('gestureService');
if (childObject.hasOwnProperty('identifier')) {
childObject = this.convertAndInstantiateDomainObject(childObject);
}
if (parentObject.hasOwnProperty('identifier')) {
parentObject = this.convertAndInstantiateDomainObject(parentObject);
}
var contextObject = new ContextualDomainObject(childObject, parentObject);
return gestureService.attachGestures($(htmlElement), contextObject, ['menu']);
};
/**
* Designate an HTML element as having a info popover associated with
* the provided item.
*
//
* @private
* @param {HTMLElement} htmlElement the element to make selectable
* @param {*} childObject the object for which a info popover should appear
* @param {*} parentObject the object which has the context required for
* showing the info popover
* @returns {Function} a function to remove this gesture from this
* HTML element.
* @method info
* @memberof module:openmct.GestureAPI#
*/
GestureAPI.prototype.info = function (htmlElement, childObject, parentObject) {
var gestureService = this.openmct.$injector.get('gestureService');
//Check if the objects have an identifier property
if (childObject.hasOwnProperty('identifier')) {
//If they don't convert them into the old object
childObject = this.convertAndInstantiateDomainObject(childObject);
}
if (parentObject.hasOwnProperty('identifier')) {
parentObject = this.convertAndInstantiateDomainObject(parentObject);
}
var contextObject = new ContextualDomainObject(childObject, parentObject);
return gestureService.attachGestures($(htmlElement), contextObject, ['info']);
};
//Converts a new domain object(nDomainObject) to an old domain object(oDomainObject) and instantiates it.
GestureAPI.prototype.convertAndInstantiateDomainObject = function (nDomainObject) {
var instantiate = this.openmct.$injector.get('instantiate');
var keystring = this.objectUtils.makeKeyString(nDomainObject.identifier);
var oDomainObject = this.objectUtils.toOldFormat(nDomainObject);
return instantiate(oDomainObject, keystring);
};
return GestureAPI;

View File

@ -0,0 +1,90 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2017, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT 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 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.
*****************************************************************************/
define([
'./GestureAPI',
'../objects/object-utils'
], function (
GestureAPI,
objectUtils
) {
describe('The Gesture API', function () {
var api, openmct;
beforeEach(function () {
openmct = jasmine.createSpyObj('openmct', ['$injector']);
var gestureService = jasmine.createSpyObj('gestureService', ['attachGestures']);
gestureService.attachGestures.andCallFake(function (arg1,arg2,arg3) {
var destroyFunction = jasmine.createSpy('destroyFunction');
return destroyFunction;
});
var instantiateFunction = jasmine.createSpy('instantiateFunction');
instantiateFunction.andCallFake(function (arg1,arg2) {
return arg1;
});
var $injector = jasmine.createSpyObj('$injector', ['get']);
$injector.get.andCallFake(function (arg) {
if (arg === "gestureService") {
return gestureService;
}
if (arg === "instantiate") {
return instantiateFunction;
}
});
openmct.$injector = $injector;
api = new GestureAPI(openmct, objectUtils);
});
it('attaches a contextmenu to an element and returns a destroy function', function () {
var htmlElement = document.createElement('div');
htmlElement.appendChild(document.createTextNode('test element'));
var nChildDomainObject = jasmine.createSpyObj('nChildDomainObject', ['identifier']);
nChildDomainObject.identifier = '555S';
var nParentDomainObject = jasmine.createSpyObj('nParentDomainObject', ['identifier']);
nParentDomainObject.identifier = '555P';
var destroyFunc = api.contextMenu(htmlElement, nChildDomainObject, nParentDomainObject);
expect(destroyFunc).toBeDefined();
});
it('attaches a infomenu to an element and returns a destroy function', function () {
var htmlElement = document.createElement('div');
htmlElement.appendChild(document.createTextNode('test element'));
var nChildDomainObject = jasmine.createSpyObj('nChildDomainObject', ['identifier']);
nChildDomainObject.identifier = '555S';
var nParentDomainObject = jasmine.createSpyObj('nParentDomainObject', ['identifier']);
nParentDomainObject.identifier = '555P';
var destroyFunc = api.info(htmlElement, nChildDomainObject, nParentDomainObject);
expect(destroyFunc).toBeDefined();
});
it('converts a new domain object to an old one and instantiates it', function () {
var nDomainObject = jasmine.createSpyObj('nDomainObject', ['identifier']);
var oDomainObject = api.convertAndInstantiateDomainObject(nDomainObject);
expect(oDomainObject).toBeDefined();
});
});
});