Merge pull request #1121 from nasa/api-containment

[API] Containment
This commit is contained in:
Victor Woeltjen 2016-08-25 13:19:14 -07:00 committed by GitHub
commit 02aa08a3ef
4 changed files with 48 additions and 2 deletions

View File

@ -3,13 +3,15 @@ define([
'./actions/ActionDialogDecorator',
'./directives/MCTView',
'./services/Instantiate',
'./capabilities/APICapabilityDecorator'
'./capabilities/APICapabilityDecorator',
'./policies/AdapterCompositionPolicy'
], function (
legacyRegistry,
ActionDialogDecorator,
MCTView,
Instantiate,
APICapabilityDecorator
APICapabilityDecorator,
AdapterCompositionPolicy
) {
legacyRegistry.register('src/adapter', {
"extensions": {
@ -50,6 +52,13 @@ define([
implementation: ActionDialogDecorator,
depends: [ "mct", "newViews[]" ]
}
],
policies: [
{
category: "composition",
implementation: AdapterCompositionPolicy,
depends: [ "mct" ]
}
]
}
});

View File

@ -0,0 +1,26 @@
define([], function () {
function AdapterCompositionPolicy(mct) {
this.mct = mct;
}
AdapterCompositionPolicy.prototype.allow = function (
containerType,
childType
) {
var containerObject = containerType.getInitialModel();
var childObject = childType.getInitialModel();
containerObject.type = containerType.getKey();
childObject.type = childType.getKey();
var composition = this.mct.Composition(containerObject);
if (composition) {
return composition.canContain(childObject);
}
return true;
};
return AdapterCompositionPolicy;
});

View File

@ -52,6 +52,9 @@ define([
if (!this._children) {
throw new Error("Must load composition before you can add!");
}
if (!this.canContain(child)) {
throw new Error("This object cannot contain that object.");
}
if (this.contains(child)) {
if (skipMutate) {
return; // don't add twice, don't error.
@ -94,6 +97,10 @@ define([
}
};
CompositionCollection.prototype.canContain = function (domainObject) {
return this.provider.canContain(this.domainObject, domainObject);
};
CompositionCollection.prototype.destroy = function () {
if (this.provider.off) {
this.provider.off(

View File

@ -59,6 +59,10 @@ define([
);
};
DefaultCompositionProvider.prototype.canContain = function (domainObject, child) {
return true;
};
DefaultCompositionProvider.prototype.remove = function (domainObject, child) {
// TODO: this needs to be synchronized via mutation
var index = domainObject.composition.indexOf(child);