Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * Class representing a menu button and its associated menu items. | 7 * Class representing a menu button and its associated menu items. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 'use strict'; | 10 'use strict'; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 * @private | 40 * @private |
| 41 */ | 41 */ |
| 42 this.onShow_ = opt_onShow; | 42 this.onShow_ = opt_onShow; |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * @type {undefined|function():void} | 45 * @type {undefined|function():void} |
| 46 * @private | 46 * @private |
| 47 */ | 47 */ |
| 48 this.onHide_ = opt_onHide; | 48 this.onHide_ = opt_onHide; |
| 49 | 49 |
| 50 /** | |
| 51 * @type {HTMLElement} | |
| 52 * @private | |
| 53 */ | |
| 54 this.clickTrap_ = /** @type {HTMLElement} */ (document.createElement('div')); | |
| 55 this.clickTrap_.classList.add('menu-button-click-trap'); | |
|
Wez
2014/09/23 16:36:49
I think the clickTrap needs some documentation, e.
Jamie
2014/09/23 21:06:34
Done.
| |
| 56 | |
| 50 /** @type {remoting.MenuButton} */ | 57 /** @type {remoting.MenuButton} */ |
| 51 var that = this; | 58 var that = this; |
| 52 | 59 |
| 53 /** | 60 var closeHandler = function() { |
| 54 * @type {function(Event):void} | |
| 55 * @private | |
| 56 */ | |
| 57 var closeHandler = function(event) { | |
| 58 that.button_.classList.remove(remoting.MenuButton.BUTTON_ACTIVE_CLASS_); | 61 that.button_.classList.remove(remoting.MenuButton.BUTTON_ACTIVE_CLASS_); |
| 59 document.body.removeEventListener('click', closeHandler, false); | 62 container.removeChild(that.clickTrap_); |
| 60 if (that.onHide_) { | 63 if (that.onHide_) { |
| 61 that.onHide_(); | 64 that.onHide_(); |
| 62 } | 65 } |
| 63 }; | 66 }; |
| 64 | 67 |
| 65 /** | 68 var onClick = function() { |
| 66 * @type {function(Event):void} | |
| 67 * @private | |
| 68 */ | |
| 69 var onClick = function(event) { | |
| 70 if (that.onShow_) { | 69 if (that.onShow_) { |
| 71 that.onShow_(); | 70 that.onShow_(); |
| 72 } | 71 } |
| 73 that.button_.classList.add(remoting.MenuButton.BUTTON_ACTIVE_CLASS_); | 72 that.button_.classList.add(remoting.MenuButton.BUTTON_ACTIVE_CLASS_); |
| 74 document.body.addEventListener('click', closeHandler, false); | 73 container.appendChild(that.clickTrap_); |
| 75 event.stopPropagation(); | |
| 76 }; | 74 }; |
| 77 | 75 |
| 78 this.button_.addEventListener('click', onClick, false); | 76 this.button_.addEventListener('click', onClick, false); |
| 77 this.clickTrap_.addEventListener('click', closeHandler, false); | |
| 78 this.menu_.addEventListener('click', closeHandler, false); | |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * @return {HTMLElement} The button that activates the menu. | 82 * @return {HTMLElement} The button that activates the menu. |
| 83 */ | 83 */ |
| 84 remoting.MenuButton.prototype.button = function() { | 84 remoting.MenuButton.prototype.button = function() { |
| 85 return this.button_; | 85 return this.button_; |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 /** | 88 /** |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 101 remoting.MenuButton.select = function(item, selected) { | 101 remoting.MenuButton.select = function(item, selected) { |
| 102 if (selected) { | 102 if (selected) { |
| 103 /** @type {DOMTokenList} */(item.classList).add('selected'); | 103 /** @type {DOMTokenList} */(item.classList).add('selected'); |
| 104 } else { | 104 } else { |
| 105 /** @type {DOMTokenList} */(item.classList).remove('selected'); | 105 /** @type {DOMTokenList} */(item.classList).remove('selected'); |
| 106 } | 106 } |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 /** @const @private */ | 109 /** @const @private */ |
| 110 remoting.MenuButton.BUTTON_ACTIVE_CLASS_ = 'active'; | 110 remoting.MenuButton.BUTTON_ACTIVE_CLASS_ = 'active'; |
| OLD | NEW |