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
22 remoting.MenuButton = function(container, opt_onShow) {
27 this.button_ = /** @type {HTMLElement} */ (container.querySelector('button'));
30 * @type {undefined|function():void}
33 this.onShow_ = opt_onShow;
35 /** @type {remoting.MenuButton} */
38 // Create event handlers to show and hide the menu, attached to the button
39 // and the document <html> tag, respectively. These handlers are added and
40 // removed depending on the state of the menu. To prevent both triggering
41 // for one click, they are added by a timer.
43 * @type {function(Event):void}
46 this.onClick_ = function(event) {
50 that.button_.classList.add(remoting.MenuButton.BUTTON_ACTIVE_CLASS_);
51 that.button_.removeEventListener('click', that.onClick_, false);
54 // Attach the click handler to the <html> node so that it includes
55 // the document area outside the plugin, which is not covered by
57 var htmlNode = document.body.parentNode;
58 htmlNode.addEventListener('click', that.closeHandler_, true);
64 * @type {function(Event):void}
67 this.closeHandler_ = function(event) {
68 that.button_.classList.remove(remoting.MenuButton.BUTTON_ACTIVE_CLASS_);
69 document.body.removeEventListener('click', that.closeHandler_, true);
72 that.button_.addEventListener('click', that.onClick_, false);
77 this.button_.addEventListener('click', this.onClick_, false);
81 * Set or unset the selected state of an <li> menu item.
82 * @param {HTMLElement} item The menu item to update.
83 * @param {boolean} selected True to select the item, false to deselect it.
84 * @return {void} Nothing.
86 remoting.MenuButton.select = function(item, selected) {
88 item.classList.add('selected');
90 item.classList.remove('selected');
94 /** @const @private */
95 remoting.MenuButton.BUTTON_ACTIVE_CLASS_ = 'active';