[Views] Repair view registry for mainViews (#1308)

* [Views] Rewrite mct-view

...to make it a simple way to include non-Angular views, without
integrating unnecessarily with other architectural concerns.
Supports restoration of view registration functionality to fix
#1307

* [API] Use identifier as property name

* [API] Add adapter capability

...to simplify adapter layer (allowing new-style objects to be
retrieved via capability.)

* [API] Add AdaptedViewController

* [API] Add template for adapted views

* [API] Add AdaptedViewPolicy

* [API] Wire in view adapter

* [API] Fix adapter capability implementation

* [API] Don't create new scope from mct-view

...to allow use on same element as ng-controller (and because a
new scope just is not necessary.)
This commit is contained in:
Victor Woeltjen 2016-11-03 10:37:58 -07:00 committed by Pete Richards
parent 025b69541e
commit 547696d797
7 changed files with 181 additions and 56 deletions

View File

@ -23,31 +23,51 @@
define([ define([
'legacyRegistry', 'legacyRegistry',
'./actions/ActionDialogDecorator', './actions/ActionDialogDecorator',
'./capabilities/AdapterCapability',
'./controllers/AdaptedViewController',
'./directives/MCTView', './directives/MCTView',
'./services/Instantiate', './services/Instantiate',
'./services/MissingModelCompatibilityDecorator', './services/MissingModelCompatibilityDecorator',
'./capabilities/APICapabilityDecorator', './capabilities/APICapabilityDecorator',
'./policies/AdapterCompositionPolicy', './policies/AdapterCompositionPolicy',
'./runs/AlternateCompositionInitializer' './policies/AdaptedViewPolicy',
'./runs/AlternateCompositionInitializer',
'text!./templates/adapted-view-template.html'
], function ( ], function (
legacyRegistry, legacyRegistry,
ActionDialogDecorator, ActionDialogDecorator,
AdapterCapability,
AdaptedViewController,
MCTView, MCTView,
Instantiate, Instantiate,
MissingModelCompatibilityDecorator, MissingModelCompatibilityDecorator,
APICapabilityDecorator, APICapabilityDecorator,
AdapterCompositionPolicy, AdapterCompositionPolicy,
AlternateCompositionInitializer AdaptedViewPolicy,
AlternateCompositionInitializer,
adaptedViewTemplate
) { ) {
legacyRegistry.register('src/adapter', { legacyRegistry.register('src/adapter', {
"extensions": { "extensions": {
"directives": [ "directives": [
{ {
key: "mctView", key: "mctView",
implementation: MCTView, implementation: MCTView
}
],
capabilities: [
{
key: "adapter",
implementation: AdapterCapability
}
],
controllers: [
{
key: "AdaptedViewController",
implementation: AdaptedViewController,
depends: [ depends: [
"newViews[]", '$scope',
"openmct" 'openmct'
] ]
} }
], ],
@ -90,6 +110,11 @@ define([
category: "composition", category: "composition",
implementation: AdapterCompositionPolicy, implementation: AdapterCompositionPolicy,
depends: ["openmct"] depends: ["openmct"]
},
{
category: "view",
implementation: AdaptedViewPolicy,
depends: ["openmct"]
} }
], ],
runs: [ runs: [
@ -98,6 +123,12 @@ define([
depends: ["openmct"] depends: ["openmct"]
} }
], ],
views: [
{
key: "adapted-view",
template: adaptedViewTemplate
}
],
licenses: [ licenses: [
{ {
"name": "almond", "name": "almond",

View File

@ -0,0 +1,36 @@
/*****************************************************************************
* 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(['../../api/objects/object-utils'], function (objectUtils) {
function AdapterCapability(domainObject) {
this.domainObject = domainObject;
}
AdapterCapability.prototype.invoke = function () {
return objectUtils.toNewFormat(
this.domainObject.getModel(),
this.domainObject.getId()
);
};
return AdapterCapability;
});

View File

@ -0,0 +1,40 @@
/*****************************************************************************
* 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 () {
function AdaptedViewController($scope, openmct) {
function refresh(legacyObject) {
if (!legacyObject) {
$scope.view = undefined;
return;
}
var domainObject = legacyObject.useCapability('adapter');
var providers = openmct.mainViews.get(domainObject);
$scope.view = providers[0] && providers[0].view(domainObject);
}
$scope.$watch('domainObject', refresh);
}
return AdaptedViewController;
});

View File

@ -29,56 +29,12 @@ define([
Region, Region,
objectUtils objectUtils
) { ) {
function MCTView(newViews, PublicAPI) { function MCTView() {
var definitions = {};
newViews.forEach(function (newView) {
definitions[newView.region] = definitions[newView.region] || {};
definitions[newView.region][newView.key] = newView.factory;
});
return { return {
restrict: 'E', restrict: 'A',
link: function (scope, element, attrs) { link: function (scope, element, attrs) {
var key, mctObject, regionId, region; var region = new Region(element[0]);
scope.$watch(attrs.mctView, region.show.bind(region));
function maybeShow() {
if (!definitions[regionId] || !definitions[regionId][key] || !mctObject) {
return;
}
region.show(definitions[regionId][key].view(mctObject));
}
function setKey(k) {
key = k;
maybeShow();
}
function setObject(obj) {
mctObject = undefined;
PublicAPI.Objects.get(objectUtils.parseKeyString(obj.getId()))
.then(function (mobj) {
mctObject = mobj;
maybeShow();
});
}
function setRegionId(r) {
regionId = r;
maybeShow();
}
region = new Region(element[0]);
scope.$watch('key', setKey);
scope.$watch('region', setRegionId);
scope.$watch('mctObject', setObject);
},
scope: {
key: "=",
region: "=",
mctObject: "="
} }
}; };
} }

View File

@ -0,0 +1,40 @@
/*****************************************************************************
* 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 () {
function AdaptedViewPolicy(openmct) {
this.openmct = openmct;
}
AdaptedViewPolicy.prototype.allow = function (
view,
legacyObject
) {
if (view.key === 'adapted-view') {
var domainObject = legacyObject.useCapability('adapter');
return this.openmct.mainViews.get(domainObject).length > 0;
}
return true;
};
return AdaptedViewPolicy;
});

View File

@ -0,0 +1,22 @@
<!--
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.
-->
<div ng-controller="AdaptedViewController" mct-view="view"></div>

View File

@ -78,7 +78,7 @@ define([
// Converts composition to use key strings instead of keys // Converts composition to use key strings instead of keys
var toOldFormat = function (model) { var toOldFormat = function (model) {
model = JSON.parse(JSON.stringify(model)); model = JSON.parse(JSON.stringify(model));
delete model.key; delete model.identifier;
if (model.composition) { if (model.composition) {
model.composition = model.composition.map(makeKeyString); model.composition = model.composition.map(makeKeyString);
} }
@ -86,9 +86,9 @@ define([
}; };
// converts composition to use keys instead of key strings // converts composition to use keys instead of key strings
var toNewFormat = function (model, key) { var toNewFormat = function (model, identifier) {
model = JSON.parse(JSON.stringify(model)); model = JSON.parse(JSON.stringify(model));
model.key = key; model.identifier = parseKeyString(identifier);
if (model.composition) { if (model.composition) {
model.composition = model.composition.map(parseKeyString); model.composition = model.composition.map(parseKeyString);
} }