[Code Style] Use prototypes in containment bundle

WTD-1482.
This commit is contained in:
Victor Woeltjen 2015-08-11 11:01:13 -07:00
parent aa08db1050
commit f377c7cb71
6 changed files with 94 additions and 114 deletions

View File

@ -36,7 +36,7 @@ define(
* @memberof platform/containment
*/
function CapabilityTable(typeService, capabilityService) {
var table = {};
var self = this;
// Build an initial model for a type
function buildModel(type) {
@ -54,26 +54,26 @@ define(
function addToTable(type) {
var typeKey = type.getKey();
Object.keys(getCapabilities(type)).forEach(function (key) {
table[key] = table[key] || {};
table[key][typeKey] = true;
self.table[key] = self.table[key] || {};
self.table[key][typeKey] = true;
});
}
// Build the table
this.table = {};
(typeService.listTypes() || []).forEach(addToTable);
return {
/**
* Check if a type is expected to expose a specific
* capability.
* @memberof platform/containment.CapabilityTable#
*/
hasCapability: function (typeKey, capabilityKey) {
return (table[capabilityKey] || {})[typeKey];
}
};
}
/**
* Check if a type is expected to expose a specific capability.
* @param {string} typeKey the type identifier
* @param {string} capabilityKey the capability identifier
* @returns {boolean} true if expected to be exposed
*/
CapabilityTable.prototype.hasCapability = function (typeKey, capabilityKey) {
return (this.table[capabilityKey] || {})[typeKey];
};
return CapabilityTable;
}
);

View File

@ -36,47 +36,48 @@ define(
* Angular's `$injector`.
* @constructor
* @memberof platform/containment
* @implements {Policy.<Action, ActionContext>}
*/
function ComposeActionPolicy($injector) {
var policyService;
function allowComposition(containerObject, selectedObject) {
// Get the object types involved in the compose action
var containerType = containerObject &&
containerObject.getCapability('type'),
selectedType = selectedObject &&
selectedObject.getCapability('type');
// Get a reference to the policy service if needed...
policyService = policyService || $injector.get('policyService');
// ...and delegate to the composition policy
return policyService.allow(
'composition',
containerType,
selectedType
);
}
return {
/**
* Check whether or not a compose action should be allowed
* in this context.
* @returns {boolean} true if it may be allowed
* @memberof platform/containment.ComposeActionPolicy#
*/
allow: function (candidate, context) {
if (candidate.getMetadata().key === 'compose') {
return allowComposition(
(context || {}).domainObject,
(context || {}).selectedObject
);
}
return true;
}
this.getPolicyService = function () {
return $injector.get('policyService');
};
}
ComposeActionPolicy.prototype.allowComposition = function (containerObject, selectedObject) {
// Get the object types involved in the compose action
var containerType = containerObject &&
containerObject.getCapability('type'),
selectedType = selectedObject &&
selectedObject.getCapability('type');
// Get a reference to the policy service if needed...
this.policyService = this.policyService || this.getPolicyService();
// ...and delegate to the composition policy
return this.policyService.allow(
'composition',
containerType,
selectedType
);
}
/**
* Check whether or not a compose action should be allowed
* in this context.
* @returns {boolean} true if it may be allowed
* @memberof platform/containment.ComposeActionPolicy#
*/
ComposeActionPolicy.prototype.allow = function (candidate, context) {
if (candidate.getMetadata().key === 'compose') {
return this.allowComposition(
(context || {}).domainObject,
(context || {}).selectedObject
);
}
return true;
};
return ComposeActionPolicy;
}

View File

@ -10,22 +10,17 @@ define(
* have a composition property.
* @constructor
* @memberof platform/containment
* @implements {Policy.<Type, Type>}
*/
function CompositionModelPolicy() {
return {
/**
* Is the type identified by the candidate allowed to
* contain the type described by the context?
* @memberof platform/containment.CompositionModelPolicy#
*/
allow: function (candidate, context) {
return Array.isArray(
(candidate.getInitialModel() || {}).composition
);
}
};
}
CompositionModelPolicy.prototype.allow = function (candidate, context) {
return Array.isArray(
(candidate.getInitialModel() || {}).composition
);
};
return CompositionModelPolicy;
}
);

View File

@ -30,24 +30,18 @@ define(
* Disallow composition changes to objects which are not mutable.
* @memberof platform/containment
* @constructor
* @implements {Policy.<Type, Type>}
*/
function CompositionMutabilityPolicy() {
return {
/**
* Is the type identified by the candidate allowed to
* contain the type described by the context?
* @param {Type} candidate the type of domain object
* @memberof platform/containment.CompositionMutabilityPolicy#
*/
allow: function (candidate) {
// Equate creatability with mutability; that is, users
// can only modify objects of types they can create, and
// vice versa.
return candidate.hasFeature('creation');
}
};
}
CompositionMutabilityPolicy.prototype.allow = function (candidate) {
// Equate creatability with mutability; that is, users
// can only modify objects of types they can create, and
// vice versa.
return candidate.hasFeature('creation');
};
return CompositionMutabilityPolicy;
}
);

View File

@ -35,31 +35,25 @@ define(
* Defines composition policy as driven by type metadata.
* @constructor
* @memberof platform/containment
* @implements {Policy.<Type, Type>}
*/
function CompositionPolicy($injector) {
// We're really just wrapping the containment table and rephrasing
// it as a policy decision.
var table;
function getTable() {
this.getTable = function () {
return (table = table || new ContainmentTable(
$injector.get('typeService'),
$injector.get('capabilityService')
));
}
return {
/**
* Is the type identified by the candidate allowed to
* contain the type described by the context?
* @memberof platform/containment.CompositionPolicy#
*/
allow: function (candidate, context) {
return getTable().canContain(candidate, context);
}
};
}
CompositionPolicy.prototype.allow = function (candidate, context) {
return this.getTable().canContain(candidate, context);
};
return CompositionPolicy;
}
);

View File

@ -41,13 +41,9 @@ define(
* @memberof platform/containment
*/
function ContainmentTable(typeService, capabilityService) {
var types = typeService.listTypes(),
capabilityTable = new CapabilityTable(typeService, capabilityService),
table = {};
// Check if one type can contain another
function canContain(containerType, containedType) {
}
var self = this,
types = typeService.listTypes(),
capabilityTable = new CapabilityTable(typeService, capabilityService);
// Add types which have all these capabilities to the set
// of allowed types
@ -84,39 +80,39 @@ define(
// Check for defined containment restrictions
if (contains === undefined) {
// If not, accept anything
table[key] = ANY;
self.table[key] = ANY;
} else {
// Start with an empty set...
table[key] = {};
self.table[key] = {};
// ...cast accepted types to array if necessary...
contains = Array.isArray(contains) ? contains : [contains];
// ...and add all containment rules to that set
contains.forEach(function (c) {
addToSet(table[key], c);
addToSet(self.table[key], c);
});
}
}
// Build the table
this.table = {};
types.forEach(addToTable);
return {
/**
* Check if domain objects of one type can contain domain
* objects of another type.
* @returns {boolean} true if allowable
* @memberof platform/containment.ContainmentTable#
*/
canContain: function (containerType, containedType) {
var set = table[containerType.getKey()] || {};
// Recognize either the symbolic value for "can contain
// anything", or lookup the specific type from the set.
return (set === ANY) || set[containedType.getKey()];
}
};
}
/**
* Check if domain objects of one type can contain domain
* objects of another type.
* @param {Type} containerType type of the containing domain object
* @param {Type} containedType type of the domain object
* to be contained
* @returns {boolean} true if allowable
*/
ContainmentTable.prototype.canContain = function (containerType, containedType) {
var set = this.table[containerType.getKey()] || {};
// Recognize either the symbolic value for "can contain
// anything", or lookup the specific type from the set.
return (set === ANY) || set[containedType.getKey()];
};
return ContainmentTable;
}
);