1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Zippy widget implementation.
*
* @author eae@google.com (Emil A Eklund)
* @see ../demos/zippy.html
*/
goog.provide('goog.ui.Zippy');
goog.provide('goog.ui.Zippy.Events');
goog.provide('goog.ui.ZippyEvent');
goog.require('goog.a11y.aria');
goog.require('goog.a11y.aria.Role');
goog.require('goog.a11y.aria.State');
goog.require('goog.dom');
goog.require('goog.dom.classlist');
goog.require('goog.events.Event');
goog.require('goog.events.EventHandler');
goog.require('goog.events.EventTarget');
goog.require('goog.events.EventType');
goog.require('goog.events.KeyCodes');
goog.require('goog.events.KeyHandler');
goog.require('goog.style');
/**
* Zippy widget. Expandable/collapsible container, clicking the header toggles
* the visibility of the content.
*
* @extends {goog.events.EventTarget}
* @param {Element|string|null} header Header element, either element
* reference, string id or null if no header exists.
* @param {Element|string|function():Element=} opt_content Content element
* (if any), either element reference or string id. If skipped, the caller
* should handle the TOGGLE event in its own way. If a function is passed,
* then if will be called to create the content element the first time the
* zippy is expanded.
* @param {boolean=} opt_expanded Initial expanded/visibility state. If
* undefined, attempts to infer the state from the DOM. Setting visibility
* using one of the standard Soy templates guarantees correct inference.
* @param {Element|string=} opt_expandedHeader Element to use as the header when
* the zippy is expanded.
* @param {goog.dom.DomHelper=} opt_domHelper An optional DOM helper.
* @param {goog.a11y.aria.Role<string>=} opt_role ARIA role, default TAB.
* @constructor
*/
goog.ui.Zippy = function(
header, opt_content, opt_expanded, opt_expandedHeader, opt_domHelper,
opt_role) {
goog.ui.Zippy.base(this, 'constructor');
/**
* DomHelper used to interact with the document, allowing components to be
* created in a different window.
* @type {!goog.dom.DomHelper}
* @private
*/
this.dom_ = opt_domHelper || goog.dom.getDomHelper();
/**
* Header element or null if no header exists.
* @type {Element}
* @private
*/
this.elHeader_ = this.dom_.getElement(header) || null;
/**
* When present, the header to use when the zippy is expanded.
* @type {Element}
* @private
*/
this.elExpandedHeader_ = this.dom_.getElement(opt_expandedHeader || null);
/**
* Function that will create the content element, or false if there is no such
* function.
* @type {?function():Element}
* @private
*/
this.lazyCreateFunc_ = goog.isFunction(opt_content) ? opt_content : null;
/**
* ARIA role.
* @type {goog.a11y.aria.Role<string>}
* @private
*/
this.role_ = opt_role || goog.a11y.aria.Role.TAB;
/**
* Content element.
* @type {Element}
* @private
*/
this.elContent_ = this.lazyCreateFunc_ || !opt_content ?
null :
this.dom_.getElement(/** @type {!Element} */ (opt_content));
/**
* Expanded state.
* @type {boolean}
* @private
*/
this.expanded_ = opt_expanded == true;
if (!goog.isDef(opt_expanded) && !this.lazyCreateFunc_) {
// For the dual caption case, we can get expanded_ from the visibility of
// the expandedHeader. For the single-caption case, we use the
// presence/absence of the relevant class. Using one of the standard Soy
// templates guarantees that this will work.
if (this.elExpandedHeader_) {
this.expanded_ = goog.style.isElementShown(this.elExpandedHeader_);
} else if (this.elHeader_) {
this.expanded_ = goog.dom.classlist.contains(
this.elHeader_, goog.getCssName('goog-zippy-expanded'));
}
}
/**
* A keyboard events handler. If there are two headers it is shared for both.
* @type {goog.events.EventHandler<!goog.ui.Zippy>}
* @private
*/
this.keyboardEventHandler_ = new goog.events.EventHandler(this);
/**
* The keyhandler used for listening on most key events. This takes care of
* abstracting away some of the browser differences.
* @private {!goog.events.KeyHandler}
*/
this.keyHandler_ = new goog.events.KeyHandler();
/**
* A mouse events handler. If there are two headers it is shared for both.
* @type {goog.events.EventHandler<!goog.ui.Zippy>}
* @private
*/
this.mouseEventHandler_ = new goog.events.EventHandler(this);
var self = this;
function addHeaderEvents(el) {
if (el) {
el.tabIndex = 0;
goog.a11y.aria.setRole(el, self.getAriaRole());
goog.dom.classlist.add(el, goog.getCssName('goog-zippy-header'));
self.enableMouseEventsHandling_(el);
self.enableKeyboardEventsHandling_(el);
}
}
addHeaderEvents(this.elHeader_);
addHeaderEvents(this.elExpandedHeader_);
// initialize based on expanded state
this.setExpanded(this.expanded_);
};
goog.inherits(goog.ui.Zippy, goog.events.EventTarget);
goog.tagUnsealableClass(goog.ui.Zippy);
/**
* Constants for event names
*
* @const
*/
goog.ui.Zippy.Events = {
// Zippy will dispatch an ACTION event for user interaction. Mimics
// {@code goog.ui.Controls#performActionInternal} by first changing
// the toggle state and then dispatching an ACTION event.
ACTION: 'action',
// Zippy state is toggled from collapsed to expanded or vice versa.
TOGGLE: 'toggle'
};
/**
* Whether to listen for and handle mouse events; defaults to true.
* @type {boolean}
* @private
*/
goog.ui.Zippy.prototype.handleMouseEvents_ = true;
/**
* Whether to listen for and handle key events; defaults to true.
* @type {boolean}
* @private
*/
goog.ui.Zippy.prototype.handleKeyEvents_ = true;
/** @override */
goog.ui.Zippy.prototype.disposeInternal = function() {
goog.ui.Zippy.base(this, 'disposeInternal');
goog.dispose(this.keyboardEventHandler_);
goog.dispose(this.keyHandler_);
goog.dispose(this.mouseEventHandler_);
};
/**
* @return {goog.a11y.aria.Role} The ARIA role to be applied to Zippy element.
*/
goog.ui.Zippy.prototype.getAriaRole = function() {
return this.role_;
};
/**
* @return {HTMLElement} The content element.
*/
goog.ui.Zippy.prototype.getContentElement = function() {
return /** @type {!HTMLElement} */ (this.elContent_);
};
/**
* @return {Element} The visible header element.
*/
goog.ui.Zippy.prototype.getVisibleHeaderElement = function() {
var expandedHeader = this.elExpandedHeader_;
return expandedHeader && goog.style.isElementShown(expandedHeader) ?
expandedHeader :
this.elHeader_;
};
/**
* Expands content pane.
*/
goog.ui.Zippy.prototype.expand = function() {
this.setExpanded(true);
};
/**
* Collapses content pane.
*/
goog.ui.Zippy.prototype.collapse = function() {
this.setExpanded(false);
};
/**
* Toggles expanded state.
*/
goog.ui.Zippy.prototype.toggle = function() {
this.setExpanded(!this.expanded_);
};
/**
* Sets expanded state.
*
* @param {boolean} expanded Expanded/visibility state.
*/
goog.ui.Zippy.prototype.setExpanded = function(expanded) {
if (this.elContent_) {
// Hide the element, if one is provided.
goog.style.setElementShown(this.elContent_, expanded);
} else if (expanded && this.lazyCreateFunc_) {
// Assume that when the element is not hidden upon creation.
this.elContent_ = this.lazyCreateFunc_();
}
if (this.elContent_) {
goog.dom.classlist.add(
this.elContent_, goog.getCssName('goog-zippy-content'));
}
if (this.elExpandedHeader_) {
// Hide the show header and show the hide one.
goog.style.setElementShown(this.elHeader_, !expanded);
goog.style.setElementShown(this.elExpandedHeader_, expanded);
} else {
// Update header image, if any.
this.updateHeaderClassName(expanded);
}
this.setExpandedInternal(expanded);
// Fire toggle event
this.dispatchEvent(
new goog.ui.ZippyEvent(
goog.ui.Zippy.Events.TOGGLE, this, this.expanded_));
};
/**
* Sets expanded internal state.
*
* @param {boolean} expanded Expanded/visibility state.
* @protected
*/
goog.ui.Zippy.prototype.setExpandedInternal = function(expanded) {
this.expanded_ = expanded;
};
/**
* @return {boolean} Whether the zippy is expanded.
*/
goog.ui.Zippy.prototype.isExpanded = function() {
return this.expanded_;
};
/**
* Updates the header element's className and ARIA (accessibility) EXPANDED
* state.
*
* @param {boolean} expanded Expanded/visibility state.
* @protected
*/
goog.ui.Zippy.prototype.updateHeaderClassName = function(expanded) {
if (this.elHeader_) {
goog.dom.classlist.enable(
this.elHeader_, goog.getCssName('goog-zippy-expanded'), expanded);
goog.dom.classlist.enable(
this.elHeader_, goog.getCssName('goog-zippy-collapsed'), !expanded);
goog.a11y.aria.setState(
this.elHeader_, goog.a11y.aria.State.EXPANDED, expanded);
}
};
/**
* @return {boolean} Whether the Zippy handles its own key events.
*/
goog.ui.Zippy.prototype.isHandleKeyEvents = function() {
return this.handleKeyEvents_;
};
/**
* @return {boolean} Whether the Zippy handles its own mouse events.
*/
goog.ui.Zippy.prototype.isHandleMouseEvents = function() {
return this.handleMouseEvents_;
};
/**
* Sets whether the Zippy handles it's own keyboard events.
* @param {boolean} enable Whether the Zippy handles keyboard events.
*/
goog.ui.Zippy.prototype.setHandleKeyboardEvents = function(enable) {
if (this.handleKeyEvents_ != enable) {
this.handleKeyEvents_ = enable;
if (enable) {
this.enableKeyboardEventsHandling_(this.elHeader_);
this.enableKeyboardEventsHandling_(this.elExpandedHeader_);
} else {
this.keyboardEventHandler_.removeAll();
this.keyHandler_.detach();
}
}
};
/**
* Sets whether the Zippy handles it's own mouse events.
* @param {boolean} enable Whether the Zippy handles mouse events.
*/
goog.ui.Zippy.prototype.setHandleMouseEvents = function(enable) {
if (this.handleMouseEvents_ != enable) {
this.handleMouseEvents_ = enable;
if (enable) {
this.enableMouseEventsHandling_(this.elHeader_);
this.enableMouseEventsHandling_(this.elExpandedHeader_);
} else {
this.mouseEventHandler_.removeAll();
}
}
};
/**
* Enables keyboard events handling for the passed header element.
* @param {Element} header The header element.
* @private
*/
goog.ui.Zippy.prototype.enableKeyboardEventsHandling_ = function(header) {
if (header) {
this.keyHandler_.attach(header);
this.keyboardEventHandler_.listen(
this.keyHandler_, goog.events.KeyHandler.EventType.KEY,
this.onHeaderKeyDown_);
}
};
/**
* Enables mouse events handling for the passed header element.
* @param {Element} header The header element.
* @private
*/
goog.ui.Zippy.prototype.enableMouseEventsHandling_ = function(header) {
if (header) {
this.mouseEventHandler_.listen(
header, goog.events.EventType.CLICK, this.onHeaderClick_);
}
};
/**
* KeyDown event handler for header element. Enter and space toggles expanded
* state.
*
* @param {!goog.events.BrowserEvent} event KeyDown event.
* @private
*/
goog.ui.Zippy.prototype.onHeaderKeyDown_ = function(event) {
if (event.keyCode == goog.events.KeyCodes.ENTER ||
event.keyCode == goog.events.KeyCodes.SPACE) {
this.toggle();
this.dispatchActionEvent_(event);
// Prevent enter key from submitting form.
event.preventDefault();
event.stopPropagation();
}
};
/**
* Click event handler for header element.
*
* @param {!goog.events.BrowserEvent} event Click event.
* @private
*/
goog.ui.Zippy.prototype.onHeaderClick_ = function(event) {
this.toggle();
this.dispatchActionEvent_(event);
};
/**
* Dispatch an ACTION event whenever there is user interaction with the header.
* Please note that after the zippy state change is completed a TOGGLE event
* will be dispatched. However, the TOGGLE event is dispatch on every toggle,
* including programmatic call to {@code #toggle}.
* @param {!goog.events.BrowserEvent} triggeringEvent
* @private
*/
goog.ui.Zippy.prototype.dispatchActionEvent_ = function(triggeringEvent) {
this.dispatchEvent(new goog.ui.ZippyEvent(
goog.ui.Zippy.Events.ACTION, this, this.expanded_, triggeringEvent));
};
/**
* Object representing a zippy toggle event.
*
* @param {string} type Event type.
* @param {goog.ui.Zippy} target Zippy widget initiating event.
* @param {boolean} expanded Expanded state.
* @param {!goog.events.BrowserEvent=} opt_triggeringEvent
* @extends {goog.events.Event}
* @constructor
* @final
*/
goog.ui.ZippyEvent = function(type, target, expanded, opt_triggeringEvent) {
goog.ui.ZippyEvent.base(this, 'constructor', type, target);
/**
* The expanded state.
* @type {boolean}
*/
this.expanded = expanded;
/**
* For ACTION events, the key or mouse event that triggered this event, if
* there was one.
* @type {?goog.events.BrowserEvent}
*/
this.triggeringEvent = opt_triggeringEvent || null;
};
goog.inherits(goog.ui.ZippyEvent, goog.events.Event);
|