Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/ui/menubutton.js
4523 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A menu button control.
9
*
10
* @see ../demos/menubutton.html
11
*/
12
13
goog.provide('goog.ui.MenuButton');
14
15
goog.require('goog.Timer');
16
goog.require('goog.a11y.aria');
17
goog.require('goog.a11y.aria.State');
18
goog.require('goog.asserts');
19
goog.require('goog.dom');
20
goog.require('goog.events.EventType');
21
goog.require('goog.events.KeyCodes');
22
goog.require('goog.events.KeyHandler');
23
goog.require('goog.math.Box');
24
goog.require('goog.math.Coordinate');
25
goog.require('goog.math.Rect');
26
goog.require('goog.positioning');
27
goog.require('goog.positioning.Corner');
28
goog.require('goog.positioning.MenuAnchoredPosition');
29
goog.require('goog.positioning.Overflow');
30
goog.require('goog.style');
31
goog.require('goog.ui.Button');
32
goog.require('goog.ui.Component');
33
goog.require('goog.ui.IdGenerator');
34
goog.require('goog.ui.Menu');
35
goog.require('goog.ui.MenuButtonRenderer');
36
goog.require('goog.ui.MenuItem');
37
goog.require('goog.ui.MenuRenderer');
38
goog.require('goog.ui.SubMenu');
39
goog.require('goog.ui.registry');
40
goog.requireType('goog.events.BrowserEvent');
41
goog.requireType('goog.events.Event');
42
goog.requireType('goog.events.EventTarget');
43
goog.requireType('goog.math.Size');
44
goog.requireType('goog.positioning.AnchoredPosition');
45
goog.requireType('goog.ui.ButtonRenderer');
46
goog.requireType('goog.ui.Control');
47
goog.requireType('goog.ui.ControlContent');
48
goog.requireType('goog.ui.MenuSeparator');
49
50
51
52
/**
53
* A menu button control. Extends {@link goog.ui.Button} by composing a button
54
* with a dropdown arrow and a popup menu.
55
*
56
* @param {goog.ui.ControlContent=} opt_content Text caption or existing DOM
57
* structure to display as the button's caption (if any).
58
* @param {goog.ui.Menu=} opt_menu Menu to render under the button when clicked.
59
* @param {goog.ui.ButtonRenderer=} opt_renderer Renderer used to render or
60
* decorate the menu button; defaults to {@link goog.ui.MenuButtonRenderer}.
61
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
62
* document interaction.
63
* @param {!goog.ui.MenuRenderer=} opt_menuRenderer Renderer used to render or
64
* decorate the menu; defaults to {@link goog.ui.MenuRenderer}.
65
* @constructor
66
* @extends {goog.ui.Button}
67
*/
68
goog.ui.MenuButton = function(
69
opt_content, opt_menu, opt_renderer, opt_domHelper, opt_menuRenderer) {
70
'use strict';
71
goog.ui.Button.call(
72
this, opt_content,
73
opt_renderer || goog.ui.MenuButtonRenderer.getInstance(), opt_domHelper);
74
75
// Menu buttons support the OPENED state.
76
this.setSupportedState(goog.ui.Component.State.OPENED, true);
77
78
/**
79
* The menu position on this button.
80
* @type {!goog.positioning.AnchoredPosition}
81
* @private
82
*/
83
this.menuPosition_ = new goog.positioning.MenuAnchoredPosition(
84
null, goog.positioning.Corner.BOTTOM_START);
85
86
if (opt_menu) {
87
this.setMenu(opt_menu);
88
}
89
this.menuMargin_ = null;
90
this.timer_ = new goog.Timer(500); // 0.5 sec
91
92
/**
93
* Whether the enter or space key should close the menu, if it is already
94
* open. This should be true for accessibility reasons, but is provided as an
95
* option for backward compatibility.
96
* @private {boolean}
97
*/
98
this.closeOnEnterOrSpace_ = true;
99
100
/** @private {!goog.ui.MenuRenderer} */
101
this.menuRenderer_ = opt_menuRenderer || goog.ui.MenuRenderer.getInstance();
102
};
103
goog.inherits(goog.ui.MenuButton, goog.ui.Button);
104
105
106
/**
107
* The menu.
108
* @type {goog.ui.Menu|undefined}
109
* @private
110
*/
111
goog.ui.MenuButton.prototype.menu_;
112
113
114
/**
115
* The position element. If set, use positionElement_ to position the
116
* popup menu instead of the default which is to use the menu button element.
117
* @type {Element|undefined}
118
* @private
119
*/
120
goog.ui.MenuButton.prototype.positionElement_;
121
122
123
/**
124
* The margin to apply to the menu's position when it is shown. If null, no
125
* margin will be applied.
126
* @type {goog.math.Box}
127
* @private
128
*/
129
goog.ui.MenuButton.prototype.menuMargin_;
130
131
132
/**
133
* Whether the attached popup menu is focusable or not (defaults to false).
134
* Popup menus attached to menu buttons usually don't need to be focusable,
135
* i.e. the button retains keyboard focus, and forwards key events to the
136
* menu for processing. However, menus like {@link goog.ui.FilteredMenu}
137
* need to be focusable.
138
* @type {boolean}
139
* @private
140
*/
141
goog.ui.MenuButton.prototype.isFocusablePopupMenu_ = false;
142
143
144
/**
145
* A Timer to correct menu position.
146
* @type {goog.Timer}
147
* @private
148
*/
149
goog.ui.MenuButton.prototype.timer_;
150
151
152
/**
153
* The bounding rectangle of the button element.
154
* @type {goog.math.Rect}
155
* @private
156
*/
157
goog.ui.MenuButton.prototype.buttonRect_;
158
159
160
/**
161
* The viewport rectangle.
162
* @type {goog.math.Box}
163
* @private
164
*/
165
goog.ui.MenuButton.prototype.viewportBox_;
166
167
168
/**
169
* The original size.
170
* @type {goog.math.Size|undefined}
171
* @private
172
*/
173
goog.ui.MenuButton.prototype.originalSize_;
174
175
176
/**
177
* Do we render the drop down menu as a sibling to the label, or at the end
178
* of the current dom?
179
* @type {boolean}
180
* @private
181
*/
182
goog.ui.MenuButton.prototype.renderMenuAsSibling_ = false;
183
184
185
/**
186
* Whether to select the first item in the menu when it is opened using
187
* enter or space. By default, the first item is selected only when
188
* opened by a key up or down event. When this is on, the first item will
189
* be selected due to any of the four events.
190
* @private
191
*/
192
goog.ui.MenuButton.prototype.selectFirstOnEnterOrSpace_ = false;
193
194
195
/**
196
* Sets up event handlers specific to menu buttons.
197
* @override
198
*/
199
goog.ui.MenuButton.prototype.enterDocument = function() {
200
'use strict';
201
goog.ui.MenuButton.superClass_.enterDocument.call(this);
202
this.attachKeyDownEventListener_(true);
203
if (this.menu_) {
204
this.attachMenuEventListeners_(this.menu_, true);
205
}
206
goog.a11y.aria.setState(
207
this.getElementStrict(), goog.a11y.aria.State.HASPOPUP, !!this.menu_);
208
};
209
210
211
/**
212
* Removes event handlers specific to menu buttons, and ensures that the
213
* attached menu also exits the document.
214
* @override
215
*/
216
goog.ui.MenuButton.prototype.exitDocument = function() {
217
'use strict';
218
goog.ui.MenuButton.superClass_.exitDocument.call(this);
219
this.attachKeyDownEventListener_(false);
220
if (this.menu_) {
221
this.setOpen(false);
222
this.menu_.exitDocument();
223
this.attachMenuEventListeners_(this.menu_, false);
224
225
var menuElement = this.menu_.getElement();
226
if (menuElement) {
227
goog.dom.removeNode(menuElement);
228
}
229
}
230
};
231
232
233
/** @override */
234
goog.ui.MenuButton.prototype.disposeInternal = function() {
235
'use strict';
236
goog.ui.MenuButton.superClass_.disposeInternal.call(this);
237
if (this.menu_) {
238
this.menu_.dispose();
239
delete this.menu_;
240
}
241
delete this.positionElement_;
242
this.timer_.dispose();
243
};
244
245
246
/**
247
* Handles mousedown events. Invokes the superclass implementation to dispatch
248
* an ACTIVATE event and activate the button. Also toggles the visibility of
249
* the attached menu.
250
* @param {goog.events.Event} e Mouse event to handle.
251
* @override
252
* @protected
253
*/
254
goog.ui.MenuButton.prototype.handleMouseDown = function(e) {
255
'use strict';
256
goog.ui.MenuButton.superClass_.handleMouseDown.call(this, e);
257
if (this.isActive()) {
258
// The component was allowed to activate; toggle menu visibility.
259
this.setOpen(!this.isOpen(), e);
260
if (this.menu_) {
261
this.menu_.setMouseButtonPressed(this.isOpen());
262
}
263
}
264
};
265
266
267
/**
268
* Handles mouseup events. Invokes the superclass implementation to dispatch
269
* an ACTION event and deactivate the button.
270
* @param {goog.events.Event} e Mouse event to handle.
271
* @override
272
* @protected
273
*/
274
goog.ui.MenuButton.prototype.handleMouseUp = function(e) {
275
'use strict';
276
goog.ui.MenuButton.superClass_.handleMouseUp.call(this, e);
277
if (this.menu_ && !this.isActive()) {
278
this.menu_.setMouseButtonPressed(false);
279
}
280
};
281
282
283
/**
284
* Performs the appropriate action when the menu button is activated by the
285
* user. Overrides the superclass implementation by not dispatching an
286
* `ACTION` event, because menu buttons exist only to reveal menus, not to
287
* perform actions themselves. Calls {@link #setActive} to deactivate the
288
* button.
289
* @param {goog.events.Event} e Mouse or key event that triggered the action.
290
* @return {boolean} Whether the action was allowed to proceed.
291
* @override
292
* @protected
293
*/
294
goog.ui.MenuButton.prototype.performActionInternal = function(e) {
295
'use strict';
296
this.setActive(false);
297
return true;
298
};
299
300
301
/**
302
* Handles mousedown events over the document. If the mousedown happens over
303
* an element unrelated to the component, hides the menu.
304
* TODO(attila): Reconcile this with goog.ui.Popup (and handle frames/windows).
305
* @param {goog.events.BrowserEvent} e Mouse event to handle.
306
* @protected
307
*/
308
goog.ui.MenuButton.prototype.handleDocumentMouseDown = function(e) {
309
'use strict';
310
if (this.menu_ && this.menu_.isVisible() &&
311
!this.containsElement(/** @type {Element} */ (e.target))) {
312
// User clicked somewhere else in the document while the menu was visible;
313
// dismiss menu.
314
this.setOpen(false);
315
}
316
};
317
318
319
/**
320
* Returns true if the given element is to be considered part of the component,
321
* even if it isn't a DOM descendant of the component's root element.
322
* @param {Element} element Element to test (if any).
323
* @return {boolean} Whether the element is considered part of the component.
324
* @protected
325
*/
326
goog.ui.MenuButton.prototype.containsElement = function(element) {
327
'use strict';
328
return element && goog.dom.contains(this.getElement(), element) ||
329
this.menu_ && this.menu_.containsElement(element) || false;
330
};
331
332
333
/** @override */
334
goog.ui.MenuButton.prototype.handleKeyEventInternal = function(e) {
335
'use strict';
336
// Handle SPACE on keyup and all other keys on keypress.
337
if (e.keyCode == goog.events.KeyCodes.SPACE) {
338
// Prevent page scrolling in Chrome.
339
e.preventDefault();
340
if (e.type != goog.events.EventType.KEYUP) {
341
// Ignore events because KeyCodes.SPACE is handled further down.
342
return true;
343
}
344
} else if (e.type != goog.events.KeyHandler.EventType.KEY) {
345
return false;
346
}
347
348
if (this.menu_ && this.menu_.isVisible()) {
349
// Menu is open.
350
const isEnterOrSpace = e.keyCode == goog.events.KeyCodes.ENTER ||
351
e.keyCode == goog.events.KeyCodes.SPACE;
352
const handledByMenu = this.menu_.handleKeyEvent(e);
353
// If the submenu has handled the key event, then defer to it to close the
354
// menu if necessary and do not close it here. This is needed because the
355
// enter key should keep the submenu open, but should close other types of
356
// menu items.
357
// Check for this.menu_ again here because some widgets set this.dispose
358
// after handleKeyEvent. Example: go/widget-dispose-ex
359
const handledBySubMenu = handledByMenu && this.menu_ &&
360
this.menu_.getOpenItem() instanceof goog.ui.SubMenu;
361
if (!handledBySubMenu &&
362
(e.keyCode == goog.events.KeyCodes.ESC ||
363
(isEnterOrSpace && this.closeOnEnterOrSpace_))) {
364
// Dismiss the menu.
365
this.setOpen(false);
366
return true;
367
}
368
return handledByMenu;
369
}
370
371
if (e.keyCode == goog.events.KeyCodes.DOWN ||
372
e.keyCode == goog.events.KeyCodes.UP ||
373
e.keyCode == goog.events.KeyCodes.SPACE ||
374
e.keyCode == goog.events.KeyCodes.ENTER) {
375
// Menu is closed, and the user hit the down/up/space/enter key; open menu.
376
this.setOpen(true, e);
377
return true;
378
}
379
380
// Key event wasn't handled by the component.
381
return false;
382
};
383
384
385
/**
386
* Handles `ACTION` events dispatched by an activated menu item.
387
* @param {goog.events.Event} e Action event to handle.
388
* @protected
389
*/
390
goog.ui.MenuButton.prototype.handleMenuAction = function(e) {
391
'use strict';
392
// Close the menu on click.
393
this.setOpen(false);
394
};
395
396
397
/**
398
* Handles `BLUR` events dispatched by the popup menu by closing it.
399
* Only registered if the menu is focusable.
400
* @param {goog.events.Event} e Blur event dispatched by a focusable menu.
401
*/
402
goog.ui.MenuButton.prototype.handleMenuBlur = function(e) {
403
'use strict';
404
// Close the menu when it reports that it lost focus, unless the button is
405
// pressed (active).
406
if (!this.isActive()) {
407
this.setOpen(false);
408
}
409
};
410
411
412
/**
413
* Handles blur events dispatched by the button's key event target when it
414
* loses keyboard focus by closing the popup menu (unless it is focusable).
415
* Only registered if the button is focusable.
416
* @param {goog.events.Event} e Blur event dispatched by the menu button.
417
* @override
418
* @protected
419
*/
420
goog.ui.MenuButton.prototype.handleBlur = function(e) {
421
'use strict';
422
if (!this.isFocusablePopupMenu()) {
423
this.setOpen(false);
424
}
425
goog.ui.MenuButton.superClass_.handleBlur.call(this, e);
426
};
427
428
429
/**
430
* Returns the menu attached to the button. If no menu is attached, creates a
431
* new empty menu.
432
* @return {goog.ui.Menu} Popup menu attached to the menu button.
433
*/
434
goog.ui.MenuButton.prototype.getMenu = function() {
435
'use strict';
436
if (!this.menu_) {
437
this.setMenu(new goog.ui.Menu(this.getDomHelper(), this.menuRenderer_));
438
}
439
return this.menu_ || null;
440
};
441
442
443
/**
444
* Replaces the menu attached to the button with the argument, and returns the
445
* previous menu (if any).
446
* @param {goog.ui.Menu?} menu New menu to be attached to the menu button (null
447
* to remove the menu).
448
* @return {goog.ui.Menu|undefined} Previous menu (undefined if none).
449
*/
450
goog.ui.MenuButton.prototype.setMenu = function(menu) {
451
'use strict';
452
var oldMenu = this.menu_;
453
454
// Do nothing unless the new menu is different from the current one.
455
if (menu != oldMenu) {
456
if (oldMenu) {
457
this.setOpen(false);
458
if (this.isInDocument()) {
459
this.attachMenuEventListeners_(oldMenu, false);
460
}
461
delete this.menu_;
462
}
463
if (this.isInDocument()) {
464
goog.a11y.aria.setState(
465
this.getElementStrict(), goog.a11y.aria.State.HASPOPUP, !!menu);
466
}
467
if (menu) {
468
this.menu_ = menu;
469
menu.setParent(this);
470
menu.setVisible(false);
471
menu.setAllowAutoFocus(this.isFocusablePopupMenu());
472
if (this.isInDocument()) {
473
this.attachMenuEventListeners_(menu, true);
474
}
475
}
476
}
477
478
return oldMenu;
479
};
480
481
482
/**
483
* Specify which positioning algorithm to use.
484
*
485
* This method is preferred over the fine-grained positioning methods like
486
* setPositionElement, setAlignMenuToStart, and setScrollOnOverflow. Calling
487
* this method will override settings by those methods.
488
*
489
* @param {goog.positioning.AnchoredPosition} position The position of the
490
* Menu the button. If the position has a null anchor, we will use the
491
* menubutton element as the anchor.
492
*/
493
goog.ui.MenuButton.prototype.setMenuPosition = function(position) {
494
'use strict';
495
if (position) {
496
this.menuPosition_ = position;
497
this.positionElement_ = position.element;
498
}
499
};
500
501
502
/**
503
* Sets an element for anchoring the menu.
504
* @param {Element} positionElement New element to use for
505
* positioning the dropdown menu. Null to use the default behavior
506
* of positioning to this menu button.
507
*/
508
goog.ui.MenuButton.prototype.setPositionElement = function(positionElement) {
509
'use strict';
510
this.positionElement_ = positionElement;
511
this.positionMenu();
512
};
513
514
515
/**
516
* Sets a margin that will be applied to the menu's position when it is shown.
517
* If null, no margin will be applied.
518
* @param {goog.math.Box} margin Margin to apply.
519
*/
520
goog.ui.MenuButton.prototype.setMenuMargin = function(margin) {
521
'use strict';
522
this.menuMargin_ = margin;
523
};
524
525
/**
526
* Sets whether the enter or space key should close the menu, if it is already
527
* open. By default, only the ESC key will close an open menu.
528
* @param {boolean} close Whether pressing Enter or Space when the button has
529
* focus will close the menu if it is already open.
530
*/
531
goog.ui.MenuButton.prototype.setCloseOnEnterOrSpace = function(close) {
532
this.closeOnEnterOrSpace_ = close;
533
};
534
535
/**
536
* Sets whether to select the first item in the menu when it is opened using
537
* enter or space. By default, the first item is selected only when
538
* opened by a key up or down event. When this is on, the first item will
539
* be selected due to any of the four events.
540
* @param {boolean} select
541
*/
542
goog.ui.MenuButton.prototype.setSelectFirstOnEnterOrSpace = function(select) {
543
'use strict';
544
this.selectFirstOnEnterOrSpace_ = select;
545
};
546
547
548
/**
549
* Adds a new menu item at the end of the menu.
550
* @param {goog.ui.MenuItem|goog.ui.MenuSeparator|goog.ui.Control} item Menu
551
* item to add to the menu.
552
*/
553
goog.ui.MenuButton.prototype.addItem = function(item) {
554
'use strict';
555
this.getMenu().addChild(item, true);
556
};
557
558
559
/**
560
* Adds a new menu item at the specific index in the menu.
561
* @param {goog.ui.MenuItem|goog.ui.MenuSeparator} item Menu item to add to the
562
* menu.
563
* @param {number} index Index at which to insert the menu item.
564
*/
565
goog.ui.MenuButton.prototype.addItemAt = function(item, index) {
566
'use strict';
567
this.getMenu().addChildAt(item, index, true);
568
};
569
570
571
/**
572
* Removes the item from the menu and disposes of it.
573
* @param {goog.ui.MenuItem|goog.ui.MenuSeparator} item The menu item to remove.
574
*/
575
goog.ui.MenuButton.prototype.removeItem = function(item) {
576
'use strict';
577
var child = this.getMenu().removeChild(item, true);
578
if (child) {
579
child.dispose();
580
}
581
};
582
583
584
/**
585
* Removes the menu item at a given index in the menu and disposes of it.
586
* @param {number} index Index of item.
587
*/
588
goog.ui.MenuButton.prototype.removeItemAt = function(index) {
589
'use strict';
590
var child = this.getMenu().removeChildAt(index, true);
591
if (child) {
592
child.dispose();
593
}
594
};
595
596
597
/**
598
* Returns the menu item at a given index.
599
* @param {number} index Index of menu item.
600
* @return {goog.ui.MenuItem?} Menu item (null if not found).
601
*/
602
goog.ui.MenuButton.prototype.getItemAt = function(index) {
603
'use strict';
604
return this.menu_ ?
605
/** @type {goog.ui.MenuItem} */ (this.menu_.getChildAt(index)) :
606
null;
607
};
608
609
610
/**
611
* Returns the number of items in the menu (including separators).
612
* @return {number} The number of items in the menu.
613
*/
614
goog.ui.MenuButton.prototype.getItemCount = function() {
615
'use strict';
616
return this.menu_ ? this.menu_.getChildCount() : 0;
617
};
618
619
620
/**
621
* Shows/hides the menu button based on the value of the argument. Also hides
622
* the popup menu if the button is being hidden.
623
* @param {boolean} visible Whether to show or hide the button.
624
* @param {boolean=} opt_force If true, doesn't check whether the component
625
* already has the requested visibility, and doesn't dispatch any events.
626
* @return {boolean} Whether the visibility was changed.
627
* @override
628
*/
629
goog.ui.MenuButton.prototype.setVisible = function(visible, opt_force) {
630
'use strict';
631
var visibilityChanged =
632
goog.ui.MenuButton.superClass_.setVisible.call(this, visible, opt_force);
633
if (visibilityChanged && !this.isVisible()) {
634
this.setOpen(false);
635
}
636
return visibilityChanged;
637
};
638
639
640
/**
641
* Enables/disables the menu button based on the value of the argument, and
642
* updates its CSS styling. Also hides the popup menu if the button is being
643
* disabled.
644
* @param {boolean} enable Whether to enable or disable the button.
645
* @override
646
*/
647
goog.ui.MenuButton.prototype.setEnabled = function(enable) {
648
'use strict';
649
goog.ui.MenuButton.superClass_.setEnabled.call(this, enable);
650
if (!this.isEnabled()) {
651
this.setOpen(false);
652
}
653
};
654
655
656
// TODO(nicksantos): AlignMenuToStart and ScrollOnOverflow and PositionElement
657
// should all be deprecated, in favor of people setting their own
658
// AnchoredPosition with the parameters they need. Right now, we try
659
// to be backwards-compatible as possible, but this is incomplete because
660
// the APIs are non-orthogonal.
661
662
663
/**
664
* @return {boolean} Whether the menu is aligned to the start of the button
665
* (left if the render direction is left-to-right, right if the render
666
* direction is right-to-left).
667
*/
668
goog.ui.MenuButton.prototype.isAlignMenuToStart = function() {
669
'use strict';
670
var corner = this.menuPosition_.corner;
671
return corner == goog.positioning.Corner.BOTTOM_START ||
672
corner == goog.positioning.Corner.TOP_START;
673
};
674
675
676
/**
677
* Sets whether the menu is aligned to the start or the end of the button.
678
* @param {boolean} alignToStart Whether the menu is to be aligned to the start
679
* of the button (left if the render direction is left-to-right, right if
680
* the render direction is right-to-left).
681
*/
682
goog.ui.MenuButton.prototype.setAlignMenuToStart = function(alignToStart) {
683
'use strict';
684
this.menuPosition_.corner = alignToStart ?
685
goog.positioning.Corner.BOTTOM_START :
686
goog.positioning.Corner.BOTTOM_END;
687
};
688
689
690
/**
691
* Sets whether the menu should scroll when it's too big to fix vertically on
692
* the screen. The css of the menu element should have overflow set to auto.
693
* Note: Adding or removing items while the menu is open will not work correctly
694
* if scrollOnOverflow is on.
695
* @param {boolean} scrollOnOverflow Whether the menu should scroll when too big
696
* to fit on the screen. If false, adjust logic will be used to try and
697
* reposition the menu to fit.
698
* @suppress {strictMissingProperties} Added to tighten compiler checks
699
*/
700
goog.ui.MenuButton.prototype.setScrollOnOverflow = function(scrollOnOverflow) {
701
'use strict';
702
if (this.menuPosition_.setLastResortOverflow) {
703
var overflowX = goog.positioning.Overflow.ADJUST_X;
704
var overflowY = scrollOnOverflow ? goog.positioning.Overflow.RESIZE_HEIGHT :
705
goog.positioning.Overflow.ADJUST_Y;
706
this.menuPosition_.setLastResortOverflow(overflowX | overflowY);
707
}
708
};
709
710
711
/**
712
* @return {boolean} Whether the menu will scroll when it's to big to fit
713
* vertically on the screen.
714
* @suppress {strictMissingProperties} Added to tighten compiler checks
715
*/
716
goog.ui.MenuButton.prototype.isScrollOnOverflow = function() {
717
'use strict';
718
return this.menuPosition_.getLastResortOverflow &&
719
!!(this.menuPosition_.getLastResortOverflow() &
720
goog.positioning.Overflow.RESIZE_HEIGHT);
721
};
722
723
724
/**
725
* @return {boolean} Whether the attached menu is focusable.
726
*/
727
goog.ui.MenuButton.prototype.isFocusablePopupMenu = function() {
728
'use strict';
729
return this.isFocusablePopupMenu_;
730
};
731
732
733
/**
734
* Sets whether the attached popup menu is focusable. If the popup menu is
735
* focusable, it may steal keyboard focus from the menu button, so the button
736
* will not hide the menu on blur.
737
* @param {boolean} focusable Whether the attached menu is focusable.
738
*/
739
goog.ui.MenuButton.prototype.setFocusablePopupMenu = function(focusable) {
740
'use strict';
741
// TODO(attila): The menu itself should advertise whether it is focusable.
742
this.isFocusablePopupMenu_ = focusable;
743
};
744
745
746
/**
747
* Sets whether to render the menu as a sibling element of the button.
748
* Normally, the menu is a child of document.body. This option is useful if
749
* you need the menu to inherit styles from a common parent element, or if you
750
* otherwise need it to share a parent element for desired event handling. One
751
* example of the latter is if the parent is in a goog.ui.Popup, to ensure that
752
* clicks on the menu are considered being within the popup.
753
* @param {boolean} renderMenuAsSibling Whether we render the menu at the end
754
* of the dom or as a sibling to the button/label that renders the drop
755
* down.
756
*/
757
goog.ui.MenuButton.prototype.setRenderMenuAsSibling = function(
758
renderMenuAsSibling) {
759
'use strict';
760
this.renderMenuAsSibling_ = renderMenuAsSibling;
761
};
762
763
764
/**
765
* Reveals the menu and hooks up menu-specific event handling.
766
* @deprecated Use {@link #setOpen} instead.
767
*/
768
goog.ui.MenuButton.prototype.showMenu = function() {
769
'use strict';
770
this.setOpen(true);
771
};
772
773
774
/**
775
* Hides the menu and cleans up menu-specific event handling.
776
* @deprecated Use {@link #setOpen} instead.
777
*/
778
goog.ui.MenuButton.prototype.hideMenu = function() {
779
'use strict';
780
this.setOpen(false);
781
};
782
783
784
/**
785
* Opens or closes the attached popup menu.
786
* @param {boolean} open Whether to open or close the menu.
787
* @param {goog.events.Event=} opt_e Event that caused the menu to be opened.
788
* @override
789
*/
790
goog.ui.MenuButton.prototype.setOpen = function(open, opt_e) {
791
'use strict';
792
goog.ui.MenuButton.superClass_.setOpen.call(this, open);
793
if (this.menu_ && this.hasState(goog.ui.Component.State.OPENED) == open) {
794
if (open) {
795
if (!this.menu_.isInDocument()) {
796
if (this.renderMenuAsSibling_) {
797
// When we render the menu in the same parent as this button, we
798
// prefer to add it immediately after the button. This way, the screen
799
// readers will go to the menu on the very next element after the
800
// button is read.
801
var nextElementSibling =
802
goog.dom.getNextElementSibling(this.getElement());
803
if (nextElementSibling) {
804
this.menu_.renderBefore(nextElementSibling);
805
} else {
806
this.menu_.render(
807
/** @type {Element} */ (this.getElement().parentNode));
808
}
809
} else {
810
this.menu_.render();
811
}
812
}
813
this.viewportBox_ =
814
goog.style.getVisibleRectForElement(this.getElement());
815
this.buttonRect_ = goog.style.getBounds(this.getElement());
816
this.positionMenu();
817
818
// As per aria spec, highlight the first element in the menu when
819
// keyboarding up or down. Thus, the first menu item will be announced
820
// for screen reader users. If selectFirstOnEnterOrSpace is set, do this
821
// for enter or space as well.
822
/**
823
* @suppress {strictMissingProperties} Added to tighten compiler checks
824
*/
825
var isEnterOrSpace = !!opt_e &&
826
(opt_e.keyCode == goog.events.KeyCodes.ENTER ||
827
opt_e.keyCode == goog.events.KeyCodes.SPACE);
828
/**
829
* @suppress {strictMissingProperties} Added to tighten compiler checks
830
*/
831
var isUpOrDown = !!opt_e &&
832
(opt_e.keyCode == goog.events.KeyCodes.DOWN ||
833
opt_e.keyCode == goog.events.KeyCodes.UP);
834
var focus =
835
isUpOrDown || (isEnterOrSpace && this.selectFirstOnEnterOrSpace_);
836
if (focus) {
837
this.menu_.highlightFirst();
838
} else {
839
this.menu_.setHighlightedIndex(-1);
840
}
841
} else {
842
this.setActive(false);
843
this.menu_.setMouseButtonPressed(false);
844
845
var element = this.getElement();
846
// Clear any remaining a11y state.
847
if (element) {
848
goog.a11y.aria.setState(
849
element, goog.a11y.aria.State.ACTIVEDESCENDANT, '');
850
goog.a11y.aria.setState(element, goog.a11y.aria.State.OWNS, '');
851
}
852
853
// Clear any sizes that might have been stored.
854
if (this.originalSize_ != null) {
855
this.originalSize_ = undefined;
856
var elem = this.menu_.getElement();
857
if (elem) {
858
goog.style.setSize(elem, '', '');
859
}
860
}
861
}
862
this.menu_.setVisible(open, false, opt_e);
863
// In Pivot Tables the menu button somehow gets disposed of during the
864
// setVisible call, causing attachPopupListeners_ to fail.
865
// TODO(user): Debug what happens.
866
if (!this.isDisposed()) {
867
this.attachPopupListeners_(open);
868
}
869
}
870
if (this.menu_ && this.menu_.getElement()) {
871
// Remove the aria-hidden state on the menu element so that it won't be
872
// hidden to screen readers if it's inside a dialog (see b/17610491).
873
goog.a11y.aria.removeState(
874
this.menu_.getElementStrict(), goog.a11y.aria.State.HIDDEN);
875
}
876
};
877
878
879
/**
880
* Resets the MenuButton's size. This is useful for cases where items are added
881
* or removed from the menu and scrollOnOverflow is on. In those cases the
882
* menu will not behave correctly and resize itself unless this is called
883
* (usually followed by positionMenu()).
884
*/
885
goog.ui.MenuButton.prototype.invalidateMenuSize = function() {
886
'use strict';
887
this.originalSize_ = undefined;
888
};
889
890
891
/**
892
* Positions the menu under the button. May be called directly in cases when
893
* the menu size is known to change.
894
*/
895
goog.ui.MenuButton.prototype.positionMenu = function() {
896
'use strict';
897
if (!this.menu_.isInDocument()) {
898
return;
899
}
900
901
var positionElement = this.positionElement_ || this.getElement();
902
var position = this.menuPosition_;
903
this.menuPosition_.element = positionElement;
904
905
var elem = this.menu_.getElement();
906
if (!this.menu_.isVisible()) {
907
elem.style.visibility = 'hidden';
908
goog.style.setElementShown(elem, true);
909
}
910
911
if (!this.originalSize_ && this.isScrollOnOverflow()) {
912
this.originalSize_ = goog.style.getSize(elem);
913
}
914
var popupCorner = goog.positioning.flipCornerVertical(position.corner);
915
position.reposition(elem, popupCorner, this.menuMargin_, this.originalSize_);
916
917
if (!this.menu_.isVisible()) {
918
goog.style.setElementShown(elem, false);
919
elem.style.visibility = 'visible';
920
}
921
};
922
923
924
/**
925
* Periodically repositions the menu while it is visible.
926
*
927
* @param {goog.events.Event} e An event object.
928
* @private
929
*/
930
goog.ui.MenuButton.prototype.onTick_ = function(e) {
931
'use strict';
932
// Call positionMenu() only if the button position or size was
933
// changed, or if the window's viewport was changed.
934
var currentButtonRect = goog.style.getBounds(this.getElement());
935
var currentViewport = goog.style.getVisibleRectForElement(this.getElement());
936
if (goog.math.Rect.equals(this.buttonRect_, currentButtonRect) &&
937
goog.math.Box.equals(this.viewportBox_, currentViewport)) {
938
return;
939
}
940
941
// Reduction in the viewport width (e.g. due to increasing the zoom) can
942
// cause the menu to get squashed against the right edge, distorting its
943
// shape. When we move the menu back where it belongs, we risk using the
944
// distorted size, causing mispositioning. To be safe, start by moving the
945
// menu to the top left to let it reassume its true shape.
946
if (this.menu_.isInDocument() && currentViewport && this.viewportBox_ &&
947
(currentViewport.getWidth() < this.viewportBox_.getWidth())) {
948
var elem = this.menu_.getElement();
949
if (!this.menu_.isVisible()) {
950
elem.style.visibility = 'hidden';
951
goog.style.setElementShown(elem, true);
952
}
953
954
goog.style.setPosition(elem, new goog.math.Coordinate(0, 0));
955
}
956
957
this.buttonRect_ = currentButtonRect;
958
this.viewportBox_ = currentViewport;
959
this.positionMenu();
960
};
961
962
963
/**
964
* Attaches or detaches menu event listeners to/from the given menu.
965
* Called each time a menu is attached to or detached from the button.
966
* @param {goog.ui.Menu} menu Menu on which to listen for events.
967
* @param {boolean} attach Whether to attach or detach event listeners.
968
* @private
969
*/
970
goog.ui.MenuButton.prototype.attachMenuEventListeners_ = function(
971
menu, attach) {
972
'use strict';
973
var handler = this.getHandler();
974
var method = attach ? handler.listen : handler.unlisten;
975
976
// Handle events dispatched by menu items.
977
method.call(
978
handler, menu, goog.ui.Component.EventType.ACTION, this.handleMenuAction);
979
method.call(
980
handler, menu, goog.ui.Component.EventType.CLOSE, this.handleCloseItem);
981
method.call(
982
handler, menu, goog.ui.Component.EventType.HIGHLIGHT,
983
this.handleHighlightItem);
984
method.call(
985
handler, menu, goog.ui.Component.EventType.UNHIGHLIGHT,
986
this.handleUnHighlightItem);
987
};
988
989
990
/**
991
* Attaches or detaches a keydown event listener to/from the given element.
992
* Called each time the button enters or exits the document.
993
* @param {boolean} attach Whether to attach or detach the event listener.
994
* @private
995
*/
996
goog.ui.MenuButton.prototype.attachKeyDownEventListener_ = function(attach) {
997
'use strict';
998
var handler = this.getHandler();
999
var method = attach ? handler.listen : handler.unlisten;
1000
1001
// Handle keydown events dispatched by the button.
1002
method.call(
1003
handler, this.getElement(), goog.events.EventType.KEYDOWN,
1004
this.handleKeyDownEvent_);
1005
};
1006
1007
1008
/**
1009
* Handles `HIGHLIGHT` events dispatched by the attached menu.
1010
* @param {goog.events.Event} e Highlight event to handle.
1011
*/
1012
goog.ui.MenuButton.prototype.handleHighlightItem = function(e) {
1013
'use strict';
1014
/** @suppress {strictMissingProperties} Added to tighten compiler checks */
1015
var targetEl = e.target.getElement();
1016
if (targetEl) {
1017
this.setAriaActiveDescendant_(targetEl);
1018
}
1019
};
1020
1021
1022
/**
1023
* Handles `KEYDOWN` events dispatched by the button element. When the
1024
* button is focusable and the menu is present and visible, prevents the event
1025
* from propagating since the desired behavior is only to close the menu.
1026
* @param {goog.events.Event} e KeyDown event to handle.
1027
* @private
1028
*/
1029
goog.ui.MenuButton.prototype.handleKeyDownEvent_ = function(e) {
1030
'use strict';
1031
if (this.isSupportedState(goog.ui.Component.State.FOCUSED) &&
1032
this.getKeyEventTarget() && this.menu_ && this.menu_.isVisible()) {
1033
e.stopPropagation();
1034
}
1035
};
1036
1037
1038
/**
1039
* Handles UNHIGHLIGHT events dispatched by the associated menu.
1040
* @param {goog.events.Event} e Unhighlight event to handle.
1041
*/
1042
goog.ui.MenuButton.prototype.handleUnHighlightItem = function(e) {
1043
'use strict';
1044
if (!this.menu_.getHighlighted()) {
1045
var element = this.getElement();
1046
goog.asserts.assert(element, 'The menu button DOM element cannot be null.');
1047
goog.a11y.aria.setState(element, goog.a11y.aria.State.ACTIVEDESCENDANT, '');
1048
goog.a11y.aria.setState(element, goog.a11y.aria.State.OWNS, '');
1049
}
1050
};
1051
1052
1053
/**
1054
* Handles `CLOSE` events dispatched by the associated menu.
1055
* @param {goog.events.Event} e Close event to handle.
1056
*/
1057
goog.ui.MenuButton.prototype.handleCloseItem = function(e) {
1058
'use strict';
1059
// When a submenu is closed by pressing left arrow, no highlight event is
1060
// dispatched because the newly focused item was already highlighted, so this
1061
// scenario is handled by listening for the submenu close event instead.
1062
if (this.isOpen() && e.target instanceof goog.ui.MenuItem) {
1063
var menuItem = /** @type {!goog.ui.MenuItem} */ (e.target);
1064
var menuItemEl = menuItem.getElement();
1065
if (menuItem.isVisible() && menuItem.isHighlighted() &&
1066
menuItemEl != null) {
1067
this.setAriaActiveDescendant_(menuItemEl);
1068
}
1069
}
1070
};
1071
1072
1073
/**
1074
* Updates the aria-activedescendant attribute to the given target element.
1075
* @param {!Element} targetEl The target element.
1076
* @private
1077
*/
1078
goog.ui.MenuButton.prototype.setAriaActiveDescendant_ = function(targetEl) {
1079
'use strict';
1080
var element = this.getElement();
1081
goog.asserts.assert(element, 'The menu button DOM element cannot be null.');
1082
1083
// If target element has an activedescendant, then set this control's
1084
// activedescendant to that, otherwise set it to the target element. This is
1085
// a workaround for some screen readers which do not handle
1086
// aria-activedescendant redirection properly.
1087
var targetActiveDescendant = goog.a11y.aria.getActiveDescendant(targetEl);
1088
var activeDescendant = targetActiveDescendant || targetEl;
1089
1090
if (!activeDescendant.id) {
1091
// Create an id if there isn't one already.
1092
var idGenerator = goog.ui.IdGenerator.getInstance();
1093
activeDescendant.id = idGenerator.getNextUniqueId();
1094
}
1095
1096
goog.a11y.aria.setActiveDescendant(element, activeDescendant);
1097
goog.a11y.aria.setState(
1098
element, goog.a11y.aria.State.OWNS, activeDescendant.id);
1099
};
1100
1101
1102
/**
1103
* Attaches or detaches event listeners depending on whether the popup menu
1104
* is being shown or hidden. Starts listening for document mousedown events
1105
* and for menu blur events when the menu is shown, and stops listening for
1106
* these events when it is hidden. Called from {@link #setOpen}.
1107
* @param {boolean} attach Whether to attach or detach event listeners.
1108
* @private
1109
*/
1110
goog.ui.MenuButton.prototype.attachPopupListeners_ = function(attach) {
1111
'use strict';
1112
var handler = this.getHandler();
1113
var method = attach ? handler.listen : handler.unlisten;
1114
1115
// Listen for document mousedown events in the capture phase, because
1116
// the target may stop propagation of the event in the bubble phase.
1117
method.call(
1118
handler, this.getDomHelper().getDocument(),
1119
goog.events.EventType.MOUSEDOWN, this.handleDocumentMouseDown, true);
1120
1121
// Only listen for blur events dispatched by the menu if it is focusable.
1122
if (this.isFocusablePopupMenu()) {
1123
method.call(
1124
handler, /** @type {!goog.events.EventTarget} */ (this.menu_),
1125
goog.ui.Component.EventType.BLUR, this.handleMenuBlur);
1126
}
1127
1128
method.call(handler, this.timer_, goog.Timer.TICK, this.onTick_);
1129
if (attach) {
1130
this.timer_.start();
1131
} else {
1132
this.timer_.stop();
1133
}
1134
};
1135
1136
1137
// Register a decorator factory function for goog.ui.MenuButtons.
1138
goog.ui.registry.setDecoratorByClassName(
1139
goog.ui.MenuButtonRenderer.CSS_CLASS, function() {
1140
'use strict';
1141
// MenuButton defaults to using MenuButtonRenderer.
1142
return new goog.ui.MenuButton(null);
1143
});
1144
1145