Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/ui/button.js
4500 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A button control. This implementation extends {@link
9
* goog.ui.Control}.
10
*
11
* @see ../demos/button.html
12
*/
13
14
goog.provide('goog.ui.Button');
15
goog.provide('goog.ui.Button.Side');
16
17
goog.require('goog.events.EventType');
18
goog.require('goog.events.KeyCodes');
19
goog.require('goog.events.KeyHandler');
20
goog.require('goog.ui.ButtonRenderer');
21
goog.require('goog.ui.ButtonSide');
22
goog.require('goog.ui.Component');
23
goog.require('goog.ui.Control');
24
goog.require('goog.ui.NativeButtonRenderer');
25
goog.require('goog.ui.registry');
26
goog.requireType('goog.dom.DomHelper');
27
goog.requireType('goog.events.KeyEvent');
28
goog.requireType('goog.ui.ControlContent');
29
30
31
32
/**
33
* A button control, rendered as a native browser button by default.
34
*
35
* @param {goog.ui.ControlContent=} opt_content Text caption or existing DOM
36
* structure to display as the button's caption (if any).
37
* @param {goog.ui.ButtonRenderer=} opt_renderer Renderer used to render or
38
* decorate the button; defaults to {@link goog.ui.NativeButtonRenderer}.
39
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
40
* document interaction.
41
* @constructor
42
* @extends {goog.ui.Control}
43
*/
44
goog.ui.Button = function(opt_content, opt_renderer, opt_domHelper) {
45
'use strict';
46
goog.ui.Control.call(
47
this, opt_content,
48
opt_renderer || goog.ui.NativeButtonRenderer.getInstance(),
49
opt_domHelper);
50
};
51
goog.inherits(goog.ui.Button, goog.ui.Control);
52
53
54
/**
55
* Constants for button sides, see {@link goog.ui.Button.prototype.setCollapsed}
56
* for details. Aliased from goog.ui.ButtonSide to support legacy users without
57
* creating a circular dependency in {@link goog.ui.ButtonRenderer}.
58
* @enum {number}
59
* @deprecated use {@link goog.ui.ButtonSide} instead.
60
*/
61
goog.ui.Button.Side = goog.ui.ButtonSide;
62
63
64
/**
65
* Value associated with the button.
66
* @type {*}
67
* @private
68
*/
69
goog.ui.Button.prototype.value_;
70
71
72
/**
73
* Tooltip text for the button, displayed on hover.
74
* @type {string|undefined}
75
* @private
76
*/
77
goog.ui.Button.prototype.tooltip_;
78
79
80
// goog.ui.Button API implementation.
81
82
83
/**
84
* Returns the value associated with the button.
85
* @return {*} Button value (undefined if none).
86
*/
87
goog.ui.Button.prototype.getValue = function() {
88
'use strict';
89
return this.value_;
90
};
91
92
93
/**
94
* Sets the value associated with the button, and updates its DOM.
95
* @param {*} value New button value.
96
*/
97
goog.ui.Button.prototype.setValue = function(value) {
98
'use strict';
99
this.value_ = value;
100
var renderer = /** @type {!goog.ui.ButtonRenderer} */ (this.getRenderer());
101
renderer.setValue(this.getElement(), /** @type {string} */ (value));
102
};
103
104
105
/**
106
* Sets the value associated with the button. Unlike {@link #setValue},
107
* doesn't update the button's DOM. Considered protected; to be called only
108
* by renderer code during element decoration.
109
* @param {*} value New button value.
110
* @protected
111
*/
112
goog.ui.Button.prototype.setValueInternal = function(value) {
113
'use strict';
114
this.value_ = value;
115
};
116
117
118
/**
119
* Returns the tooltip for the button.
120
* @return {string|undefined} Tooltip text (undefined if none).
121
*/
122
goog.ui.Button.prototype.getTooltip = function() {
123
'use strict';
124
return this.tooltip_;
125
};
126
127
128
/**
129
* Sets the tooltip for the button, and updates its DOM.
130
* @param {string} tooltip New tooltip text.
131
* @suppress {strictMissingProperties} Added to tighten compiler checks
132
*/
133
goog.ui.Button.prototype.setTooltip = function(tooltip) {
134
'use strict';
135
this.tooltip_ = tooltip;
136
this.getRenderer().setTooltip(this.getElement(), tooltip);
137
};
138
139
140
/**
141
* Sets the tooltip for the button. Unlike {@link #setTooltip}, doesn't update
142
* the button's DOM. Considered protected; to be called only by renderer code
143
* during element decoration.
144
* @param {string} tooltip New tooltip text.
145
* @protected
146
*/
147
goog.ui.Button.prototype.setTooltipInternal = function(tooltip) {
148
'use strict';
149
this.tooltip_ = tooltip;
150
};
151
152
153
/**
154
* Collapses the border on one or both sides of the button, allowing it to be
155
* combined with the adjancent button(s), forming a single UI componenet with
156
* multiple targets.
157
* @param {number} sides Bitmap of one or more {@link goog.ui.ButtonSide}s for
158
* which borders should be collapsed.
159
* @suppress {strictMissingProperties} Added to tighten compiler checks
160
*/
161
goog.ui.Button.prototype.setCollapsed = function(sides) {
162
'use strict';
163
this.getRenderer().setCollapsed(this, sides);
164
};
165
166
167
// goog.ui.Control & goog.ui.Component API implementation.
168
169
170
/** @override */
171
goog.ui.Button.prototype.disposeInternal = function() {
172
'use strict';
173
goog.ui.Button.superClass_.disposeInternal.call(this);
174
delete this.value_;
175
delete this.tooltip_;
176
};
177
178
179
/** @override */
180
goog.ui.Button.prototype.enterDocument = function() {
181
'use strict';
182
goog.ui.Button.superClass_.enterDocument.call(this);
183
if (this.isSupportedState(goog.ui.Component.State.FOCUSED)) {
184
var keyTarget = this.getKeyEventTarget();
185
if (keyTarget) {
186
this.getHandler().listen(
187
keyTarget, goog.events.EventType.KEYUP, this.handleKeyEventInternal);
188
}
189
}
190
};
191
192
193
/**
194
* Attempts to handle a keyboard event; returns true if the event was handled,
195
* false otherwise. If the button is enabled and the Enter/Space key was
196
* pressed, handles the event by dispatching an `ACTION` event,
197
* and returns true. Overrides {@link goog.ui.Control#handleKeyEventInternal}.
198
* @param {goog.events.KeyEvent} e Key event to handle.
199
* @return {boolean} Whether the key event was handled.
200
* @protected
201
* @override
202
*/
203
goog.ui.Button.prototype.handleKeyEventInternal = function(e) {
204
'use strict';
205
if (e.keyCode == goog.events.KeyCodes.ENTER &&
206
e.type == goog.events.KeyHandler.EventType.KEY ||
207
e.keyCode == goog.events.KeyCodes.SPACE &&
208
e.type == goog.events.EventType.KEYUP) {
209
return this.performActionInternal(e);
210
}
211
// Return true for space keypress (even though the event is handled on keyup)
212
// as preventDefault needs to be called up keypress to take effect in IE and
213
// WebKit.
214
return e.keyCode == goog.events.KeyCodes.SPACE;
215
};
216
217
218
// Register a decorator factory function for goog.ui.Buttons.
219
goog.ui.registry.setDecoratorByClassName(
220
goog.ui.ButtonRenderer.CSS_CLASS, function() {
221
'use strict';
222
return new goog.ui.Button(null);
223
});
224
225