From f377c7cb716122f09cd3393f366735bd4d48e3c6 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 11 Aug 2015 11:01:13 -0700 Subject: [PATCH] [Code Style] Use prototypes in containment bundle WTD-1482. --- platform/containment/src/CapabilityTable.js | 28 +++---- .../containment/src/ComposeActionPolicy.js | 73 ++++++++++--------- .../containment/src/CompositionModelPolicy.js | 19 ++--- .../src/CompositionMutabilityPolicy.js | 22 ++---- platform/containment/src/CompositionPolicy.js | 18 ++--- platform/containment/src/ContainmentTable.js | 48 ++++++------ 6 files changed, 94 insertions(+), 114 deletions(-) diff --git a/platform/containment/src/CapabilityTable.js b/platform/containment/src/CapabilityTable.js index 5a2374af5d..db89f9e68f 100644 --- a/platform/containment/src/CapabilityTable.js +++ b/platform/containment/src/CapabilityTable.js @@ -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; } ); diff --git a/platform/containment/src/ComposeActionPolicy.js b/platform/containment/src/ComposeActionPolicy.js index 90c7983bc9..53a1bf0ab8 100644 --- a/platform/containment/src/ComposeActionPolicy.js +++ b/platform/containment/src/ComposeActionPolicy.js @@ -36,47 +36,48 @@ define( * Angular's `$injector`. * @constructor * @memberof platform/containment + * @implements {Policy.} */ 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; } diff --git a/platform/containment/src/CompositionModelPolicy.js b/platform/containment/src/CompositionModelPolicy.js index 0c76097ed4..d5e5cb5f72 100644 --- a/platform/containment/src/CompositionModelPolicy.js +++ b/platform/containment/src/CompositionModelPolicy.js @@ -10,22 +10,17 @@ define( * have a composition property. * @constructor * @memberof platform/containment + * @implements {Policy.} */ 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; } ); diff --git a/platform/containment/src/CompositionMutabilityPolicy.js b/platform/containment/src/CompositionMutabilityPolicy.js index 375f26e405..8c5ef6a765 100644 --- a/platform/containment/src/CompositionMutabilityPolicy.js +++ b/platform/containment/src/CompositionMutabilityPolicy.js @@ -30,24 +30,18 @@ define( * Disallow composition changes to objects which are not mutable. * @memberof platform/containment * @constructor + * @implements {Policy.} */ 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; } ); diff --git a/platform/containment/src/CompositionPolicy.js b/platform/containment/src/CompositionPolicy.js index 9445803557..1f5239ec59 100644 --- a/platform/containment/src/CompositionPolicy.js +++ b/platform/containment/src/CompositionPolicy.js @@ -35,31 +35,25 @@ define( * Defines composition policy as driven by type metadata. * @constructor * @memberof platform/containment + * @implements {Policy.} */ 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; } ); diff --git a/platform/containment/src/ContainmentTable.js b/platform/containment/src/ContainmentTable.js index a71570ca3a..823c782faf 100644 --- a/platform/containment/src/ContainmentTable.js +++ b/platform/containment/src/ContainmentTable.js @@ -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; } );