From 0e82250c843742247169545a2bd19781585a158a Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Mon, 3 Nov 2014 08:44:52 -0800 Subject: [PATCH] [Framework] Continue implementing fundamentals Continue implementing classes which represent fundamental concepts within the framework layer. WTD-518. In particular, add methods which will be useful during the extension resolution phase of framework start up. --- platform/framework/src/Bundle.js | 80 ++++++++++++++++++++++++++--- platform/framework/src/Constants.js | 9 +++- platform/framework/src/Extension.js | 13 +++++ platform/framework/src/Main.js | 3 +- 4 files changed, 95 insertions(+), 10 deletions(-) diff --git a/platform/framework/src/Bundle.js b/platform/framework/src/Bundle.js index 8a67ed7c5b..2372f17ac0 100644 --- a/platform/framework/src/Bundle.js +++ b/platform/framework/src/Bundle.js @@ -1,8 +1,8 @@ /*global define*/ define( - [], - function () { + ['./Constants', './Extension'], + function (Constants, Extension) { "use strict"; @@ -11,6 +11,10 @@ define( * * @name BundleDefinition * @property {string} name the human-readable name of this bundle + * @property {string} sources the name of the directory which + * contains source code used by this bundle + * @property {string} resources the name of the directory which + * contains resource files used by this bundle * @property {Object.} [extensions={}] * all extensions exposed by this bundle */ @@ -22,26 +26,86 @@ define( * * @param {string} path the path to the directory containing * this bundle - * @param {BundleDefinition} definition + * @param {BundleDefinition} bundleDefinition * @returns {{getDefinition: Function}} * @constructor */ - function Bundle(path, definition) { + function Bundle(path, bundleDefinition) { + // Start with defaults + var definition = Object.create(Constants.DEFAULT_BUNDLE), + self; + // Utility function for resolving paths in this bundle + function resolvePath(elements) { + return path.concat(elements || []).join(Constants.SEPARATOR); + } - return { + // Override defaults with specifics from bundle definition + Object.keys(bundleDefinition).forEach(function (k) { + definition[k] = bundleDefinition[k]; + }); + + return (self = { /** * + * @memberof Bundle# + * @returns {string} + */ + 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} + */ + 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} + */ + getResourcePath: function (resourceFile) { + var subpath = resourceFile ? + [ definition.resources, resourceFile ] : + [ definition.resources ]; + + return resolvePath(subpath); + }, + getExtensions: function (category) { + var extensions = definition.extensions[category] || []; + + return extensions.map(function objectify(extDefinition) { + return new Extension(self, category, extDefinition); + }); + }, + /** + * + * @memberof Bundle# * @returns {BundleDefinition} the raw definition of this bundle */ getDefinition: function () { return definition; } - }; + }); } - new Bundle().getDefinition().extensions['k'][0]. - return Bundle; } ); \ No newline at end of file diff --git a/platform/framework/src/Constants.js b/platform/framework/src/Constants.js index 1c14b2c248..72a4ed8618 100644 --- a/platform/framework/src/Constants.js +++ b/platform/framework/src/Constants.js @@ -6,5 +6,12 @@ define({ MODULE_NAME: "OpenMCTWeb", BUNDLE_LISTING_FILE: "bundles.json", - BUNDLE_FILE: "bundle.json" + BUNDLE_FILE: "bundle.json", + SEPARATOR: "/", + DEFAULT_BUNDLE: { + "sources": "src", + "resources": "res", + "test": "test", + "extensions": {} + } }); \ No newline at end of file diff --git a/platform/framework/src/Extension.js b/platform/framework/src/Extension.js index 0429423cde..358884550f 100644 --- a/platform/framework/src/Extension.js +++ b/platform/framework/src/Extension.js @@ -48,6 +48,19 @@ define( getCategory: function () { return category; }, + /** + * 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 + */ + getImplementationPath: function () { + return definition.implementation ? + bundle.getSourcePath(definition.implementation) : + undefined; + }, /** * @memberof Extension# * @returns {ExtensionDefinition} diff --git a/platform/framework/src/Main.js b/platform/framework/src/Main.js index c914184329..f8765bd9cf 100644 --- a/platform/framework/src/Main.js +++ b/platform/framework/src/Main.js @@ -12,7 +12,8 @@ define( [ 'require', '../lib/es6-promise-2.0.0.min', - '../lib/angular.min' + '../lib/angular.min', + './BundleLoader' ], function (require, es6promise, angular) { "use strict";