Debian Codegraph @

closure-library/closure/goog/ui/popupbase.js

raw
1 // Copyright 2006 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 Definition of the PopupBase class.
17 *
18 */
19
20 goog.provide('goog.ui.PopupBase');
21 goog.provide('goog.ui.PopupBase.EventType');
22 goog.provide('goog.ui.PopupBase.Type');
23
24 goog.require('goog.Timer');
25 goog.require('goog.array');
26 goog.require('goog.dom');
27 goog.require('goog.dom.TagName');
28 goog.require('goog.events');
29 goog.require('goog.events.EventHandler');
30 goog.require('goog.events.EventTarget');
31 goog.require('goog.events.EventType');
32 goog.require('goog.events.KeyCodes');
33 goog.require('goog.fx.Transition');
34 goog.require('goog.style');
35 goog.require('goog.userAgent');
36
37
38
39 /**
40 * The PopupBase class provides functionality for showing and hiding a generic
41 * container element. It also provides the option for hiding the popup element
42 * if the user clicks outside the popup or the popup loses focus.
43 *
44 * @constructor
45 * @extends {goog.events.EventTarget}
46 * @param {Element=} opt_element A DOM element for the popup.
47 * @param {goog.ui.PopupBase.Type=} opt_type Type of popup.
48 */
49 goog.ui.PopupBase = function(opt_element, opt_type) {
50 goog.events.EventTarget.call(this);
51
52 /**
53 * An event handler to manage the events easily
54 * @type {goog.events.EventHandler<!goog.ui.PopupBase>}
55 * @private
56 */
57 this.handler_ = new goog.events.EventHandler(this);
58
59 this.setElement(opt_element || null);
60 if (opt_type) {
61 this.setType(opt_type);
62 }
63 };
64 goog.inherits(goog.ui.PopupBase, goog.events.EventTarget);
65 goog.tagUnsealableClass(goog.ui.PopupBase);
66
67
68 /**
69 * Constants for type of Popup
70 * @enum {string}
71 */
72 goog.ui.PopupBase.Type = {
73 TOGGLE_DISPLAY: 'toggle_display',
74 MOVE_OFFSCREEN: 'move_offscreen'
75 };
76
77
78 /**
79 * The popup dom element that this Popup wraps.
80 * @type {Element}
81 * @private
82 */
83 goog.ui.PopupBase.prototype.element_ = null;
84
85
86 /**
87 * Whether the Popup dismisses itself it the user clicks outside of it or the
88 * popup loses focus
89 * @type {boolean}
90 * @private
91 */
92 goog.ui.PopupBase.prototype.autoHide_ = true;
93
94
95 /**
96 * Mouse events without auto hide partner elements will not dismiss the popup.
97 * @type {Array<Element>}
98 * @private
99 */
100 goog.ui.PopupBase.prototype.autoHidePartners_ = null;
101
102
103 /**
104 * Clicks outside the popup but inside this element will cause the popup to
105 * hide if autoHide_ is true. If this is null, then the entire document is used.
106 * For example, you can use a body-size div so that clicks on the browser
107 * scrollbar do not dismiss the popup.
108 * @type {Element}
109 * @private
110 */
111 goog.ui.PopupBase.prototype.autoHideRegion_ = null;
112
113
114 /**
115 * Whether the popup is currently being shown.
116 * @type {boolean}
117 * @private
118 */
119 goog.ui.PopupBase.prototype.isVisible_ = false;
120
121
122 /**
123 * Whether the popup should hide itself asynchrously. This was added because
124 * there are cases where hiding the element in mouse down handler in IE can
125 * cause textinputs to get into a bad state if the element that had focus is
126 * hidden.
127 * @type {boolean}
128 * @private
129 */
130 goog.ui.PopupBase.prototype.shouldHideAsync_ = false;
131
132
133 /**
134 * The time when the popup was last shown.
135 * @type {number}
136 * @private
137 */
138 goog.ui.PopupBase.prototype.lastShowTime_ = -1;
139
140
141 /**
142 * The time when the popup was last hidden.
143 * @type {number}
144 * @private
145 */
146 goog.ui.PopupBase.prototype.lastHideTime_ = -1;
147
148
149 /**
150 * Whether to hide when the escape key is pressed.
151 * @type {boolean}
152 * @private
153 */
154 goog.ui.PopupBase.prototype.hideOnEscape_ = false;
155
156
157 /**
158 * Whether to enable cross-iframe dismissal.
159 * @type {boolean}
160 * @private
161 */
162 goog.ui.PopupBase.prototype.enableCrossIframeDismissal_ = true;
163
164
165 /**
166 * The type of popup
167 * @type {goog.ui.PopupBase.Type}
168 * @private
169 */
170 goog.ui.PopupBase.prototype.type_ = goog.ui.PopupBase.Type.TOGGLE_DISPLAY;
171
172
173 /**
174 * Transition to play on showing the popup.
175 * @type {goog.fx.Transition|undefined}
176 * @private
177 */
178 goog.ui.PopupBase.prototype.showTransition_;
179
180
181 /**
182 * Transition to play on hiding the popup.
183 * @type {goog.fx.Transition|undefined}
184 * @private
185 */
186 goog.ui.PopupBase.prototype.hideTransition_;
187
188
189 /**
190 * Constants for event type fired by Popup
191 *
192 * @enum {string}
193 */
194 goog.ui.PopupBase.EventType = {
195 BEFORE_SHOW: 'beforeshow',
196 SHOW: 'show',
197 BEFORE_HIDE: 'beforehide',
198 HIDE: 'hide'
199 };
200
201
202 /**
203 * A time in ms used to debounce events that happen right after each other.
204 *
205 * A note about why this is necessary. There are two cases to consider.
206 * First case, a popup will usually see a focus event right after it's launched
207 * because it's typical for it to be launched in a mouse-down event which will
208 * then move focus to the launching button. We don't want to think this is a
209 * separate user action moving focus. Second case, a user clicks on the
210 * launcher button to close the menu. In that case, we'll close the menu in the
211 * focus event and then show it again because of the mouse down event, even
212 * though the intention is to just close the menu. This workaround appears to
213 * be the least intrusive fix.
214 *
215 * @type {number}
216 */
217 goog.ui.PopupBase.DEBOUNCE_DELAY_MS = 150;
218
219
220 /**
221 * @return {goog.ui.PopupBase.Type} The type of popup this is.
222 */
223 goog.ui.PopupBase.prototype.getType = function() {
224 return this.type_;
225 };
226
227
228 /**
229 * Specifies the type of popup to use.
230 *
231 * @param {goog.ui.PopupBase.Type} type Type of popup.
232 */
233 goog.ui.PopupBase.prototype.setType = function(type) {
234 this.type_ = type;
235 };
236
237
238 /**
239 * Returns whether the popup should hide itself asynchronously using a timeout
240 * instead of synchronously.
241 * @return {boolean} Whether to hide async.
242 */
243 goog.ui.PopupBase.prototype.shouldHideAsync = function() {
244 return this.shouldHideAsync_;
245 };
246
247
248 /**
249 * Sets whether the popup should hide itself asynchronously using a timeout
250 * instead of synchronously.
251 * @param {boolean} b Whether to hide async.
252 */
253 goog.ui.PopupBase.prototype.setShouldHideAsync = function(b) {
254 this.shouldHideAsync_ = b;
255 };
256
257
258 /**
259 * Returns the dom element that should be used for the popup.
260 *
261 * @return {Element} The popup element.
262 */
263 goog.ui.PopupBase.prototype.getElement = function() {
264 return this.element_;
265 };
266
267
268 /**
269 * Specifies the dom element that should be used for the popup.
270 *
271 * @param {Element} elt A DOM element for the popup.
272 */
273 goog.ui.PopupBase.prototype.setElement = function(elt) {
274 this.ensureNotVisible_();
275 this.element_ = elt;
276 };
277
278
279 /**
280 * Returns whether the Popup dismisses itself when the user clicks outside of
281 * it.
282 * @return {boolean} Whether the Popup autohides on an external click.
283 */
284 goog.ui.PopupBase.prototype.getAutoHide = function() {
285 return this.autoHide_;
286 };
287
288
289 /**
290 * Sets whether the Popup dismisses itself when the user clicks outside of it.
291 * @param {boolean} autoHide Whether to autohide on an external click.
292 */
293 goog.ui.PopupBase.prototype.setAutoHide = function(autoHide) {
294 this.ensureNotVisible_();
295 this.autoHide_ = autoHide;
296 };
297
298
299 /**
300 * Mouse events that occur within an autoHide partner will not hide a popup
301 * set to autoHide.
302 * @param {!Element} partner The auto hide partner element.
303 */
304 goog.ui.PopupBase.prototype.addAutoHidePartner = function(partner) {
305 if (!this.autoHidePartners_) {
306 this.autoHidePartners_ = [];
307 }
308
309 goog.array.insert(this.autoHidePartners_, partner);
310 };
311
312
313 /**
314 * Removes a previously registered auto hide partner.
315 * @param {!Element} partner The auto hide partner element.
316 */
317 goog.ui.PopupBase.prototype.removeAutoHidePartner = function(partner) {
318 if (this.autoHidePartners_) {
319 goog.array.remove(this.autoHidePartners_, partner);
320 }
321 };
322
323
324 /**
325 * @return {boolean} Whether the Popup autohides on the escape key.
326 */
327 goog.ui.PopupBase.prototype.getHideOnEscape = function() {
328 return this.hideOnEscape_;
329 };
330
331
332 /**
333 * Sets whether the Popup dismisses itself on the escape key.
334 * @param {boolean} hideOnEscape Whether to autohide on the escape key.
335 */
336 goog.ui.PopupBase.prototype.setHideOnEscape = function(hideOnEscape) {
337 this.ensureNotVisible_();
338 this.hideOnEscape_ = hideOnEscape;
339 };
340
341
342 /**
343 * @return {boolean} Whether cross iframe dismissal is enabled.
344 */
345 goog.ui.PopupBase.prototype.getEnableCrossIframeDismissal = function() {
346 return this.enableCrossIframeDismissal_;
347 };
348
349
350 /**
351 * Sets whether clicks in other iframes should dismiss this popup. In some
352 * cases it should be disabled, because it can cause spurious
353 * @param {boolean} enable Whether to enable cross iframe dismissal.
354 */
355 goog.ui.PopupBase.prototype.setEnableCrossIframeDismissal = function(enable) {
356 this.enableCrossIframeDismissal_ = enable;
357 };
358
359
360 /**
361 * Returns the region inside which the Popup dismisses itself when the user
362 * clicks, or null if it's the entire document.
363 * @return {Element} The DOM element for autohide, or null if it hasn't been
364 * set.
365 */
366 goog.ui.PopupBase.prototype.getAutoHideRegion = function() {
367 return this.autoHideRegion_;
368 };
369
370
371 /**
372 * Sets the region inside which the Popup dismisses itself when the user
373 * clicks.
374 * @param {Element} element The DOM element for autohide.
375 */
376 goog.ui.PopupBase.prototype.setAutoHideRegion = function(element) {
377 this.autoHideRegion_ = element;
378 };
379
380
381 /**
382 * Sets transition animation on showing and hiding the popup.
383 * @param {goog.fx.Transition=} opt_showTransition Transition to play on
384 * showing the popup.
385 * @param {goog.fx.Transition=} opt_hideTransition Transition to play on
386 * hiding the popup.
387 */
388 goog.ui.PopupBase.prototype.setTransition = function(
389 opt_showTransition, opt_hideTransition) {
390 this.showTransition_ = opt_showTransition;
391 this.hideTransition_ = opt_hideTransition;
392 };
393
394
395 /**
396 * Returns the time when the popup was last shown.
397 *
398 * @return {number} time in ms since epoch when the popup was last shown, or
399 * -1 if the popup was never shown.
400 */
401 goog.ui.PopupBase.prototype.getLastShowTime = function() {
402 return this.lastShowTime_;
403 };
404
405
406 /**
407 * Returns the time when the popup was last hidden.
408 *
409 * @return {number} time in ms since epoch when the popup was last hidden, or
410 * -1 if the popup was never hidden or is currently showing.
411 */
412 goog.ui.PopupBase.prototype.getLastHideTime = function() {
413 return this.lastHideTime_;
414 };
415
416
417 /**
418 * Returns the event handler for the popup. All event listeners belonging to
419 * this handler are removed when the tooltip is hidden. Therefore,
420 * the recommended usage of this handler is to listen on events in
421 * {@link #onShow}.
422 * @return {goog.events.EventHandler<T>} Event handler for this popup.
423 * @protected
424 * @this {T}
425 * @template T
426 */
427 goog.ui.PopupBase.prototype.getHandler = function() {
428 // As the template type is unbounded, narrow the "this" type
429 var self = /** @type {!goog.ui.PopupBase} */ (this);
430
431 return self.handler_;
432 };
433
434
435 /**
436 * Helper to throw exception if the popup is showing.
437 * @private
438 */
439 goog.ui.PopupBase.prototype.ensureNotVisible_ = function() {
440 if (this.isVisible_) {
441 throw new Error('Can not change this state of the popup while showing.');
442 }
443 };
444
445
446 /**
447 * Returns whether the popup is currently visible.
448 *
449 * @return {boolean} whether the popup is currently visible.
450 */
451 goog.ui.PopupBase.prototype.isVisible = function() {
452 return this.isVisible_;
453 };
454
455
456 /**
457 * Returns whether the popup is currently visible or was visible within about
458 * 150 ms ago. This is used by clients to handle a very specific, but common,
459 * popup scenario. The button that launches the popup should close the popup
460 * on mouse down if the popup is alrady open. The problem is that the popup
461 * closes itself during the capture phase of the mouse down and thus the button
462 * thinks it's hidden and this should show it again. This method provides a
463 * good heuristic for clients. Typically in their event handler they will have
464 * code that is:
465 *
466 * if (menu.isOrWasRecentlyVisible()) {
467 * menu.setVisible(false);
468 * } else {
469 * ... // code to position menu and initialize other state
470 * menu.setVisible(true);
471 * }
472 * @return {boolean} Whether the popup is currently visible or was visible
473 * within about 150 ms ago.
474 */
475 goog.ui.PopupBase.prototype.isOrWasRecentlyVisible = function() {
476 return this.isVisible_ ||
477 (goog.now() - this.lastHideTime_ < goog.ui.PopupBase.DEBOUNCE_DELAY_MS);
478 };
479
480
481 /**
482 * Sets whether the popup should be visible. After this method
483 * returns, isVisible() will always return the new state, even if
484 * there is a transition.
485 *
486 * @param {boolean} visible Desired visibility state.
487 */
488 goog.ui.PopupBase.prototype.setVisible = function(visible) {
489 // Make sure that any currently running transition is stopped.
490 if (this.showTransition_) this.showTransition_.stop();
491 if (this.hideTransition_) this.hideTransition_.stop();
492
493 if (visible) {
494 this.show_();
495 } else {
496 this.hide_();
497 }
498 };
499
500
501 /**
502 * Repositions the popup according to the current state.
503 * Should be overriden by subclases.
504 */
505 goog.ui.PopupBase.prototype.reposition = goog.nullFunction;
506
507
508 /**
509 * Does the work to show the popup.
510 * @private
511 */
512 goog.ui.PopupBase.prototype.show_ = function() {
513 // Ignore call if we are already showing.
514 if (this.isVisible_) {
515 return;
516 }
517
518 // Give derived classes and handlers a chance to customize popup.
519 if (!this.onBeforeShow()) {
520 return;
521 }
522
523 // Allow callers to set the element in the BEFORE_SHOW event.
524 if (!this.element_) {
525 throw new Error(
526 'Caller must call setElement before trying to show the popup');
527 }
528
529 // Call reposition after onBeforeShow, as it may change the style and/or
530 // content of the popup and thereby affecting the size which is used for the
531 // viewport calculation.
532 this.reposition();
533
534 var doc = goog.dom.getOwnerDocument(this.element_);
535
536 if (this.hideOnEscape_) {
537 // Handle the escape keys. Listen in the capture phase so that we can
538 // stop the escape key from propagating to other elements. For example,
539 // if there is a popup within a dialog box, we want the popup to be
540 // dismissed first, rather than the dialog.
541 this.handler_.listen(
542 doc, goog.events.EventType.KEYDOWN, this.onDocumentKeyDown_, true);
543 }
544
545 // Set up event handlers.
546 if (this.autoHide_) {
547 // Even if the popup is not in the focused document, we want to
548 // close it on mousedowns in the document it's in.
549 this.handler_.listen(
550 doc, goog.events.EventType.MOUSEDOWN, this.onDocumentMouseDown_, true);
551
552 if (goog.userAgent.IE) {
553 // We want to know about deactivates/mousedowns on the document with focus
554 // The top-level document won't get a deactivate event if the focus is
555 // in an iframe and the deactivate fires within that iframe.
556 // The active element in the top-level document will remain the iframe
557 // itself.
558 var activeElement;
559
560 try {
561 activeElement = doc.activeElement;
562 } catch (e) {
563 // There is an IE browser bug which can cause just the reading of
564 // document.activeElement to throw an Unspecified Error. This
565 // may have to do with loading a popup within a hidden iframe.
566 }
567 while (activeElement &&
568 activeElement.nodeName == goog.dom.TagName.IFRAME) {
569
570 try {
571 var tempDoc = goog.dom.getFrameContentDocument(activeElement);
572 } catch (e) {
573 // The frame is on a different domain that its parent document
574 // This way, we grab the lowest-level document object we can get
575 // a handle on given cross-domain security.
576 break;
577 }
578 doc = tempDoc;
579 activeElement = doc.activeElement;
580 }
581
582 // Handle mousedowns in the focused document in case the user clicks
583 // on the activeElement (in which case the popup should hide).
584 this.handler_.listen(
585 doc, goog.events.EventType.MOUSEDOWN, this.onDocumentMouseDown_,
586 true);
587
588 // If the active element inside the focused document changes, then
589 // we probably need to hide the popup.
590 this.handler_.listen(
591 doc, goog.events.EventType.DEACTIVATE, this.onDocumentBlur_);
592
593 } else {
594 this.handler_.listen(
595 doc, goog.events.EventType.BLUR, this.onDocumentBlur_);
596 }
597 }
598
599 // Make the popup visible.
600 if (this.type_ == goog.ui.PopupBase.Type.TOGGLE_DISPLAY) {
601 this.showPopupElement();
602 } else if (this.type_ == goog.ui.PopupBase.Type.MOVE_OFFSCREEN) {
603 this.reposition();
604 }
605 this.isVisible_ = true;
606
607 this.lastShowTime_ = goog.now();
608 this.lastHideTime_ = -1;
609
610 // If there is transition to play, we play it and fire SHOW event after
611 // the transition is over.
612 if (this.showTransition_) {
613 goog.events.listenOnce(
614 /** @type {!goog.events.EventTarget} */ (this.showTransition_),
615 goog.fx.Transition.EventType.END, this.onShow, false, this);
616 this.showTransition_.play();
617 } else {
618 // Notify derived classes and handlers.
619 this.onShow();
620 }
621 };
622
623
624 /**
625 * Hides the popup. This call is idempotent.
626 *
627 * @param {?Node=} opt_target Target of the event causing the hide.
628 * @return {boolean} Whether the popup was hidden and not cancelled.
629 * @private
630 */
631 goog.ui.PopupBase.prototype.hide_ = function(opt_target) {
632 // Give derived classes and handlers a chance to cancel hiding.
633 if (!this.isVisible_ || !this.onBeforeHide(opt_target)) {
634 return false;
635 }
636
637 // Remove any listeners we attached when showing the popup.
638 if (this.handler_) {
639 this.handler_.removeAll();
640 }
641
642 // Set visibility to hidden even if there is a transition.
643 this.isVisible_ = false;
644 this.lastHideTime_ = goog.now();
645
646 // If there is transition to play, we play it and only hide the element
647 // (and fire HIDE event) after the transition is over.
648 if (this.hideTransition_) {
649 goog.events.listenOnce(
650 /** @type {!goog.events.EventTarget} */ (this.hideTransition_),
651 goog.fx.Transition.EventType.END,
652 goog.partial(this.continueHidingPopup_, opt_target), false, this);
653 this.hideTransition_.play();
654 } else {
655 this.continueHidingPopup_(opt_target);
656 }
657
658 return true;
659 };
660
661
662 /**
663 * Continues hiding the popup. This is a continuation from hide_. It is
664 * a separate method so that we can add a transition before hiding.
665 * @param {?Node=} opt_target Target of the event causing the hide.
666 * @private
667 */
668 goog.ui.PopupBase.prototype.continueHidingPopup_ = function(opt_target) {
669 // Hide the popup.
670 if (this.type_ == goog.ui.PopupBase.Type.TOGGLE_DISPLAY) {
671 if (this.shouldHideAsync_) {
672 goog.Timer.callOnce(this.hidePopupElement, 0, this);
673 } else {
674 this.hidePopupElement();
675 }
676 } else if (this.type_ == goog.ui.PopupBase.Type.MOVE_OFFSCREEN) {
677 this.moveOffscreen_();
678 }
679
680 // Notify derived classes and handlers.
681 this.onHide(opt_target);
682 };
683
684
685 /**
686 * Shows the popup element.
687 * @protected
688 */
689 goog.ui.PopupBase.prototype.showPopupElement = function() {
690 this.element_.style.visibility = 'visible';
691 goog.style.setElementShown(this.element_, true);
692 };
693
694
695 /**
696 * Hides the popup element.
697 * @protected
698 */
699 goog.ui.PopupBase.prototype.hidePopupElement = function() {
700 this.element_.style.visibility = 'hidden';
701 goog.style.setElementShown(this.element_, false);
702 };
703
704
705 /**
706 * Hides the popup by moving it offscreen.
707 *
708 * @private
709 */
710 goog.ui.PopupBase.prototype.moveOffscreen_ = function() {
711 this.element_.style.top = '-10000px';
712 };
713
714
715 /**
716 * Called before the popup is shown. Derived classes can override to hook this
717 * event but should make sure to call the parent class method.
718 *
719 * @return {boolean} If anyone called preventDefault on the event object (or
720 * if any of the handlers returns false this will also return false.
721 * @protected
722 */
723 goog.ui.PopupBase.prototype.onBeforeShow = function() {
724 return this.dispatchEvent(goog.ui.PopupBase.EventType.BEFORE_SHOW);
725 };
726
727
728 /**
729 * Called after the popup is shown. Derived classes can override to hook this
730 * event but should make sure to call the parent class method.
731 * @protected
732 */
733 goog.ui.PopupBase.prototype.onShow = function() {
734 this.dispatchEvent(goog.ui.PopupBase.EventType.SHOW);
735 };
736
737
738 /**
739 * Called before the popup is hidden. Derived classes can override to hook this
740 * event but should make sure to call the parent class method.
741 *
742 * @param {?Node=} opt_target Target of the event causing the hide.
743 * @return {boolean} If anyone called preventDefault on the event object (or
744 * if any of the handlers returns false this will also return false.
745 * @protected
746 */
747 goog.ui.PopupBase.prototype.onBeforeHide = function(opt_target) {
748 return this.dispatchEvent(
749 {type: goog.ui.PopupBase.EventType.BEFORE_HIDE, target: opt_target});
750 };
751
752
753 /**
754 * Called after the popup is hidden. Derived classes can override to hook this
755 * event but should make sure to call the parent class method.
756 * @param {?Node=} opt_target Target of the event causing the hide.
757 * @protected
758 */
759 goog.ui.PopupBase.prototype.onHide = function(opt_target) {
760 this.dispatchEvent(
761 {type: goog.ui.PopupBase.EventType.HIDE, target: opt_target});
762 };
763
764
765 /**
766 * Mouse down handler for the document on capture phase. Used to hide the
767 * popup for auto-hide mode.
768 *
769 * @param {goog.events.BrowserEvent} e The event object.
770 * @private
771 */
772 goog.ui.PopupBase.prototype.onDocumentMouseDown_ = function(e) {
773 var target = e.target;
774
775 if (!goog.dom.contains(this.element_, target) &&
776 !this.isOrWithinAutoHidePartner_(target) &&
777 this.isWithinAutoHideRegion_(target) && !this.shouldDebounce_()) {
778 // Mouse click was outside popup and partners, so hide.
779 this.hide_(target);
780 }
781 };
782
783
784 /**
785 * Handles key-downs on the document to handle the escape key.
786 *
787 * @param {goog.events.BrowserEvent} e The event object.
788 * @private
789 */
790 goog.ui.PopupBase.prototype.onDocumentKeyDown_ = function(e) {
791 if (e.keyCode == goog.events.KeyCodes.ESC) {
792 if (this.hide_(e.target)) {
793 // Eat the escape key, but only if this popup was actually closed.
794 e.preventDefault();
795 e.stopPropagation();
796 }
797 }
798 };
799
800
801 /**
802 * Deactivate handler(IE) and blur handler (other browsers) for document.
803 * Used to hide the popup for auto-hide mode.
804 *
805 * @param {goog.events.BrowserEvent} e The event object.
806 * @private
807 */
808 goog.ui.PopupBase.prototype.onDocumentBlur_ = function(e) {
809 if (!this.enableCrossIframeDismissal_) {
810 return;
811 }
812
813 var doc = goog.dom.getOwnerDocument(this.element_);
814
815 // Ignore blur events if the active element is still inside the popup or if
816 // there is no longer an active element. For example, a widget like a
817 // goog.ui.Button might programatically blur itself before losing tabIndex.
818 if (typeof document.activeElement != 'undefined') {
819 var activeElement = doc.activeElement;
820 if (!activeElement || goog.dom.contains(this.element_, activeElement) ||
821 activeElement.tagName == goog.dom.TagName.BODY) {
822 return;
823 }
824
825 // Ignore blur events not for the document itself in non-IE browsers.
826 } else if (e.target != doc) {
827 return;
828 }
829
830 // Debounce the initial focus move.
831 if (this.shouldDebounce_()) {
832 return;
833 }
834
835 this.hide_();
836 };
837
838
839 /**
840 * @param {Node} element The element to inspect.
841 * @return {boolean} Returns true if the given element is one of the auto hide
842 * partners or is a child of an auto hide partner.
843 * @private
844 */
845 goog.ui.PopupBase.prototype.isOrWithinAutoHidePartner_ = function(element) {
846 return goog.array.some(this.autoHidePartners_ || [], function(partner) {
847 return element === partner || goog.dom.contains(partner, element);
848 });
849 };
850
851
852 /**
853 * @param {Node} element The element to inspect.
854 * @return {boolean} Returns true if the element is contained within
855 * the autohide region. If unset, the autohide region is the entire
856 * entire document.
857 * @private
858 */
859 goog.ui.PopupBase.prototype.isWithinAutoHideRegion_ = function(element) {
860 return this.autoHideRegion_ ?
861 goog.dom.contains(this.autoHideRegion_, element) :
862 true;
863 };
864
865
866 /**
867 * @return {boolean} Whether the time since last show is less than the debounce
868 * delay.
869 * @private
870 */
871 goog.ui.PopupBase.prototype.shouldDebounce_ = function() {
872 return goog.now() - this.lastShowTime_ < goog.ui.PopupBase.DEBOUNCE_DELAY_MS;
873 };
874
875
876 /** @override */
877 goog.ui.PopupBase.prototype.disposeInternal = function() {
878 goog.ui.PopupBase.base(this, 'disposeInternal');
879 this.handler_.dispose();
880 goog.dispose(this.showTransition_);
881 goog.dispose(this.hideTransition_);
882 delete this.element_;
883 delete this.handler_;
884 delete this.autoHidePartners_;
885 };