2015-05-13 16:42:35 -07: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-12-05 12:48:21 -08:00
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
define(
|
|
|
|
[],
|
|
|
|
function () {
|
|
|
|
"use strict";
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
/**
|
|
|
|
* The EditRepresenter is responsible for implementing
|
|
|
|
* representation-level behavior relevant to Edit mode.
|
|
|
|
* Specifically, this listens for changes to view configuration
|
|
|
|
* or to domain object models, and triggers persistence when
|
|
|
|
* these are detected.
|
|
|
|
*
|
|
|
|
* This is exposed as an extension of category `representers`,
|
|
|
|
* which mct-representation will utilize to add additional
|
|
|
|
* behavior to each representation.
|
|
|
|
*
|
|
|
|
* This will be called once per mct-representation directive,
|
|
|
|
* and may be reused for different domain objects and/or
|
|
|
|
* representations resulting from changes there.
|
|
|
|
*
|
2015-08-07 11:44:54 -07:00
|
|
|
* @memberof platform/commonUI/edit
|
2015-08-10 16:38:13 -07:00
|
|
|
* @implements {Representer}
|
2014-12-05 18:01:38 -08:00
|
|
|
* @constructor
|
|
|
|
*/
|
2014-12-06 09:38:28 -08:00
|
|
|
function EditRepresenter($q, $log, scope) {
|
2015-08-10 16:38:13 -07:00
|
|
|
var self = this;
|
2014-12-05 12:48:21 -08:00
|
|
|
|
2015-11-25 10:40:37 -08:00
|
|
|
this.scope = scope;
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Mutate and persist a new version of a domain object's model.
|
2014-12-05 12:48:21 -08:00
|
|
|
function doPersist(model) {
|
2015-08-10 16:38:13 -07:00
|
|
|
var domainObject = self.domainObject;
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// First, mutate; then, persist.
|
2014-12-05 16:27:32 -08:00
|
|
|
return $q.when(domainObject.useCapability("mutation", function () {
|
|
|
|
return model;
|
|
|
|
})).then(function (result) {
|
2014-12-05 18:01:38 -08:00
|
|
|
// Only persist when mutation was successful
|
2014-12-05 12:48:21 -08:00
|
|
|
return result &&
|
|
|
|
domainObject.getCapability("persistence").persist();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Handle changes to model and/or view configuration
|
2014-12-06 09:38:28 -08:00
|
|
|
function commit(message) {
|
2014-12-05 18:01:38 -08:00
|
|
|
// Look up from scope; these will have been populated by
|
|
|
|
// mct-representation.
|
2014-12-05 12:48:21 -08:00
|
|
|
var model = scope.model,
|
2015-08-10 16:38:13 -07:00
|
|
|
configuration = scope.configuration,
|
|
|
|
domainObject = self.domainObject;
|
2014-12-05 12:48:21 -08:00
|
|
|
|
2014-12-06 09:38:28 -08:00
|
|
|
// Log the commit message
|
|
|
|
$log.debug([
|
|
|
|
"Committing ",
|
|
|
|
domainObject && domainObject.getModel().name,
|
|
|
|
"(" + (domainObject && domainObject.getId()) + "):",
|
|
|
|
message
|
|
|
|
].join(" "));
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Update the configuration stored in the model, and persist.
|
2014-12-05 12:48:21 -08:00
|
|
|
if (domainObject && domainObject.hasCapability("persistence")) {
|
2014-12-05 18:01:38 -08:00
|
|
|
// Configurations for specific views are stored by
|
|
|
|
// key in the "configuration" field of the model.
|
2015-08-10 16:38:13 -07:00
|
|
|
if (self.key && configuration) {
|
2014-12-05 16:27:32 -08:00
|
|
|
model.configuration = model.configuration || {};
|
2015-08-10 16:38:13 -07:00
|
|
|
model.configuration[self.key] = configuration;
|
2014-12-05 16:27:32 -08:00
|
|
|
}
|
2014-12-05 12:48:21 -08:00
|
|
|
doPersist(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 09:38:28 -08:00
|
|
|
// Place the "commit" method in the scope
|
|
|
|
scope.commit = commit;
|
2014-12-05 12:48:21 -08:00
|
|
|
}
|
|
|
|
|
2015-08-10 16:38:13 -07:00
|
|
|
// Handle a specific representation of a specific domain object
|
|
|
|
EditRepresenter.prototype.represent = function represent(representation, representedObject) {
|
|
|
|
// Track the key, to know which view configuration to save to.
|
|
|
|
this.key = (representation || {}).key;
|
|
|
|
// Track the represented object
|
|
|
|
this.domainObject = representedObject;
|
2015-11-25 10:40:37 -08:00
|
|
|
this.scope.editMode = representedObject.hasCapability("editor");
|
|
|
|
|
2015-08-10 16:38:13 -07:00
|
|
|
// Ensure existing watches are released
|
|
|
|
this.destroy();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Respond to the destruction of the current representation.
|
|
|
|
EditRepresenter.prototype.destroy = function destroy() {
|
|
|
|
// Nothing to clean up
|
|
|
|
};
|
|
|
|
|
2014-12-05 12:48:21 -08:00
|
|
|
return EditRepresenter;
|
|
|
|
}
|
2015-08-07 11:44:54 -07:00
|
|
|
);
|