| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Global renderer and decorator registry. |
| 17 |
* @author [email protected] (Attila Bodis) |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.ui.registry'); |
| 21 |
|
| 22 |
goog.require('goog.asserts'); |
| 23 |
goog.require('goog.dom.classlist'); |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Given a {@link goog.ui.Component} constructor, returns an instance of its |
| 28 |
* default renderer. If the default renderer is a singleton, returns the |
| 29 |
* singleton instance; otherwise returns a new instance of the renderer class. |
| 30 |
* @param {Function} componentCtor Component constructor function (for example |
| 31 |
* {@code goog.ui.Button}). |
| 32 |
* @return {goog.ui.ControlRenderer?} Renderer instance (for example the |
| 33 |
* singleton instance of {@code goog.ui.ButtonRenderer}), or null if |
| 34 |
* no default renderer was found. |
| 35 |
*/ |
| 36 |
goog.ui.registry.getDefaultRenderer = function(componentCtor) { |
| 37 |
// Locate the default renderer based on the constructor's unique ID. If no |
| 38 |
// renderer is registered for this class, walk up the superClass_ chain. |
| 39 |
var key; |
| 40 |
/** @type {Function|undefined} */ var rendererCtor; |
| 41 |
while (componentCtor) { |
| 42 |
key = goog.getUid(componentCtor); |
| 43 |
if ((rendererCtor = goog.ui.registry.defaultRenderers_[key])) { |
| 44 |
break; |
| 45 |
} |
| 46 |
componentCtor = componentCtor.superClass_ ? |
| 47 |
componentCtor.superClass_.constructor : |
| 48 |
null; |
| 49 |
} |
| 50 |
|
| 51 |
// If the renderer has a static getInstance method, return the singleton |
| 52 |
// instance; otherwise create and return a new instance. |
| 53 |
if (rendererCtor) { |
| 54 |
return goog.isFunction(rendererCtor.getInstance) ? |
| 55 |
rendererCtor.getInstance() : |
| 56 |
new rendererCtor(); |
| 57 |
} |
| 58 |
|
| 59 |
return null; |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Sets the default renderer for the given {@link goog.ui.Component} |
| 65 |
* constructor. |
| 66 |
* @param {Function} componentCtor Component constructor function (for example |
| 67 |
* {@code goog.ui.Button}). |
| 68 |
* @param {Function} rendererCtor Renderer constructor function (for example |
| 69 |
* {@code goog.ui.ButtonRenderer}). |
| 70 |
* @throws {Error} If the arguments aren't functions. |
| 71 |
*/ |
| 72 |
goog.ui.registry.setDefaultRenderer = function(componentCtor, rendererCtor) { |
| 73 |
// In this case, explicit validation has negligible overhead (since each |
| 74 |
// renderer is only registered once), and helps catch subtle bugs. |
| 75 |
if (!goog.isFunction(componentCtor)) { |
| 76 |
throw new Error('Invalid component class ' + componentCtor); |
| 77 |
} |
| 78 |
if (!goog.isFunction(rendererCtor)) { |
| 79 |
throw new Error('Invalid renderer class ' + rendererCtor); |
| 80 |
} |
| 81 |
|
| 82 |
// Map the component constructor's unique ID to the renderer constructor. |
| 83 |
var key = goog.getUid(componentCtor); |
| 84 |
goog.ui.registry.defaultRenderers_[key] = rendererCtor; |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the {@link goog.ui.Component} instance created by the decorator |
| 90 |
* factory function registered for the given CSS class name, or null if no |
| 91 |
* decorator factory function was found. |
| 92 |
* @param {string} className CSS class name. |
| 93 |
* @return {goog.ui.Component?} Component instance. |
| 94 |
*/ |
| 95 |
goog.ui.registry.getDecoratorByClassName = function(className) { |
| 96 |
return className in goog.ui.registry.decoratorFunctions_ ? |
| 97 |
goog.ui.registry.decoratorFunctions_[className]() : |
| 98 |
null; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Maps a CSS class name to a function that returns a new instance of |
| 104 |
* {@link goog.ui.Component} or a subclass, suitable to decorate an element |
| 105 |
* that has the specified CSS class. |
| 106 |
* @param {string} className CSS class name. |
| 107 |
* @param {Function} decoratorFn No-argument function that returns a new |
| 108 |
* instance of a {@link goog.ui.Component} to decorate an element. |
| 109 |
* @throws {Error} If the class name or the decorator function is invalid. |
| 110 |
*/ |
| 111 |
goog.ui.registry.setDecoratorByClassName = function(className, decoratorFn) { |
| 112 |
// In this case, explicit validation has negligible overhead (since each |
| 113 |
// decorator is only registered once), and helps catch subtle bugs. |
| 114 |
if (!className) { |
| 115 |
throw new Error('Invalid class name ' + className); |
| 116 |
} |
| 117 |
if (!goog.isFunction(decoratorFn)) { |
| 118 |
throw new Error('Invalid decorator function ' + decoratorFn); |
| 119 |
} |
| 120 |
|
| 121 |
goog.ui.registry.decoratorFunctions_[className] = decoratorFn; |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Returns an instance of {@link goog.ui.Component} or a subclass suitable to |
| 127 |
* decorate the given element, based on its CSS class. |
| 128 |
* |
| 129 |
* TODO(nnaze): Type of element should be {!Element}. |
| 130 |
* |
| 131 |
* @param {Element} element Element to decorate. |
| 132 |
* @return {goog.ui.Component?} Component to decorate the element (null if |
| 133 |
* none). |
| 134 |
*/ |
| 135 |
goog.ui.registry.getDecorator = function(element) { |
| 136 |
var decorator; |
| 137 |
goog.asserts.assert(element); |
| 138 |
var classNames = goog.dom.classlist.get(element); |
| 139 |
for (var i = 0, len = classNames.length; i < len; i++) { |
| 140 |
if ((decorator = goog.ui.registry.getDecoratorByClassName(classNames[i]))) { |
| 141 |
return decorator; |
| 142 |
} |
| 143 |
} |
| 144 |
return null; |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Resets the global renderer and decorator registry. |
| 150 |
*/ |
| 151 |
goog.ui.registry.reset = function() { |
| 152 |
goog.ui.registry.defaultRenderers_ = {}; |
| 153 |
goog.ui.registry.decoratorFunctions_ = {}; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Map of {@link goog.ui.Component} constructor unique IDs to the constructors |
| 159 |
* of their default {@link goog.ui.Renderer}s. |
| 160 |
* @type {Object} |
| 161 |
* @private |
| 162 |
*/ |
| 163 |
goog.ui.registry.defaultRenderers_ = {}; |
| 164 |
|
| 165 |
|
| 166 |
/** |
| 167 |
* Map of CSS class names to registry factory functions. The keys are |
| 168 |
* class names. The values are function objects that return new instances |
| 169 |
* of {@link goog.ui.registry} or one of its subclasses, suitable to |
| 170 |
* decorate elements marked with the corresponding CSS class. Used by |
| 171 |
* containers while decorating their children. |
| 172 |
* @type {Object} |
| 173 |
* @private |
| 174 |
*/ |
| 175 |
goog.ui.registry.decoratorFunctions_ = {}; |