Path: blob/trunk/third_party/closure/goog/ui/menurenderer.js
4116 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Renderer for {@link goog.ui.Menu}s.8*/910goog.provide('goog.ui.MenuRenderer');1112goog.require('goog.a11y.aria');13goog.require('goog.a11y.aria.Role');14goog.require('goog.a11y.aria.State');15goog.require('goog.asserts');16goog.require('goog.dom');17goog.require('goog.dom.TagName');18goog.require('goog.ui.ContainerRenderer');19goog.require('goog.ui.Separator');20goog.requireType('goog.ui.Control');21goog.requireType('goog.ui.Menu');22232425/**26* Default renderer for {@link goog.ui.Menu}s, based on {@link27* goog.ui.ContainerRenderer}.28* @param {string=} opt_ariaRole Optional ARIA role used for the element.29* @constructor30* @extends {goog.ui.ContainerRenderer}31*/32goog.ui.MenuRenderer = function(opt_ariaRole) {33'use strict';34goog.ui.ContainerRenderer.call(35this, opt_ariaRole || goog.a11y.aria.Role.MENU);36};37goog.inherits(goog.ui.MenuRenderer, goog.ui.ContainerRenderer);38goog.addSingletonGetter(goog.ui.MenuRenderer);394041/**42* Default CSS class to be applied to the root element of toolbars rendered43* by this renderer.44* @type {string}45*/46goog.ui.MenuRenderer.CSS_CLASS = goog.getCssName('goog-menu');474849/**50* Returns whether the element is a UL or acceptable to our superclass.51* @param {Element} element Element to decorate.52* @return {boolean} Whether the renderer can decorate the element.53* @override54*/55goog.ui.MenuRenderer.prototype.canDecorate = function(element) {56'use strict';57return element.tagName == goog.dom.TagName.UL ||58goog.ui.MenuRenderer.superClass_.canDecorate.call(this, element);59};606162/**63* Inspects the element, and creates an instance of {@link goog.ui.Control} or64* an appropriate subclass best suited to decorate it. Overrides the superclass65* implementation by recognizing HR elements as separators.66* @param {Element} element Element to decorate.67* @return {goog.ui.Control?} A new control suitable to decorate the element68* (null if none).69* @override70*/71goog.ui.MenuRenderer.prototype.getDecoratorForChild = function(element) {72'use strict';73return element.tagName == goog.dom.TagName.HR ?74new goog.ui.Separator() :75goog.ui.MenuRenderer.superClass_.getDecoratorForChild.call(this, element);76};777879/**80* Returns whether the given element is contained in the menu's DOM.81* @param {goog.ui.Menu} menu The menu to test.82* @param {Element} element The element to test.83* @return {boolean} Whether the given element is contained in the menu.84*/85goog.ui.MenuRenderer.prototype.containsElement = function(menu, element) {86'use strict';87return goog.dom.contains(menu.getElement(), element);88};899091/**92* Returns the CSS class to be applied to the root element of containers93* rendered using this renderer.94* @return {string} Renderer-specific CSS class.95* @override96*/97goog.ui.MenuRenderer.prototype.getCssClass = function() {98'use strict';99return goog.ui.MenuRenderer.CSS_CLASS;100};101102103/** @override */104goog.ui.MenuRenderer.prototype.initializeDom = function(container) {105'use strict';106goog.ui.MenuRenderer.superClass_.initializeDom.call(this, container);107108var element = container.getElement();109goog.asserts.assert(element, 'The menu DOM element cannot be null.');110goog.a11y.aria.setState(element, goog.a11y.aria.State.HASPOPUP, 'true');111};112113114