Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/ui/buttonrenderer.js
4500 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Default renderer for {@link goog.ui.Button}s.
9
*/
10
11
goog.provide('goog.ui.ButtonRenderer');
12
13
goog.require('goog.a11y.aria');
14
goog.require('goog.a11y.aria.Role');
15
goog.require('goog.a11y.aria.State');
16
goog.require('goog.asserts');
17
goog.require('goog.ui.ButtonSide');
18
goog.require('goog.ui.Component');
19
goog.require('goog.ui.ControlRenderer'); // circular
20
goog.requireType('goog.ui.Button');
21
22
23
24
/**
25
* Default renderer for {@link goog.ui.Button}s. Extends the superclass with
26
* the following button-specific API methods:
27
* <ul>
28
* <li>`getValue` - returns the button element's value
29
* <li>`setValue` - updates the button element to reflect its new value
30
* <li>`getTooltip` - returns the button element's tooltip text
31
* <li>`setTooltip` - updates the button element's tooltip text
32
* <li>`setCollapsed` - removes one or both of the button element's
33
* borders
34
* </ul>
35
* For alternate renderers, see {@link goog.ui.NativeButtonRenderer},
36
* {@link goog.ui.CustomButtonRenderer}, and {@link goog.ui.FlatButtonRenderer}.
37
* @constructor
38
* @extends {goog.ui.ControlRenderer}
39
*/
40
goog.ui.ButtonRenderer = function() {
41
'use strict';
42
goog.ui.ControlRenderer.call(this);
43
};
44
goog.inherits(goog.ui.ButtonRenderer, goog.ui.ControlRenderer);
45
goog.addSingletonGetter(goog.ui.ButtonRenderer);
46
47
48
/**
49
* Default CSS class to be applied to the root element of components rendered
50
* by this renderer.
51
* @type {string}
52
*/
53
goog.ui.ButtonRenderer.CSS_CLASS = goog.getCssName('goog-button');
54
55
56
/**
57
* Returns the ARIA role to be applied to buttons.
58
* @return {goog.a11y.aria.Role|undefined} ARIA role.
59
* @override
60
*/
61
goog.ui.ButtonRenderer.prototype.getAriaRole = function() {
62
'use strict';
63
return goog.a11y.aria.Role.BUTTON;
64
};
65
66
67
/**
68
* Updates the button's ARIA (accessibility) state if the button is being
69
* treated as a checkbox. Also makes sure that attributes which aren't
70
* supported by buttons aren't being added.
71
* @param {Element} element Element whose ARIA state is to be updated.
72
* @param {goog.ui.Component.State} state Component state being enabled or
73
* disabled.
74
* @param {boolean} enable Whether the state is being enabled or disabled.
75
* @protected
76
* @override
77
*/
78
goog.ui.ButtonRenderer.prototype.updateAriaState = function(
79
element, state, enable) {
80
'use strict';
81
switch (state) {
82
// If button has CHECKED or SELECTED state, assign aria-pressed
83
case goog.ui.Component.State.SELECTED:
84
case goog.ui.Component.State.CHECKED:
85
goog.asserts.assert(element, 'The button DOM element cannot be null.');
86
goog.a11y.aria.setState(element, goog.a11y.aria.State.PRESSED, enable);
87
break;
88
default:
89
case goog.ui.Component.State.OPENED:
90
case goog.ui.Component.State.DISABLED:
91
goog.ui.ButtonRenderer.base(
92
this, 'updateAriaState', element, state, enable);
93
break;
94
}
95
};
96
97
98
/**
99
* @override
100
* @suppress {strictMissingProperties} Added to tighten compiler checks
101
*/
102
goog.ui.ButtonRenderer.prototype.createDom = function(button) {
103
'use strict';
104
var element = goog.ui.ButtonRenderer.base(this, 'createDom', button);
105
this.setTooltip(element, button.getTooltip());
106
107
/** @suppress {strictMissingProperties} Added to tighten compiler checks */
108
var value = button.getValue();
109
if (value) {
110
this.setValue(element, value);
111
}
112
113
// If this is a toggle button, set ARIA state
114
if (button.isSupportedState(goog.ui.Component.State.CHECKED)) {
115
this.updateAriaState(
116
element, goog.ui.Component.State.CHECKED, button.isChecked());
117
}
118
119
return element;
120
};
121
122
123
/**
124
* @override
125
* @suppress {strictMissingProperties} Added to tighten compiler checks
126
*/
127
goog.ui.ButtonRenderer.prototype.decorate = function(button, element) {
128
'use strict';
129
// The superclass implementation takes care of common attributes; we only
130
// need to set the value and the tooltip.
131
element =
132
goog.ui.ButtonRenderer.superClass_.decorate.call(this, button, element);
133
134
button.setValueInternal(this.getValue(element));
135
button.setTooltipInternal(this.getTooltip(element));
136
137
// If this is a toggle button, set ARIA state
138
if (button.isSupportedState(goog.ui.Component.State.CHECKED)) {
139
this.updateAriaState(
140
element, goog.ui.Component.State.CHECKED, button.isChecked());
141
}
142
143
return element;
144
};
145
146
147
/**
148
* Takes a button's root element, and returns the value associated with it.
149
* No-op in the base class.
150
* @param {Element} element The button's root element.
151
* @return {string|undefined} The button's value (undefined if none).
152
*/
153
goog.ui.ButtonRenderer.prototype.getValue = function(element) {};
154
155
156
/**
157
* Takes a button's root element and a value, and updates the element to reflect
158
* the new value. No-op in the base class.
159
* @param {Element} element The button's root element.
160
* @param {string} value New value.
161
*/
162
goog.ui.ButtonRenderer.prototype.setValue = function(element, value) {};
163
164
165
/**
166
* Takes a button's root element, and returns its tooltip text.
167
* @param {Element} element The button's root element.
168
* @return {string|undefined} The tooltip text.
169
* @suppress {strictMissingProperties} Added to tighten compiler checks
170
*/
171
goog.ui.ButtonRenderer.prototype.getTooltip = function(element) {
172
'use strict';
173
return element.title;
174
};
175
176
177
/**
178
* Takes a button's root element and a tooltip string, and updates the element
179
* with the new tooltip.
180
* @param {Element} element The button's root element.
181
* @param {string} tooltip New tooltip text.
182
* @protected
183
*/
184
goog.ui.ButtonRenderer.prototype.setTooltip = function(element, tooltip) {
185
'use strict';
186
if (element) {
187
// Don't set a title attribute if there isn't a tooltip. Blank title
188
// attributes can be interpreted incorrectly by screen readers.
189
if (tooltip) {
190
/**
191
* @suppress {strictMissingProperties} Added to tighten compiler checks
192
*/
193
element.title = tooltip;
194
} else {
195
element.removeAttribute('title');
196
}
197
}
198
};
199
200
201
/**
202
* Collapses the border on one or both sides of the button, allowing it to be
203
* combined with the adjacent button(s), forming a single UI componenet with
204
* multiple targets.
205
* @param {goog.ui.Button} button Button to update.
206
* @param {number} sides Bitmap of one or more {@link goog.ui.ButtonSide}s for
207
* which borders should be collapsed.
208
* @protected
209
*/
210
goog.ui.ButtonRenderer.prototype.setCollapsed = function(button, sides) {
211
'use strict';
212
var isRtl = button.isRightToLeft();
213
var collapseLeftClassName =
214
goog.getCssName(this.getStructuralCssClass(), 'collapse-left');
215
var collapseRightClassName =
216
goog.getCssName(this.getStructuralCssClass(), 'collapse-right');
217
218
button.enableClassName(
219
isRtl ? collapseRightClassName : collapseLeftClassName,
220
!!(sides & goog.ui.ButtonSide.START));
221
button.enableClassName(
222
isRtl ? collapseLeftClassName : collapseRightClassName,
223
!!(sides & goog.ui.ButtonSide.END));
224
};
225
226
227
/** @override */
228
goog.ui.ButtonRenderer.prototype.getCssClass = function() {
229
'use strict';
230
return goog.ui.ButtonRenderer.CSS_CLASS;
231
};
232
233