2015-08-11 21:54:01 +00:00
|
|
|
/*****************************************************************************
|
2017-04-05 21:35:12 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2017, United States Government
|
2015-08-11 21:54:01 +00:00
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
2015-08-11 21:54:01 +00:00
|
|
|
* "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.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT includes source code licensed under additional open source
|
2015-08-11 21:54:01 +00:00
|
|
|
* 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(
|
2017-04-05 22:53:17 +00:00
|
|
|
['./CancelError'],
|
|
|
|
function (CancelError) {
|
|
|
|
var CANCEL_MESSAGE = "User cancelled location selection.";
|
2015-08-11 21:54:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Common interface exposed by services which support move, copy,
|
|
|
|
* and link actions.
|
|
|
|
* @interface platform/entanglement.AbstractComposeService
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
/**
|
2015-09-22 21:10:06 +00:00
|
|
|
* Change the composition of the specified objects. Note that this
|
|
|
|
* should only be invoked after successfully validating.
|
2015-08-11 21:54:01 +00:00
|
|
|
*
|
|
|
|
* @param {DomainObject} domainObject the domain object to
|
|
|
|
* move, copy, or link.
|
|
|
|
* @param {DomainObject} parent the domain object whose composition
|
|
|
|
* will be changed to contain the domainObject (or its duplicate)
|
|
|
|
* @returns {Promise} A promise that is fulfilled when the
|
|
|
|
* duplicate operation has completed.
|
|
|
|
* @method platform/entanglement.AbstractComposeService#perform
|
|
|
|
*/
|
|
|
|
/**
|
2015-09-22 22:42:39 +00:00
|
|
|
* Check if this composition change is valid for these objects.
|
|
|
|
*
|
2015-08-11 21:54:01 +00:00
|
|
|
* @param {DomainObject} domainObject the domain object to
|
|
|
|
* move, copy, or link.
|
|
|
|
* @param {DomainObject} parent the domain object whose composition
|
|
|
|
* will be changed to contain the domainObject (or its duplicate)
|
|
|
|
* @returns {boolean} true if this composition change is allowed
|
|
|
|
* @method platform/entanglement.AbstractComposeService#validate
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Template class for Move, Copy, and Link actions.
|
|
|
|
*
|
|
|
|
* @implements {Action}
|
|
|
|
* @constructor
|
|
|
|
* @private
|
|
|
|
* @memberof platform/entanglement
|
2015-11-25 17:04:11 +00:00
|
|
|
* @param {PolicyService} policyService the policy service to use to
|
|
|
|
* verify that variants of this action are allowed
|
2015-08-11 21:54:01 +00:00
|
|
|
* @param {platform/entanglement.LocationService} locationService a
|
|
|
|
* service to request destinations from the user
|
|
|
|
* @param {platform/entanglement.AbstractComposeService} composeService
|
|
|
|
* a service which will handle actual changes to composition
|
|
|
|
* @param {ActionContext} the context in which the action will be performed
|
|
|
|
* @param {string} verb the verb to display for the action (e.g. "Move")
|
|
|
|
* @param {string} [suffix] a string to display in the dialog title;
|
|
|
|
* default is "to a new location"
|
|
|
|
*/
|
2015-11-25 17:04:11 +00:00
|
|
|
function AbstractComposeAction(
|
|
|
|
policyService,
|
|
|
|
locationService,
|
|
|
|
composeService,
|
|
|
|
context,
|
|
|
|
verb,
|
|
|
|
suffix
|
|
|
|
) {
|
2015-08-11 21:54:01 +00:00
|
|
|
if (context.selectedObject) {
|
|
|
|
this.newParent = context.domainObject;
|
|
|
|
this.object = context.selectedObject;
|
|
|
|
} else {
|
|
|
|
this.object = context.domainObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentParent = this.object
|
|
|
|
.getCapability('context')
|
|
|
|
.getParent();
|
|
|
|
|
2015-11-25 17:04:11 +00:00
|
|
|
this.context = context;
|
|
|
|
this.policyService = policyService;
|
2015-08-11 21:54:01 +00:00
|
|
|
this.locationService = locationService;
|
|
|
|
this.composeService = composeService;
|
|
|
|
this.verb = verb || "Compose";
|
2016-08-09 23:06:53 +00:00
|
|
|
this.suffix = suffix || "To a New Location";
|
2015-08-11 21:54:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 17:04:11 +00:00
|
|
|
AbstractComposeAction.prototype.cloneContext = function () {
|
|
|
|
var clone = {}, original = this.context;
|
|
|
|
Object.keys(original).forEach(function (k) {
|
|
|
|
clone[k] = original[k];
|
|
|
|
});
|
|
|
|
return clone;
|
|
|
|
};
|
|
|
|
|
2015-08-11 21:54:01 +00:00
|
|
|
AbstractComposeAction.prototype.perform = function () {
|
|
|
|
var dialogTitle,
|
|
|
|
label,
|
|
|
|
validateLocation,
|
2015-11-25 17:04:11 +00:00
|
|
|
self = this,
|
2015-08-11 21:54:01 +00:00
|
|
|
locationService = this.locationService,
|
|
|
|
composeService = this.composeService,
|
|
|
|
currentParent = this.currentParent,
|
|
|
|
newParent = this.newParent,
|
|
|
|
object = this.object;
|
|
|
|
|
|
|
|
if (newParent) {
|
|
|
|
return composeService.perform(object, newParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
dialogTitle = [this.verb, object.getModel().name, this.suffix]
|
|
|
|
.join(" ");
|
|
|
|
|
|
|
|
label = this.verb + " To";
|
|
|
|
|
2016-05-20 18:39:49 +00:00
|
|
|
validateLocation = function (newParentObj) {
|
2015-11-25 17:04:11 +00:00
|
|
|
var newContext = self.cloneContext();
|
|
|
|
newContext.selectedObject = object;
|
2016-05-20 18:39:49 +00:00
|
|
|
newContext.domainObject = newParentObj;
|
|
|
|
return composeService.validate(object, newParentObj) &&
|
2015-11-25 17:04:11 +00:00
|
|
|
self.policyService.allow("action", self, newContext);
|
2015-08-11 21:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return locationService.getLocationFromUser(
|
|
|
|
dialogTitle,
|
|
|
|
label,
|
|
|
|
validateLocation,
|
|
|
|
currentParent
|
2016-05-20 18:39:49 +00:00
|
|
|
).then(function (newParentObj) {
|
|
|
|
return composeService.perform(object, newParentObj);
|
2017-02-25 13:05:29 +00:00
|
|
|
}, function () {
|
2017-04-05 22:53:17 +00:00
|
|
|
return Promise.reject(new CancelError(CANCEL_MESSAGE));
|
|
|
|
}.bind(this));
|
2015-08-11 21:54:01 +00:00
|
|
|
};
|
|
|
|
|
2015-11-10 00:28:04 +00:00
|
|
|
AbstractComposeAction.appliesTo = function (context) {
|
|
|
|
var applicableObject =
|
|
|
|
context.selectedObject || context.domainObject;
|
|
|
|
|
|
|
|
return !!(applicableObject &&
|
|
|
|
applicableObject.hasCapability('context'));
|
|
|
|
};
|
|
|
|
|
2015-08-11 21:54:01 +00:00
|
|
|
return AbstractComposeAction;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|