registry.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * @fileoverview Global renderer and decorator registry.
  16. * @author attila@google.com (Attila Bodis)
  17. */
  18. goog.provide('goog.ui.registry');
  19. goog.require('goog.asserts');
  20. goog.require('goog.dom.classlist');
  21. /**
  22. * Given a {@link goog.ui.Component} constructor, returns an instance of its
  23. * default renderer. If the default renderer is a singleton, returns the
  24. * singleton instance; otherwise returns a new instance of the renderer class.
  25. * @param {Function} componentCtor Component constructor function (for example
  26. * {@code goog.ui.Button}).
  27. * @return {goog.ui.ControlRenderer?} Renderer instance (for example the
  28. * singleton instance of {@code goog.ui.ButtonRenderer}), or null if
  29. * no default renderer was found.
  30. */
  31. goog.ui.registry.getDefaultRenderer = function(componentCtor) {
  32. // Locate the default renderer based on the constructor's unique ID. If no
  33. // renderer is registered for this class, walk up the superClass_ chain.
  34. var key;
  35. /** @type {Function|undefined} */ var rendererCtor;
  36. while (componentCtor) {
  37. key = goog.getUid(componentCtor);
  38. if ((rendererCtor = goog.ui.registry.defaultRenderers_[key])) {
  39. break;
  40. }
  41. componentCtor = componentCtor.superClass_ ?
  42. componentCtor.superClass_.constructor :
  43. null;
  44. }
  45. // If the renderer has a static getInstance method, return the singleton
  46. // instance; otherwise create and return a new instance.
  47. if (rendererCtor) {
  48. return goog.isFunction(rendererCtor.getInstance) ?
  49. rendererCtor.getInstance() :
  50. new rendererCtor();
  51. }
  52. return null;
  53. };
  54. /**
  55. * Sets the default renderer for the given {@link goog.ui.Component}
  56. * constructor.
  57. * @param {Function} componentCtor Component constructor function (for example
  58. * {@code goog.ui.Button}).
  59. * @param {Function} rendererCtor Renderer constructor function (for example
  60. * {@code goog.ui.ButtonRenderer}).
  61. * @throws {Error} If the arguments aren't functions.
  62. */
  63. goog.ui.registry.setDefaultRenderer = function(componentCtor, rendererCtor) {
  64. // In this case, explicit validation has negligible overhead (since each
  65. // renderer is only registered once), and helps catch subtle bugs.
  66. if (!goog.isFunction(componentCtor)) {
  67. throw Error('Invalid component class ' + componentCtor);
  68. }
  69. if (!goog.isFunction(rendererCtor)) {
  70. throw Error('Invalid renderer class ' + rendererCtor);
  71. }
  72. // Map the component constructor's unique ID to the renderer constructor.
  73. var key = goog.getUid(componentCtor);
  74. goog.ui.registry.defaultRenderers_[key] = rendererCtor;
  75. };
  76. /**
  77. * Returns the {@link goog.ui.Component} instance created by the decorator
  78. * factory function registered for the given CSS class name, or null if no
  79. * decorator factory function was found.
  80. * @param {string} className CSS class name.
  81. * @return {goog.ui.Component?} Component instance.
  82. */
  83. goog.ui.registry.getDecoratorByClassName = function(className) {
  84. return className in goog.ui.registry.decoratorFunctions_ ?
  85. goog.ui.registry.decoratorFunctions_[className]() :
  86. null;
  87. };
  88. /**
  89. * Maps a CSS class name to a function that returns a new instance of
  90. * {@link goog.ui.Component} or a subclass, suitable to decorate an element
  91. * that has the specified CSS class.
  92. * @param {string} className CSS class name.
  93. * @param {Function} decoratorFn No-argument function that returns a new
  94. * instance of a {@link goog.ui.Component} to decorate an element.
  95. * @throws {Error} If the class name or the decorator function is invalid.
  96. */
  97. goog.ui.registry.setDecoratorByClassName = function(className, decoratorFn) {
  98. // In this case, explicit validation has negligible overhead (since each
  99. // decorator is only registered once), and helps catch subtle bugs.
  100. if (!className) {
  101. throw Error('Invalid class name ' + className);
  102. }
  103. if (!goog.isFunction(decoratorFn)) {
  104. throw Error('Invalid decorator function ' + decoratorFn);
  105. }
  106. goog.ui.registry.decoratorFunctions_[className] = decoratorFn;
  107. };
  108. /**
  109. * Returns an instance of {@link goog.ui.Component} or a subclass suitable to
  110. * decorate the given element, based on its CSS class.
  111. *
  112. * TODO(nnaze): Type of element should be {!Element}.
  113. *
  114. * @param {Element} element Element to decorate.
  115. * @return {goog.ui.Component?} Component to decorate the element (null if
  116. * none).
  117. */
  118. goog.ui.registry.getDecorator = function(element) {
  119. var decorator;
  120. goog.asserts.assert(element);
  121. var classNames = goog.dom.classlist.get(element);
  122. for (var i = 0, len = classNames.length; i < len; i++) {
  123. if ((decorator = goog.ui.registry.getDecoratorByClassName(classNames[i]))) {
  124. return decorator;
  125. }
  126. }
  127. return null;
  128. };
  129. /**
  130. * Resets the global renderer and decorator registry.
  131. */
  132. goog.ui.registry.reset = function() {
  133. goog.ui.registry.defaultRenderers_ = {};
  134. goog.ui.registry.decoratorFunctions_ = {};
  135. };
  136. /**
  137. * Map of {@link goog.ui.Component} constructor unique IDs to the constructors
  138. * of their default {@link goog.ui.Renderer}s.
  139. * @type {Object}
  140. * @private
  141. */
  142. goog.ui.registry.defaultRenderers_ = {};
  143. /**
  144. * Map of CSS class names to registry factory functions. The keys are
  145. * class names. The values are function objects that return new instances
  146. * of {@link goog.ui.registry} or one of its subclasses, suitable to
  147. * decorate elements marked with the corresponding CSS class. Used by
  148. * containers while decorating their children.
  149. * @type {Object}
  150. * @private
  151. */
  152. goog.ui.registry.decoratorFunctions_ = {};