Path: blob/trunk/third_party/closure/goog/ui/registry.js
4079 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Global renderer and decorator registry.8*/910goog.provide('goog.ui.registry');1112goog.require('goog.asserts');13goog.require('goog.dom.classlist');14goog.require('goog.object');15goog.requireType('goog.ui.Component');16goog.requireType('goog.ui.ControlRenderer');171819/**20* Given a {@link goog.ui.Component} constructor, returns an instance of its21* default renderer. If the default renderer is a singleton, returns the22* singleton instance; otherwise returns a new instance of the renderer class.23* @param {!Function} componentCtor Component constructor function (for example24* `goog.ui.Button`).25* @return {?goog.ui.ControlRenderer} Renderer instance (for example the26* singleton instance of `goog.ui.ButtonRenderer`), or null if27* no default renderer was found.28*/29goog.ui.registry.getDefaultRenderer = function(componentCtor) {30'use strict';31// TODO(user): This should probably be implemented with a `WeakMap`.32// Locate the default renderer based on the constructor's unique ID. If no33// renderer is registered for this class, walk up the superClass_ chain.34var key;35var /** ?Function|undefined */ ctor = componentCtor;36var /** ?Function|undefined */ rendererCtor;37while (ctor) {38key = goog.getUid(ctor);39if ((rendererCtor = goog.ui.registry.defaultRenderers_[key])) break;40ctor = /** @type {?Function|undefined} */ (goog.object.getSuperClass(ctor));41}4243// If the renderer has a static getInstance method, return the singleton44// instance; otherwise create and return a new instance.45if (rendererCtor) {46return typeof rendererCtor.getInstance === 'function' ?47rendererCtor.getInstance() :48new rendererCtor();49}5051return null;52};535455/**56* Sets the default renderer for the given {@link goog.ui.Component}57* constructor.58* @param {Function} componentCtor Component constructor function (for example59* `goog.ui.Button`).60* @param {Function} rendererCtor Renderer constructor function (for example61* `goog.ui.ButtonRenderer`).62* @throws {Error} If the arguments aren't functions.63*/64goog.ui.registry.setDefaultRenderer = function(componentCtor, rendererCtor) {65'use strict';66// In this case, explicit validation has negligible overhead (since each67// renderer is only registered once), and helps catch subtle bugs.68if (typeof componentCtor !== 'function') {69throw new Error('Invalid component class ' + componentCtor);70}71if (typeof rendererCtor !== 'function') {72throw new Error('Invalid renderer class ' + rendererCtor);73}7475// Map the component constructor's unique ID to the renderer constructor.76var key = goog.getUid(componentCtor);77goog.ui.registry.defaultRenderers_[key] = rendererCtor;78};798081/**82* Returns the {@link goog.ui.Component} instance created by the decorator83* factory function registered for the given CSS class name, or null if no84* decorator factory function was found.85* @param {string} className CSS class name.86* @return {goog.ui.Component?} Component instance.87*/88goog.ui.registry.getDecoratorByClassName = function(className) {89'use strict';90return className in goog.ui.registry.decoratorFunctions_ ?91goog.ui.registry.decoratorFunctions_[className]() :92null;93};949596/**97* Maps a CSS class name to a function that returns a new instance of98* {@link goog.ui.Component} or a subclass, suitable to decorate an element99* that has the specified CSS class.100* @param {string} className CSS class name.101* @param {Function} decoratorFn No-argument function that returns a new102* instance of a {@link goog.ui.Component} to decorate an element.103* @throws {Error} If the class name or the decorator function is invalid.104*/105goog.ui.registry.setDecoratorByClassName = function(className, decoratorFn) {106'use strict';107// In this case, explicit validation has negligible overhead (since each108// decorator is only registered once), and helps catch subtle bugs.109if (!className) {110throw new Error('Invalid class name ' + className);111}112if (typeof decoratorFn !== 'function') {113throw new Error('Invalid decorator function ' + decoratorFn);114}115116goog.ui.registry.decoratorFunctions_[className] = decoratorFn;117};118119120/**121* Returns an instance of {@link goog.ui.Component} or a subclass suitable to122* decorate the given element, based on its CSS class.123*124* TODO(nnaze): Type of element should be {!Element}.125*126* @param {Element} element Element to decorate.127* @return {goog.ui.Component?} Component to decorate the element (null if128* none).129*/130goog.ui.registry.getDecorator = function(element) {131'use strict';132var decorator;133goog.asserts.assert(element);134var classNames = goog.dom.classlist.get(element);135for (var i = 0, len = classNames.length; i < len; i++) {136if ((decorator = goog.ui.registry.getDecoratorByClassName(classNames[i]))) {137return decorator;138}139}140return null;141};142143144/**145* Resets the global renderer and decorator registry.146*/147goog.ui.registry.reset = function() {148'use strict';149goog.ui.registry.defaultRenderers_ = {};150goog.ui.registry.decoratorFunctions_ = {};151};152153154/**155* Map of {@link goog.ui.Component} constructor unique IDs to the constructors156* of their default {@link goog.ui.Renderer}s.157* @type {Object}158* @private159*/160goog.ui.registry.defaultRenderers_ = {};161162163/**164* Map of CSS class names to registry factory functions. The keys are165* class names. The values are function objects that return new instances166* of {@link goog.ui.registry} or one of its subclasses, suitable to167* decorate elements marked with the corresponding CSS class. Used by168* containers while decorating their children.169* @type {Object}170* @private171*/172goog.ui.registry.decoratorFunctions_ = {};173174175