Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 * Class representing a menu button and its associated menu items. | |
| 8 */ | |
| 9 | |
| 10 'use strict'; | |
| 11 | |
| 12 /** @suppress {duplicate} */ | |
| 13 var remoting = remoting || {}; | |
| 14 | |
| 15 /** | |
| 16 * @constructor | |
| 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 | |
| 20 * menu is shown. | |
| 21 */ | |
| 22 remoting.MenuButton = function(container, opt_onShow) { | |
| 23 /** | |
| 24 * @type {HTMLElement} | |
| 25 * @private | |
| 26 */ | |
| 27 this.button_ = /** @type {HTMLElement} */ (container.querySelector('button')); | |
| 28 | |
| 29 /** | |
| 30 * @type {undefined|function():void} | |
| 31 * @private | |
| 32 */ | |
| 33 this.onShow_ = opt_onShow; | |
| 34 | |
| 35 /** @type {remoting.MenuButton} */ | |
| 36 var that = this; | |
| 37 | |
| 38 // Create event handlers to show and hide the menu, attached to the button | |
| 39 // and the document body, respectively. These handlers are added and removed | |
| 40 // depending on the state of the menu. To prevent both triggering for one | |
| 41 // click, they are added by a timer. | |
|
simonmorris
2012/03/28 22:55:02
Would it be simpler to leave the handlers permanen
Jamie
2012/03/28 23:47:46
I don't want to leave the <html> handler permanent
| |
| 42 /** | |
| 43 * @type {function(Event):void} | |
| 44 * @private | |
| 45 */ | |
| 46 this.onClick_ = function(event) { | |
| 47 if (that.onShow_) { | |
| 48 that.onShow_(); | |
| 49 } | |
| 50 that.button_.classList.add(remoting.MenuButton.BUTTON_ACTIVE_CLASS_); | |
| 51 that.button_.removeEventListener('click', that.onClick_, false); | |
| 52 window.setTimeout( | |
| 53 function() { | |
| 54 document.body.addEventListener('click', that.closeHandler_, false); | |
| 55 }, | |
| 56 100); | |
| 57 }; | |
| 58 | |
| 59 /** | |
| 60 * @type {function(Event):void} | |
| 61 * @private | |
| 62 */ | |
| 63 this.closeHandler_ = function(event) { | |
| 64 that.button_.classList.remove(remoting.MenuButton.BUTTON_ACTIVE_CLASS_); | |
| 65 document.body.removeEventListener('click', that.closeHandler_, false); | |
| 66 window.setTimeout( | |
| 67 function() { | |
| 68 that.button_.addEventListener('click', that.onClick_, false); | |
| 69 }, | |
| 70 100); | |
| 71 }; | |
| 72 | |
| 73 this.button_.addEventListener('click', this.onClick_, false); | |
| 74 }; | |
| 75 | |
| 76 /** @const @private */ | |
| 77 remoting.MenuButton.BUTTON_ACTIVE_CLASS_ = 'active'; | |
| 78 | |
| 79 /** @const */ | |
| 80 remoting.MenuButton.ITEM_SELECTED_CLASS = 'selected'; | |
| OLD | NEW |