* [Composition] provide ids, sync via mutation Composition provides ids, and we sync things via mutation. This simplifies the composition provider interface some, and also fixes some issues with the previous default composition provider related to #1322 fixes #1253 * [Style] Fix style, update jsdoc Fix style, update jsdoc, clean up composition api changes for Fixes #1322 * [Style] Tidy and JSDoc * [Composition] Utilize new composition API Ensures that composition provided by new API works in old API. Some functionality is not present in both places, but for the time being this is sufficient. https://github.com/nasa/openmct/pull/1332 * [Utils] add tests, fix bugs Add tests to objectUtils to ensure correctness. This caught a bug where character escapes were not properly applied or removed. As a result, any missing object that contained a colon in it's key would cause an infinite loop and cause the application to crash. Bug discovered in VISTA integration. * [Style] Fix style * [Roots] Depend on new api for ROOT Depend on new API for ROOT model, ensuring consistency when fetching ROOT model. * [Style] Remove commented code
172 lines
5.0 KiB
JavaScript
172 lines
5.0 KiB
JavaScript
/*****************************************************************************
|
|
* Open MCT, Copyright (c) 2014-2016, 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([
|
|
|
|
], function (
|
|
|
|
) {
|
|
|
|
/**
|
|
* Utility for checking if a thing is an Open MCT Identifier.
|
|
* @private
|
|
*/
|
|
function isIdentifier(thing) {
|
|
return typeof thing === 'object' &&
|
|
thing.hasOwnProperty('key') &&
|
|
thing.hasOwnProperty('namespace');
|
|
}
|
|
|
|
/**
|
|
* Utility for checking if a thing is a key string. Not perfect.
|
|
* @private
|
|
*/
|
|
function isKeyString(thing) {
|
|
return typeof thing === 'string';
|
|
}
|
|
|
|
/**
|
|
* Convert a keyString into an Open MCT Identifier, ex:
|
|
* 'scratch:root' ==> {namespace: 'scratch', key: 'root'}
|
|
*
|
|
* Idempotent.
|
|
*
|
|
* @param keyString
|
|
* @returns identifier
|
|
*/
|
|
function parseKeyString(keyString) {
|
|
if (isIdentifier(keyString)) {
|
|
return keyString;
|
|
}
|
|
var namespace = '',
|
|
key = keyString;
|
|
for (var i = 0; i < key.length; i++) {
|
|
if (key[i] === "\\" && key[i + 1] === ":") {
|
|
i++; // skip escape character.
|
|
} else if (key[i] === ":") {
|
|
key = key.slice(i + 1);
|
|
break;
|
|
}
|
|
namespace += key[i];
|
|
}
|
|
|
|
if (keyString === namespace) {
|
|
namespace = '';
|
|
}
|
|
|
|
return {
|
|
namespace: namespace,
|
|
key: key
|
|
};
|
|
}
|
|
|
|
|
|
/**
|
|
* Convert an Open MCT Identifier into a keyString, ex:
|
|
* {namespace: 'scratch', key: 'root'} ==> 'scratch:root'
|
|
*
|
|
* Idempotent
|
|
*
|
|
* @param identifier
|
|
* @returns keyString
|
|
*/
|
|
function makeKeyString(identifier) {
|
|
if (isKeyString(identifier)) {
|
|
return identifier;
|
|
}
|
|
if (!identifier.namespace) {
|
|
return identifier.key;
|
|
}
|
|
return [
|
|
identifier.namespace.replace(/\:/g, '\\:'),
|
|
identifier.key
|
|
].join(':');
|
|
}
|
|
|
|
/**
|
|
* Convert a new domain object into an old format model, removing the
|
|
* identifier and converting the composition array from Open MCT Identifiers
|
|
* to old format keyStrings.
|
|
*
|
|
* @param domainObject
|
|
* @returns oldFormatModel
|
|
*/
|
|
function toOldFormat(model) {
|
|
model = JSON.parse(JSON.stringify(model));
|
|
delete model.identifier;
|
|
if (model.composition) {
|
|
model.composition = model.composition.map(makeKeyString);
|
|
}
|
|
return model;
|
|
}
|
|
|
|
/**
|
|
* Convert an old format domain object model into a new format domain
|
|
* object. Adds an identifier using the provided keyString, and converts
|
|
* the composition array to utilize Open MCT Identifiers.
|
|
*
|
|
* @param model
|
|
* @param keyString
|
|
* @returns domainObject
|
|
*/
|
|
function toNewFormat(model, keyString) {
|
|
model = JSON.parse(JSON.stringify(model));
|
|
model.identifier = parseKeyString(keyString);
|
|
if (model.composition) {
|
|
model.composition = model.composition.map(parseKeyString);
|
|
}
|
|
return model;
|
|
}
|
|
|
|
/**
|
|
* Compare two Open MCT Identifiers, returning true if they are equal.
|
|
*
|
|
* @param identifier
|
|
* @param otherIdentifier
|
|
* @returns Boolean true if identifiers are equal.
|
|
*/
|
|
function identifierEquals(a, b) {
|
|
return a.key === b.key && a.namespace === b.namespace;
|
|
}
|
|
|
|
/**
|
|
* Compare two domain objects, return true if they're the same object.
|
|
* Equality is determined by identifier.
|
|
*
|
|
* @param domainObject
|
|
* @param otherDomainOBject
|
|
* @returns Boolean true if objects are equal.
|
|
*/
|
|
function objectEquals(a, b) {
|
|
return identifierEquals(a.identifier, b.identifier);
|
|
}
|
|
|
|
return {
|
|
toOldFormat: toOldFormat,
|
|
toNewFormat: toNewFormat,
|
|
makeKeyString: makeKeyString,
|
|
parseKeyString: parseKeyString,
|
|
equals: objectEquals,
|
|
identifierEquals: identifierEquals
|
|
};
|
|
});
|