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', './actions/ActionDialogDecorator',
'./directives/MCTView', './directives/MCTView',
'./services/Instantiate', './services/Instantiate',
'./capabilities/APICapabilityDecorator' './capabilities/APICapabilityDecorator',
'./policies/AdapterCompositionPolicy'
], function ( ], function (
legacyRegistry, legacyRegistry,
ActionDialogDecorator, ActionDialogDecorator,
MCTView, MCTView,
Instantiate, Instantiate,
APICapabilityDecorator APICapabilityDecorator,
AdapterCompositionPolicy
) { ) {
legacyRegistry.register('src/adapter', { legacyRegistry.register('src/adapter', {
"extensions": { "extensions": {
@ -50,6 +52,13 @@ define([
implementation: ActionDialogDecorator, implementation: ActionDialogDecorator,
depends: [ "mct", "newViews[]" ] 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) { if (!this._children) {
throw new Error("Must load composition before you can add!"); 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 (this.contains(child)) {
if (skipMutate) { if (skipMutate) {
return; // don't add twice, don't error. 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 () { CompositionCollection.prototype.destroy = function () {
if (this.provider.off) { if (this.provider.off) {
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) { DefaultCompositionProvider.prototype.remove = function (domainObject, child) {
// TODO: this needs to be synchronized via mutation // TODO: this needs to be synchronized via mutation
var index = domainObject.composition.indexOf(child); var index = domainObject.composition.indexOf(child);