2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
2014-11-23 23:41:20 +00:00
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines EditableDomainObject, which wraps domain objects
|
|
|
|
* such that user code may work with and mutate a copy of the
|
|
|
|
* domain object model; these changes may then be propagated
|
|
|
|
* up to the real domain object (or not) by way of invoking
|
|
|
|
* save or cancel behaviors of the "editor.completion"
|
|
|
|
* capability (a capability intended as internal to edit
|
|
|
|
* mode; invoked by way of the Save and Cancel actions.)
|
|
|
|
*/
|
|
|
|
define(
|
|
|
|
[
|
|
|
|
'../capabilities/EditablePersistenceCapability',
|
|
|
|
'../capabilities/EditableContextCapability',
|
2015-01-27 20:18:28 +00:00
|
|
|
'../capabilities/EditableCompositionCapability',
|
2015-03-09 22:29:25 +00:00
|
|
|
'../capabilities/EditableRelationshipCapability',
|
2014-11-23 23:41:20 +00:00
|
|
|
'../capabilities/EditorCapability',
|
|
|
|
'./EditableDomainObjectCache'
|
|
|
|
],
|
|
|
|
function (
|
|
|
|
EditablePersistenceCapability,
|
|
|
|
EditableContextCapability,
|
2015-01-27 20:18:28 +00:00
|
|
|
EditableCompositionCapability,
|
2015-03-09 22:29:25 +00:00
|
|
|
EditableRelationshipCapability,
|
2014-11-23 23:41:20 +00:00
|
|
|
EditorCapability,
|
|
|
|
EditableDomainObjectCache
|
|
|
|
) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var capabilityFactories = {
|
|
|
|
persistence: EditablePersistenceCapability,
|
|
|
|
context: EditableContextCapability,
|
2015-01-27 20:18:28 +00:00
|
|
|
composition: EditableCompositionCapability,
|
2015-03-09 22:29:25 +00:00
|
|
|
relationship: EditableRelationshipCapability,
|
2014-11-23 23:41:20 +00:00
|
|
|
editor: EditorCapability
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle special case where "editor.completion" wraps persistence
|
|
|
|
// (other capability overrides wrap capabilities of the same type.)
|
|
|
|
function getDelegateArguments(name, args) {
|
|
|
|
return name === "editor" ? ['persistence'] : args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An EditableDomainObject overrides capabilities
|
|
|
|
* which need to behave differently in edit mode,
|
|
|
|
* and provides a "working copy" of the object's
|
|
|
|
* model to allow changes to be easily cancelled.
|
2015-08-07 18:44:54 +00:00
|
|
|
* @constructor
|
|
|
|
* @memberof platform/commonUI/edit
|
2015-08-10 23:38:13 +00:00
|
|
|
* @implements {DomainObject}
|
2014-11-23 23:41:20 +00:00
|
|
|
*/
|
2015-03-24 17:01:45 +00:00
|
|
|
function EditableDomainObject(domainObject, $q) {
|
2014-11-23 23:41:20 +00:00
|
|
|
// The cache will hold all domain objects reached from
|
|
|
|
// the initial EditableDomainObject; this ensures that
|
|
|
|
// different versions of the same editable domain object
|
|
|
|
// are not shown in different sections of the same Edit
|
|
|
|
// UI, which might thereby fall out of sync.
|
|
|
|
var cache;
|
|
|
|
|
|
|
|
// Constructor for EditableDomainObject, which adheres
|
|
|
|
// to the same shared cache.
|
2015-01-27 19:52:58 +00:00
|
|
|
function EditableDomainObjectImpl(domainObject, model) {
|
|
|
|
var editableObject = Object.create(domainObject);
|
2014-11-23 23:41:20 +00:00
|
|
|
|
|
|
|
// Only provide the cloned model.
|
|
|
|
editableObject.getModel = function () { return model; };
|
|
|
|
|
|
|
|
// Override certain capabilities
|
|
|
|
editableObject.getCapability = function (name) {
|
|
|
|
var delegateArguments = getDelegateArguments(name, arguments),
|
2015-03-09 22:29:25 +00:00
|
|
|
capability = domainObject.getCapability.apply(
|
|
|
|
this,
|
|
|
|
delegateArguments
|
|
|
|
),
|
2015-08-10 23:38:13 +00:00
|
|
|
Factory = capabilityFactories[name];
|
2014-11-23 23:41:20 +00:00
|
|
|
|
2015-08-10 23:38:13 +00:00
|
|
|
return (Factory && capability) ?
|
|
|
|
new Factory(capability, editableObject, domainObject, cache) :
|
2014-11-23 23:41:20 +00:00
|
|
|
capability;
|
|
|
|
};
|
2015-10-21 04:03:36 +00:00
|
|
|
|
|
|
|
editableObject.getDomainObject = function() {
|
|
|
|
return domainObject;
|
|
|
|
}
|
2014-11-23 23:41:20 +00:00
|
|
|
|
|
|
|
return editableObject;
|
|
|
|
}
|
|
|
|
|
2015-03-24 17:01:45 +00:00
|
|
|
cache = new EditableDomainObjectCache(EditableDomainObjectImpl, $q);
|
2014-11-23 23:41:20 +00:00
|
|
|
|
|
|
|
return cache.getEditableObject(domainObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EditableDomainObject;
|
|
|
|
}
|
2015-08-07 18:44:54 +00:00
|
|
|
);
|