Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/ui/nativebuttonrenderer.js
4501 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Native browser button renderer for {@link goog.ui.Button}s.
9
*/
10
11
goog.provide('goog.ui.NativeButtonRenderer');
12
13
goog.require('goog.asserts');
14
goog.require('goog.dom.InputType');
15
goog.require('goog.dom.TagName');
16
goog.require('goog.dom.classlist');
17
goog.require('goog.events.EventType');
18
goog.require('goog.ui.ButtonRenderer');
19
goog.require('goog.ui.Component');
20
goog.requireType('goog.ui.Control');
21
22
23
24
/**
25
* Renderer for {@link goog.ui.Button}s. Renders and decorates native HTML
26
* button elements. Since native HTML buttons have built-in support for many
27
* features, overrides many expensive (and redundant) superclass methods to
28
* be no-ops.
29
* @constructor
30
* @extends {goog.ui.ButtonRenderer}
31
*/
32
goog.ui.NativeButtonRenderer = function() {
33
'use strict';
34
goog.ui.ButtonRenderer.call(this);
35
};
36
goog.inherits(goog.ui.NativeButtonRenderer, goog.ui.ButtonRenderer);
37
goog.addSingletonGetter(goog.ui.NativeButtonRenderer);
38
39
40
/** @override */
41
goog.ui.NativeButtonRenderer.prototype.getAriaRole = function() {
42
'use strict';
43
// Native buttons don't need ARIA roles to be recognized by screen readers.
44
return undefined;
45
};
46
47
48
/**
49
* Returns the button's contents wrapped in a native HTML button element. Sets
50
* the button's disabled attribute as needed.
51
* @param {goog.ui.Control} button Button to render.
52
* @return {!Element} Root element for the button (a native HTML button
53
* element).
54
* @override
55
* @suppress {strictMissingProperties} Added to tighten compiler checks
56
*/
57
goog.ui.NativeButtonRenderer.prototype.createDom = function(button) {
58
'use strict';
59
this.setUpNativeButton_(button);
60
return button.getDomHelper().createDom(
61
goog.dom.TagName.BUTTON, {
62
'class': this.getClassNames(button).join(' '),
63
'disabled': !button.isEnabled(),
64
'title': button.getTooltip() || '',
65
'value': button.getValue() || ''
66
},
67
button.getCaption() || '');
68
};
69
70
71
/**
72
* Overrides {@link goog.ui.ButtonRenderer#canDecorate} by returning true only
73
* if the element is an HTML button.
74
* @param {Element} element Element to decorate.
75
* @return {boolean} Whether the renderer can decorate the element.
76
* @override
77
* @suppress {strictMissingProperties} Added to tighten compiler checks
78
*/
79
goog.ui.NativeButtonRenderer.prototype.canDecorate = function(element) {
80
'use strict';
81
return element.tagName == goog.dom.TagName.BUTTON ||
82
(element.tagName == goog.dom.TagName.INPUT &&
83
(element.type == goog.dom.InputType.BUTTON ||
84
element.type == goog.dom.InputType.SUBMIT ||
85
element.type == goog.dom.InputType.RESET));
86
};
87
88
89
/**
90
* @override
91
* @suppress {strictMissingProperties} Added to tighten compiler checks
92
*/
93
goog.ui.NativeButtonRenderer.prototype.decorate = function(button, element) {
94
'use strict';
95
this.setUpNativeButton_(button);
96
if (element.disabled) {
97
// Add the marker class for the DISABLED state before letting the superclass
98
// implementation decorate the element, so its state will be correct.
99
var disabledClassName = goog.asserts.assertString(
100
this.getClassForState(goog.ui.Component.State.DISABLED));
101
goog.dom.classlist.add(element, disabledClassName);
102
}
103
return goog.ui.NativeButtonRenderer.superClass_.decorate.call(
104
this, button, element);
105
};
106
107
108
/**
109
* Native buttons natively support BiDi and keyboard focus.
110
* @suppress {visibility} getHandler and performActionInternal
111
* @override
112
*/
113
goog.ui.NativeButtonRenderer.prototype.initializeDom = function(button) {
114
'use strict';
115
// WARNING: This is a hack, and it is only applicable to native buttons,
116
// which are special because they do natively what most goog.ui.Controls
117
// do programmatically. Do not use your renderer's initializeDom method
118
// to hook up event handlers!
119
button.getHandler().listen(
120
button.getElement(), goog.events.EventType.CLICK,
121
button.performActionInternal);
122
};
123
124
125
/**
126
* @override
127
* Native buttons don't support text selection.
128
*/
129
goog.ui.NativeButtonRenderer.prototype.setAllowTextSelection = function() {};
130
131
132
/**
133
* @override
134
* Native buttons natively support right-to-left rendering.
135
*/
136
goog.ui.NativeButtonRenderer.prototype.setRightToLeft = function() {};
137
138
139
/**
140
* @override
141
* Native buttons are always focusable as long as they are enabled.
142
*/
143
goog.ui.NativeButtonRenderer.prototype.isFocusable = function(button) {
144
'use strict';
145
return button.isEnabled();
146
};
147
148
149
/**
150
* @override
151
* Native buttons natively support keyboard focus.
152
*/
153
goog.ui.NativeButtonRenderer.prototype.setFocusable = function() {};
154
155
156
/**
157
* @override
158
* Native buttons also expose the DISABLED state in the HTML button's
159
* `disabled` attribute.
160
*/
161
goog.ui.NativeButtonRenderer.prototype.setState = function(
162
button, state, enable) {
163
'use strict';
164
goog.ui.NativeButtonRenderer.superClass_.setState.call(
165
this, button, state, enable);
166
var element = button.getElement();
167
if (element && state == goog.ui.Component.State.DISABLED) {
168
/** @suppress {strictMissingProperties} Added to tighten compiler checks */
169
element.disabled = enable;
170
}
171
};
172
173
174
/**
175
* @override
176
* Native buttons store their value in the HTML button's `value`
177
* attribute.
178
* @suppress {strictMissingProperties} Added to tighten compiler checks
179
*/
180
goog.ui.NativeButtonRenderer.prototype.getValue = function(element) {
181
'use strict';
182
// TODO(attila): Make this work on IE! This never worked...
183
// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html
184
// for a description of the problem.
185
return element.value;
186
};
187
188
189
/**
190
* @override
191
* Native buttons also expose their value in the HTML button's `value`
192
* attribute.
193
*/
194
goog.ui.NativeButtonRenderer.prototype.setValue = function(element, value) {
195
'use strict';
196
if (element) {
197
// TODO(attila): Make this work on IE! This never worked...
198
// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html
199
// for a description of the problem.
200
/** @suppress {strictMissingProperties} Added to tighten compiler checks */
201
element.value = value;
202
}
203
};
204
205
206
/**
207
* @override
208
* Native buttons don't need ARIA states to support accessibility, so this is
209
* a no-op.
210
*/
211
goog.ui.NativeButtonRenderer.prototype.updateAriaState = function() {};
212
213
214
/**
215
* Sets up the button control such that it doesn't waste time adding
216
* functionality that is already natively supported by native browser
217
* buttons.
218
* @param {goog.ui.Control} button Button control to configure.
219
* @private
220
*/
221
goog.ui.NativeButtonRenderer.prototype.setUpNativeButton_ = function(button) {
222
'use strict';
223
button.setHandleMouseEvents(false);
224
button.setAutoStates(goog.ui.Component.State.ALL, false);
225
button.setSupportedState(goog.ui.Component.State.FOCUSED, false);
226
};
227
228