[Code Style] Use prototypes in framework layer

WTD-1482
This commit is contained in:
Victor Woeltjen
2015-08-14 14:42:25 -07:00
parent 7fe866060b
commit edca2a9f03
14 changed files with 750 additions and 676 deletions

View File

@ -56,13 +56,7 @@ define(
function Bundle(path, bundleDefinition) {
// Start with defaults
var definition = Object.create(Constants.DEFAULT_BUNDLE),
logName = path,
self;
// Utility function for resolving paths in this bundle
function resolvePath(elements) {
return [path].concat(elements || []).join(Constants.SEPARATOR);
}
logName = path;
// Override defaults with specifics from bundle definition
Object.keys(bundleDefinition).forEach(function (k) {
@ -81,135 +75,138 @@ define(
logName += ")";
}
self = {
/**
* Get the path to this bundle.
* @memberof Bundle#
* @returns {string}
* @memberof platform/framework.Bundle#
*/
getPath: function () {
return path;
},
/**
* Get the path to this bundle's source folder. If an
* argument is provided, the path will be to the source
* file within the bundle's source file.
*
* @memberof Bundle#
* @param {string} [sourceFile] optionally, give a path to
* a specific source file in the bundle.
* @returns {string}
* @memberof platform/framework.Bundle#
*/
getSourcePath: function (sourceFile) {
var subpath = sourceFile ?
[ definition.sources, sourceFile ] :
[ definition.sources ];
return resolvePath(subpath);
},
/**
* Get the path to this bundle's resource folder. If an
* argument is provided, the path will be to the resource
* file within the bundle's resource file.
*
* @memberof Bundle#
* @param {string} [resourceFile] optionally, give a path to
* a specific resource file in the bundle.
* @returns {string}
* @memberof platform/framework.Bundle#
*/
getResourcePath: function (resourceFile) {
var subpath = resourceFile ?
[ definition.resources, resourceFile ] :
[ definition.resources ];
return resolvePath(subpath);
},
/**
* Get the path to this bundle's library folder. If an
* argument is provided, the path will be to the library
* file within the bundle's resource file.
*
* @memberof Bundle#
* @param {string} [libraryFile] optionally, give a path to
* a specific library file in the bundle.
* @returns {string}
* @memberof platform/framework.Bundle#
*/
getLibraryPath: function (libraryFile) {
var subpath = libraryFile ?
[ definition.libraries, libraryFile ] :
[ definition.libraries ];
return resolvePath(subpath);
},
/**
* Get library configuration for this bundle. This is read
* from the bundle's definition; if the bundle is well-formed,
* it will resemble a require.config object.
* @memberof Bundle#
* @returns {object}
* @memberof platform/framework.Bundle#
*/
getConfiguration: function () {
return definition.configuration || {};
},
/**
* Get a log-friendly name for this bundle; this will
* include both the key (machine-readable name for this
* bundle) and the name (human-readable name for this
* bundle.)
* @returns {string} log-friendly name for this bundle
* @memberof platform/framework.Bundle#
*/
getLogName: function () {
return logName;
},
/**
* Get all extensions exposed by this bundle of a given
* category.
*
* @param category
* @memberof Bundle#
* @returns {Array}
* @memberof platform/framework.Bundle#
*/
getExtensions: function (category) {
var extensions = definition.extensions[category] || [];
return extensions.map(function objectify(extDefinition) {
return new Extension(self, category, extDefinition);
});
},
/**
* Get a list of all categories of extension exposed by
* this bundle.
*
* @memberof Bundle#
* @returns {Array}
* @memberof platform/framework.Bundle#
*/
getExtensionCategories: function () {
return Object.keys(definition.extensions);
},
/**
* Get the plain definition of this bundle, as read from
* its JSON declaration.
*
* @memberof Bundle#
* @returns {BundleDefinition} the raw definition of this bundle
* @memberof platform/framework.Bundle#
*/
getDefinition: function () {
return definition;
}
};
return self;
this.path = path;
this.definition = definition;
this.logName = logName;
}
// Utility function for resolving paths in this bundle
Bundle.prototype.resolvePath = function (elements) {
var path = this.path;
return [path].concat(elements || []).join(Constants.SEPARATOR);
};
/**
* Get the path to this bundle.
* @returns {string} path to this bundle;
*/
Bundle.prototype.getPath = function () {
return this.path;
};
/**
* Get the path to this bundle's source folder. If an
* argument is provided, the path will be to the source
* file within the bundle's source file.
*
* @param {string} [sourceFile] optionally, give a path to
* a specific source file in the bundle.
* @returns {string} path to the source folder (or to the
* source file within it)
*/
Bundle.prototype.getSourcePath = function (sourceFile) {
var subpath = sourceFile ?
[ this.definition.sources, sourceFile ] :
[ this.definition.sources ];
return this.resolvePath(subpath);
};
/**
* Get the path to this bundle's resource folder. If an
* argument is provided, the path will be to the resource
* file within the bundle's resource file.
*
* @param {string} [resourceFile] optionally, give a path to
* a specific resource file in the bundle.
* @returns {string} path to the resource folder (or to the
* resource file within it)
*/
Bundle.prototype.getResourcePath = function (resourceFile) {
var subpath = resourceFile ?
[ this.definition.resources, resourceFile ] :
[ this.definition.resources ];
return this.resolvePath(subpath);
};
/**
* Get the path to this bundle's library folder. If an
* argument is provided, the path will be to the library
* file within the bundle's resource file.
*
* @param {string} [libraryFile] optionally, give a path to
* a specific library file in the bundle.
* @returns {string} path to the resource folder (or to the
* resource file within it)
*/
Bundle.prototype.getLibraryPath = function (libraryFile) {
var subpath = libraryFile ?
[ this.definition.libraries, libraryFile ] :
[ this.definition.libraries ];
return this.resolvePath(subpath);
};
/**
* Get library configuration for this bundle. This is read
* from the bundle's definition; if the bundle is well-formed,
* it will resemble a require.config object.
* @returns {object} library configuration
*/
Bundle.prototype.getConfiguration = function () {
return this.definition.configuration || {};
};
/**
* Get a log-friendly name for this bundle; this will
* include both the key (machine-readable name for this
* bundle) and the name (human-readable name for this
* bundle.)
* @returns {string} log-friendly name for this bundle
*/
Bundle.prototype.getLogName = function () {
return this.logName;
};
/**
* Get all extensions exposed by this bundle of a given
* category.
*
* @param {string} category name of the extension category
* @returns {Array} extension definitions of that cataegory
*/
Bundle.prototype.getExtensions = function (category) {
var extensions = this.definition.extensions[category] || [],
self = this;
return extensions.map(function objectify(extDefinition) {
return new Extension(self, category, extDefinition);
});
};
/**
* Get a list of all extension categories exposed by this bundle.
*
* @returns {string[]} the extension categories
*/
Bundle.prototype.getExtensionCategories = function () {
return Object.keys(this.definition.extensions);
};
/**
* Get the plain definition of this bundle, as read from
* its JSON declaration.
*
* @returns {platform/framework.BundleDefinition} the raw
* definition of this bundle
*/
Bundle.prototype.getDefinition = function () {
return this.definition;
};
return Bundle;
}
);

View File

@ -41,10 +41,26 @@ define(
*
* @memberof platform/framework
* @constructor
* @param {object} $http Angular's HTTP requester
* @param {object} $log Angular's logging service
* @param $http Angular's HTTP requester
* @param $log Angular's logging service
*/
function BundleLoader($http, $log) {
this.$http = $http;
this.$log = $log;
}
/**
* Load a group of bundles, to be used to constitute the
* application by later framework initialization phases.
*
* @param {string|string[]} an array of bundle names to load, or
* the name of a JSON file containing that array
* @returns {Promise.<Bundle[]>} a promise for the loaded bundles
*/
BundleLoader.prototype.loadBundles = function (bundles) {
var $http = this.$http,
$log = this.$log;
// Utility function; load contents of JSON file using $http
function getJSON(file) {
@ -92,7 +108,7 @@ define(
var bundlePromises = bundleArray.map(loadBundle);
return Promise.all(bundlePromises)
.then(filterBundles);
.then(filterBundles);
}
// Load all bundles named in the referenced file. The file is
@ -101,31 +117,10 @@ define(
return getJSON(listFile).then(loadBundlesFromArray);
}
// Load all indicated bundles. If the argument is an array,
// this is taken to be a list of all bundles to load; if it
// is a string, then it is treated as the name of a JSON
// file containing the list of bundles to load.
function loadBundles(bundles) {
return Array.isArray(bundles) ? loadBundlesFromArray(bundles) :
(typeof bundles === 'string') ? loadBundlesFromFile(bundles) :
Promise.reject(new Error(INVALID_ARGUMENT_MESSAGE));
}
return {
/**
* Load a group of bundles, to be used to constitute the
* application by later framework initialization phases.
*
* @memberof BundleLoader#
* @param {string|string[]} an array of bundle names to load, or
* the name of a JSON file containing that array
* @returns {Promise.<Bundle[]>}
* @memberof platform/framework.BundleLoader#
*/
loadBundles: loadBundles
};
}
return Array.isArray(bundles) ? loadBundlesFromArray(bundles) :
(typeof bundles === 'string') ? loadBundlesFromFile(bundles) :
Promise.reject(new Error(INVALID_ARGUMENT_MESSAGE));
};
return BundleLoader;
}

View File

@ -78,94 +78,93 @@ define(
// Attach bundle metadata
extensionDefinition.bundle = bundle.getDefinition();
return {
/**
* Get the machine-readable identifier for this extension.
*
* @returns {string}
* @memberof platform/framework.Extension#
*/
getKey: function () {
return definition.key || "undefined";
},
/**
* Get the bundle which declared this extension.
*
* @memberof Extension#
* @returns {Bundle}
* @memberof platform/framework.Extension#
*/
getBundle: function () {
return bundle;
},
/**
* Get the category into which this extension falls.
* (e.g. "directives")
*
* @memberof Extension#
* @returns {string}
* @memberof platform/framework.Extension#
*/
getCategory: function () {
return category;
},
/**
* Check whether or not this extension should have an
* associated implementation module which may need to
* be loaded.
*
* @returns {boolean} true if an implementation separate
* from this definition should also be loaded
* @memberof platform/framework.Extension#
*/
hasImplementation: function () {
return definition.implementation !== undefined;
},
/**
* Get the path to the AMD module which implements this
* extension. Will return undefined if there is no
* implementation associated with this extension.
*
* @memberof Extension#
* @returns {string} path to implementation, or undefined
* @memberof platform/framework.Extension#
*/
getImplementationPath: function () {
return definition.implementation ?
bundle.getSourcePath(definition.implementation) :
undefined;
},
/**
* Get a log-friendly name for this extension; this will
* include both the key (machine-readable name for this
* extension) and the name (human-readable name for this
* extension.)
* @returns {string} log-friendly name for this extension
* @memberof platform/framework.Extension#
*/
getLogName: function () {
return logName;
},
/**
* Get the plain definition of the extension.
*
* Note that this definition will have an additional "bundle"
* field which points back to the bundle which defined the
* extension, as a convenience.
*
* @memberof Extension#
* @returns {ExtensionDefinition} the plain definition of
* this extension, as read from the bundle
* declaration.
* @memberof platform/framework.Extension#
*/
getDefinition: function () {
return extensionDefinition;
}
};
this.logName = logName;
this.bundle = bundle;
this.category = category;
this.definition = definition;
this.extensionDefinition = extensionDefinition;
}
/**
* Get the machine-readable identifier for this extension.
*
* @returns {string} the identifier for this extension
*/
Extension.prototype.getKey = function () {
return this.definition.key || "undefined";
};
/**
* Get the bundle which declared this extension.
*
* @returns {Bundle} the declaring bundle
*/
Extension.prototype.getBundle = function () {
return this.bundle;
};
/**
* Get the category into which this extension falls.
* (e.g. "directives")
*
* @returns {string} the extension category
*/
Extension.prototype.getCategory = function () {
return this.category;
};
/**
* Check whether or not this extension should have an
* associated implementation module which may need to
* be loaded.
*
* @returns {boolean} true if an implementation separate
* from this definition should also be loaded
*/
Extension.prototype.hasImplementation = function () {
return this.definition.implementation !== undefined;
};
/**
* Get the path to the AMD module which implements this
* extension. Will return undefined if there is no
* implementation associated with this extension.
*
* @returns {string} path to implementation, or undefined
*/
Extension.prototype.getImplementationPath = function () {
return this.definition.implementation ?
this.bundle.getSourcePath(this.definition.implementation) :
undefined;
};
/**
* Get a log-friendly name for this extension; this will
* include both the key (machine-readable name for this
* extension) and the name (human-readable name for this
* extension.)
*
* @returns {string} log-friendly name for this extension
*/
Extension.prototype.getLogName = function () {
return this.logName;
};
/**
* Get the plain definition of the extension.
*
* Note that this definition will have an additional "bundle"
* field which points back to the bundle which defined the
* extension, as a convenience.
*
* @returns {ExtensionDefinition} the plain definition of
* this extension, as read from the bundle
* declaration.
*/
Extension.prototype.getDefinition = function () {
return this.extensionDefinition;
};
return Extension;
}