1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * Class representing a menu button and its associated menu items.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
17 * @param {Element} container The element containing the <button> and <ul>
18 * elements comprising the menu. It should have the "menu-button" class.
19 * @param {function():void=} opt_onShow Optional callback invoked before the
21 * @param {function():void=} opt_onHide Optional callback after before the
24 remoting.MenuButton = function(container, opt_onShow, opt_onHide) {
29 this.button_ = /** @type {HTMLElement} */
30 (container.querySelector('button,.menu-button-activator'));
36 this.menu_ = /** @type {HTMLElement} */ (container.querySelector('ul'));
39 * @type {undefined|function():void}
42 this.onShow_ = opt_onShow;
45 * @type {undefined|function():void}
48 this.onHide_ = opt_onHide;
50 /** @type {remoting.MenuButton} */
54 * @type {function(Event):void}
57 var closeHandler = function(event) {
58 that.button_.classList.remove(remoting.MenuButton.BUTTON_ACTIVE_CLASS_);
59 document.body.removeEventListener('click', closeHandler, false);
66 * @type {function(Event):void}
69 var onClick = function(event) {
73 that.button_.classList.toggle(remoting.MenuButton.BUTTON_ACTIVE_CLASS_);
74 document.body.addEventListener('click', closeHandler, false);
75 event.stopPropagation();
78 this.button_.addEventListener('click', onClick, false);
82 * @return {HTMLElement} The button that activates the menu.
84 remoting.MenuButton.prototype.button = function() {
89 * @return {HTMLElement} The menu.
91 remoting.MenuButton.prototype.menu = function() {
96 * Set or unset the selected state of an <li> menu item.
97 * @param {Element} item The menu item to update.
98 * @param {boolean} selected True to select the item, false to deselect it.
99 * @return {void} Nothing.
101 remoting.MenuButton.select = function(item, selected) {
103 /** @type {DOMTokenList} */(item.classList).add('selected');
105 /** @type {DOMTokenList} */(item.classList).remove('selected');
109 /** @const @private */
110 remoting.MenuButton.BUTTON_ACTIVE_CLASS_ = 'active';