container.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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 Base class for containers that host {@link goog.ui.Control}s,
  16. * such as menus and toolbars. Provides default keyboard and mouse event
  17. * handling and child management, based on a generalized version of
  18. * {@link goog.ui.Menu}.
  19. *
  20. * @author attila@google.com (Attila Bodis)
  21. * @see ../demos/container.html
  22. */
  23. // TODO(attila): Fix code/logic duplication between this and goog.ui.Control.
  24. // TODO(attila): Maybe pull common stuff all the way up into Component...?
  25. goog.provide('goog.ui.Container');
  26. goog.provide('goog.ui.Container.EventType');
  27. goog.provide('goog.ui.Container.Orientation');
  28. goog.require('goog.a11y.aria');
  29. goog.require('goog.a11y.aria.State');
  30. goog.require('goog.asserts');
  31. goog.require('goog.dom');
  32. goog.require('goog.events.EventType');
  33. goog.require('goog.events.KeyCodes');
  34. goog.require('goog.events.KeyHandler');
  35. goog.require('goog.object');
  36. goog.require('goog.style');
  37. goog.require('goog.ui.Component');
  38. goog.require('goog.ui.ContainerRenderer');
  39. goog.require('goog.ui.Control');
  40. /**
  41. * Base class for containers. Extends {@link goog.ui.Component} by adding
  42. * the following:
  43. * <ul>
  44. * <li>a {@link goog.events.KeyHandler}, to simplify keyboard handling,
  45. * <li>a pluggable <em>renderer</em> framework, to simplify the creation of
  46. * containers without the need to subclass this class,
  47. * <li>methods to manage child controls hosted in the container,
  48. * <li>default mouse and keyboard event handling methods.
  49. * </ul>
  50. * @param {?goog.ui.Container.Orientation=} opt_orientation Container
  51. * orientation; defaults to {@code VERTICAL}.
  52. * @param {goog.ui.ContainerRenderer=} opt_renderer Renderer used to render or
  53. * decorate the container; defaults to {@link goog.ui.ContainerRenderer}.
  54. * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for document
  55. * interaction.
  56. * @extends {goog.ui.Component}
  57. * @constructor
  58. */
  59. goog.ui.Container = function(opt_orientation, opt_renderer, opt_domHelper) {
  60. goog.ui.Component.call(this, opt_domHelper);
  61. this.renderer_ = opt_renderer || goog.ui.ContainerRenderer.getInstance();
  62. this.orientation_ = opt_orientation || this.renderer_.getDefaultOrientation();
  63. };
  64. goog.inherits(goog.ui.Container, goog.ui.Component);
  65. goog.tagUnsealableClass(goog.ui.Container);
  66. /**
  67. * Container-specific events.
  68. * @enum {string}
  69. */
  70. goog.ui.Container.EventType = {
  71. /**
  72. * Dispatched after a goog.ui.Container becomes visible. Non-cancellable.
  73. * NOTE(user): This event really shouldn't exist, because the
  74. * goog.ui.Component.EventType.SHOW event should behave like this one. But the
  75. * SHOW event for containers has been behaving as other components'
  76. * BEFORE_SHOW event for a long time, and too much code relies on that old
  77. * behavior to fix it now.
  78. */
  79. AFTER_SHOW: 'aftershow',
  80. /**
  81. * Dispatched after a goog.ui.Container becomes invisible. Non-cancellable.
  82. */
  83. AFTER_HIDE: 'afterhide'
  84. };
  85. /**
  86. * Container orientation constants.
  87. * @enum {string}
  88. */
  89. goog.ui.Container.Orientation = {
  90. HORIZONTAL: 'horizontal',
  91. VERTICAL: 'vertical'
  92. };
  93. /**
  94. * Allows an alternative element to be set to receive key events, otherwise
  95. * defers to the renderer's element choice.
  96. * @type {Element|undefined}
  97. * @private
  98. */
  99. goog.ui.Container.prototype.keyEventTarget_ = null;
  100. /**
  101. * Keyboard event handler.
  102. * @type {goog.events.KeyHandler?}
  103. * @private
  104. */
  105. goog.ui.Container.prototype.keyHandler_ = null;
  106. /**
  107. * Renderer for the container. Defaults to {@link goog.ui.ContainerRenderer}.
  108. * @type {goog.ui.ContainerRenderer?}
  109. * @private
  110. */
  111. goog.ui.Container.prototype.renderer_ = null;
  112. /**
  113. * Container orientation; determines layout and default keyboard navigation.
  114. * @type {?goog.ui.Container.Orientation}
  115. * @private
  116. */
  117. goog.ui.Container.prototype.orientation_ = null;
  118. /**
  119. * Whether the container is set to be visible. Defaults to true.
  120. * @type {boolean}
  121. * @private
  122. */
  123. goog.ui.Container.prototype.visible_ = true;
  124. /**
  125. * Whether the container is enabled and reacting to keyboard and mouse events.
  126. * Defaults to true.
  127. * @type {boolean}
  128. * @private
  129. */
  130. goog.ui.Container.prototype.enabled_ = true;
  131. /**
  132. * Whether the container supports keyboard focus. Defaults to true. Focusable
  133. * containers have a {@code tabIndex} and can be navigated to via the keyboard.
  134. * @type {boolean}
  135. * @private
  136. */
  137. goog.ui.Container.prototype.focusable_ = true;
  138. /**
  139. * The 0-based index of the currently highlighted control in the container
  140. * (-1 if none).
  141. * @type {number}
  142. * @private
  143. */
  144. goog.ui.Container.prototype.highlightedIndex_ = -1;
  145. /**
  146. * The currently open (expanded) control in the container (null if none).
  147. * @type {goog.ui.Control?}
  148. * @private
  149. */
  150. goog.ui.Container.prototype.openItem_ = null;
  151. /**
  152. * Whether the mouse button is held down. Defaults to false. This flag is set
  153. * when the user mouses down over the container, and remains set until they
  154. * release the mouse button.
  155. * @type {boolean}
  156. * @private
  157. */
  158. goog.ui.Container.prototype.mouseButtonPressed_ = false;
  159. /**
  160. * Whether focus of child components should be allowed. Only effective if
  161. * focusable_ is set to false.
  162. * @type {boolean}
  163. * @private
  164. */
  165. goog.ui.Container.prototype.allowFocusableChildren_ = false;
  166. /**
  167. * Whether highlighting a child component should also open it.
  168. * @type {boolean}
  169. * @private
  170. */
  171. goog.ui.Container.prototype.openFollowsHighlight_ = true;
  172. /**
  173. * Map of DOM IDs to child controls. Each key is the DOM ID of a child
  174. * control's root element; each value is a reference to the child control
  175. * itself. Used for looking up the child control corresponding to a DOM
  176. * node in O(1) time.
  177. * @type {Object}
  178. * @private
  179. */
  180. goog.ui.Container.prototype.childElementIdMap_ = null;
  181. // Event handler and renderer management.
  182. /**
  183. * Returns the DOM element on which the container is listening for keyboard
  184. * events (null if none).
  185. * @return {Element} Element on which the container is listening for key
  186. * events.
  187. */
  188. goog.ui.Container.prototype.getKeyEventTarget = function() {
  189. // Delegate to renderer, unless we've set an explicit target.
  190. return this.keyEventTarget_ || this.renderer_.getKeyEventTarget(this);
  191. };
  192. /**
  193. * Attaches an element on which to listen for key events.
  194. * @param {Element|undefined} element The element to attach, or null/undefined
  195. * to attach to the default element.
  196. */
  197. goog.ui.Container.prototype.setKeyEventTarget = function(element) {
  198. if (this.focusable_) {
  199. var oldTarget = this.getKeyEventTarget();
  200. var inDocument = this.isInDocument();
  201. this.keyEventTarget_ = element;
  202. var newTarget = this.getKeyEventTarget();
  203. if (inDocument) {
  204. // Unlisten for events on the old key target. Requires us to reset
  205. // key target state temporarily.
  206. this.keyEventTarget_ = oldTarget;
  207. this.enableFocusHandling_(false);
  208. this.keyEventTarget_ = element;
  209. // Listen for events on the new key target.
  210. this.getKeyHandler().attach(newTarget);
  211. this.enableFocusHandling_(true);
  212. }
  213. } else {
  214. throw Error(
  215. 'Can\'t set key event target for container ' +
  216. 'that doesn\'t support keyboard focus!');
  217. }
  218. };
  219. /**
  220. * Returns the keyboard event handler for this container, lazily created the
  221. * first time this method is called. The keyboard event handler listens for
  222. * keyboard events on the container's key event target, as determined by its
  223. * renderer.
  224. * @return {!goog.events.KeyHandler} Keyboard event handler for this container.
  225. */
  226. goog.ui.Container.prototype.getKeyHandler = function() {
  227. return this.keyHandler_ ||
  228. (this.keyHandler_ = new goog.events.KeyHandler(this.getKeyEventTarget()));
  229. };
  230. /**
  231. * Returns the renderer used by this container to render itself or to decorate
  232. * an existing element.
  233. * @return {goog.ui.ContainerRenderer} Renderer used by the container.
  234. */
  235. goog.ui.Container.prototype.getRenderer = function() {
  236. return this.renderer_;
  237. };
  238. /**
  239. * Registers the given renderer with the container. Changing renderers after
  240. * the container has already been rendered or decorated is an error.
  241. * @param {goog.ui.ContainerRenderer} renderer Renderer used by the container.
  242. */
  243. goog.ui.Container.prototype.setRenderer = function(renderer) {
  244. if (this.getElement()) {
  245. // Too late.
  246. throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
  247. }
  248. this.renderer_ = renderer;
  249. };
  250. // Standard goog.ui.Component implementation.
  251. /**
  252. * Creates the container's DOM.
  253. * @override
  254. */
  255. goog.ui.Container.prototype.createDom = function() {
  256. // Delegate to renderer.
  257. this.setElementInternal(this.renderer_.createDom(this));
  258. };
  259. /**
  260. * Returns the DOM element into which child components are to be rendered,
  261. * or null if the container itself hasn't been rendered yet. Overrides
  262. * {@link goog.ui.Component#getContentElement} by delegating to the renderer.
  263. * @return {Element} Element to contain child elements (null if none).
  264. * @override
  265. */
  266. goog.ui.Container.prototype.getContentElement = function() {
  267. // Delegate to renderer.
  268. return this.renderer_.getContentElement(this.getElement());
  269. };
  270. /**
  271. * Returns true if the given element can be decorated by this container.
  272. * Overrides {@link goog.ui.Component#canDecorate}.
  273. * @param {Element} element Element to decorate.
  274. * @return {boolean} True iff the element can be decorated.
  275. * @override
  276. */
  277. goog.ui.Container.prototype.canDecorate = function(element) {
  278. // Delegate to renderer.
  279. return this.renderer_.canDecorate(element);
  280. };
  281. /**
  282. * Decorates the given element with this container. Overrides {@link
  283. * goog.ui.Component#decorateInternal}. Considered protected.
  284. * @param {Element} element Element to decorate.
  285. * @override
  286. */
  287. goog.ui.Container.prototype.decorateInternal = function(element) {
  288. // Delegate to renderer.
  289. this.setElementInternal(this.renderer_.decorate(this, element));
  290. // Check whether the decorated element is explicitly styled to be invisible.
  291. if (element.style.display == 'none') {
  292. this.visible_ = false;
  293. }
  294. };
  295. /**
  296. * Configures the container after its DOM has been rendered, and sets up event
  297. * handling. Overrides {@link goog.ui.Component#enterDocument}.
  298. * @override
  299. */
  300. goog.ui.Container.prototype.enterDocument = function() {
  301. goog.ui.Container.superClass_.enterDocument.call(this);
  302. this.forEachChild(function(child) {
  303. if (child.isInDocument()) {
  304. this.registerChildId_(child);
  305. }
  306. }, this);
  307. var elem = this.getElement();
  308. // Call the renderer's initializeDom method to initialize the container's DOM.
  309. this.renderer_.initializeDom(this);
  310. // Initialize visibility (opt_force = true, so we don't dispatch events).
  311. this.setVisible(this.visible_, true);
  312. // Handle events dispatched by child controls.
  313. this.getHandler()
  314. .listen(this, goog.ui.Component.EventType.ENTER, this.handleEnterItem)
  315. .listen(
  316. this, goog.ui.Component.EventType.HIGHLIGHT, this.handleHighlightItem)
  317. .listen(
  318. this, goog.ui.Component.EventType.UNHIGHLIGHT,
  319. this.handleUnHighlightItem)
  320. .listen(this, goog.ui.Component.EventType.OPEN, this.handleOpenItem)
  321. .listen(this, goog.ui.Component.EventType.CLOSE, this.handleCloseItem)
  322. .
  323. // Handle mouse events.
  324. listen(elem, goog.events.EventType.MOUSEDOWN, this.handleMouseDown)
  325. .listen(
  326. goog.dom.getOwnerDocument(elem), goog.events.EventType.MOUSEUP,
  327. this.handleDocumentMouseUp)
  328. .
  329. // Handle mouse events on behalf of controls in the container.
  330. listen(
  331. elem,
  332. [
  333. goog.events.EventType.MOUSEDOWN, goog.events.EventType.MOUSEUP,
  334. goog.events.EventType.MOUSEOVER, goog.events.EventType.MOUSEOUT,
  335. goog.events.EventType.CONTEXTMENU
  336. ],
  337. this.handleChildMouseEvents);
  338. // If the container is focusable, set up keyboard event handling.
  339. if (this.isFocusable()) {
  340. this.enableFocusHandling_(true);
  341. }
  342. };
  343. /**
  344. * Sets up listening for events applicable to focusable containers.
  345. * @param {boolean} enable Whether to enable or disable focus handling.
  346. * @private
  347. */
  348. goog.ui.Container.prototype.enableFocusHandling_ = function(enable) {
  349. var handler = this.getHandler();
  350. var keyTarget = this.getKeyEventTarget();
  351. if (enable) {
  352. handler.listen(keyTarget, goog.events.EventType.FOCUS, this.handleFocus)
  353. .listen(keyTarget, goog.events.EventType.BLUR, this.handleBlur)
  354. .listen(
  355. this.getKeyHandler(), goog.events.KeyHandler.EventType.KEY,
  356. this.handleKeyEvent);
  357. } else {
  358. handler.unlisten(keyTarget, goog.events.EventType.FOCUS, this.handleFocus)
  359. .unlisten(keyTarget, goog.events.EventType.BLUR, this.handleBlur)
  360. .unlisten(
  361. this.getKeyHandler(), goog.events.KeyHandler.EventType.KEY,
  362. this.handleKeyEvent);
  363. }
  364. };
  365. /**
  366. * Cleans up the container before its DOM is removed from the document, and
  367. * removes event handlers. Overrides {@link goog.ui.Component#exitDocument}.
  368. * @override
  369. */
  370. goog.ui.Container.prototype.exitDocument = function() {
  371. // {@link #setHighlightedIndex} has to be called before
  372. // {@link goog.ui.Component#exitDocument}, otherwise it has no effect.
  373. this.setHighlightedIndex(-1);
  374. if (this.openItem_) {
  375. this.openItem_.setOpen(false);
  376. }
  377. this.mouseButtonPressed_ = false;
  378. goog.ui.Container.superClass_.exitDocument.call(this);
  379. };
  380. /** @override */
  381. goog.ui.Container.prototype.disposeInternal = function() {
  382. goog.ui.Container.superClass_.disposeInternal.call(this);
  383. if (this.keyHandler_) {
  384. this.keyHandler_.dispose();
  385. this.keyHandler_ = null;
  386. }
  387. this.keyEventTarget_ = null;
  388. this.childElementIdMap_ = null;
  389. this.openItem_ = null;
  390. this.renderer_ = null;
  391. };
  392. // Default event handlers.
  393. /**
  394. * Handles ENTER events raised by child controls when they are navigated to.
  395. * @param {goog.events.Event} e ENTER event to handle.
  396. * @return {boolean} Whether to prevent handleMouseOver from handling
  397. * the event.
  398. */
  399. goog.ui.Container.prototype.handleEnterItem = function(e) {
  400. // Allow the Control to highlight itself.
  401. return true;
  402. };
  403. /**
  404. * Handles HIGHLIGHT events dispatched by items in the container when
  405. * they are highlighted.
  406. * @param {goog.events.Event} e Highlight event to handle.
  407. */
  408. goog.ui.Container.prototype.handleHighlightItem = function(e) {
  409. var index = this.indexOfChild(/** @type {goog.ui.Control} */ (e.target));
  410. if (index > -1 && index != this.highlightedIndex_) {
  411. var item = this.getHighlighted();
  412. if (item) {
  413. // Un-highlight previously highlighted item.
  414. item.setHighlighted(false);
  415. }
  416. this.highlightedIndex_ = index;
  417. item = this.getHighlighted();
  418. if (this.isMouseButtonPressed()) {
  419. // Activate item when mouse button is pressed, to allow MacOS-style
  420. // dragging to choose menu items. Although this should only truly
  421. // happen if the highlight is due to mouse movements, there is little
  422. // harm in doing it for keyboard or programmatic highlights.
  423. item.setActive(true);
  424. }
  425. // Update open item if open item needs follow highlight.
  426. if (this.openFollowsHighlight_ && this.openItem_ &&
  427. item != this.openItem_) {
  428. if (item.isSupportedState(goog.ui.Component.State.OPENED)) {
  429. item.setOpen(true);
  430. } else {
  431. this.openItem_.setOpen(false);
  432. }
  433. }
  434. }
  435. var element = this.getElement();
  436. goog.asserts.assert(
  437. element, 'The DOM element for the container cannot be null.');
  438. if (e.target.getElement() != null) {
  439. goog.a11y.aria.setState(
  440. element, goog.a11y.aria.State.ACTIVEDESCENDANT,
  441. e.target.getElement().id);
  442. }
  443. };
  444. /**
  445. * Handles UNHIGHLIGHT events dispatched by items in the container when
  446. * they are unhighlighted.
  447. * @param {goog.events.Event} e Unhighlight event to handle.
  448. */
  449. goog.ui.Container.prototype.handleUnHighlightItem = function(e) {
  450. if (e.target == this.getHighlighted()) {
  451. this.highlightedIndex_ = -1;
  452. }
  453. var element = this.getElement();
  454. goog.asserts.assert(
  455. element, 'The DOM element for the container cannot be null.');
  456. // Setting certain ARIA attributes to empty strings is problematic.
  457. // Just remove the attribute instead.
  458. goog.a11y.aria.removeState(element, goog.a11y.aria.State.ACTIVEDESCENDANT);
  459. };
  460. /**
  461. * Handles OPEN events dispatched by items in the container when they are
  462. * opened.
  463. * @param {goog.events.Event} e Open event to handle.
  464. */
  465. goog.ui.Container.prototype.handleOpenItem = function(e) {
  466. var item = /** @type {goog.ui.Control} */ (e.target);
  467. if (item && item != this.openItem_ && item.getParent() == this) {
  468. if (this.openItem_) {
  469. this.openItem_.setOpen(false);
  470. }
  471. this.openItem_ = item;
  472. }
  473. };
  474. /**
  475. * Handles CLOSE events dispatched by items in the container when they are
  476. * closed.
  477. * @param {goog.events.Event} e Close event to handle.
  478. */
  479. goog.ui.Container.prototype.handleCloseItem = function(e) {
  480. if (e.target == this.openItem_) {
  481. this.openItem_ = null;
  482. }
  483. var element = this.getElement();
  484. var targetEl = e.target.getElement();
  485. // Set the active descendant to the menu item when its submenu is closed and
  486. // it is still highlighted. This can sometimes be called when the menuitem is
  487. // unhighlighted because the focus moved elsewhere, do nothing at that point.
  488. if (element && e.target.isHighlighted() && targetEl) {
  489. goog.a11y.aria.setActiveDescendant(element, targetEl);
  490. }
  491. };
  492. /**
  493. * Handles mousedown events over the container. The default implementation
  494. * sets the "mouse button pressed" flag and, if the container is focusable,
  495. * grabs keyboard focus.
  496. * @param {goog.events.BrowserEvent} e Mousedown event to handle.
  497. */
  498. goog.ui.Container.prototype.handleMouseDown = function(e) {
  499. if (this.enabled_) {
  500. this.setMouseButtonPressed(true);
  501. }
  502. var keyTarget = this.getKeyEventTarget();
  503. if (keyTarget && goog.dom.isFocusableTabIndex(keyTarget)) {
  504. // The container is configured to receive keyboard focus.
  505. keyTarget.focus();
  506. } else {
  507. // The control isn't configured to receive keyboard focus; prevent it
  508. // from stealing focus or destroying the selection.
  509. e.preventDefault();
  510. }
  511. };
  512. /**
  513. * Handles mouseup events over the document. The default implementation
  514. * clears the "mouse button pressed" flag.
  515. * @param {goog.events.BrowserEvent} e Mouseup event to handle.
  516. */
  517. goog.ui.Container.prototype.handleDocumentMouseUp = function(e) {
  518. this.setMouseButtonPressed(false);
  519. };
  520. /**
  521. * Handles mouse events originating from nodes belonging to the controls hosted
  522. * in the container. Locates the child control based on the DOM node that
  523. * dispatched the event, and forwards the event to the control for handling.
  524. * @param {goog.events.BrowserEvent} e Mouse event to handle.
  525. */
  526. goog.ui.Container.prototype.handleChildMouseEvents = function(e) {
  527. var control = this.getOwnerControl(/** @type {Node} */ (e.target));
  528. if (control) {
  529. // Child control identified; forward the event.
  530. switch (e.type) {
  531. case goog.events.EventType.MOUSEDOWN:
  532. control.handleMouseDown(e);
  533. break;
  534. case goog.events.EventType.MOUSEUP:
  535. control.handleMouseUp(e);
  536. break;
  537. case goog.events.EventType.MOUSEOVER:
  538. control.handleMouseOver(e);
  539. break;
  540. case goog.events.EventType.MOUSEOUT:
  541. control.handleMouseOut(e);
  542. break;
  543. case goog.events.EventType.CONTEXTMENU:
  544. control.handleContextMenu(e);
  545. break;
  546. }
  547. }
  548. };
  549. /**
  550. * Returns the child control that owns the given DOM node, or null if no such
  551. * control is found.
  552. * @param {Node} node DOM node whose owner is to be returned.
  553. * @return {goog.ui.Control?} Control hosted in the container to which the node
  554. * belongs (if found).
  555. * @protected
  556. */
  557. goog.ui.Container.prototype.getOwnerControl = function(node) {
  558. // Ensure that this container actually has child controls before
  559. // looking up the owner.
  560. if (this.childElementIdMap_) {
  561. var elem = this.getElement();
  562. // See http://b/2964418 . IE9 appears to evaluate '!=' incorrectly, so
  563. // using '!==' instead.
  564. // TODO(user): Possibly revert this change if/when IE9 fixes the issue.
  565. while (node && node !== elem) {
  566. var id = node.id;
  567. if (id in this.childElementIdMap_) {
  568. return this.childElementIdMap_[id];
  569. }
  570. node = node.parentNode;
  571. }
  572. }
  573. return null;
  574. };
  575. /**
  576. * Handles focus events raised when the container's key event target receives
  577. * keyboard focus.
  578. * @param {goog.events.BrowserEvent} e Focus event to handle.
  579. */
  580. goog.ui.Container.prototype.handleFocus = function(e) {
  581. // No-op in the base class.
  582. };
  583. /**
  584. * Handles blur events raised when the container's key event target loses
  585. * keyboard focus. The default implementation clears the highlight index.
  586. * @param {goog.events.BrowserEvent} e Blur event to handle.
  587. */
  588. goog.ui.Container.prototype.handleBlur = function(e) {
  589. this.setHighlightedIndex(-1);
  590. this.setMouseButtonPressed(false);
  591. // If the container loses focus, and one of its children is open, close it.
  592. if (this.openItem_) {
  593. this.openItem_.setOpen(false);
  594. }
  595. };
  596. /**
  597. * Attempts to handle a keyboard event, if the control is enabled, by calling
  598. * {@link handleKeyEventInternal}. Considered protected; should only be used
  599. * within this package and by subclasses.
  600. * @param {goog.events.KeyEvent} e Key event to handle.
  601. * @return {boolean} Whether the key event was handled.
  602. */
  603. goog.ui.Container.prototype.handleKeyEvent = function(e) {
  604. if (this.isEnabled() && this.isVisible() &&
  605. (this.getChildCount() != 0 || this.keyEventTarget_) &&
  606. this.handleKeyEventInternal(e)) {
  607. e.preventDefault();
  608. e.stopPropagation();
  609. return true;
  610. }
  611. return false;
  612. };
  613. /**
  614. * Attempts to handle a keyboard event; returns true if the event was handled,
  615. * false otherwise. If the container is enabled, and a child is highlighted,
  616. * calls the child control's {@code handleKeyEvent} method to give the control
  617. * a chance to handle the event first.
  618. * @param {goog.events.KeyEvent} e Key event to handle.
  619. * @return {boolean} Whether the event was handled by the container (or one of
  620. * its children).
  621. */
  622. goog.ui.Container.prototype.handleKeyEventInternal = function(e) {
  623. // Give the highlighted control the chance to handle the key event.
  624. var highlighted = this.getHighlighted();
  625. if (highlighted && typeof highlighted.handleKeyEvent == 'function' &&
  626. highlighted.handleKeyEvent(e)) {
  627. return true;
  628. }
  629. // Give the open control the chance to handle the key event.
  630. if (this.openItem_ && this.openItem_ != highlighted &&
  631. typeof this.openItem_.handleKeyEvent == 'function' &&
  632. this.openItem_.handleKeyEvent(e)) {
  633. return true;
  634. }
  635. // Do not handle the key event if any modifier key is pressed.
  636. if (e.shiftKey || e.ctrlKey || e.metaKey || e.altKey) {
  637. return false;
  638. }
  639. // Either nothing is highlighted, or the highlighted control didn't handle
  640. // the key event, so attempt to handle it here.
  641. switch (e.keyCode) {
  642. case goog.events.KeyCodes.ESC:
  643. if (this.isFocusable()) {
  644. this.getKeyEventTarget().blur();
  645. } else {
  646. return false;
  647. }
  648. break;
  649. case goog.events.KeyCodes.HOME:
  650. this.highlightFirst();
  651. break;
  652. case goog.events.KeyCodes.END:
  653. this.highlightLast();
  654. break;
  655. case goog.events.KeyCodes.UP:
  656. if (this.orientation_ == goog.ui.Container.Orientation.VERTICAL) {
  657. this.highlightPrevious();
  658. } else {
  659. return false;
  660. }
  661. break;
  662. case goog.events.KeyCodes.LEFT:
  663. if (this.orientation_ == goog.ui.Container.Orientation.HORIZONTAL) {
  664. if (this.isRightToLeft()) {
  665. this.highlightNext();
  666. } else {
  667. this.highlightPrevious();
  668. }
  669. } else {
  670. return false;
  671. }
  672. break;
  673. case goog.events.KeyCodes.DOWN:
  674. if (this.orientation_ == goog.ui.Container.Orientation.VERTICAL) {
  675. this.highlightNext();
  676. } else {
  677. return false;
  678. }
  679. break;
  680. case goog.events.KeyCodes.RIGHT:
  681. if (this.orientation_ == goog.ui.Container.Orientation.HORIZONTAL) {
  682. if (this.isRightToLeft()) {
  683. this.highlightPrevious();
  684. } else {
  685. this.highlightNext();
  686. }
  687. } else {
  688. return false;
  689. }
  690. break;
  691. default:
  692. return false;
  693. }
  694. return true;
  695. };
  696. // Child component management.
  697. /**
  698. * Creates a DOM ID for the child control and registers it to an internal
  699. * hash table to be able to find it fast by id.
  700. * @param {goog.ui.Component} child The child control. Its root element has
  701. * to be created yet.
  702. * @private
  703. */
  704. goog.ui.Container.prototype.registerChildId_ = function(child) {
  705. // Map the DOM ID of the control's root element to the control itself.
  706. var childElem = child.getElement();
  707. // If the control's root element doesn't have a DOM ID assign one.
  708. var id = childElem.id || (childElem.id = child.getId());
  709. // Lazily create the child element ID map on first use.
  710. if (!this.childElementIdMap_) {
  711. this.childElementIdMap_ = {};
  712. }
  713. this.childElementIdMap_[id] = child;
  714. };
  715. /**
  716. * Adds the specified control as the last child of this container. See
  717. * {@link goog.ui.Container#addChildAt} for detailed semantics.
  718. * @param {goog.ui.Component} child The new child control.
  719. * @param {boolean=} opt_render Whether the new child should be rendered
  720. * immediately after being added (defaults to false).
  721. * @override
  722. */
  723. goog.ui.Container.prototype.addChild = function(child, opt_render) {
  724. goog.asserts.assertInstanceof(
  725. child, goog.ui.Control, 'The child of a container must be a control');
  726. goog.ui.Container.superClass_.addChild.call(this, child, opt_render);
  727. };
  728. /**
  729. * Overrides {@link goog.ui.Container#getChild} to make it clear that it
  730. * only returns {@link goog.ui.Control}s.
  731. * @param {string} id Child component ID.
  732. * @return {goog.ui.Control} The child with the given ID; null if none.
  733. * @override
  734. */
  735. goog.ui.Container.prototype.getChild;
  736. /**
  737. * Overrides {@link goog.ui.Container#getChildAt} to make it clear that it
  738. * only returns {@link goog.ui.Control}s.
  739. * @param {number} index 0-based index.
  740. * @return {goog.ui.Control} The child with the given ID; null if none.
  741. * @override
  742. */
  743. goog.ui.Container.prototype.getChildAt;
  744. /**
  745. * Adds the control as a child of this container at the given 0-based index.
  746. * Overrides {@link goog.ui.Component#addChildAt} by also updating the
  747. * container's highlight index. Since {@link goog.ui.Component#addChild} uses
  748. * {@link #addChildAt} internally, we only need to override this method.
  749. * @param {goog.ui.Component} control New child.
  750. * @param {number} index Index at which the new child is to be added.
  751. * @param {boolean=} opt_render Whether the new child should be rendered
  752. * immediately after being added (defaults to false).
  753. * @override
  754. */
  755. goog.ui.Container.prototype.addChildAt = function(control, index, opt_render) {
  756. goog.asserts.assertInstanceof(control, goog.ui.Control);
  757. // Make sure the child control dispatches HIGHLIGHT, UNHIGHLIGHT, OPEN, and
  758. // CLOSE events, and that it doesn't steal keyboard focus.
  759. control.setDispatchTransitionEvents(goog.ui.Component.State.HOVER, true);
  760. control.setDispatchTransitionEvents(goog.ui.Component.State.OPENED, true);
  761. if (this.isFocusable() || !this.isFocusableChildrenAllowed()) {
  762. control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
  763. }
  764. // Disable mouse event handling by child controls.
  765. control.setHandleMouseEvents(false);
  766. var srcIndex =
  767. (control.getParent() == this) ? this.indexOfChild(control) : -1;
  768. // Let the superclass implementation do the work.
  769. goog.ui.Container.superClass_.addChildAt.call(
  770. this, control, index, opt_render);
  771. if (control.isInDocument() && this.isInDocument()) {
  772. this.registerChildId_(control);
  773. }
  774. this.updateHighlightedIndex_(srcIndex, index);
  775. };
  776. /**
  777. * Updates the highlighted index when children are added or moved.
  778. * @param {number} fromIndex Index of the child before it was moved, or -1 if
  779. * the child was added.
  780. * @param {number} toIndex Index of the child after it was moved or added.
  781. * @private
  782. */
  783. goog.ui.Container.prototype.updateHighlightedIndex_ = function(
  784. fromIndex, toIndex) {
  785. if (fromIndex == -1) {
  786. fromIndex = this.getChildCount();
  787. }
  788. if (fromIndex == this.highlightedIndex_) {
  789. // The highlighted element itself was moved.
  790. this.highlightedIndex_ = Math.min(this.getChildCount() - 1, toIndex);
  791. } else if (
  792. fromIndex > this.highlightedIndex_ && toIndex <= this.highlightedIndex_) {
  793. // The control was added or moved behind the highlighted index.
  794. this.highlightedIndex_++;
  795. } else if (
  796. fromIndex < this.highlightedIndex_ && toIndex > this.highlightedIndex_) {
  797. // The control was moved from before to behind the highlighted index.
  798. this.highlightedIndex_--;
  799. }
  800. };
  801. /**
  802. * Removes a child control. Overrides {@link goog.ui.Component#removeChild} by
  803. * updating the highlight index. Since {@link goog.ui.Component#removeChildAt}
  804. * uses {@link #removeChild} internally, we only need to override this method.
  805. * @param {string|goog.ui.Component} control The ID of the child to remove, or
  806. * the control itself.
  807. * @param {boolean=} opt_unrender Whether to call {@code exitDocument} on the
  808. * removed control, and detach its DOM from the document (defaults to
  809. * false).
  810. * @return {goog.ui.Control} The removed control, if any.
  811. * @override
  812. */
  813. goog.ui.Container.prototype.removeChild = function(control, opt_unrender) {
  814. control = goog.isString(control) ? this.getChild(control) : control;
  815. goog.asserts.assertInstanceof(control, goog.ui.Control);
  816. if (control) {
  817. var index = this.indexOfChild(control);
  818. if (index != -1) {
  819. if (index == this.highlightedIndex_) {
  820. control.setHighlighted(false);
  821. this.highlightedIndex_ = -1;
  822. } else if (index < this.highlightedIndex_) {
  823. this.highlightedIndex_--;
  824. }
  825. }
  826. // Remove the mapping from the child element ID map.
  827. var childElem = control.getElement();
  828. if (childElem && childElem.id && this.childElementIdMap_) {
  829. goog.object.remove(this.childElementIdMap_, childElem.id);
  830. }
  831. }
  832. control = /** @type {!goog.ui.Control} */ (
  833. goog.ui.Container.superClass_.removeChild.call(
  834. this, control, opt_unrender));
  835. // Re-enable mouse event handling (in case the control is reused elsewhere).
  836. control.setHandleMouseEvents(true);
  837. return control;
  838. };
  839. // Container state management.
  840. /**
  841. * Returns the container's orientation.
  842. * @return {?goog.ui.Container.Orientation} Container orientation.
  843. */
  844. goog.ui.Container.prototype.getOrientation = function() {
  845. return this.orientation_;
  846. };
  847. /**
  848. * Sets the container's orientation.
  849. * @param {goog.ui.Container.Orientation} orientation Container orientation.
  850. */
  851. // TODO(attila): Do we need to support containers with dynamic orientation?
  852. goog.ui.Container.prototype.setOrientation = function(orientation) {
  853. if (this.getElement()) {
  854. // Too late.
  855. throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
  856. }
  857. this.orientation_ = orientation;
  858. };
  859. /**
  860. * Returns true if the container's visibility is set to visible, false if
  861. * it is set to hidden. A container that is set to hidden is guaranteed
  862. * to be hidden from the user, but the reverse isn't necessarily true.
  863. * A container may be set to visible but can otherwise be obscured by another
  864. * element, rendered off-screen, or hidden using direct CSS manipulation.
  865. * @return {boolean} Whether the container is set to be visible.
  866. */
  867. goog.ui.Container.prototype.isVisible = function() {
  868. return this.visible_;
  869. };
  870. /**
  871. * Shows or hides the container. Does nothing if the container already has
  872. * the requested visibility. Otherwise, dispatches a SHOW or HIDE event as
  873. * appropriate, giving listeners a chance to prevent the visibility change.
  874. * @param {boolean} visible Whether to show or hide the container.
  875. * @param {boolean=} opt_force If true, doesn't check whether the container
  876. * already has the requested visibility, and doesn't dispatch any events.
  877. * @return {boolean} Whether the visibility was changed.
  878. */
  879. goog.ui.Container.prototype.setVisible = function(visible, opt_force) {
  880. if (opt_force || (this.visible_ != visible &&
  881. this.dispatchEvent(
  882. visible ? goog.ui.Component.EventType.SHOW :
  883. goog.ui.Component.EventType.HIDE))) {
  884. this.visible_ = visible;
  885. var elem = this.getElement();
  886. if (elem) {
  887. goog.style.setElementShown(elem, visible);
  888. if (this.isFocusable()) {
  889. // Enable keyboard access only for enabled & visible containers.
  890. this.renderer_.enableTabIndex(
  891. this.getKeyEventTarget(), this.enabled_ && this.visible_);
  892. }
  893. if (!opt_force) {
  894. this.dispatchEvent(
  895. this.visible_ ? goog.ui.Container.EventType.AFTER_SHOW :
  896. goog.ui.Container.EventType.AFTER_HIDE);
  897. }
  898. }
  899. return true;
  900. }
  901. return false;
  902. };
  903. /**
  904. * Returns true if the container is enabled, false otherwise.
  905. * @return {boolean} Whether the container is enabled.
  906. */
  907. goog.ui.Container.prototype.isEnabled = function() {
  908. return this.enabled_;
  909. };
  910. /**
  911. * Enables/disables the container based on the {@code enable} argument.
  912. * Dispatches an {@code ENABLED} or {@code DISABLED} event prior to changing
  913. * the container's state, which may be caught and canceled to prevent the
  914. * container from changing state. Also enables/disables child controls.
  915. * @param {boolean} enable Whether to enable or disable the container.
  916. */
  917. goog.ui.Container.prototype.setEnabled = function(enable) {
  918. if (this.enabled_ != enable &&
  919. this.dispatchEvent(
  920. enable ? goog.ui.Component.EventType.ENABLE :
  921. goog.ui.Component.EventType.DISABLE)) {
  922. if (enable) {
  923. // Flag the container as enabled first, then update children. This is
  924. // because controls can't be enabled if their parent is disabled.
  925. this.enabled_ = true;
  926. this.forEachChild(function(child) {
  927. // Enable child control unless it is flagged.
  928. if (child.wasDisabled) {
  929. delete child.wasDisabled;
  930. } else {
  931. child.setEnabled(true);
  932. }
  933. });
  934. } else {
  935. // Disable children first, then flag the container as disabled. This is
  936. // because controls can't be disabled if their parent is already disabled.
  937. this.forEachChild(function(child) {
  938. // Disable child control, or flag it if it's already disabled.
  939. if (child.isEnabled()) {
  940. child.setEnabled(false);
  941. } else {
  942. child.wasDisabled = true;
  943. }
  944. });
  945. this.enabled_ = false;
  946. this.setMouseButtonPressed(false);
  947. }
  948. if (this.isFocusable()) {
  949. // Enable keyboard access only for enabled & visible components.
  950. this.renderer_.enableTabIndex(
  951. this.getKeyEventTarget(), enable && this.visible_);
  952. }
  953. }
  954. };
  955. /**
  956. * Returns true if the container is focusable, false otherwise. The default
  957. * is true. Focusable containers always have a tab index and allocate a key
  958. * handler to handle keyboard events while focused.
  959. * @return {boolean} Whether the component is focusable.
  960. */
  961. goog.ui.Container.prototype.isFocusable = function() {
  962. return this.focusable_;
  963. };
  964. /**
  965. * Sets whether the container is focusable. The default is true. Focusable
  966. * containers always have a tab index and allocate a key handler to handle
  967. * keyboard events while focused.
  968. * @param {boolean} focusable Whether the component is to be focusable.
  969. */
  970. goog.ui.Container.prototype.setFocusable = function(focusable) {
  971. if (focusable != this.focusable_ && this.isInDocument()) {
  972. this.enableFocusHandling_(focusable);
  973. }
  974. this.focusable_ = focusable;
  975. if (this.enabled_ && this.visible_) {
  976. this.renderer_.enableTabIndex(this.getKeyEventTarget(), focusable);
  977. }
  978. };
  979. /**
  980. * Returns true if the container allows children to be focusable, false
  981. * otherwise. Only effective if the container is not focusable.
  982. * @return {boolean} Whether children should be focusable.
  983. */
  984. goog.ui.Container.prototype.isFocusableChildrenAllowed = function() {
  985. return this.allowFocusableChildren_;
  986. };
  987. /**
  988. * Sets whether the container allows children to be focusable, false
  989. * otherwise. Only effective if the container is not focusable.
  990. * @param {boolean} focusable Whether the children should be focusable.
  991. */
  992. goog.ui.Container.prototype.setFocusableChildrenAllowed = function(focusable) {
  993. this.allowFocusableChildren_ = focusable;
  994. };
  995. /**
  996. * @return {boolean} Whether highlighting a child component should also open it.
  997. */
  998. goog.ui.Container.prototype.isOpenFollowsHighlight = function() {
  999. return this.openFollowsHighlight_;
  1000. };
  1001. /**
  1002. * Sets whether highlighting a child component should also open it.
  1003. * @param {boolean} follow Whether highlighting a child component also opens it.
  1004. */
  1005. goog.ui.Container.prototype.setOpenFollowsHighlight = function(follow) {
  1006. this.openFollowsHighlight_ = follow;
  1007. };
  1008. // Highlight management.
  1009. /**
  1010. * Returns the index of the currently highlighted item (-1 if none).
  1011. * @return {number} Index of the currently highlighted item.
  1012. */
  1013. goog.ui.Container.prototype.getHighlightedIndex = function() {
  1014. return this.highlightedIndex_;
  1015. };
  1016. /**
  1017. * Highlights the item at the given 0-based index (if any). If another item
  1018. * was previously highlighted, it is un-highlighted.
  1019. * @param {number} index Index of item to highlight (-1 removes the current
  1020. * highlight).
  1021. */
  1022. goog.ui.Container.prototype.setHighlightedIndex = function(index) {
  1023. var child = this.getChildAt(index);
  1024. if (child) {
  1025. child.setHighlighted(true);
  1026. } else if (this.highlightedIndex_ > -1) {
  1027. this.getHighlighted().setHighlighted(false);
  1028. }
  1029. };
  1030. /**
  1031. * Highlights the given item if it exists and is a child of the container;
  1032. * otherwise un-highlights the currently highlighted item.
  1033. * @param {goog.ui.Control} item Item to highlight.
  1034. */
  1035. goog.ui.Container.prototype.setHighlighted = function(item) {
  1036. this.setHighlightedIndex(this.indexOfChild(item));
  1037. };
  1038. /**
  1039. * Returns the currently highlighted item (if any).
  1040. * @return {goog.ui.Control?} Highlighted item (null if none).
  1041. */
  1042. goog.ui.Container.prototype.getHighlighted = function() {
  1043. return this.getChildAt(this.highlightedIndex_);
  1044. };
  1045. /**
  1046. * Highlights the first highlightable item in the container
  1047. */
  1048. goog.ui.Container.prototype.highlightFirst = function() {
  1049. this.highlightHelper(function(index, max) {
  1050. return (index + 1) % max;
  1051. }, this.getChildCount() - 1);
  1052. };
  1053. /**
  1054. * Highlights the last highlightable item in the container.
  1055. */
  1056. goog.ui.Container.prototype.highlightLast = function() {
  1057. this.highlightHelper(function(index, max) {
  1058. index--;
  1059. return index < 0 ? max - 1 : index;
  1060. }, 0);
  1061. };
  1062. /**
  1063. * Highlights the next highlightable item (or the first if nothing is currently
  1064. * highlighted).
  1065. */
  1066. goog.ui.Container.prototype.highlightNext = function() {
  1067. this.highlightHelper(function(index, max) {
  1068. return (index + 1) % max;
  1069. }, this.highlightedIndex_);
  1070. };
  1071. /**
  1072. * Highlights the previous highlightable item (or the last if nothing is
  1073. * currently highlighted).
  1074. */
  1075. goog.ui.Container.prototype.highlightPrevious = function() {
  1076. this.highlightHelper(function(index, max) {
  1077. index--;
  1078. return index < 0 ? max - 1 : index;
  1079. }, this.highlightedIndex_);
  1080. };
  1081. /**
  1082. * Helper function that manages the details of moving the highlight among
  1083. * child controls in response to keyboard events.
  1084. * @param {function(this: goog.ui.Container, number, number) : number} fn
  1085. * Function that accepts the current and maximum indices, and returns the
  1086. * next index to check.
  1087. * @param {number} startIndex Start index.
  1088. * @return {boolean} Whether the highlight has changed.
  1089. * @protected
  1090. */
  1091. goog.ui.Container.prototype.highlightHelper = function(fn, startIndex) {
  1092. // If the start index is -1 (meaning there's nothing currently highlighted),
  1093. // try starting from the currently open item, if any.
  1094. var curIndex =
  1095. startIndex < 0 ? this.indexOfChild(this.openItem_) : startIndex;
  1096. var numItems = this.getChildCount();
  1097. curIndex = fn.call(this, curIndex, numItems);
  1098. var visited = 0;
  1099. while (visited <= numItems) {
  1100. var control = this.getChildAt(curIndex);
  1101. if (control && this.canHighlightItem(control)) {
  1102. this.setHighlightedIndexFromKeyEvent(curIndex);
  1103. return true;
  1104. }
  1105. visited++;
  1106. curIndex = fn.call(this, curIndex, numItems);
  1107. }
  1108. return false;
  1109. };
  1110. /**
  1111. * Returns whether the given item can be highlighted.
  1112. * @param {goog.ui.Control} item The item to check.
  1113. * @return {boolean} Whether the item can be highlighted.
  1114. * @protected
  1115. */
  1116. goog.ui.Container.prototype.canHighlightItem = function(item) {
  1117. return item.isVisible() && item.isEnabled() &&
  1118. item.isSupportedState(goog.ui.Component.State.HOVER);
  1119. };
  1120. /**
  1121. * Helper method that sets the highlighted index to the given index in response
  1122. * to a keyboard event. The base class implementation simply calls the
  1123. * {@link #setHighlightedIndex} method, but subclasses can override this
  1124. * behavior as needed.
  1125. * @param {number} index Index of item to highlight.
  1126. * @protected
  1127. */
  1128. goog.ui.Container.prototype.setHighlightedIndexFromKeyEvent = function(index) {
  1129. this.setHighlightedIndex(index);
  1130. };
  1131. /**
  1132. * Returns the currently open (expanded) control in the container (null if
  1133. * none).
  1134. * @return {goog.ui.Control?} The currently open control.
  1135. */
  1136. goog.ui.Container.prototype.getOpenItem = function() {
  1137. return this.openItem_;
  1138. };
  1139. /**
  1140. * Returns true if the mouse button is pressed, false otherwise.
  1141. * @return {boolean} Whether the mouse button is pressed.
  1142. */
  1143. goog.ui.Container.prototype.isMouseButtonPressed = function() {
  1144. return this.mouseButtonPressed_;
  1145. };
  1146. /**
  1147. * Sets or clears the "mouse button pressed" flag.
  1148. * @param {boolean} pressed Whether the mouse button is presed.
  1149. */
  1150. goog.ui.Container.prototype.setMouseButtonPressed = function(pressed) {
  1151. this.mouseButtonPressed_ = pressed;
  1152. };