From 30b769d7412e98d02545f17f342139c246d141d3 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 27 Oct 2016 12:10:45 -0700 Subject: [PATCH 1/6] [API] Repair type registration Fixes #1294 --- src/MCT.js | 7 +++++++ src/api/Type.js | 24 ++++++++++++++++++++++++ src/api/types/TypeRegistry.js | 20 ++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/MCT.js b/src/MCT.js index c1b328b736..31cf0dbf47 100644 --- a/src/MCT.js +++ b/src/MCT.js @@ -248,6 +248,13 @@ define([ }.bind(this) }); + this.types.listKeys().forEach(function (typeKey) { + var type = this.types.get(typeKey); + var legacyDefinition = type.toLegacyDefinition(); + legacyDefinition.key = typeKey; + this.legacyExtension('types', legacyDefinition); + }.bind(this)); + legacyRegistry.register('adapter', this.legacyBundle); legacyRegistry.enable('adapter'); /** diff --git a/src/api/Type.js b/src/api/Type.js index 717e417b21..af0bf73fab 100644 --- a/src/api/Type.js +++ b/src/api/Type.js @@ -55,5 +55,29 @@ define(function () { return domainObject.type === this.key; }; + /** + * Get a definition for this type that can be registered using the + * legacy bundle format. + * @private + */ + Type.prototype.toLegacyDefinition = function () { + var def = {}; + def.name = this.definition.label; + def.cssclass = this.definition.cssclass; + def.description = this.definition.description; + def.properties = this.definition.form; + + if (this.definition.initialize) { + def.model = {}; + this.definition.initialize(def.model); + } + + if (this.definition.creatable) { + def.features = ['creation']; + } + + return def; + }; + return Type; }); diff --git a/src/api/types/TypeRegistry.js b/src/api/types/TypeRegistry.js index e01bea4a9d..7a58ac52a6 100644 --- a/src/api/types/TypeRegistry.js +++ b/src/api/types/TypeRegistry.js @@ -44,6 +44,26 @@ define([], function () { this.types[typeKey] = type; }; + /** + * List keys for all registered types. + * @method list + * @memberof module:openmct.TypeRegistry# + * @returns {string[]} all registered type keys + */ + TypeRegistry.prototype.listKeys = function () { + return Object.keys(this.types); + }; + + /** + * Retrieve a registered type by its key. + * @method get + * @param {string} typeKey the key for htis type + * @memberof module:openmct.TypeRegistry# + * @returns {module:openmct.Type} the registered type + */ + TypeRegistry.prototype.get = function (typeKey) { + return this.types[typeKey]; + } return TypeRegistry; }); From 23eff4b924cfa11232e76fbc1f8d85c39add7e90 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 27 Oct 2016 12:16:07 -0700 Subject: [PATCH 2/6] [API] Fix TypeRegistry docstring Fixes #1295 --- src/api/types/TypeRegistry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/types/TypeRegistry.js b/src/api/types/TypeRegistry.js index 7a58ac52a6..ffea660510 100644 --- a/src/api/types/TypeRegistry.js +++ b/src/api/types/TypeRegistry.js @@ -37,7 +37,7 @@ define([], function () { * * @param {string} typeKey a string identifier for this type * @param {module:openmct.Type} type the type to add - * @method addProvider + * @method addType * @memberof module:openmct.TypeRegistry# */ TypeRegistry.prototype.addType = function (typeKey, type) { From 833bad067edaceb6ce812cd0a468cc14c2dfb55a Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 27 Oct 2016 12:16:57 -0700 Subject: [PATCH 3/6] [API] Add creatable property to example --- API.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/API.md b/API.md index fff93ef2d7..bc6422ad43 100644 --- a/API.md +++ b/API.md @@ -73,7 +73,8 @@ Custom types may be registered via ``` openmct.types.addType('my-type', new openmct.Type({ label: "My Type", - description: "This is a type that I added!" + description: "This is a type that I added!", + creatable: true }); ``` From 2bf05ae40ffb79b0f555828ba7900b0e50b5acfc Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 27 Oct 2016 12:21:16 -0700 Subject: [PATCH 4/6] [API] Add missing semicolon, satisfy JSHint --- src/api/types/TypeRegistry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/types/TypeRegistry.js b/src/api/types/TypeRegistry.js index ffea660510..720a43d6c4 100644 --- a/src/api/types/TypeRegistry.js +++ b/src/api/types/TypeRegistry.js @@ -63,7 +63,7 @@ define([], function () { */ TypeRegistry.prototype.get = function (typeKey) { return this.types[typeKey]; - } + }; return TypeRegistry; }); From 12333f34179e34509abca8b54a2a961e63659b98 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 27 Oct 2016 12:22:46 -0700 Subject: [PATCH 5/6] [API] Use correct method name in docstring --- src/api/types/TypeRegistry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/types/TypeRegistry.js b/src/api/types/TypeRegistry.js index 720a43d6c4..dc49df1887 100644 --- a/src/api/types/TypeRegistry.js +++ b/src/api/types/TypeRegistry.js @@ -46,7 +46,7 @@ define([], function () { /** * List keys for all registered types. - * @method list + * @method listKeys * @memberof module:openmct.TypeRegistry# * @returns {string[]} all registered type keys */ From 9a0fcc045c876c1bb78472d40862fdfc2aeff177 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 27 Oct 2016 15:30:34 -0700 Subject: [PATCH 6/6] [API] Simplify type registration https://github.com/nasa/openmct/pull/1302#discussion_r85417042 --- API.md | 2 +- src/api/api.js | 3 --- src/api/{ => types}/Type.js | 11 +---------- src/api/types/TypeRegistry.js | 17 ++++++++++++++--- 4 files changed, 16 insertions(+), 17 deletions(-) rename src/api/{ => types}/Type.js (83%) diff --git a/API.md b/API.md index bc6422ad43..8bef3c3889 100644 --- a/API.md +++ b/API.md @@ -71,7 +71,7 @@ Custom types may be registered via [`openmct.types`]{@link module:openmct.MCT#types}: ``` -openmct.types.addType('my-type', new openmct.Type({ +openmct.types.addType('my-type', { label: "My Type", description: "This is a type that I added!", creatable: true diff --git a/src/api/api.js b/src/api/api.js index f68460578b..c118e84ef7 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -21,7 +21,6 @@ *****************************************************************************/ define([ - './Type', './TimeConductor', './objects/ObjectAPI', './composition/CompositionAPI', @@ -30,7 +29,6 @@ define([ './ui/GestureAPI', './telemetry/TelemetryAPI' ], function ( - Type, TimeConductor, ObjectAPI, CompositionAPI, @@ -40,7 +38,6 @@ define([ TelemetryAPI ) { return { - Type: Type, TimeConductor: TimeConductor, ObjectAPI: ObjectAPI, CompositionAPI: CompositionAPI, diff --git a/src/api/Type.js b/src/api/types/Type.js similarity index 83% rename from src/api/Type.js rename to src/api/types/Type.js index af0bf73fab..c135972b31 100644 --- a/src/api/Type.js +++ b/src/api/types/Type.js @@ -21,21 +21,12 @@ *****************************************************************************/ define(function () { - /** - * @typedef TypeDefinition - * @memberof module:openmct.Type~ - * @property {Metadata} metadata displayable metadata about this type - * @property {function (object)} [initialize] a function which initializes - * the model for new domain objects of this type - * @property {boolean} [creatable] true if users should be allowed to - * create this type (default: false) - */ /** * A Type describes a kind of domain object that may appear or be * created within Open MCT. * - * @param {module:opemct.Type~TypeDefinition} definition + * @param {module:opemct.TypeRegistry~TypeDefinition} definition * @class Type * @memberof module:openmct */ diff --git a/src/api/types/TypeRegistry.js b/src/api/types/TypeRegistry.js index dc49df1887..6a887850a2 100644 --- a/src/api/types/TypeRegistry.js +++ b/src/api/types/TypeRegistry.js @@ -20,7 +20,18 @@ * at runtime from the About dialog for additional information. *****************************************************************************/ -define([], function () { +define(['./Type'], function (Type) { + /** + * @typedef TypeDefinition + * @memberof module:openmct.TypeRegistry~ + * @property {string} label the name for this type of object + * @property {string} description a longer-form description of this type + * @property {function (object)} [initialize] a function which initializes + * the model for new domain objects of this type + * @property {boolean} [creatable] true if users should be allowed to + * create this type (default: false) + * @property {string} [cssclass] the CSS class to apply for icons + */ /** * A TypeRegistry maintains the definitions for different types @@ -40,8 +51,8 @@ define([], function () { * @method addType * @memberof module:openmct.TypeRegistry# */ - TypeRegistry.prototype.addType = function (typeKey, type) { - this.types[typeKey] = type; + TypeRegistry.prototype.addType = function (typeKey, typeDef) { + this.types[typeKey] = new Type(typeDef); }; /**