| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A customized MenuButton for selection of items among lists. |
| 17 |
* Menu contains 'select all' and 'select none' MenuItems for selecting all and |
| 18 |
* no items by default. Other MenuItems can be added by user. |
| 19 |
* |
| 20 |
* The checkbox content fires the action events associated with the 'select all' |
| 21 |
* and 'select none' menu items. |
| 22 |
* |
| 23 |
* @see ../demos/selectionmenubutton.html |
| 24 |
*/ |
| 25 |
|
| 26 |
goog.provide('goog.ui.SelectionMenuButton'); |
| 27 |
goog.provide('goog.ui.SelectionMenuButton.SelectionState'); |
| 28 |
|
| 29 |
goog.require('goog.dom.InputType'); |
| 30 |
goog.require('goog.dom.TagName'); |
| 31 |
goog.require('goog.events.EventType'); |
| 32 |
goog.require('goog.style'); |
| 33 |
goog.require('goog.ui.Component'); |
| 34 |
goog.require('goog.ui.MenuButton'); |
| 35 |
goog.require('goog.ui.MenuItem'); |
| 36 |
goog.require('goog.ui.registry'); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* A selection menu button control. Extends {@link goog.ui.MenuButton}. |
| 42 |
* Menu contains 'select all' and 'select none' MenuItems for selecting all and |
| 43 |
* no items by default. Other MenuItems can be added by user. |
| 44 |
* |
| 45 |
* The checkbox content fires the action events associated with the 'select all' |
| 46 |
* and 'select none' menu items. |
| 47 |
* |
| 48 |
* @param {goog.ui.ButtonRenderer=} opt_renderer Renderer used to render or |
| 49 |
* decorate the menu button; defaults to {@link goog.ui.MenuButtonRenderer}. |
| 50 |
* @param {goog.ui.MenuItemRenderer=} opt_itemRenderer Optional menu item |
| 51 |
* renderer. |
| 52 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for |
| 53 |
* document interaction. |
| 54 |
* @constructor |
| 55 |
* @extends {goog.ui.MenuButton} |
| 56 |
*/ |
| 57 |
goog.ui.SelectionMenuButton = function( |
| 58 |
opt_renderer, opt_itemRenderer, opt_domHelper) { |
| 59 |
goog.ui.MenuButton.call(this, null, null, opt_renderer, opt_domHelper); |
| 60 |
this.initialItemRenderer_ = opt_itemRenderer || null; |
| 61 |
}; |
| 62 |
goog.inherits(goog.ui.SelectionMenuButton, goog.ui.MenuButton); |
| 63 |
goog.tagUnsealableClass(goog.ui.SelectionMenuButton); |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Constants for menu action types. |
| 68 |
* @enum {number} |
| 69 |
*/ |
| 70 |
goog.ui.SelectionMenuButton.SelectionState = { |
| 71 |
ALL: 0, |
| 72 |
SOME: 1, |
| 73 |
NONE: 2 |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Select button state |
| 79 |
* @type {goog.ui.SelectionMenuButton.SelectionState} |
| 80 |
* @protected |
| 81 |
*/ |
| 82 |
goog.ui.SelectionMenuButton.prototype.selectionState = |
| 83 |
goog.ui.SelectionMenuButton.SelectionState.NONE; |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* Item renderer used for the first 2 items, 'select all' and 'select none'. |
| 88 |
* @type {goog.ui.MenuItemRenderer} |
| 89 |
* @private |
| 90 |
*/ |
| 91 |
goog.ui.SelectionMenuButton.prototype.initialItemRenderer_; |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Enables button and embedded checkbox. |
| 96 |
* @param {boolean} enable Whether to enable or disable the button. |
| 97 |
* @override |
| 98 |
*/ |
| 99 |
goog.ui.SelectionMenuButton.prototype.setEnabled = function(enable) { |
| 100 |
goog.ui.SelectionMenuButton.base(this, 'setEnabled', enable); |
| 101 |
this.setCheckboxEnabled(enable); |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Enables the embedded checkbox. |
| 107 |
* @param {boolean} enable Whether to enable or disable the checkbox. |
| 108 |
* @protected |
| 109 |
*/ |
| 110 |
goog.ui.SelectionMenuButton.prototype.setCheckboxEnabled = function(enable) { |
| 111 |
this.getCheckboxElement().disabled = !enable; |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
/** @override */ |
| 116 |
goog.ui.SelectionMenuButton.prototype.handleMouseDown = function(e) { |
| 117 |
if (!this.getDomHelper().contains( |
| 118 |
this.getCheckboxElement(), |
| 119 |
/** @type {Element} */ (e.target))) { |
| 120 |
goog.ui.SelectionMenuButton.superClass_.handleMouseDown.call(this, e); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Gets the checkbox element. Needed because if decorating html, getContent() |
| 127 |
* may include and comment/text elements in addition to the input element. |
| 128 |
* @return {Element} Checkbox. |
| 129 |
* @protected |
| 130 |
*/ |
| 131 |
goog.ui.SelectionMenuButton.prototype.getCheckboxElement = function() { |
| 132 |
var elements = this.getDomHelper().getElementsByTagNameAndClass( |
| 133 |
goog.dom.TagName.INPUT, |
| 134 |
goog.getCssName('goog-selectionmenubutton-checkbox'), |
| 135 |
this.getContentElement()); |
| 136 |
return elements[0]; |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* Checkbox click handler. |
| 142 |
* @param {goog.events.BrowserEvent} e Checkbox click event. |
| 143 |
* @protected |
| 144 |
*/ |
| 145 |
goog.ui.SelectionMenuButton.prototype.handleCheckboxClick = function(e) { |
| 146 |
if (this.selectionState == goog.ui.SelectionMenuButton.SelectionState.NONE) { |
| 147 |
this.setSelectionState(goog.ui.SelectionMenuButton.SelectionState.ALL); |
| 148 |
if (this.getItemAt(0)) { |
| 149 |
this.getItemAt(0).dispatchEvent( // 'All' item |
| 150 |
goog.ui.Component.EventType.ACTION); |
| 151 |
} |
| 152 |
} else { |
| 153 |
this.setSelectionState(goog.ui.SelectionMenuButton.SelectionState.NONE); |
| 154 |
if (this.getItemAt(1)) { |
| 155 |
this.getItemAt(1).dispatchEvent( // 'None' item |
| 156 |
goog.ui.Component.EventType.ACTION); |
| 157 |
} |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Menu action handler to update checkbox checked state. |
| 164 |
* @param {goog.events.Event} e Menu action event. |
| 165 |
* @private |
| 166 |
*/ |
| 167 |
goog.ui.SelectionMenuButton.prototype.handleMenuAction_ = function(e) { |
| 168 |
if (e.target.getModel() == goog.ui.SelectionMenuButton.SelectionState.ALL) { |
| 169 |
this.setSelectionState(goog.ui.SelectionMenuButton.SelectionState.ALL); |
| 170 |
} else { |
| 171 |
this.setSelectionState(goog.ui.SelectionMenuButton.SelectionState.NONE); |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
/** |
| 177 |
* Set up events related to the menu items. |
| 178 |
* @private |
| 179 |
*/ |
| 180 |
goog.ui.SelectionMenuButton.prototype.addMenuEvent_ = function() { |
| 181 |
if (this.getItemAt(0) && this.getItemAt(1)) { |
| 182 |
this.getHandler().listen( |
| 183 |
this.getMenu(), goog.ui.Component.EventType.ACTION, |
| 184 |
this.handleMenuAction_); |
| 185 |
this.getItemAt(0).setModel(goog.ui.SelectionMenuButton.SelectionState.ALL); |
| 186 |
this.getItemAt(1).setModel(goog.ui.SelectionMenuButton.SelectionState.NONE); |
| 187 |
} |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Set up events related to the checkbox. |
| 193 |
* @protected |
| 194 |
*/ |
| 195 |
goog.ui.SelectionMenuButton.prototype.addCheckboxEvent = function() { |
| 196 |
this.getHandler().listen( |
| 197 |
this.getCheckboxElement(), goog.events.EventType.CLICK, |
| 198 |
this.handleCheckboxClick); |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
/** |
| 203 |
* Adds the checkbox to the button, and adds 2 items to the menu corresponding |
| 204 |
* to 'select all' and 'select none'. |
| 205 |
* @override |
| 206 |
* @protected |
| 207 |
*/ |
| 208 |
goog.ui.SelectionMenuButton.prototype.createDom = function() { |
| 209 |
goog.ui.SelectionMenuButton.superClass_.createDom.call(this); |
| 210 |
|
| 211 |
this.createCheckbox(); |
| 212 |
|
| 213 |
/** @desc Text for 'All' button, used to select all items in a list. */ |
| 214 |
var MSG_SELECTIONMENUITEM_ALL = goog.getMsg('All'); |
| 215 |
/** @desc Text for 'None' button, used to unselect all items in a list. */ |
| 216 |
var MSG_SELECTIONMENUITEM_NONE = goog.getMsg('None'); |
| 217 |
|
| 218 |
var itemAll = new goog.ui.MenuItem( |
| 219 |
MSG_SELECTIONMENUITEM_ALL, null, this.getDomHelper(), |
| 220 |
this.initialItemRenderer_); |
| 221 |
var itemNone = new goog.ui.MenuItem( |
| 222 |
MSG_SELECTIONMENUITEM_NONE, null, this.getDomHelper(), |
| 223 |
this.initialItemRenderer_); |
| 224 |
this.addItem(itemAll); |
| 225 |
this.addItem(itemNone); |
| 226 |
|
| 227 |
this.addCheckboxEvent(); |
| 228 |
this.addMenuEvent_(); |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
/** |
| 233 |
* Creates and adds the checkbox to the button. |
| 234 |
* @protected |
| 235 |
*/ |
| 236 |
goog.ui.SelectionMenuButton.prototype.createCheckbox = function() { |
| 237 |
var checkbox = this.getDomHelper().createElement(goog.dom.TagName.INPUT); |
| 238 |
checkbox.type = goog.dom.InputType.CHECKBOX; |
| 239 |
checkbox.className = goog.getCssName('goog-selectionmenubutton-checkbox'); |
| 240 |
this.setContent(checkbox); |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
/** @override */ |
| 245 |
goog.ui.SelectionMenuButton.prototype.decorateInternal = function(element) { |
| 246 |
goog.ui.SelectionMenuButton.superClass_.decorateInternal.call(this, element); |
| 247 |
this.addCheckboxEvent(); |
| 248 |
this.addMenuEvent_(); |
| 249 |
}; |
| 250 |
|
| 251 |
|
| 252 |
/** @override */ |
| 253 |
goog.ui.SelectionMenuButton.prototype.setMenu = function(menu) { |
| 254 |
goog.ui.SelectionMenuButton.superClass_.setMenu.call(this, menu); |
| 255 |
this.addMenuEvent_(); |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* Set selection state and update checkbox. |
| 261 |
* @param {goog.ui.SelectionMenuButton.SelectionState} state Selection state. |
| 262 |
*/ |
| 263 |
goog.ui.SelectionMenuButton.prototype.setSelectionState = function(state) { |
| 264 |
if (this.selectionState != state) { |
| 265 |
var checkbox = this.getCheckboxElement(); |
| 266 |
if (state == goog.ui.SelectionMenuButton.SelectionState.ALL) { |
| 267 |
checkbox.checked = true; |
| 268 |
goog.style.setOpacity(checkbox, 1); |
| 269 |
} else if (state == goog.ui.SelectionMenuButton.SelectionState.SOME) { |
| 270 |
checkbox.checked = true; |
| 271 |
// TODO(user): Get UX help to style this |
| 272 |
goog.style.setOpacity(checkbox, 0.5); |
| 273 |
} else { // NONE |
| 274 |
checkbox.checked = false; |
| 275 |
goog.style.setOpacity(checkbox, 1); |
| 276 |
} |
| 277 |
this.selectionState = state; |
| 278 |
} |
| 279 |
}; |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* Get selection state. |
| 284 |
* @return {goog.ui.SelectionMenuButton.SelectionState} Selection state. |
| 285 |
*/ |
| 286 |
goog.ui.SelectionMenuButton.prototype.getSelectionState = function() { |
| 287 |
return this.selectionState; |
| 288 |
}; |
| 289 |
|
| 290 |
|
| 291 |
// Register a decorator factory function for goog.ui.SelectionMenuButton. |
| 292 |
goog.ui.registry.setDecoratorByClassName( |
| 293 |
goog.getCssName('goog-selectionmenubutton-button'), |
| 294 |
function() { return new goog.ui.SelectionMenuButton(); }); |