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;
51 * Create a "click-trap" div covering the entire document, but below the
52 * menu in the z-order. This ensures the the menu can be closed by clicking
53 * anywhere. Note that adding this event handler to <body> is not enough,
54 * because elements can prevent event propagation; specifically, the client
55 * plugin element does this.
60 this.clickTrap_ = /** @type {HTMLElement} */ (document.createElement('div'));
61 this.clickTrap_.classList.add('menu-button-click-trap');
63 /** @type {remoting.MenuButton} */
66 var closeHandler = function() {
67 that.button_.classList.remove(remoting.MenuButton.BUTTON_ACTIVE_CLASS_);
68 container.removeChild(that.clickTrap_);
74 var onClick = function() {
78 that.button_.classList.add(remoting.MenuButton.BUTTON_ACTIVE_CLASS_);
79 container.appendChild(that.clickTrap_);
82 this.button_.addEventListener('click', onClick, false);
83 this.clickTrap_.addEventListener('click', closeHandler, false);
84 this.menu_.addEventListener('click', closeHandler, false);
88 * @return {HTMLElement} The button that activates the menu.
90 remoting.MenuButton.prototype.button = function() {
95 * @return {HTMLElement} The menu.
97 remoting.MenuButton.prototype.menu = function() {
102 * Set or unset the selected state of an <li> menu item.
103 * @param {Element} item The menu item to update.
104 * @param {boolean} selected True to select the item, false to deselect it.
105 * @return {void} Nothing.
107 remoting.MenuButton.select = function(item, selected) {
109 item.classList.add('selected');
111 item.classList.remove('selected');
115 /** @const @private */
116 remoting.MenuButton.BUTTON_ACTIVE_CLASS_ = 'active';