Debian Codegraph @

closure-library/closure/goog/ui/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.MenuButton}s and subclasses.
17 *
18 * @author [email protected] (Attila Bodis)
19 */
20
21 goog.provide('goog.ui.MenuButtonRenderer');
22
23 goog.require('goog.dom');
24 goog.require('goog.dom.TagName');
25 goog.require('goog.style');
26 goog.require('goog.ui.CustomButtonRenderer');
27 goog.require('goog.ui.INLINE_BLOCK_CLASSNAME');
28 goog.require('goog.ui.Menu');
29 goog.require('goog.ui.MenuRenderer');
30
31
32
33 /**
34 * Renderer for {@link goog.ui.MenuButton}s. This implementation overrides
35 * {@link goog.ui.CustomButtonRenderer#createButton} to create a separate
36 * caption and dropdown element.
37 * @constructor
38 * @extends {goog.ui.CustomButtonRenderer}
39 */
40 goog.ui.MenuButtonRenderer = function() {
41 goog.ui.CustomButtonRenderer.call(this);
42 };
43 goog.inherits(goog.ui.MenuButtonRenderer, goog.ui.CustomButtonRenderer);
44 goog.addSingletonGetter(goog.ui.MenuButtonRenderer);
45
46
47 /**
48 * Default CSS class to be applied to the root element of components rendered
49 * by this renderer.
50 * @type {string}
51 */
52 goog.ui.MenuButtonRenderer.CSS_CLASS = goog.getCssName('goog-menu-button');
53
54
55 /**
56 * Takes the button's root element and returns the parent element of the
57 * button's contents. Overrides the superclass implementation by taking
58 * the nested DIV structure of menu buttons into account.
59 * @param {Element} element Root element of the button whose content element
60 * is to be returned.
61 * @return {Element} The button's content element.
62 * @override
63 */
64 goog.ui.MenuButtonRenderer.prototype.getContentElement = function(element) {
65 return goog.ui.MenuButtonRenderer.superClass_.getContentElement.call(
66 this,
67 /** @type {Element} */ (element && element.firstChild));
68 };
69
70
71 /**
72 * Takes an element, decorates it with the menu button control, and returns
73 * the element. Overrides {@link goog.ui.CustomButtonRenderer#decorate} by
74 * looking for a child element that can be decorated by a menu, and if it
75 * finds one, decorates it and attaches it to the menu button.
76 * @param {goog.ui.Control} control goog.ui.MenuButton to decorate the element.
77 * @param {Element} element Element to decorate.
78 * @return {Element} Decorated element.
79 * @override
80 */
81 goog.ui.MenuButtonRenderer.prototype.decorate = function(control, element) {
82 var button = /** @type {goog.ui.MenuButton} */ (control);
83 // TODO(attila): Add more robust support for subclasses of goog.ui.Menu.
84 var menuElem = goog.dom.getElementsByTagNameAndClass(
85 '*', goog.ui.MenuRenderer.CSS_CLASS, element)[0];
86 if (menuElem) {
87 // Move the menu element directly under the body (but hide it first to
88 // prevent flicker; see bug 1089244).
89 goog.style.setElementShown(menuElem, false);
90 goog.dom.appendChild(goog.dom.getOwnerDocument(menuElem).body, menuElem);
91
92 // Decorate the menu and attach it to the button.
93 var menu = new goog.ui.Menu();
94 menu.decorate(menuElem);
95 button.setMenu(menu);
96 }
97
98 // Let the superclass do the rest.
99 return goog.ui.MenuButtonRenderer.superClass_.decorate.call(
100 this, button, element);
101 };
102
103
104 /**
105 * Takes a text caption or existing DOM structure, and returns the content and
106 * a dropdown arrow element wrapped in a pseudo-rounded-corner box. Creates
107 * the following DOM structure:
108 *
109 * <div class="goog-inline-block goog-menu-button-outer-box">
110 * <div class="goog-inline-block goog-menu-button-inner-box">
111 * <div class="goog-inline-block goog-menu-button-caption">
112 * Contents...
113 * </div>
114 * <div class="goog-inline-block goog-menu-button-dropdown">
115 * &nbsp;
116 * </div>
117 * </div>
118 * </div>
119 *
120 * @param {goog.ui.ControlContent} content Text caption or DOM structure
121 * to wrap in a box.
122 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
123 * @return {Element} Pseudo-rounded-corner box containing the content.
124 * @override
125 */
126 goog.ui.MenuButtonRenderer.prototype.createButton = function(content, dom) {
127 return goog.ui.MenuButtonRenderer.superClass_.createButton.call(
128 this, [this.createCaption(content, dom), this.createDropdown(dom)], dom);
129 };
130
131
132 /**
133 * Takes a text caption or existing DOM structure, and returns it wrapped in
134 * an appropriately-styled DIV. Creates the following DOM structure:
135 *
136 * <div class="goog-inline-block goog-menu-button-caption">
137 * Contents...
138 * </div>
139 *
140 * @param {goog.ui.ControlContent} content Text caption or DOM structure
141 * to wrap in a box.
142 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
143 * @return {Element} Caption element.
144 */
145 goog.ui.MenuButtonRenderer.prototype.createCaption = function(content, dom) {
146 return goog.ui.MenuButtonRenderer.wrapCaption(
147 content, this.getCssClass(), dom);
148 };
149
150
151 /**
152 * Takes a text caption or existing DOM structure, and returns it wrapped in
153 * an appropriately-styled DIV. Creates the following DOM structure:
154 *
155 * <div class="goog-inline-block goog-menu-button-caption">
156 * Contents...
157 * </div>
158 *
159 * @param {goog.ui.ControlContent} content Text caption or DOM structure
160 * to wrap in a box.
161 * @param {string} cssClass The CSS class for the renderer.
162 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
163 * @return {!Element} Caption element.
164 */
165 goog.ui.MenuButtonRenderer.wrapCaption = function(content, cssClass, dom) {
166 return dom.createDom(
167 goog.dom.TagName.DIV, goog.ui.INLINE_BLOCK_CLASSNAME + ' ' +
168 goog.getCssName(cssClass, 'caption'),
169 content);
170 };
171
172
173 /**
174 * Returns an appropriately-styled DIV containing a dropdown arrow element.
175 * Creates the following DOM structure:
176 *
177 * <div class="goog-inline-block goog-menu-button-dropdown">
178 * &nbsp;
179 * </div>
180 *
181 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
182 * @return {Element} Dropdown element.
183 */
184 goog.ui.MenuButtonRenderer.prototype.createDropdown = function(dom) {
185 // 00A0 is &nbsp;
186 return dom.createDom(
187 goog.dom.TagName.DIV, goog.ui.INLINE_BLOCK_CLASSNAME + ' ' +
188 goog.getCssName(this.getCssClass(), 'dropdown'),
189 '\u00A0');
190 };
191
192
193 /**
194 * Returns the CSS class to be applied to the root element of components
195 * rendered using this renderer.
196 * @return {string} Renderer-specific CSS class.
197 * @override
198 */
199 goog.ui.MenuButtonRenderer.prototype.getCssClass = function() {
200 return goog.ui.MenuButtonRenderer.CSS_CLASS;
201 };