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