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