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