diff options
author | jamiewalch@google.com <jamiewalch@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-29 00:28:01 +0000 |
---|---|---|
committer | jamiewalch@google.com <jamiewalch@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-29 00:28:01 +0000 |
commit | d411762a5e07707e85c07248b82631ed12822cfd (patch) | |
tree | ecf55bfbf31dccef4f2bbb8382855994e205ec1c /remoting/webapp/menu_button.js | |
parent | fecd0ba29d3b380fee34ede8b8c709fefd804025 (diff) | |
download | chromium_src-d411762a5e07707e85c07248b82631ed12822cfd.zip chromium_src-d411762a5e07707e85c07248b82631ed12822cfd.tar.gz chromium_src-d411762a5e07707e85c07248b82631ed12822cfd.tar.bz2 |
Added screen options menu.
BUG=119092
TEST=Manual
Review URL: https://chromiumcodereview.appspot.com/9884001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@129535 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/webapp/menu_button.js')
-rw-r--r-- | remoting/webapp/menu_button.js | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/remoting/webapp/menu_button.js b/remoting/webapp/menu_button.js new file mode 100644 index 0000000..03ad39e --- /dev/null +++ b/remoting/webapp/menu_button.js @@ -0,0 +1,91 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * @fileoverview + * Class representing a menu button and its associated menu items. + */ + +'use strict'; + +/** @suppress {duplicate} */ +var remoting = remoting || {}; + +/** + * @constructor + * @param {Element} container The element containing the <button> and <ul> + * elements comprising the menu. It should have the "menu-button" class. + * @param {function():void=} opt_onShow Optional callback invoked before the + * menu is shown. + */ +remoting.MenuButton = function(container, opt_onShow) { + /** + * @type {HTMLElement} + * @private + */ + this.button_ = /** @type {HTMLElement} */ (container.querySelector('button')); + + /** + * @type {undefined|function():void} + * @private + */ + this.onShow_ = opt_onShow; + + /** @type {remoting.MenuButton} */ + var that = this; + + // Create event handlers to show and hide the menu, attached to the button + // and the document <html> tag, respectively. These handlers are added and + // removed depending on the state of the menu. To prevent both triggering + // for one click, they are added by a timer. + /** + * @type {function(Event):void} + * @private + */ + this.onClick_ = function(event) { + if (that.onShow_) { + that.onShow_(); + } + that.button_.classList.add(remoting.MenuButton.BUTTON_ACTIVE_CLASS_); + that.button_.removeEventListener('click', that.onClick_, false); + window.setTimeout( + function() { + document.all[0].addEventListener('click', that.closeHandler_, true); + }, + 100); + }; + + /** + * @type {function(Event):void} + * @private + */ + this.closeHandler_ = function(event) { + that.button_.classList.remove(remoting.MenuButton.BUTTON_ACTIVE_CLASS_); + document.all[0].removeEventListener('click', that.closeHandler_, true); + window.setTimeout( + function() { + that.button_.addEventListener('click', that.onClick_, false); + }, + 100); + }; + + this.button_.addEventListener('click', this.onClick_, false); +}; + +/** + * Set or unset the selected state of an <li> menu item. + * @param {HTMLElement} item The menu item to update. + * @param {boolean} selected True to select the item, false to deselect it. + * @return {void} Nothing. + */ +remoting.MenuButton.select = function(item, selected) { + if (selected) { + item.classList.add('selected'); + } else { + item.classList.remove('selected'); + } +} + +/** @const @private */ +remoting.MenuButton.BUTTON_ACTIVE_CLASS_ = 'active'; |