Debian Codegraph @

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

raw
1 // Copyright 2007 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 Tooltip widget implementation.
17 *
18 * @author [email protected] (Emil A Eklund)
19 * @see ../demos/tooltip.html
20 */
21
22 goog.provide('goog.ui.Tooltip');
23 goog.provide('goog.ui.Tooltip.CursorTooltipPosition');
24 goog.provide('goog.ui.Tooltip.ElementTooltipPosition');
25 goog.provide('goog.ui.Tooltip.State');
26
27 goog.require('goog.Timer');
28 goog.require('goog.array');
29 goog.require('goog.asserts');
30 goog.require('goog.dom');
31 goog.require('goog.dom.TagName');
32 goog.require('goog.dom.safe');
33 goog.require('goog.events');
34 goog.require('goog.events.EventType');
35 goog.require('goog.events.FocusHandler');
36 goog.require('goog.math.Box');
37 goog.require('goog.math.Coordinate');
38 goog.require('goog.positioning');
39 goog.require('goog.positioning.AnchoredPosition');
40 goog.require('goog.positioning.Corner');
41 goog.require('goog.positioning.Overflow');
42 goog.require('goog.positioning.OverflowStatus');
43 goog.require('goog.positioning.ViewportPosition');
44 goog.require('goog.structs.Set');
45 goog.require('goog.style');
46 goog.require('goog.ui.Popup');
47 goog.require('goog.ui.PopupBase');
48
49
50
51 /**
52 * Tooltip widget. Can be attached to one or more elements and is shown, with a
53 * slight delay, when the the cursor is over the element or the element gains
54 * focus.
55 *
56 * @param {Element|string=} opt_el Element to display tooltip for, either
57 * element reference or string id.
58 * @param {?string=} opt_str Text message to display in tooltip.
59 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
60 * @constructor
61 * @extends {goog.ui.Popup}
62 */
63 goog.ui.Tooltip = function(opt_el, opt_str, opt_domHelper) {
64 /**
65 * Dom Helper
66 * @type {goog.dom.DomHelper}
67 * @private
68 */
69 this.dom_ = opt_domHelper ||
70 (opt_el ? goog.dom.getDomHelper(goog.dom.getElement(opt_el)) :
71 goog.dom.getDomHelper());
72
73 goog.ui.Popup.call(
74 this,
75 this.dom_.createDom(
76 goog.dom.TagName.DIV, {'style': 'position:absolute;display:none;'}));
77
78 /**
79 * Cursor position relative to the page.
80 * @type {!goog.math.Coordinate}
81 * @protected
82 */
83 this.cursorPosition = new goog.math.Coordinate(1, 1);
84
85 /**
86 * Elements this widget is attached to.
87 * @type {goog.structs.Set}
88 * @private
89 */
90 this.elements_ = new goog.structs.Set();
91
92 /**
93 * Keyboard focus event handler for elements inside the tooltip.
94 * @private {goog.events.FocusHandler}
95 */
96 this.tooltipFocusHandler_ = null;
97
98 // Attach to element, if specified
99 if (opt_el) {
100 this.attach(opt_el);
101 }
102
103 // Set message, if specified.
104 if (opt_str != null) {
105 this.setText(opt_str);
106 }
107 };
108 goog.inherits(goog.ui.Tooltip, goog.ui.Popup);
109 goog.tagUnsealableClass(goog.ui.Tooltip);
110
111
112 /**
113 * List of active (open) tooltip widgets. Used to prevent multiple tooltips
114 * from appearing at once.
115 *
116 * @type {!Array<goog.ui.Tooltip>}
117 * @private
118 */
119 goog.ui.Tooltip.activeInstances_ = [];
120
121
122 /**
123 * Active element reference. Used by the delayed show functionality to keep
124 * track of the element the mouse is over or the element with focus.
125 * @type {Element}
126 * @private
127 */
128 goog.ui.Tooltip.prototype.activeEl_ = null;
129
130
131 /**
132 * CSS class name for tooltip.
133 *
134 * @type {string}
135 */
136 goog.ui.Tooltip.prototype.className = goog.getCssName('goog-tooltip');
137
138
139 /**
140 * Delay in milliseconds since the last mouseover or mousemove before the
141 * tooltip is displayed for an element.
142 *
143 * @type {number}
144 * @private
145 */
146 goog.ui.Tooltip.prototype.showDelayMs_ = 500;
147
148
149 /**
150 * Timer for when to show.
151 *
152 * @type {number|undefined}
153 * @protected
154 */
155 goog.ui.Tooltip.prototype.showTimer;
156
157
158 /**
159 * Delay in milliseconds before tooltips are hidden.
160 *
161 * @type {number}
162 * @private
163 */
164 goog.ui.Tooltip.prototype.hideDelayMs_ = 0;
165
166
167 /**
168 * Timer for when to hide.
169 *
170 * @type {number|undefined}
171 * @protected
172 */
173 goog.ui.Tooltip.prototype.hideTimer;
174
175
176 /**
177 * Element that triggered the tooltip. Note that if a second element triggers
178 * this tooltip, anchor becomes that second element, even if its show is
179 * cancelled and the original tooltip survives.
180 *
181 * @type {Element|undefined}
182 * @protected
183 */
184 goog.ui.Tooltip.prototype.anchor;
185
186
187 /**
188 * Possible states for the tooltip to be in.
189 * @enum {number}
190 */
191 goog.ui.Tooltip.State = {
192 INACTIVE: 0,
193 WAITING_TO_SHOW: 1,
194 SHOWING: 2,
195 WAITING_TO_HIDE: 3,
196 UPDATING: 4 // waiting to show new hovercard while old one still showing.
197 };
198
199
200 /**
201 * Popup activation types. Used to select a positioning strategy.
202 * @enum {number}
203 */
204 goog.ui.Tooltip.Activation = {
205 CURSOR: 0,
206 FOCUS: 1
207 };
208
209
210 /**
211 * Whether the anchor has seen the cursor move or has received focus since the
212 * tooltip was last shown. Used to ignore mouse over events triggered by view
213 * changes and UI updates.
214 * @type {boolean|undefined}
215 * @private
216 */
217 goog.ui.Tooltip.prototype.seenInteraction_;
218
219
220 /**
221 * Whether the cursor must have moved before the tooltip will be shown.
222 * @type {boolean|undefined}
223 * @private
224 */
225 goog.ui.Tooltip.prototype.requireInteraction_;
226
227
228 /**
229 * If this tooltip's element contains another tooltip that becomes active, this
230 * property identifies that tooltip so that we can check if this tooltip should
231 * not be hidden because the nested tooltip is active.
232 * @type {goog.ui.Tooltip}
233 * @private
234 */
235 goog.ui.Tooltip.prototype.childTooltip_;
236
237
238 /**
239 * If this tooltip is inside another tooltip's element, then it may have
240 * prevented that tooltip from hiding. When this tooltip hides, we'll need
241 * to check if the parent should be hidden as well.
242 * @type {goog.ui.Tooltip}
243 * @private
244 */
245 goog.ui.Tooltip.prototype.parentTooltip_;
246
247
248 /**
249 * Returns the dom helper that is being used on this component.
250 * @return {goog.dom.DomHelper} The dom helper used on this component.
251 */
252 goog.ui.Tooltip.prototype.getDomHelper = function() {
253 return this.dom_;
254 };
255
256
257 /**
258 * @return {goog.ui.Tooltip} Active tooltip in a child element, or null if none.
259 * @protected
260 */
261 goog.ui.Tooltip.prototype.getChildTooltip = function() {
262 return this.childTooltip_;
263 };
264
265
266 /**
267 * Attach to element. Tooltip will be displayed when the cursor is over the
268 * element or when the element has been active for a few milliseconds.
269 *
270 * @param {Element|string} el Element to display tooltip for, either element
271 * reference or string id.
272 */
273 goog.ui.Tooltip.prototype.attach = function(el) {
274 el = goog.dom.getElement(el);
275
276 this.elements_.add(el);
277 goog.events.listen(
278 el, goog.events.EventType.MOUSEOVER, this.handleMouseOver, false, this);
279 goog.events.listen(
280 el, goog.events.EventType.MOUSEOUT, this.handleMouseOutAndBlur, false,
281 this);
282 goog.events.listen(
283 el, goog.events.EventType.MOUSEMOVE, this.handleMouseMove, false, this);
284 goog.events.listen(
285 el, goog.events.EventType.FOCUS, this.handleFocus, false, this);
286 goog.events.listen(
287 el, goog.events.EventType.BLUR, this.handleMouseOutAndBlur, false, this);
288 };
289
290
291 /**
292 * Detach from element(s).
293 *
294 * @param {Element|string=} opt_el Element to detach from, either element
295 * reference or string id. If no element is
296 * specified all are detached.
297 */
298 goog.ui.Tooltip.prototype.detach = function(opt_el) {
299 if (opt_el) {
300 var el = goog.dom.getElement(opt_el);
301 this.detachElement_(el);
302 this.elements_.remove(el);
303 } else {
304 var a = this.elements_.getValues();
305 for (var el, i = 0; el = a[i]; i++) {
306 this.detachElement_(el);
307 }
308 this.elements_.clear();
309 }
310 };
311
312
313 /**
314 * Detach from element.
315 *
316 * @param {Element} el Element to detach from.
317 * @private
318 */
319 goog.ui.Tooltip.prototype.detachElement_ = function(el) {
320 goog.events.unlisten(
321 el, goog.events.EventType.MOUSEOVER, this.handleMouseOver, false, this);
322 goog.events.unlisten(
323 el, goog.events.EventType.MOUSEOUT, this.handleMouseOutAndBlur, false,
324 this);
325 goog.events.unlisten(
326 el, goog.events.EventType.MOUSEMOVE, this.handleMouseMove, false, this);
327 goog.events.unlisten(
328 el, goog.events.EventType.FOCUS, this.handleFocus, false, this);
329 goog.events.unlisten(
330 el, goog.events.EventType.BLUR, this.handleMouseOutAndBlur, false, this);
331 };
332
333
334 /**
335 * Sets delay in milliseconds before tooltip is displayed for an element.
336 *
337 * @param {number} delay The delay in milliseconds.
338 */
339 goog.ui.Tooltip.prototype.setShowDelayMs = function(delay) {
340 this.showDelayMs_ = delay;
341 };
342
343
344 /**
345 * @return {number} The delay in milliseconds before tooltip is displayed for an
346 * element.
347 */
348 goog.ui.Tooltip.prototype.getShowDelayMs = function() {
349 return this.showDelayMs_;
350 };
351
352
353 /**
354 * Sets delay in milliseconds before tooltip is hidden once the cursor leavs
355 * the element.
356 *
357 * @param {number} delay The delay in milliseconds.
358 */
359 goog.ui.Tooltip.prototype.setHideDelayMs = function(delay) {
360 this.hideDelayMs_ = delay;
361 };
362
363
364 /**
365 * @return {number} The delay in milliseconds before tooltip is hidden once the
366 * cursor leaves the element.
367 */
368 goog.ui.Tooltip.prototype.getHideDelayMs = function() {
369 return this.hideDelayMs_;
370 };
371
372
373 /**
374 * Sets tooltip message as plain text.
375 *
376 * @param {string} str Text message to display in tooltip.
377 */
378 goog.ui.Tooltip.prototype.setText = function(str) {
379 goog.dom.setTextContent(this.getElement(), str);
380 };
381
382
383 /**
384 * Sets tooltip message as HTML markup.
385 * @param {!goog.html.SafeHtml} html HTML message to display in tooltip.
386 */
387 goog.ui.Tooltip.prototype.setSafeHtml = function(html) {
388 var element = this.getElement();
389 if (element) {
390 goog.dom.safe.setInnerHtml(element, html);
391 }
392 };
393
394
395 /**
396 * Sets tooltip element.
397 *
398 * @param {Element} el HTML element to use as the tooltip.
399 * @override
400 */
401 goog.ui.Tooltip.prototype.setElement = function(el) {
402 var oldElement = this.getElement();
403 if (oldElement) {
404 goog.dom.removeNode(oldElement);
405 }
406 goog.ui.Tooltip.superClass_.setElement.call(this, el);
407 if (el) {
408 var body = this.dom_.getDocument().body;
409 body.insertBefore(el, body.lastChild);
410 this.registerContentFocusEvents_();
411 } else {
412 goog.dispose(this.tooltipFocusHandler_);
413 this.tooltipFocusHandler_ = null;
414 }
415 };
416
417
418 /**
419 * Handler for keyboard focus events of elements inside the tooltip's content
420 * element. This should only be invoked if this.getElement() != null.
421 * @private
422 */
423 goog.ui.Tooltip.prototype.registerContentFocusEvents_ = function() {
424 goog.dispose(this.tooltipFocusHandler_);
425 this.tooltipFocusHandler_ =
426 new goog.events.FocusHandler(goog.asserts.assert(this.getElement()));
427 this.registerDisposable(this.tooltipFocusHandler_);
428
429 goog.events.listen(
430 this.tooltipFocusHandler_, goog.events.FocusHandler.EventType.FOCUSIN,
431 this.clearHideTimer, undefined /* opt_capt */, this);
432 goog.events.listen(
433 this.tooltipFocusHandler_, goog.events.FocusHandler.EventType.FOCUSOUT,
434 this.startHideTimer, undefined /* opt_capt */, this);
435 };
436
437
438 /**
439 * @return {string} The tooltip message as plain text.
440 */
441 goog.ui.Tooltip.prototype.getText = function() {
442 return goog.dom.getTextContent(this.getElement());
443 };
444
445
446 /**
447 * @return {string} The tooltip message as HTML as plain string.
448 */
449 goog.ui.Tooltip.prototype.getHtml = function() {
450 return this.getElement().innerHTML;
451 };
452
453
454 /**
455 * @return {goog.ui.Tooltip.State} Current state of tooltip.
456 */
457 goog.ui.Tooltip.prototype.getState = function() {
458 return this.showTimer ?
459 (this.isVisible() ? goog.ui.Tooltip.State.UPDATING :
460 goog.ui.Tooltip.State.WAITING_TO_SHOW) :
461 this.hideTimer ? goog.ui.Tooltip.State.WAITING_TO_HIDE :
462 this.isVisible() ? goog.ui.Tooltip.State.SHOWING :
463 goog.ui.Tooltip.State.INACTIVE;
464 };
465
466
467 /**
468 * Sets whether tooltip requires the mouse to have moved or the anchor receive
469 * focus before the tooltip will be shown.
470 * @param {boolean} requireInteraction Whether tooltip should require some user
471 * interaction before showing tooltip.
472 */
473 goog.ui.Tooltip.prototype.setRequireInteraction = function(requireInteraction) {
474 this.requireInteraction_ = requireInteraction;
475 };
476
477
478 /**
479 * Returns true if the coord is in the tooltip.
480 * @param {goog.math.Coordinate} coord Coordinate being tested.
481 * @return {boolean} Whether the coord is in the tooltip.
482 */
483 goog.ui.Tooltip.prototype.isCoordinateInTooltip = function(coord) {
484 // Check if coord is inside the the tooltip
485 if (!this.isVisible()) {
486 return false;
487 }
488
489 var offset = goog.style.getPageOffset(this.getElement());
490 var size = goog.style.getSize(this.getElement());
491 return offset.x <= coord.x && coord.x <= offset.x + size.width &&
492 offset.y <= coord.y && coord.y <= offset.y + size.height;
493 };
494
495
496 /**
497 * Called before the popup is shown.
498 *
499 * @return {boolean} Whether tooltip should be shown.
500 * @protected
501 * @override
502 */
503 goog.ui.Tooltip.prototype.onBeforeShow = function() {
504 if (!goog.ui.PopupBase.prototype.onBeforeShow.call(this)) {
505 return false;
506 }
507
508 // Hide all open tooltips except if this tooltip is triggered by an element
509 // inside another tooltip.
510 if (this.anchor) {
511 for (var tt, i = 0; tt = goog.ui.Tooltip.activeInstances_[i]; i++) {
512 if (!goog.dom.contains(tt.getElement(), this.anchor)) {
513 tt.setVisible(false);
514 }
515 }
516 }
517 goog.array.insert(goog.ui.Tooltip.activeInstances_, this);
518
519 var element = this.getElement();
520 element.className = this.className;
521 this.clearHideTimer();
522
523 // Register event handlers for tooltip. Used to prevent the tooltip from
524 // closing if the cursor is over the tooltip rather then the element that
525 // triggered it.
526 goog.events.listen(
527 element, goog.events.EventType.MOUSEOVER, this.handleTooltipMouseOver,
528 false, this);
529 goog.events.listen(
530 element, goog.events.EventType.MOUSEOUT, this.handleTooltipMouseOut,
531 false, this);
532
533 this.clearShowTimer();
534 return true;
535 };
536
537
538 /** @override */
539 goog.ui.Tooltip.prototype.onHide = function() {
540 goog.array.remove(goog.ui.Tooltip.activeInstances_, this);
541
542 // Hide all open tooltips triggered by an element inside this tooltip.
543 var element = this.getElement();
544 for (var tt, i = 0; tt = goog.ui.Tooltip.activeInstances_[i]; i++) {
545 if (tt.anchor && goog.dom.contains(element, tt.anchor)) {
546 tt.setVisible(false);
547 }
548 }
549
550 // If this tooltip is inside another tooltip, start hide timer for that
551 // tooltip in case this tooltip was the only reason it was still showing.
552 if (this.parentTooltip_) {
553 this.parentTooltip_.startHideTimer();
554 }
555
556 goog.events.unlisten(
557 element, goog.events.EventType.MOUSEOVER, this.handleTooltipMouseOver,
558 false, this);
559 goog.events.unlisten(
560 element, goog.events.EventType.MOUSEOUT, this.handleTooltipMouseOut,
561 false, this);
562
563 this.anchor = undefined;
564 // If we are still waiting to show a different hovercard, don't abort it
565 // because you think you haven't seen a mouse move:
566 if (this.getState() == goog.ui.Tooltip.State.INACTIVE) {
567 this.seenInteraction_ = false;
568 }
569
570 goog.ui.PopupBase.prototype.onHide.call(this);
571 };
572
573
574 /**
575 * Called by timer from mouse over handler. Shows tooltip if cursor is still
576 * over the same element.
577 *
578 * @param {Element} el Element to show tooltip for.
579 * @param {goog.positioning.AbstractPosition=} opt_pos Position to display popup
580 * at.
581 */
582 goog.ui.Tooltip.prototype.maybeShow = function(el, opt_pos) {
583 // Assert that the mouse is still over the same element, and that we have not
584 // detached from the anchor in the meantime.
585 if (this.anchor == el && this.elements_.contains(this.anchor)) {
586 if (this.seenInteraction_ || !this.requireInteraction_) {
587 // If it is currently showing, then hide it, and abort if it doesn't hide.
588 this.setVisible(false);
589 if (!this.isVisible()) {
590 this.positionAndShow_(el, opt_pos);
591 }
592 } else {
593 this.anchor = undefined;
594 }
595 }
596 this.showTimer = undefined;
597 };
598
599
600 /**
601 * @return {goog.structs.Set} Elements this widget is attached to.
602 * @protected
603 */
604 goog.ui.Tooltip.prototype.getElements = function() {
605 return this.elements_;
606 };
607
608
609 /**
610 * @return {Element} Active element reference.
611 */
612 goog.ui.Tooltip.prototype.getActiveElement = function() {
613 return this.activeEl_;
614 };
615
616
617 /**
618 * @param {Element} activeEl Active element reference.
619 * @protected
620 */
621 goog.ui.Tooltip.prototype.setActiveElement = function(activeEl) {
622 this.activeEl_ = activeEl;
623 };
624
625
626 /**
627 * Shows tooltip for a specific element.
628 *
629 * @param {Element} el Element to show tooltip for.
630 * @param {goog.positioning.AbstractPosition=} opt_pos Position to display popup
631 * at.
632 */
633 goog.ui.Tooltip.prototype.showForElement = function(el, opt_pos) {
634 this.attach(el);
635 this.activeEl_ = el;
636
637 this.positionAndShow_(el, opt_pos);
638 };
639
640
641 /**
642 * Sets tooltip position and shows it.
643 *
644 * @param {Element} el Element to show tooltip for.
645 * @param {goog.positioning.AbstractPosition=} opt_pos Position to display popup
646 * at.
647 * @private
648 */
649 goog.ui.Tooltip.prototype.positionAndShow_ = function(el, opt_pos) {
650 this.anchor = el;
651 this.setPosition(
652 opt_pos ||
653 this.getPositioningStrategy(goog.ui.Tooltip.Activation.CURSOR));
654 this.setVisible(true);
655 };
656
657
658 /**
659 * Called by timer from mouse out handler. Hides tooltip if cursor is still
660 * outside element and tooltip, or if a child of tooltip has the focus.
661 * @param {?Element|undefined} el Tooltip's anchor when hide timer was started.
662 */
663 goog.ui.Tooltip.prototype.maybeHide = function(el) {
664 this.hideTimer = undefined;
665 if (el == this.anchor) {
666 var dom = this.getDomHelper();
667 var focusedEl = dom.getActiveElement();
668 // If the tooltip content is focused, then don't hide the tooltip.
669 var tooltipContentFocused = focusedEl && this.getElement() &&
670 dom.contains(this.getElement(), focusedEl);
671 if ((this.activeEl_ == null ||
672 (this.activeEl_ != this.getElement() &&
673 !this.elements_.contains(this.activeEl_))) &&
674 !tooltipContentFocused && !this.hasActiveChild()) {
675 this.setVisible(false);
676 }
677 }
678 };
679
680
681 /**
682 * @return {boolean} Whether tooltip element contains an active child tooltip,
683 * and should thus not be hidden. When the child tooltip is hidden, it
684 * will check if the parent should be hidden, too.
685 * @protected
686 */
687 goog.ui.Tooltip.prototype.hasActiveChild = function() {
688 return !!(this.childTooltip_ && this.childTooltip_.activeEl_);
689 };
690
691
692 /**
693 * Saves the current mouse cursor position to {@code this.cursorPosition}.
694 * @param {goog.events.BrowserEvent} event MOUSEOVER or MOUSEMOVE event.
695 * @private
696 */
697 goog.ui.Tooltip.prototype.saveCursorPosition_ = function(event) {
698 var scroll = this.dom_.getDocumentScroll();
699 this.cursorPosition.x = event.clientX + scroll.x;
700 this.cursorPosition.y = event.clientY + scroll.y;
701 };
702
703
704 /**
705 * Handler for mouse over events.
706 *
707 * @param {goog.events.BrowserEvent} event Event object.
708 * @protected
709 */
710 goog.ui.Tooltip.prototype.handleMouseOver = function(event) {
711 var el = this.getAnchorFromElement(/** @type {Element} */ (event.target));
712 this.activeEl_ = el;
713 this.clearHideTimer();
714 if (el != this.anchor) {
715 this.anchor = el;
716 this.startShowTimer(el);
717 this.checkForParentTooltip_();
718 this.saveCursorPosition_(event);
719 }
720 };
721
722
723 /**
724 * Find anchor containing the given element, if any.
725 *
726 * @param {Element} el Element that triggered event.
727 * @return {Element} Element in elements_ array that contains given element,
728 * or null if not found.
729 * @protected
730 */
731 goog.ui.Tooltip.prototype.getAnchorFromElement = function(el) {
732 // FireFox has a bug where mouse events relating to <input> elements are
733 // sometimes duplicated (often in FF2, rarely in FF3): once for the
734 // <input> element and once for a magic hidden <div> element. Javascript
735 // code does not have sufficient permissions to read properties on that
736 // magic element and thus will throw an error in this call to
737 // getAnchorFromElement_(). In that case we swallow the error.
738 // See https://bugzilla.mozilla.org/show_bug.cgi?id=330961
739 try {
740 while (el && !this.elements_.contains(el)) {
741 el = /** @type {Element} */ (el.parentNode);
742 }
743 return el;
744 } catch (e) {
745 return null;
746 }
747 };
748
749
750 /**
751 * Handler for mouse move events.
752 *
753 * @param {goog.events.BrowserEvent} event MOUSEMOVE event.
754 * @protected
755 */
756 goog.ui.Tooltip.prototype.handleMouseMove = function(event) {
757 this.saveCursorPosition_(event);
758 this.seenInteraction_ = true;
759 };
760
761
762 /**
763 * Handler for focus events.
764 *
765 * @param {goog.events.BrowserEvent} event Event object.
766 * @protected
767 */
768 goog.ui.Tooltip.prototype.handleFocus = function(event) {
769 var el = this.getAnchorFromElement(/** @type {Element} */ (event.target));
770 this.activeEl_ = el;
771 this.seenInteraction_ = true;
772
773 if (this.anchor != el) {
774 this.anchor = el;
775 var pos = this.getPositioningStrategy(goog.ui.Tooltip.Activation.FOCUS);
776 this.clearHideTimer();
777 this.startShowTimer(el, pos);
778
779 this.checkForParentTooltip_();
780 }
781 };
782
783
784 /**
785 * Return a Position instance for repositioning the tooltip. Override in
786 * subclasses to customize the way repositioning is done.
787 *
788 * @param {goog.ui.Tooltip.Activation} activationType Information about what
789 * kind of event caused the popup to be shown.
790 * @return {!goog.positioning.AbstractPosition} The position object used
791 * to position the tooltip.
792 * @protected
793 */
794 goog.ui.Tooltip.prototype.getPositioningStrategy = function(activationType) {
795 if (activationType == goog.ui.Tooltip.Activation.CURSOR) {
796 var coord = this.cursorPosition.clone();
797 return new goog.ui.Tooltip.CursorTooltipPosition(coord);
798 }
799 return new goog.ui.Tooltip.ElementTooltipPosition(this.activeEl_);
800 };
801
802
803 /**
804 * Looks for an active tooltip whose element contains this tooltip's anchor.
805 * This allows us to prevent hides until they are really necessary.
806 *
807 * @private
808 */
809 goog.ui.Tooltip.prototype.checkForParentTooltip_ = function() {
810 if (this.anchor) {
811 for (var tt, i = 0; tt = goog.ui.Tooltip.activeInstances_[i]; i++) {
812 if (goog.dom.contains(tt.getElement(), this.anchor)) {
813 tt.childTooltip_ = this;
814 this.parentTooltip_ = tt;
815 }
816 }
817 }
818 };
819
820
821 /**
822 * Handler for mouse out and blur events.
823 *
824 * @param {goog.events.BrowserEvent} event Event object.
825 * @protected
826 */
827 goog.ui.Tooltip.prototype.handleMouseOutAndBlur = function(event) {
828 var el = this.getAnchorFromElement(/** @type {Element} */ (event.target));
829 var elTo = this.getAnchorFromElement(
830 /** @type {Element} */ (event.relatedTarget));
831 if (el == elTo) {
832 // We haven't really left the anchor, just moved from one child to
833 // another.
834 return;
835 }
836
837 if (el == this.activeEl_) {
838 this.activeEl_ = null;
839 }
840
841 this.clearShowTimer();
842 this.seenInteraction_ = false;
843 if (this.isVisible() &&
844 (!event.relatedTarget ||
845 !goog.dom.contains(this.getElement(), event.relatedTarget))) {
846 this.startHideTimer();
847 } else {
848 this.anchor = undefined;
849 }
850 };
851
852
853 /**
854 * Handler for mouse over events for the tooltip element.
855 *
856 * @param {goog.events.BrowserEvent} event Event object.
857 * @protected
858 */
859 goog.ui.Tooltip.prototype.handleTooltipMouseOver = function(event) {
860 var element = this.getElement();
861 if (this.activeEl_ != element) {
862 this.clearHideTimer();
863 this.activeEl_ = element;
864 }
865 };
866
867
868 /**
869 * Handler for mouse out events for the tooltip element.
870 *
871 * @param {goog.events.BrowserEvent} event Event object.
872 * @protected
873 */
874 goog.ui.Tooltip.prototype.handleTooltipMouseOut = function(event) {
875 var element = this.getElement();
876 if (this.activeEl_ == element &&
877 (!event.relatedTarget ||
878 !goog.dom.contains(element, event.relatedTarget))) {
879 this.activeEl_ = null;
880 this.startHideTimer();
881 }
882 };
883
884
885 /**
886 * Helper method, starts timer that calls maybeShow. Parameters are passed to
887 * the maybeShow method.
888 *
889 * @param {Element} el Element to show tooltip for.
890 * @param {goog.positioning.AbstractPosition=} opt_pos Position to display popup
891 * at.
892 * @protected
893 */
894 goog.ui.Tooltip.prototype.startShowTimer = function(el, opt_pos) {
895 if (!this.showTimer) {
896 this.showTimer = goog.Timer.callOnce(
897 goog.bind(this.maybeShow, this, el, opt_pos), this.showDelayMs_);
898 }
899 };
900
901
902 /**
903 * Helper method called to clear the show timer.
904 *
905 * @protected
906 */
907 goog.ui.Tooltip.prototype.clearShowTimer = function() {
908 if (this.showTimer) {
909 goog.Timer.clear(this.showTimer);
910 this.showTimer = undefined;
911 }
912 };
913
914
915 /**
916 * Helper method called to start the close timer.
917 * @protected
918 */
919 goog.ui.Tooltip.prototype.startHideTimer = function() {
920 if (this.getState() == goog.ui.Tooltip.State.SHOWING) {
921 this.hideTimer = goog.Timer.callOnce(
922 goog.bind(this.maybeHide, this, this.anchor), this.getHideDelayMs());
923 }
924 };
925
926
927 /**
928 * Helper method called to clear the close timer.
929 * @protected
930 */
931 goog.ui.Tooltip.prototype.clearHideTimer = function() {
932 if (this.hideTimer) {
933 goog.Timer.clear(this.hideTimer);
934 this.hideTimer = undefined;
935 }
936 };
937
938
939 /** @override */
940 goog.ui.Tooltip.prototype.disposeInternal = function() {
941 this.setVisible(false);
942 this.clearShowTimer();
943 this.detach();
944 if (this.getElement()) {
945 goog.dom.removeNode(this.getElement());
946 }
947 this.activeEl_ = null;
948 delete this.dom_;
949 goog.ui.Tooltip.superClass_.disposeInternal.call(this);
950 };
951
952
953
954 /**
955 * Popup position implementation that positions the popup (the tooltip in this
956 * case) based on the cursor position. It's positioned below the cursor to the
957 * right if there's enough room to fit all of it inside the Viewport. Otherwise
958 * it's displayed as far right as possible either above or below the element.
959 *
960 * Used to position tooltips triggered by the cursor.
961 *
962 * @param {number|!goog.math.Coordinate} arg1 Left position or coordinate.
963 * @param {number=} opt_arg2 Top position.
964 * @constructor
965 * @extends {goog.positioning.ViewportPosition}
966 * @final
967 */
968 goog.ui.Tooltip.CursorTooltipPosition = function(arg1, opt_arg2) {
969 goog.positioning.ViewportPosition.call(this, arg1, opt_arg2);
970 };
971 goog.inherits(
972 goog.ui.Tooltip.CursorTooltipPosition, goog.positioning.ViewportPosition);
973
974
975 /**
976 * Repositions the popup based on cursor position.
977 *
978 * @param {Element} element The DOM element of the popup.
979 * @param {goog.positioning.Corner} popupCorner The corner of the popup element
980 * that that should be positioned adjacent to the anchorElement.
981 * @param {goog.math.Box=} opt_margin A margin specified in pixels.
982 * @override
983 */
984 goog.ui.Tooltip.CursorTooltipPosition.prototype.reposition = function(
985 element, popupCorner, opt_margin) {
986 var viewportElt = goog.style.getClientViewportElement(element);
987 var viewport = goog.style.getVisibleRectForElement(viewportElt);
988 var margin = opt_margin ?
989 new goog.math.Box(
990 opt_margin.top + 10, opt_margin.right, opt_margin.bottom,
991 opt_margin.left + 10) :
992 new goog.math.Box(10, 0, 0, 10);
993
994 if (goog.positioning.positionAtCoordinate(
995 this.coordinate, element, goog.positioning.Corner.TOP_START, margin,
996 viewport, goog.positioning.Overflow.ADJUST_X |
997 goog.positioning.Overflow.FAIL_Y) &
998 goog.positioning.OverflowStatus.FAILED) {
999 goog.positioning.positionAtCoordinate(
1000 this.coordinate, element, goog.positioning.Corner.TOP_START, margin,
1001 viewport, goog.positioning.Overflow.ADJUST_X |
1002 goog.positioning.Overflow.ADJUST_Y);
1003 }
1004 };
1005
1006
1007
1008 /**
1009 * Popup position implementation that positions the popup (the tooltip in this
1010 * case) based on the element position. It's positioned below the element to the
1011 * right if there's enough room to fit all of it inside the Viewport. Otherwise
1012 * it's displayed as far right as possible either above or below the element.
1013 *
1014 * Used to position tooltips triggered by focus changes.
1015 *
1016 * @param {Element} element The element to anchor the popup at.
1017 * @constructor
1018 * @extends {goog.positioning.AnchoredPosition}
1019 */
1020 goog.ui.Tooltip.ElementTooltipPosition = function(element) {
1021 goog.positioning.AnchoredPosition.call(
1022 this, element, goog.positioning.Corner.BOTTOM_RIGHT);
1023 };
1024 goog.inherits(
1025 goog.ui.Tooltip.ElementTooltipPosition, goog.positioning.AnchoredPosition);
1026
1027
1028 /**
1029 * Repositions the popup based on element position.
1030 *
1031 * @param {Element} element The DOM element of the popup.
1032 * @param {goog.positioning.Corner} popupCorner The corner of the popup element
1033 * that should be positioned adjacent to the anchorElement.
1034 * @param {goog.math.Box=} opt_margin A margin specified in pixels.
1035 * @override
1036 */
1037 goog.ui.Tooltip.ElementTooltipPosition.prototype.reposition = function(
1038 element, popupCorner, opt_margin) {
1039 var offset = new goog.math.Coordinate(10, 0);
1040
1041 if (goog.positioning.positionAtAnchor(
1042 this.element, this.corner, element, popupCorner, offset, opt_margin,
1043 goog.positioning.Overflow.ADJUST_X |
1044 goog.positioning.Overflow.FAIL_Y) &
1045 goog.positioning.OverflowStatus.FAILED) {
1046 goog.positioning.positionAtAnchor(
1047 this.element, goog.positioning.Corner.TOP_RIGHT, element,
1048 goog.positioning.Corner.BOTTOM_LEFT, offset, opt_margin,
1049 goog.positioning.Overflow.ADJUST_X |
1050 goog.positioning.Overflow.ADJUST_Y);
1051 }
1052 };