[Roots] Register roots with new API

Register roots added via old API with new API when the application
starts. Fixes #1264.
This commit is contained in:
Victor Woeltjen 2016-10-19 14:43:04 -07:00
parent 64ff463200
commit a143b21ea1
4 changed files with 104 additions and 19 deletions

View File

@ -27,7 +27,8 @@ define([
'./services/Instantiate',
'./capabilities/APICapabilityDecorator',
'./policies/AdapterCompositionPolicy',
'./runs/AlternateCompositionInitializer'
'./runs/AlternateCompositionInitializer',
'./runs/RootRegistrar'
], function (
legacyRegistry,
ActionDialogDecorator,
@ -35,7 +36,8 @@ define([
Instantiate,
APICapabilityDecorator,
AdapterCompositionPolicy,
AlternateCompositionInitializer
AlternateCompositionInitializer,
RootRegistrar
) {
legacyRegistry.register('src/adapter', {
"extensions": {
@ -88,6 +90,10 @@ define([
{
implementation: AlternateCompositionInitializer,
depends: ["openmct"]
},
{
implementation: RootRegistrar,
depends: ["openmct", "roots[]"]
}
],
licenses: [

View File

@ -0,0 +1,32 @@
/*****************************************************************************
* 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 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.
*****************************************************************************/
define(['../../api/objects/object-utils', 'lodash'], function (utils, _) {
function RootRegistrar(openmct, roots) {
_.map(roots, 'id').map(utils.parseKeyString)
.forEach(function (identifier) {
openmct.objects.addRoot(identifier);
});
}
return RootRegistrar;
});

View File

@ -0,0 +1,47 @@
/*****************************************************************************
* 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 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.
*****************************************************************************/
define(['./RootRegistrar'], function (RootRegistrar) {
describe("RootRegistrar", function () {
var openmct,
roots;
beforeEach(function () {
roots = ['a:a', 'a:b', 'x:c'].map(function (id) {
return { id: id, model: { name: "My root " + id } };
});
openmct = {
objects: jasmine.createSpyObj('objects', ['addRoot'])
};
return new RootRegistrar(openmct, roots);
});
it("adds legacy root extensions using ObjectAPI", function () {
expect(openmct.objects.addRoot)
.toHaveBeenCalledWith({ key: 'a', namespace: 'a' });
expect(openmct.objects.addRoot)
.toHaveBeenCalledWith({ key: 'b', namespace: 'a' });
expect(openmct.objects.addRoot)
.toHaveBeenCalledWith({ key: 'c', namespace: 'x' });
});
});
});

View File

@ -28,50 +28,50 @@ define([
// take a key string and turn it into a key object
// 'scratch:root' ==> {namespace: 'scratch', identifier: 'root'}
var parseKeyString = function (key) {
if (typeof key === 'object') {
return key;
var parseKeyString = function (identifier) {
if (typeof identifier === 'object') {
return identifier;
}
var namespace = '',
identifier = key;
key = identifier;
for (var i = 0, escaped = false; i < key.length; i++) {
if (escaped) {
escaped = false;
namespace += key[i];
} else {
if (key[i] === "\\") {
if (identifier[i] === "\\") {
escaped = true;
} else if (key[i] === ":") {
} else if (identifier[i] === ":") {
// namespace = key.slice(0, i);
identifier = key.slice(i + 1);
key = identifier.slice(i + 1);
break;
}
namespace += key[i];
namespace += identifier[i];
}
}
if (key === namespace) {
if (identifier === namespace) {
namespace = '';
}
return {
namespace: namespace,
identifier: identifier
key: key
};
};
// take a key and turn it into a key string
// {namespace: 'scratch', identifier: 'root'} ==> 'scratch:root'
var makeKeyString = function (key) {
if (typeof key === 'string') {
return key;
var makeKeyString = function (identifier) {
if (typeof identifier === 'string') {
return identifier;
}
if (!key.namespace) {
return key.identifier;
if (!identifier.namespace) {
return identifier.key;
}
return [
key.namespace.replace(':', '\\:'),
key.identifier.replace(':', '\\:')
identifier.namespace.replace(':', '\\:'),
identifier.key.replace(':', '\\:')
].join(':');
};