menurenderer.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Renderer for {@link goog.ui.Menu}s.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.ui.MenuRenderer');
  20. goog.require('goog.a11y.aria');
  21. goog.require('goog.a11y.aria.Role');
  22. goog.require('goog.a11y.aria.State');
  23. goog.require('goog.asserts');
  24. goog.require('goog.dom');
  25. goog.require('goog.dom.TagName');
  26. goog.require('goog.ui.ContainerRenderer');
  27. goog.require('goog.ui.Separator');
  28. /**
  29. * Default renderer for {@link goog.ui.Menu}s, based on {@link
  30. * goog.ui.ContainerRenderer}.
  31. * @param {string=} opt_ariaRole Optional ARIA role used for the element.
  32. * @constructor
  33. * @extends {goog.ui.ContainerRenderer}
  34. */
  35. goog.ui.MenuRenderer = function(opt_ariaRole) {
  36. goog.ui.ContainerRenderer.call(
  37. this, opt_ariaRole || goog.a11y.aria.Role.MENU);
  38. };
  39. goog.inherits(goog.ui.MenuRenderer, goog.ui.ContainerRenderer);
  40. goog.addSingletonGetter(goog.ui.MenuRenderer);
  41. /**
  42. * Default CSS class to be applied to the root element of toolbars rendered
  43. * by this renderer.
  44. * @type {string}
  45. */
  46. goog.ui.MenuRenderer.CSS_CLASS = goog.getCssName('goog-menu');
  47. /**
  48. * Returns whether the element is a UL or acceptable to our superclass.
  49. * @param {Element} element Element to decorate.
  50. * @return {boolean} Whether the renderer can decorate the element.
  51. * @override
  52. */
  53. goog.ui.MenuRenderer.prototype.canDecorate = function(element) {
  54. return element.tagName == goog.dom.TagName.UL ||
  55. goog.ui.MenuRenderer.superClass_.canDecorate.call(this, element);
  56. };
  57. /**
  58. * Inspects the element, and creates an instance of {@link goog.ui.Control} or
  59. * an appropriate subclass best suited to decorate it. Overrides the superclass
  60. * implementation by recognizing HR elements as separators.
  61. * @param {Element} element Element to decorate.
  62. * @return {goog.ui.Control?} A new control suitable to decorate the element
  63. * (null if none).
  64. * @override
  65. */
  66. goog.ui.MenuRenderer.prototype.getDecoratorForChild = function(element) {
  67. return element.tagName == goog.dom.TagName.HR ?
  68. new goog.ui.Separator() :
  69. goog.ui.MenuRenderer.superClass_.getDecoratorForChild.call(this, element);
  70. };
  71. /**
  72. * Returns whether the given element is contained in the menu's DOM.
  73. * @param {goog.ui.Menu} menu The menu to test.
  74. * @param {Element} element The element to test.
  75. * @return {boolean} Whether the given element is contained in the menu.
  76. */
  77. goog.ui.MenuRenderer.prototype.containsElement = function(menu, element) {
  78. return goog.dom.contains(menu.getElement(), element);
  79. };
  80. /**
  81. * Returns the CSS class to be applied to the root element of containers
  82. * rendered using this renderer.
  83. * @return {string} Renderer-specific CSS class.
  84. * @override
  85. */
  86. goog.ui.MenuRenderer.prototype.getCssClass = function() {
  87. return goog.ui.MenuRenderer.CSS_CLASS;
  88. };
  89. /** @override */
  90. goog.ui.MenuRenderer.prototype.initializeDom = function(container) {
  91. goog.ui.MenuRenderer.superClass_.initializeDom.call(this, container);
  92. var element = container.getElement();
  93. goog.asserts.assert(element, 'The menu DOM element cannot be null.');
  94. goog.a11y.aria.setState(element, goog.a11y.aria.State.HASPOPUP, 'true');
  95. };