menubutton.js 34 KB

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