Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / remoting / webapp / menu_button.js
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  * @param {function():void=} opt_onHide Optional callback after before the
22  *     menu is hidden.
23  */
24 remoting.MenuButton = function(container, opt_onShow, opt_onHide) {
25   /**
26    * @type {HTMLElement}
27    * @private
28    */
29   this.button_ = /** @type {HTMLElement} */
30       (container.querySelector('button,.menu-button-activator'));
31
32   /**
33    * @type {HTMLElement}
34    * @private
35    */
36   this.menu_ = /** @type {HTMLElement} */ (container.querySelector('ul'));
37
38   /**
39    * @type {undefined|function():void}
40    * @private
41    */
42   this.onShow_ = opt_onShow;
43
44   /**
45    * @type {undefined|function():void}
46    * @private
47    */
48   this.onHide_ = opt_onHide;
49
50   /** @type {remoting.MenuButton} */
51   var that = this;
52
53   /**
54    * @type {function(Event):void}
55    * @private
56    */
57   var closeHandler = function(event) {
58     that.button_.classList.remove(remoting.MenuButton.BUTTON_ACTIVE_CLASS_);
59     document.body.removeEventListener('click', closeHandler, false);
60     if (that.onHide_) {
61       that.onHide_();
62     }
63   };
64
65   /**
66    * @type {function(Event):void}
67    * @private
68    */
69   var onClick = function(event) {
70     if (that.onShow_) {
71       that.onShow_();
72     }
73     that.button_.classList.add(remoting.MenuButton.BUTTON_ACTIVE_CLASS_);
74     document.body.addEventListener('click', closeHandler, false);
75     event.stopPropagation();
76   };
77
78   this.button_.addEventListener('click', onClick, false);
79 };
80
81 /**
82  * @return {HTMLElement} The button that activates the menu.
83  */
84 remoting.MenuButton.prototype.button = function() {
85   return this.button_;
86 };
87
88 /**
89  * @return {HTMLElement} The menu.
90  */
91 remoting.MenuButton.prototype.menu = function() {
92   return this.menu_;
93 };
94
95 /**
96  * Set or unset the selected state of an <li> menu item.
97  * @param {Element} item The menu item to update.
98  * @param {boolean} selected True to select the item, false to deselect it.
99  * @return {void} Nothing.
100  */
101 remoting.MenuButton.select = function(item, selected) {
102   if (selected) {
103     /** @type {DOMTokenList} */(item.classList).add('selected');
104   } else {
105     /** @type {DOMTokenList} */(item.classList).remove('selected');
106   }
107 };
108
109 /** @const @private */
110 remoting.MenuButton.BUTTON_ACTIVE_CLASS_ = 'active';