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

View File

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

View File

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

View File

@ -30,23 +30,17 @@ define(
* Disallow composition changes to objects which are not mutable. * Disallow composition changes to objects which are not mutable.
* @memberof platform/containment * @memberof platform/containment
* @constructor * @constructor
* @implements {Policy.<Type, Type>}
*/ */
function CompositionMutabilityPolicy() { function CompositionMutabilityPolicy() {
return { }
/**
* Is the type identified by the candidate allowed to CompositionMutabilityPolicy.prototype.allow = function (candidate) {
* 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 // Equate creatability with mutability; that is, users
// can only modify objects of types they can create, and // can only modify objects of types they can create, and
// vice versa. // vice versa.
return candidate.hasFeature('creation'); return candidate.hasFeature('creation');
}
}; };
}
return CompositionMutabilityPolicy; return CompositionMutabilityPolicy;
} }

View File

@ -35,31 +35,25 @@ define(
* Defines composition policy as driven by type metadata. * Defines composition policy as driven by type metadata.
* @constructor * @constructor
* @memberof platform/containment * @memberof platform/containment
* @implements {Policy.<Type, Type>}
*/ */
function CompositionPolicy($injector) { function CompositionPolicy($injector) {
// We're really just wrapping the containment table and rephrasing // We're really just wrapping the containment table and rephrasing
// it as a policy decision. // it as a policy decision.
var table; var table;
function getTable() { this.getTable = function () {
return (table = table || new ContainmentTable( return (table = table || new ContainmentTable(
$injector.get('typeService'), $injector.get('typeService'),
$injector.get('capabilityService') $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; return CompositionPolicy;
} }
); );

View File

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