| 1 |
// Copyright 2011 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 Class for showing simple modal popup. |
| 17 |
* @author [email protected] (Chris Henry) |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.ui.ModalPopup'); |
| 21 |
|
| 22 |
goog.require('goog.Timer'); |
| 23 |
goog.require('goog.asserts'); |
| 24 |
goog.require('goog.dom'); |
| 25 |
goog.require('goog.dom.TagName'); |
| 26 |
goog.require('goog.dom.animationFrame'); |
| 27 |
goog.require('goog.dom.classlist'); |
| 28 |
goog.require('goog.dom.iframe'); |
| 29 |
goog.require('goog.events'); |
| 30 |
goog.require('goog.events.EventType'); |
| 31 |
goog.require('goog.events.FocusHandler'); |
| 32 |
goog.require('goog.fx.Transition'); |
| 33 |
goog.require('goog.string'); |
| 34 |
goog.require('goog.style'); |
| 35 |
goog.require('goog.ui.Component'); |
| 36 |
goog.require('goog.ui.ModalAriaVisibilityHelper'); |
| 37 |
goog.require('goog.ui.PopupBase'); |
| 38 |
goog.require('goog.userAgent'); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Base class for modal popup UI components. This can also be used as |
| 44 |
* a standalone component to render a modal popup with an empty div. |
| 45 |
* |
| 46 |
* WARNING: goog.ui.ModalPopup is only guaranteed to work when it is rendered |
| 47 |
* directly in the 'body' element. |
| 48 |
* |
| 49 |
* The Html structure of the modal popup is: |
| 50 |
* <pre> |
| 51 |
* Element Function Class-name, goog-modalpopup = default |
| 52 |
* ---------------------------------------------------------------------------- |
| 53 |
* - iframe Iframe mask goog-modalpopup-bg |
| 54 |
* - div Background mask goog-modalpopup-bg |
| 55 |
* - div Modal popup area goog-modalpopup |
| 56 |
* - span Tab catcher |
| 57 |
* </pre> |
| 58 |
* @constructor |
| 59 |
* @param {boolean=} opt_useIframeMask Work around windowed controls z-index |
| 60 |
* issue by using an iframe instead of a div for bg element. |
| 61 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper; see {@link |
| 62 |
* goog.ui.Component} for semantics. |
| 63 |
* @extends {goog.ui.Component} |
| 64 |
*/ |
| 65 |
goog.ui.ModalPopup = function(opt_useIframeMask, opt_domHelper) { |
| 66 |
goog.ui.ModalPopup.base(this, 'constructor', opt_domHelper); |
| 67 |
|
| 68 |
/** |
| 69 |
* Whether the modal popup should use an iframe as the background |
| 70 |
* element to work around z-order issues. |
| 71 |
* @type {boolean} |
| 72 |
* @private |
| 73 |
*/ |
| 74 |
this.useIframeMask_ = !!opt_useIframeMask; |
| 75 |
|
| 76 |
/** |
| 77 |
* The element that had focus before the popup was displayed. |
| 78 |
* @type {Element} |
| 79 |
* @private |
| 80 |
*/ |
| 81 |
this.lastFocus_ = null; |
| 82 |
|
| 83 |
/** |
| 84 |
* The animation task that resizes the background, scheduled to run in the |
| 85 |
* next animation frame. |
| 86 |
* @type {function(...?)} |
| 87 |
* @private |
| 88 |
*/ |
| 89 |
this.resizeBackgroundTask_ = goog.dom.animationFrame.createTask( |
| 90 |
{mutate: this.resizeBackground_}, this); |
| 91 |
}; |
| 92 |
goog.inherits(goog.ui.ModalPopup, goog.ui.Component); |
| 93 |
goog.tagUnsealableClass(goog.ui.ModalPopup); |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* Focus handler. It will be initialized in enterDocument. |
| 98 |
* @type {goog.events.FocusHandler} |
| 99 |
* @private |
| 100 |
*/ |
| 101 |
goog.ui.ModalPopup.prototype.focusHandler_ = null; |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Whether the modal popup is visible. |
| 106 |
* @type {boolean} |
| 107 |
* @private |
| 108 |
*/ |
| 109 |
goog.ui.ModalPopup.prototype.visible_ = false; |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Element for the background which obscures the UI and blocks events. |
| 114 |
* @type {Element} |
| 115 |
* @private |
| 116 |
*/ |
| 117 |
goog.ui.ModalPopup.prototype.bgEl_ = null; |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* Iframe element that is only used for IE as a workaround to keep select-type |
| 122 |
* elements from burning through background. |
| 123 |
* @type {Element} |
| 124 |
* @private |
| 125 |
*/ |
| 126 |
goog.ui.ModalPopup.prototype.bgIframeEl_ = null; |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Element used to catch focus and prevent the user from tabbing out |
| 131 |
* of the popup. |
| 132 |
* @type {Element} |
| 133 |
* @private |
| 134 |
*/ |
| 135 |
goog.ui.ModalPopup.prototype.tabCatcherElement_ = null; |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Whether the modal popup is in the process of wrapping focus from the top of |
| 140 |
* the popup to the last tabbable element. |
| 141 |
* @type {boolean} |
| 142 |
* @private |
| 143 |
*/ |
| 144 |
goog.ui.ModalPopup.prototype.backwardTabWrapInProgress_ = false; |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Transition to show the popup. |
| 149 |
* @type {goog.fx.Transition} |
| 150 |
* @private |
| 151 |
*/ |
| 152 |
goog.ui.ModalPopup.prototype.popupShowTransition_; |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Transition to hide the popup. |
| 157 |
* @type {goog.fx.Transition} |
| 158 |
* @private |
| 159 |
*/ |
| 160 |
goog.ui.ModalPopup.prototype.popupHideTransition_; |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* Transition to show the background. |
| 165 |
* @type {goog.fx.Transition} |
| 166 |
* @private |
| 167 |
*/ |
| 168 |
goog.ui.ModalPopup.prototype.bgShowTransition_; |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* Transition to hide the background. |
| 173 |
* @type {goog.fx.Transition} |
| 174 |
* @private |
| 175 |
*/ |
| 176 |
goog.ui.ModalPopup.prototype.bgHideTransition_; |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Helper object to control aria visibility of the rest of the page. |
| 181 |
* @type {goog.ui.ModalAriaVisibilityHelper} |
| 182 |
* @private |
| 183 |
*/ |
| 184 |
goog.ui.ModalPopup.prototype.modalAriaVisibilityHelper_; |
| 185 |
|
| 186 |
|
| 187 |
/** |
| 188 |
* @return {string} Base CSS class for this component. |
| 189 |
* @protected |
| 190 |
*/ |
| 191 |
goog.ui.ModalPopup.prototype.getCssClass = function() { |
| 192 |
return goog.getCssName('goog-modalpopup'); |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Returns the background iframe mask element, if any. |
| 198 |
* @return {Element} The background iframe mask element, may return |
| 199 |
* null/undefined if the modal popup does not use iframe mask. |
| 200 |
*/ |
| 201 |
goog.ui.ModalPopup.prototype.getBackgroundIframe = function() { |
| 202 |
return this.bgIframeEl_; |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* Returns the background mask element. |
| 208 |
* @return {Element} The background mask element. |
| 209 |
*/ |
| 210 |
goog.ui.ModalPopup.prototype.getBackgroundElement = function() { |
| 211 |
return this.bgEl_; |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
/** |
| 216 |
* Creates the initial DOM representation for the modal popup. |
| 217 |
* @override |
| 218 |
*/ |
| 219 |
goog.ui.ModalPopup.prototype.createDom = function() { |
| 220 |
// Create the modal popup element, and make sure it's hidden. |
| 221 |
goog.ui.ModalPopup.base(this, 'createDom'); |
| 222 |
|
| 223 |
var element = this.getElement(); |
| 224 |
goog.asserts.assert(element); |
| 225 |
var allClasses = goog.string.trim(this.getCssClass()).split(' '); |
| 226 |
goog.dom.classlist.addAll(element, allClasses); |
| 227 |
goog.dom.setFocusableTabIndex(element, true); |
| 228 |
goog.style.setElementShown(element, false); |
| 229 |
|
| 230 |
// Manages the DOM for background mask elements. |
| 231 |
this.manageBackgroundDom_(); |
| 232 |
this.createTabCatcher_(); |
| 233 |
}; |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* Creates and disposes of the DOM for background mask elements. |
| 238 |
* @private |
| 239 |
*/ |
| 240 |
goog.ui.ModalPopup.prototype.manageBackgroundDom_ = function() { |
| 241 |
if (this.useIframeMask_ && !this.bgIframeEl_) { |
| 242 |
// IE renders the iframe on top of the select elements while still |
| 243 |
// respecting the z-index of the other elements on the page. See |
| 244 |
// http://support.microsoft.com/kb/177378 for more information. |
| 245 |
// Flash and other controls behave in similar ways for other browsers |
| 246 |
this.bgIframeEl_ = goog.dom.iframe.createBlank(this.getDomHelper()); |
| 247 |
this.bgIframeEl_.className = goog.getCssName(this.getCssClass(), 'bg'); |
| 248 |
goog.style.setElementShown(this.bgIframeEl_, false); |
| 249 |
goog.style.setOpacity(this.bgIframeEl_, 0); |
| 250 |
} |
| 251 |
|
| 252 |
// Create the backgound mask, initialize its opacity, and make sure it's |
| 253 |
// hidden. |
| 254 |
if (!this.bgEl_) { |
| 255 |
this.bgEl_ = this.getDomHelper().createDom( |
| 256 |
goog.dom.TagName.DIV, goog.getCssName(this.getCssClass(), 'bg')); |
| 257 |
goog.style.setElementShown(this.bgEl_, false); |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Creates the tab catcher element. |
| 264 |
* @private |
| 265 |
*/ |
| 266 |
goog.ui.ModalPopup.prototype.createTabCatcher_ = function() { |
| 267 |
// Creates tab catcher element. |
| 268 |
if (!this.tabCatcherElement_) { |
| 269 |
this.tabCatcherElement_ = |
| 270 |
this.getDomHelper().createElement(goog.dom.TagName.SPAN); |
| 271 |
goog.style.setElementShown(this.tabCatcherElement_, false); |
| 272 |
goog.dom.setFocusableTabIndex(this.tabCatcherElement_, true); |
| 273 |
this.tabCatcherElement_.style.position = 'absolute'; |
| 274 |
} |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* Allow a shift-tab from the top of the modal popup to the last tabbable |
| 280 |
* element by moving focus to the tab catcher. This should be called after |
| 281 |
* catching a wrapping shift-tab event and before allowing it to propagate, so |
| 282 |
* that focus will land on the last tabbable element before the tab catcher. |
| 283 |
* @protected |
| 284 |
*/ |
| 285 |
goog.ui.ModalPopup.prototype.setupBackwardTabWrap = function() { |
| 286 |
this.backwardTabWrapInProgress_ = true; |
| 287 |
try { |
| 288 |
this.tabCatcherElement_.focus(); |
| 289 |
} catch (e) { |
| 290 |
// Swallow this. IE can throw an error if the element can not be focused. |
| 291 |
} |
| 292 |
// Reset the flag on a timer in case anything goes wrong with the followup |
| 293 |
// event. |
| 294 |
goog.Timer.callOnce(this.resetBackwardTabWrap_, 0, this); |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
/** |
| 299 |
* Resets the backward tab wrap flag. |
| 300 |
* @private |
| 301 |
*/ |
| 302 |
goog.ui.ModalPopup.prototype.resetBackwardTabWrap_ = function() { |
| 303 |
this.backwardTabWrapInProgress_ = false; |
| 304 |
}; |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* Renders the background mask. |
| 309 |
* @private |
| 310 |
*/ |
| 311 |
goog.ui.ModalPopup.prototype.renderBackground_ = function() { |
| 312 |
goog.asserts.assert(!!this.bgEl_, 'Background element must not be null.'); |
| 313 |
if (this.bgIframeEl_) { |
| 314 |
goog.dom.insertSiblingBefore(this.bgIframeEl_, this.getElement()); |
| 315 |
} |
| 316 |
goog.dom.insertSiblingBefore(this.bgEl_, this.getElement()); |
| 317 |
}; |
| 318 |
|
| 319 |
|
| 320 |
/** @override */ |
| 321 |
goog.ui.ModalPopup.prototype.canDecorate = function(element) { |
| 322 |
// Assume we can decorate any DIV. |
| 323 |
return !!element && element.tagName == goog.dom.TagName.DIV; |
| 324 |
}; |
| 325 |
|
| 326 |
|
| 327 |
/** @override */ |
| 328 |
goog.ui.ModalPopup.prototype.decorateInternal = function(element) { |
| 329 |
// Decorate the modal popup area element. |
| 330 |
goog.ui.ModalPopup.base(this, 'decorateInternal', element); |
| 331 |
var allClasses = goog.string.trim(this.getCssClass()).split(' '); |
| 332 |
|
| 333 |
goog.dom.classlist.addAll(goog.asserts.assert(this.getElement()), allClasses); |
| 334 |
|
| 335 |
// Create the background mask... |
| 336 |
this.manageBackgroundDom_(); |
| 337 |
this.createTabCatcher_(); |
| 338 |
|
| 339 |
// Make sure the decorated modal popup is focusable and hidden. |
| 340 |
goog.dom.setFocusableTabIndex(this.getElement(), true); |
| 341 |
goog.style.setElementShown(this.getElement(), false); |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
/** @override */ |
| 346 |
goog.ui.ModalPopup.prototype.enterDocument = function() { |
| 347 |
this.renderBackground_(); |
| 348 |
goog.ui.ModalPopup.base(this, 'enterDocument'); |
| 349 |
|
| 350 |
goog.dom.insertSiblingAfter(this.tabCatcherElement_, this.getElement()); |
| 351 |
|
| 352 |
this.focusHandler_ = |
| 353 |
new goog.events.FocusHandler(this.getDomHelper().getDocument()); |
| 354 |
|
| 355 |
// We need to watch the entire document so that we can detect when the |
| 356 |
// focus is moved out of this modal popup. |
| 357 |
this.getHandler().listen( |
| 358 |
this.focusHandler_, goog.events.FocusHandler.EventType.FOCUSIN, |
| 359 |
this.onFocus); |
| 360 |
this.setA11YDetectBackground(false); |
| 361 |
}; |
| 362 |
|
| 363 |
|
| 364 |
/** @override */ |
| 365 |
goog.ui.ModalPopup.prototype.exitDocument = function() { |
| 366 |
if (this.isVisible()) { |
| 367 |
this.setVisible(false); |
| 368 |
} |
| 369 |
|
| 370 |
goog.dispose(this.focusHandler_); |
| 371 |
|
| 372 |
goog.ui.ModalPopup.base(this, 'exitDocument'); |
| 373 |
goog.dom.removeNode(this.bgIframeEl_); |
| 374 |
goog.dom.removeNode(this.bgEl_); |
| 375 |
goog.dom.removeNode(this.tabCatcherElement_); |
| 376 |
}; |
| 377 |
|
| 378 |
|
| 379 |
/** |
| 380 |
* Sets the visibility of the modal popup box and focus to the popup. |
| 381 |
* @param {boolean} visible Whether the modal popup should be visible. |
| 382 |
*/ |
| 383 |
goog.ui.ModalPopup.prototype.setVisible = function(visible) { |
| 384 |
goog.asserts.assert( |
| 385 |
this.isInDocument(), 'ModalPopup must be rendered first.'); |
| 386 |
|
| 387 |
if (visible == this.visible_) { |
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
if (this.popupShowTransition_) this.popupShowTransition_.stop(); |
| 392 |
if (this.bgShowTransition_) this.bgShowTransition_.stop(); |
| 393 |
if (this.popupHideTransition_) this.popupHideTransition_.stop(); |
| 394 |
if (this.bgHideTransition_) this.bgHideTransition_.stop(); |
| 395 |
|
| 396 |
if (this.isInDocument()) { |
| 397 |
this.setA11YDetectBackground(visible); |
| 398 |
} |
| 399 |
if (visible) { |
| 400 |
this.show_(); |
| 401 |
} else { |
| 402 |
this.hide_(); |
| 403 |
} |
| 404 |
}; |
| 405 |
|
| 406 |
|
| 407 |
/** |
| 408 |
* Sets aria-hidden on the rest of the page to restrict screen reader focus. |
| 409 |
* Top-level elements with an explicit aria-hidden state are not altered. |
| 410 |
* @param {boolean} hide Whether to hide or show the rest of the page. |
| 411 |
* @protected |
| 412 |
*/ |
| 413 |
goog.ui.ModalPopup.prototype.setA11YDetectBackground = function(hide) { |
| 414 |
if (!this.modalAriaVisibilityHelper_) { |
| 415 |
this.modalAriaVisibilityHelper_ = new goog.ui.ModalAriaVisibilityHelper( |
| 416 |
this.getElementStrict(), this.dom_); |
| 417 |
} |
| 418 |
this.modalAriaVisibilityHelper_.setBackgroundVisibility(hide); |
| 419 |
}; |
| 420 |
|
| 421 |
|
| 422 |
/** |
| 423 |
* Sets the transitions to show and hide the popup and background. |
| 424 |
* @param {!goog.fx.Transition} popupShowTransition Transition to show the |
| 425 |
* popup. |
| 426 |
* @param {!goog.fx.Transition} popupHideTransition Transition to hide the |
| 427 |
* popup. |
| 428 |
* @param {!goog.fx.Transition} bgShowTransition Transition to show |
| 429 |
* the background. |
| 430 |
* @param {!goog.fx.Transition} bgHideTransition Transition to hide |
| 431 |
* the background. |
| 432 |
*/ |
| 433 |
goog.ui.ModalPopup.prototype.setTransition = function( |
| 434 |
popupShowTransition, popupHideTransition, bgShowTransition, |
| 435 |
bgHideTransition) { |
| 436 |
this.popupShowTransition_ = popupShowTransition; |
| 437 |
this.popupHideTransition_ = popupHideTransition; |
| 438 |
this.bgShowTransition_ = bgShowTransition; |
| 439 |
this.bgHideTransition_ = bgHideTransition; |
| 440 |
}; |
| 441 |
|
| 442 |
|
| 443 |
/** |
| 444 |
* Shows the popup. |
| 445 |
* @private |
| 446 |
*/ |
| 447 |
goog.ui.ModalPopup.prototype.show_ = function() { |
| 448 |
if (!this.dispatchEvent(goog.ui.PopupBase.EventType.BEFORE_SHOW)) { |
| 449 |
return; |
| 450 |
} |
| 451 |
|
| 452 |
try { |
| 453 |
this.lastFocus_ = this.getDomHelper().getDocument().activeElement; |
| 454 |
} catch (e) { |
| 455 |
// Focus-related actions often throw exceptions. |
| 456 |
// Sample past issue: https://bugzilla.mozilla.org/show_bug.cgi?id=656283 |
| 457 |
} |
| 458 |
this.resizeBackground_(); |
| 459 |
this.reposition(); |
| 460 |
|
| 461 |
// Listen for keyboard and resize events while the modal popup is visible. |
| 462 |
this.getHandler() |
| 463 |
.listen( |
| 464 |
this.getDomHelper().getWindow(), goog.events.EventType.RESIZE, |
| 465 |
this.resizeBackground_) |
| 466 |
.listen( |
| 467 |
this.getDomHelper().getWindow(), |
| 468 |
goog.events.EventType.ORIENTATIONCHANGE, this.resizeBackgroundTask_); |
| 469 |
|
| 470 |
this.showPopupElement_(true); |
| 471 |
this.focus(); |
| 472 |
this.visible_ = true; |
| 473 |
|
| 474 |
if (this.popupShowTransition_ && this.bgShowTransition_) { |
| 475 |
goog.events.listenOnce( |
| 476 |
/** @type {!goog.events.EventTarget} */ (this.popupShowTransition_), |
| 477 |
goog.fx.Transition.EventType.END, this.onShow, false, this); |
| 478 |
this.bgShowTransition_.play(); |
| 479 |
this.popupShowTransition_.play(); |
| 480 |
} else { |
| 481 |
this.onShow(); |
| 482 |
} |
| 483 |
}; |
| 484 |
|
| 485 |
|
| 486 |
/** |
| 487 |
* Hides the popup. |
| 488 |
* @private |
| 489 |
*/ |
| 490 |
goog.ui.ModalPopup.prototype.hide_ = function() { |
| 491 |
if (!this.dispatchEvent(goog.ui.PopupBase.EventType.BEFORE_HIDE)) { |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
// Stop listening for keyboard and resize events while the modal |
| 496 |
// popup is hidden. |
| 497 |
this.getHandler() |
| 498 |
.unlisten( |
| 499 |
this.getDomHelper().getWindow(), goog.events.EventType.RESIZE, |
| 500 |
this.resizeBackground_) |
| 501 |
.unlisten( |
| 502 |
this.getDomHelper().getWindow(), |
| 503 |
goog.events.EventType.ORIENTATIONCHANGE, this.resizeBackgroundTask_); |
| 504 |
|
| 505 |
// Set visibility to hidden even if there is a transition. This |
| 506 |
// reduces complexity in subclasses who may want to override |
| 507 |
// setVisible (such as goog.ui.Dialog). |
| 508 |
this.visible_ = false; |
| 509 |
|
| 510 |
if (this.popupHideTransition_ && this.bgHideTransition_) { |
| 511 |
goog.events.listenOnce( |
| 512 |
/** @type {!goog.events.EventTarget} */ (this.popupHideTransition_), |
| 513 |
goog.fx.Transition.EventType.END, this.onHide, false, this); |
| 514 |
this.bgHideTransition_.play(); |
| 515 |
// The transition whose END event you are listening to must be played last |
| 516 |
// to prevent errors when disposing on hide event, which occur on browsers |
| 517 |
// that do not support CSS3 transitions. |
| 518 |
this.popupHideTransition_.play(); |
| 519 |
} else { |
| 520 |
this.onHide(); |
| 521 |
} |
| 522 |
|
| 523 |
this.returnFocus_(); |
| 524 |
}; |
| 525 |
|
| 526 |
|
| 527 |
/** |
| 528 |
* Attempts to return the focus back to the element that had it before the popup |
| 529 |
* was opened. |
| 530 |
* @private |
| 531 |
*/ |
| 532 |
goog.ui.ModalPopup.prototype.returnFocus_ = function() { |
| 533 |
try { |
| 534 |
var dom = this.getDomHelper(); |
| 535 |
var body = dom.getDocument().body; |
| 536 |
var active = dom.getDocument().activeElement || body; |
| 537 |
if (!this.lastFocus_ || this.lastFocus_ == body) { |
| 538 |
this.lastFocus_ = null; |
| 539 |
return; |
| 540 |
} |
| 541 |
// We only want to move the focus if we actually have it, i.e.: |
| 542 |
// - if we immediately hid the popup the focus should have moved to the |
| 543 |
// body element |
| 544 |
// - if there is a hiding transition in progress the focus would still be |
| 545 |
// within the dialog and it is safe to move it if the current focused |
| 546 |
// element is a child of the dialog |
| 547 |
if (active == body || dom.contains(this.getElement(), active)) { |
| 548 |
this.lastFocus_.focus(); |
| 549 |
} |
| 550 |
} catch (e) { |
| 551 |
// Swallow this. IE can throw an error if the element can not be focused. |
| 552 |
} |
| 553 |
// Explicitly want to null this out even if there was an error focusing to |
| 554 |
// avoid bleed over between dialog invocations. |
| 555 |
this.lastFocus_ = null; |
| 556 |
}; |
| 557 |
|
| 558 |
|
| 559 |
/** |
| 560 |
* Shows or hides the popup element. |
| 561 |
* @param {boolean} visible Shows the popup element if true, hides if false. |
| 562 |
* @private |
| 563 |
*/ |
| 564 |
goog.ui.ModalPopup.prototype.showPopupElement_ = function(visible) { |
| 565 |
if (this.bgIframeEl_) { |
| 566 |
goog.style.setElementShown(this.bgIframeEl_, visible); |
| 567 |
} |
| 568 |
if (this.bgEl_) { |
| 569 |
goog.style.setElementShown(this.bgEl_, visible); |
| 570 |
} |
| 571 |
goog.style.setElementShown(this.getElement(), visible); |
| 572 |
goog.style.setElementShown(this.tabCatcherElement_, visible); |
| 573 |
}; |
| 574 |
|
| 575 |
|
| 576 |
/** |
| 577 |
* Called after the popup is shown. If there is a transition, this |
| 578 |
* will be called after the transition completed or stopped. |
| 579 |
* @protected |
| 580 |
*/ |
| 581 |
goog.ui.ModalPopup.prototype.onShow = function() { |
| 582 |
this.dispatchEvent(goog.ui.PopupBase.EventType.SHOW); |
| 583 |
}; |
| 584 |
|
| 585 |
|
| 586 |
/** |
| 587 |
* Called after the popup is hidden. If there is a transition, this |
| 588 |
* will be called after the transition completed or stopped. |
| 589 |
* @protected |
| 590 |
*/ |
| 591 |
goog.ui.ModalPopup.prototype.onHide = function() { |
| 592 |
this.showPopupElement_(false); |
| 593 |
this.dispatchEvent(goog.ui.PopupBase.EventType.HIDE); |
| 594 |
}; |
| 595 |
|
| 596 |
|
| 597 |
/** |
| 598 |
* @return {boolean} Whether the modal popup is visible. |
| 599 |
*/ |
| 600 |
goog.ui.ModalPopup.prototype.isVisible = function() { |
| 601 |
return this.visible_; |
| 602 |
}; |
| 603 |
|
| 604 |
|
| 605 |
/** |
| 606 |
* Focuses on the modal popup. |
| 607 |
*/ |
| 608 |
goog.ui.ModalPopup.prototype.focus = function() { |
| 609 |
this.focusElement_(); |
| 610 |
}; |
| 611 |
|
| 612 |
|
| 613 |
/** |
| 614 |
* Make the background element the size of the document. |
| 615 |
* |
| 616 |
* NOTE(user): We must hide the background element before measuring the |
| 617 |
* document, otherwise the size of the background will stop the document from |
| 618 |
* shrinking to fit a smaller window. This does cause a slight flicker in Linux |
| 619 |
* browsers, but should not be a common scenario. |
| 620 |
* @private |
| 621 |
*/ |
| 622 |
goog.ui.ModalPopup.prototype.resizeBackground_ = function() { |
| 623 |
if (this.bgIframeEl_) { |
| 624 |
goog.style.setElementShown(this.bgIframeEl_, false); |
| 625 |
} |
| 626 |
if (this.bgEl_) { |
| 627 |
goog.style.setElementShown(this.bgEl_, false); |
| 628 |
} |
| 629 |
|
| 630 |
var doc = this.getDomHelper().getDocument(); |
| 631 |
var win = goog.dom.getWindow(doc) || window; |
| 632 |
|
| 633 |
// Take the max of document height and view height, in case the document does |
| 634 |
// not fill the viewport. Read from both the body element and the html element |
| 635 |
// to account for browser differences in treatment of absolutely-positioned |
| 636 |
// content. |
| 637 |
var viewSize = goog.dom.getViewportSize(win); |
| 638 |
var w = Math.max( |
| 639 |
viewSize.width, |
| 640 |
Math.max(doc.body.scrollWidth, doc.documentElement.scrollWidth)); |
| 641 |
var h = Math.max( |
| 642 |
viewSize.height, |
| 643 |
Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)); |
| 644 |
|
| 645 |
if (this.bgIframeEl_) { |
| 646 |
goog.style.setElementShown(this.bgIframeEl_, true); |
| 647 |
goog.style.setSize(this.bgIframeEl_, w, h); |
| 648 |
} |
| 649 |
if (this.bgEl_) { |
| 650 |
goog.style.setElementShown(this.bgEl_, true); |
| 651 |
goog.style.setSize(this.bgEl_, w, h); |
| 652 |
} |
| 653 |
}; |
| 654 |
|
| 655 |
|
| 656 |
/** |
| 657 |
* Centers the modal popup in the viewport, taking scrolling into account. |
| 658 |
*/ |
| 659 |
goog.ui.ModalPopup.prototype.reposition = function() { |
| 660 |
// TODO(chrishenry): Make this use goog.positioning as in goog.ui.PopupBase? |
| 661 |
|
| 662 |
// Get the current viewport to obtain the scroll offset. |
| 663 |
var doc = this.getDomHelper().getDocument(); |
| 664 |
var win = goog.dom.getWindow(doc) || window; |
| 665 |
if (goog.style.getComputedPosition(this.getElement()) == 'fixed') { |
| 666 |
var x = 0; |
| 667 |
var y = 0; |
| 668 |
} else { |
| 669 |
var scroll = this.getDomHelper().getDocumentScroll(); |
| 670 |
var x = scroll.x; |
| 671 |
var y = scroll.y; |
| 672 |
} |
| 673 |
|
| 674 |
var popupSize = goog.style.getSize(this.getElement()); |
| 675 |
var viewSize = goog.dom.getViewportSize(win); |
| 676 |
|
| 677 |
// Make sure left and top are non-negatives. |
| 678 |
var left = Math.max(x + viewSize.width / 2 - popupSize.width / 2, 0); |
| 679 |
var top = Math.max(y + viewSize.height / 2 - popupSize.height / 2, 0); |
| 680 |
goog.style.setPosition(this.getElement(), left, top); |
| 681 |
|
| 682 |
// We place the tab catcher at the same position as the dialog to |
| 683 |
// prevent IE from scrolling when users try to tab out of the dialog. |
| 684 |
goog.style.setPosition(this.tabCatcherElement_, left, top); |
| 685 |
}; |
| 686 |
|
| 687 |
|
| 688 |
/** |
| 689 |
* Handles focus events. Makes sure that if the user tabs past the |
| 690 |
* elements in the modal popup, the focus wraps back to the beginning, and that |
| 691 |
* if the user shift-tabs past the front of the modal popup, focus wraps around |
| 692 |
* to the end. |
| 693 |
* @param {goog.events.BrowserEvent} e Browser's event object. |
| 694 |
* @protected |
| 695 |
*/ |
| 696 |
goog.ui.ModalPopup.prototype.onFocus = function(e) { |
| 697 |
if (this.backwardTabWrapInProgress_) { |
| 698 |
this.resetBackwardTabWrap_(); |
| 699 |
} else if (e.target == this.tabCatcherElement_) { |
| 700 |
goog.Timer.callOnce(this.focusElement_, 0, this); |
| 701 |
} |
| 702 |
}; |
| 703 |
|
| 704 |
|
| 705 |
/** |
| 706 |
* Returns the magic tab catcher element used to detect when the user has |
| 707 |
* rolled focus off of the popup content. It is automatically created during |
| 708 |
* the createDom method() and can be used by subclasses to implement custom |
| 709 |
* tab-loop behavior. |
| 710 |
* @return {Element} The tab catcher element. |
| 711 |
* @protected |
| 712 |
*/ |
| 713 |
goog.ui.ModalPopup.prototype.getTabCatcherElement = function() { |
| 714 |
return this.tabCatcherElement_; |
| 715 |
}; |
| 716 |
|
| 717 |
|
| 718 |
/** |
| 719 |
* Moves the focus to the modal popup. |
| 720 |
* @private |
| 721 |
*/ |
| 722 |
goog.ui.ModalPopup.prototype.focusElement_ = function() { |
| 723 |
try { |
| 724 |
if (goog.userAgent.IE) { |
| 725 |
// In IE, we must first focus on the body or else focussing on a |
| 726 |
// sub-element will not work. |
| 727 |
this.getDomHelper().getDocument().body.focus(); |
| 728 |
} |
| 729 |
this.getElement().focus(); |
| 730 |
} catch (e) { |
| 731 |
// Swallow this. IE can throw an error if the element can not be focused. |
| 732 |
} |
| 733 |
}; |
| 734 |
|
| 735 |
|
| 736 |
/** @override */ |
| 737 |
goog.ui.ModalPopup.prototype.disposeInternal = function() { |
| 738 |
goog.dispose(this.popupShowTransition_); |
| 739 |
this.popupShowTransition_ = null; |
| 740 |
|
| 741 |
goog.dispose(this.popupHideTransition_); |
| 742 |
this.popupHideTransition_ = null; |
| 743 |
|
| 744 |
goog.dispose(this.bgShowTransition_); |
| 745 |
this.bgShowTransition_ = null; |
| 746 |
|
| 747 |
goog.dispose(this.bgHideTransition_); |
| 748 |
this.bgHideTransition_ = null; |
| 749 |
|
| 750 |
goog.ui.ModalPopup.base(this, 'disposeInternal'); |
| 751 |
}; |