Debian Codegraph @

closure-library/closure/goog/ui/style/app/menubuttonrenderer.js

raw
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.style.app.MenuButton}s and
17 * subclasses.
18 *
19 * @author [email protected] (Attila Bodis)
20 */
21
22 goog.provide('goog.ui.style.app.MenuButtonRenderer');
23
24 goog.require('goog.a11y.aria.Role');
25 goog.require('goog.array');
26 goog.require('goog.dom');
27 goog.require('goog.dom.TagName');
28 goog.require('goog.style');
29 goog.require('goog.ui.Menu');
30 goog.require('goog.ui.MenuRenderer');
31 goog.require('goog.ui.style.app.ButtonRenderer');
32
33
34
35 /**
36 * Renderer for {@link goog.ui.style.app.MenuButton}s. This implementation
37 * overrides {@link goog.ui.style.app.ButtonRenderer#createButton} to insert a
38 * dropdown element into the content element after the specified content.
39 * @constructor
40 * @extends {goog.ui.style.app.ButtonRenderer}
41 * @final
42 */
43 goog.ui.style.app.MenuButtonRenderer = function() {
44 goog.ui.style.app.ButtonRenderer.call(this);
45 };
46 goog.inherits(
47 goog.ui.style.app.MenuButtonRenderer, goog.ui.style.app.ButtonRenderer);
48 goog.addSingletonGetter(goog.ui.style.app.MenuButtonRenderer);
49
50
51 /**
52 * Default CSS class to be applied to the root element of components rendered
53 * by this renderer.
54 * @type {string}
55 */
56 goog.ui.style.app.MenuButtonRenderer.CSS_CLASS =
57 goog.getCssName('goog-menu-button');
58
59
60 /**
61 * Array of arrays of CSS classes that we want composite classes added and
62 * removed for in IE6 and lower as a workaround for lack of multi-class CSS
63 * selector support.
64 * @type {Array<Array<string>>}
65 */
66 goog.ui.style.app.MenuButtonRenderer.IE6_CLASS_COMBINATIONS = [
67 [
68 goog.getCssName('goog-button-base-rtl'), goog.getCssName('goog-menu-button')
69 ],
70
71 [
72 goog.getCssName('goog-button-base-hover'),
73 goog.getCssName('goog-menu-button')
74 ],
75
76 [
77 goog.getCssName('goog-button-base-focused'),
78 goog.getCssName('goog-menu-button')
79 ],
80
81 [
82 goog.getCssName('goog-button-base-disabled'),
83 goog.getCssName('goog-menu-button')
84 ],
85
86 [
87 goog.getCssName('goog-button-base-active'),
88 goog.getCssName('goog-menu-button')
89 ],
90
91 [
92 goog.getCssName('goog-button-base-open'),
93 goog.getCssName('goog-menu-button')
94 ],
95
96 [
97 goog.getCssName('goog-button-base-active'),
98 goog.getCssName('goog-button-base-open'),
99 goog.getCssName('goog-menu-button')
100 ]
101 ];
102
103
104 /**
105 * Returns the ARIA role to be applied to menu buttons, which
106 * have a menu attached to them.
107 * @return {goog.a11y.aria.Role} ARIA role.
108 * @override
109 */
110 goog.ui.style.app.MenuButtonRenderer.prototype.getAriaRole = function() {
111 // If we apply the 'button' ARIA role to the menu button, the
112 // screen reader keeps referring to menus as buttons, which
113 // might be misleading for the users. Hence the ARIA role
114 // 'menu' is assigned.
115 return goog.a11y.aria.Role.MENU;
116 };
117
118
119 /**
120 * Takes the button's root element and returns the parent element of the
121 * button's contents. Overrides the superclass implementation by taking
122 * the nested DIV structure of menu buttons into account.
123 * @param {Element} element Root element of the button whose content element
124 * is to be returned.
125 * @return {Element} The button's content element.
126 * @override
127 */
128 goog.ui.style.app.MenuButtonRenderer.prototype.getContentElement = function(
129 element) {
130 return goog.ui.style.app.MenuButtonRenderer.superClass_.getContentElement
131 .call(this, element);
132 };
133
134
135 /**
136 * Takes an element, decorates it with the menu button control, and returns
137 * the element. Overrides {@link goog.ui.style.app.ButtonRenderer#decorate} by
138 * looking for a child element that can be decorated by a menu, and if it
139 * finds one, decorates it and attaches it to the menu button.
140 * @param {goog.ui.Control} control goog.ui.MenuButton to decorate the element.
141 * @param {Element} element Element to decorate.
142 * @return {Element} Decorated element.
143 * @override
144 */
145 goog.ui.style.app.MenuButtonRenderer.prototype.decorate = function(
146 control, element) {
147 var button = /** @type {goog.ui.MenuButton} */ (control);
148 // TODO(attila): Add more robust support for subclasses of goog.ui.Menu.
149 var menuElem = goog.dom.getElementsByTagNameAndClass(
150 '*', goog.ui.MenuRenderer.CSS_CLASS, element)[0];
151 if (menuElem) {
152 // Move the menu element directly under the body (but hide it first to
153 // prevent flicker; see bug 1089244).
154 goog.style.setElementShown(menuElem, false);
155 goog.dom.appendChild(goog.dom.getOwnerDocument(menuElem).body, menuElem);
156
157 // Decorate the menu and attach it to the button.
158 var menu = new goog.ui.Menu();
159 menu.decorate(menuElem);
160 button.setMenu(menu);
161 }
162
163 // Let the superclass do the rest.
164 return goog.ui.style.app.MenuButtonRenderer.superClass_.decorate.call(
165 this, button, element);
166 };
167
168
169 /**
170 * Takes a text caption or existing DOM structure, and returns the content and
171 * a dropdown arrow element wrapped in a pseudo-rounded-corner box. Creates
172 * the following DOM structure:
173 *
174 * <div class="goog-inline-block goog-button-outer-box">
175 * <div class="goog-inline-block goog-button-inner-box">
176 * <div class="goog-button-pos">
177 * <div class="goog-button-top-shadow">&nbsp;</div>
178 * <div class="goog-button-content">
179 * Contents...
180 * <div class="goog-menu-button-dropdown"> </div>
181 * </div>
182 * </div>
183 * </div>
184 * </div>
185 *
186 * @param {goog.ui.ControlContent} content Text caption or DOM structure to wrap
187 * in a box.
188 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
189 * @return {Element} Pseudo-rounded-corner box containing the content.
190 * @override
191 */
192 goog.ui.style.app.MenuButtonRenderer.prototype.createButton = function(
193 content, dom) {
194 var contentWithDropdown = this.createContentWithDropdown(content, dom);
195 return goog.ui.style.app.MenuButtonRenderer.superClass_.createButton.call(
196 this, contentWithDropdown, dom);
197 };
198
199
200 /** @override */
201 goog.ui.style.app.MenuButtonRenderer.prototype.setContent = function(
202 element, content) {
203 var dom = goog.dom.getDomHelper(this.getContentElement(element));
204 goog.ui.style.app.MenuButtonRenderer.superClass_.setContent.call(
205 this, element, this.createContentWithDropdown(content, dom));
206 };
207
208
209 /**
210 * Inserts dropdown element as last child of existing content.
211 * @param {goog.ui.ControlContent} content Text caption or DOM structure.
212 * @param {goog.dom.DomHelper} dom DOM helper, used for document ineraction.
213 * @return {Array<Node>} DOM structure to be set as the button's content.
214 */
215 goog.ui.style.app.MenuButtonRenderer.prototype.createContentWithDropdown =
216 function(content, dom) {
217 var caption = dom.createDom(
218 goog.dom.TagName.DIV, null, content, this.createDropdown(dom));
219 return goog.array.toArray(caption.childNodes);
220 };
221
222
223 /**
224 * Returns an appropriately-styled DIV containing a dropdown arrow.
225 * Creates the following DOM structure:
226 *
227 * <div class="goog-menu-button-dropdown"> </div>
228 *
229 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
230 * @return {Element} Dropdown element.
231 */
232 goog.ui.style.app.MenuButtonRenderer.prototype.createDropdown = function(dom) {
233 return dom.createDom(
234 goog.dom.TagName.DIV, goog.getCssName(this.getCssClass(), 'dropdown'));
235 };
236
237
238 /**
239 * Returns the CSS class to be applied to the root element of components
240 * rendered using this renderer.
241 * @return {string} Renderer-specific CSS class.
242 * @override
243 */
244 goog.ui.style.app.MenuButtonRenderer.prototype.getCssClass = function() {
245 return goog.ui.style.app.MenuButtonRenderer.CSS_CLASS;
246 };
247
248
249 /** @override */
250 goog.ui.style.app.MenuButtonRenderer.prototype.getIe6ClassCombinations =
251 function() {
252 return goog.ui.style.app.MenuButtonRenderer.IE6_CLASS_COMBINATIONS;
253 };