12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052 |
- goog.provide('goog.ui.Tooltip');
- goog.provide('goog.ui.Tooltip.CursorTooltipPosition');
- goog.provide('goog.ui.Tooltip.ElementTooltipPosition');
- goog.provide('goog.ui.Tooltip.State');
- goog.require('goog.Timer');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.dom.safe');
- goog.require('goog.events');
- goog.require('goog.events.EventType');
- goog.require('goog.events.FocusHandler');
- goog.require('goog.math.Box');
- goog.require('goog.math.Coordinate');
- goog.require('goog.positioning');
- goog.require('goog.positioning.AnchoredPosition');
- goog.require('goog.positioning.Corner');
- goog.require('goog.positioning.Overflow');
- goog.require('goog.positioning.OverflowStatus');
- goog.require('goog.positioning.ViewportPosition');
- goog.require('goog.structs.Set');
- goog.require('goog.style');
- goog.require('goog.ui.Popup');
- goog.require('goog.ui.PopupBase');
- goog.ui.Tooltip = function(opt_el, opt_str, opt_domHelper) {
-
- this.dom_ = opt_domHelper ||
- (opt_el ? goog.dom.getDomHelper(goog.dom.getElement(opt_el)) :
- goog.dom.getDomHelper());
- goog.ui.Popup.call(
- this,
- this.dom_.createDom(
- goog.dom.TagName.DIV, {'style': 'position:absolute;display:none;'}));
-
- this.cursorPosition = new goog.math.Coordinate(1, 1);
-
- this.elements_ = new goog.structs.Set();
-
- this.tooltipFocusHandler_ = null;
-
- if (opt_el) {
- this.attach(opt_el);
- }
-
- if (opt_str != null) {
- this.setText(opt_str);
- }
- };
- goog.inherits(goog.ui.Tooltip, goog.ui.Popup);
- goog.tagUnsealableClass(goog.ui.Tooltip);
- goog.ui.Tooltip.activeInstances_ = [];
- goog.ui.Tooltip.prototype.activeEl_ = null;
- goog.ui.Tooltip.prototype.className = goog.getCssName('goog-tooltip');
- goog.ui.Tooltip.prototype.showDelayMs_ = 500;
- goog.ui.Tooltip.prototype.showTimer;
- goog.ui.Tooltip.prototype.hideDelayMs_ = 0;
- goog.ui.Tooltip.prototype.hideTimer;
- goog.ui.Tooltip.prototype.anchor;
- goog.ui.Tooltip.State = {
- INACTIVE: 0,
- WAITING_TO_SHOW: 1,
- SHOWING: 2,
- WAITING_TO_HIDE: 3,
- UPDATING: 4
- };
- goog.ui.Tooltip.Activation = {
- CURSOR: 0,
- FOCUS: 1
- };
- goog.ui.Tooltip.prototype.seenInteraction_;
- goog.ui.Tooltip.prototype.requireInteraction_;
- goog.ui.Tooltip.prototype.childTooltip_;
- goog.ui.Tooltip.prototype.parentTooltip_;
- goog.ui.Tooltip.prototype.getDomHelper = function() {
- return this.dom_;
- };
- goog.ui.Tooltip.prototype.getChildTooltip = function() {
- return this.childTooltip_;
- };
- goog.ui.Tooltip.prototype.attach = function(el) {
- el = goog.dom.getElement(el);
- this.elements_.add(el);
- goog.events.listen(
- el, goog.events.EventType.MOUSEOVER, this.handleMouseOver, false, this);
- goog.events.listen(
- el, goog.events.EventType.MOUSEOUT, this.handleMouseOutAndBlur, false,
- this);
- goog.events.listen(
- el, goog.events.EventType.MOUSEMOVE, this.handleMouseMove, false, this);
- goog.events.listen(
- el, goog.events.EventType.FOCUS, this.handleFocus, false, this);
- goog.events.listen(
- el, goog.events.EventType.BLUR, this.handleMouseOutAndBlur, false, this);
- };
- goog.ui.Tooltip.prototype.detach = function(opt_el) {
- if (opt_el) {
- var el = goog.dom.getElement(opt_el);
- this.detachElement_(el);
- this.elements_.remove(el);
- } else {
- var a = this.elements_.getValues();
- for (var el, i = 0; el = a[i]; i++) {
- this.detachElement_(el);
- }
- this.elements_.clear();
- }
- };
- goog.ui.Tooltip.prototype.detachElement_ = function(el) {
- goog.events.unlisten(
- el, goog.events.EventType.MOUSEOVER, this.handleMouseOver, false, this);
- goog.events.unlisten(
- el, goog.events.EventType.MOUSEOUT, this.handleMouseOutAndBlur, false,
- this);
- goog.events.unlisten(
- el, goog.events.EventType.MOUSEMOVE, this.handleMouseMove, false, this);
- goog.events.unlisten(
- el, goog.events.EventType.FOCUS, this.handleFocus, false, this);
- goog.events.unlisten(
- el, goog.events.EventType.BLUR, this.handleMouseOutAndBlur, false, this);
- };
- goog.ui.Tooltip.prototype.setShowDelayMs = function(delay) {
- this.showDelayMs_ = delay;
- };
- goog.ui.Tooltip.prototype.getShowDelayMs = function() {
- return this.showDelayMs_;
- };
- goog.ui.Tooltip.prototype.setHideDelayMs = function(delay) {
- this.hideDelayMs_ = delay;
- };
- goog.ui.Tooltip.prototype.getHideDelayMs = function() {
- return this.hideDelayMs_;
- };
- goog.ui.Tooltip.prototype.setText = function(str) {
- goog.dom.setTextContent(this.getElement(), str);
- };
- goog.ui.Tooltip.prototype.setSafeHtml = function(html) {
- var element = this.getElement();
- if (element) {
- goog.dom.safe.setInnerHtml(element, html);
- }
- };
- goog.ui.Tooltip.prototype.setElement = function(el) {
- var oldElement = this.getElement();
- if (oldElement) {
- goog.dom.removeNode(oldElement);
- }
- goog.ui.Tooltip.superClass_.setElement.call(this, el);
- if (el) {
- var body = this.dom_.getDocument().body;
- body.insertBefore(el, body.lastChild);
- this.registerContentFocusEvents_();
- } else {
- goog.dispose(this.tooltipFocusHandler_);
- this.tooltipFocusHandler_ = null;
- }
- };
- goog.ui.Tooltip.prototype.registerContentFocusEvents_ = function() {
- goog.dispose(this.tooltipFocusHandler_);
- this.tooltipFocusHandler_ =
- new goog.events.FocusHandler(goog.asserts.assert(this.getElement()));
- this.registerDisposable(this.tooltipFocusHandler_);
- goog.events.listen(
- this.tooltipFocusHandler_, goog.events.FocusHandler.EventType.FOCUSIN,
- this.clearHideTimer, undefined , this);
- goog.events.listen(
- this.tooltipFocusHandler_, goog.events.FocusHandler.EventType.FOCUSOUT,
- this.startHideTimer, undefined , this);
- };
- goog.ui.Tooltip.prototype.getText = function() {
- return goog.dom.getTextContent(this.getElement());
- };
- goog.ui.Tooltip.prototype.getHtml = function() {
- return this.getElement().innerHTML;
- };
- goog.ui.Tooltip.prototype.getState = function() {
- return this.showTimer ?
- (this.isVisible() ? goog.ui.Tooltip.State.UPDATING :
- goog.ui.Tooltip.State.WAITING_TO_SHOW) :
- this.hideTimer ? goog.ui.Tooltip.State.WAITING_TO_HIDE :
- this.isVisible() ? goog.ui.Tooltip.State.SHOWING :
- goog.ui.Tooltip.State.INACTIVE;
- };
- goog.ui.Tooltip.prototype.setRequireInteraction = function(requireInteraction) {
- this.requireInteraction_ = requireInteraction;
- };
- goog.ui.Tooltip.prototype.isCoordinateInTooltip = function(coord) {
-
- if (!this.isVisible()) {
- return false;
- }
- var offset = goog.style.getPageOffset(this.getElement());
- var size = goog.style.getSize(this.getElement());
- return offset.x <= coord.x && coord.x <= offset.x + size.width &&
- offset.y <= coord.y && coord.y <= offset.y + size.height;
- };
- goog.ui.Tooltip.prototype.onBeforeShow = function() {
- if (!goog.ui.PopupBase.prototype.onBeforeShow.call(this)) {
- return false;
- }
-
-
- if (this.anchor) {
- for (var tt, i = 0; tt = goog.ui.Tooltip.activeInstances_[i]; i++) {
- if (!goog.dom.contains(tt.getElement(), this.anchor)) {
- tt.setVisible(false);
- }
- }
- }
- goog.array.insert(goog.ui.Tooltip.activeInstances_, this);
- var element = this.getElement();
- element.className = this.className;
- this.clearHideTimer();
-
-
-
- goog.events.listen(
- element, goog.events.EventType.MOUSEOVER, this.handleTooltipMouseOver,
- false, this);
- goog.events.listen(
- element, goog.events.EventType.MOUSEOUT, this.handleTooltipMouseOut,
- false, this);
- this.clearShowTimer();
- return true;
- };
- goog.ui.Tooltip.prototype.onHide = function() {
- goog.array.remove(goog.ui.Tooltip.activeInstances_, this);
-
- var element = this.getElement();
- for (var tt, i = 0; tt = goog.ui.Tooltip.activeInstances_[i]; i++) {
- if (tt.anchor && goog.dom.contains(element, tt.anchor)) {
- tt.setVisible(false);
- }
- }
-
-
- if (this.parentTooltip_) {
- this.parentTooltip_.startHideTimer();
- }
- goog.events.unlisten(
- element, goog.events.EventType.MOUSEOVER, this.handleTooltipMouseOver,
- false, this);
- goog.events.unlisten(
- element, goog.events.EventType.MOUSEOUT, this.handleTooltipMouseOut,
- false, this);
- this.anchor = undefined;
-
-
- if (this.getState() == goog.ui.Tooltip.State.INACTIVE) {
- this.seenInteraction_ = false;
- }
- goog.ui.PopupBase.prototype.onHide.call(this);
- };
- goog.ui.Tooltip.prototype.maybeShow = function(el, opt_pos) {
-
-
- if (this.anchor == el && this.elements_.contains(this.anchor)) {
- if (this.seenInteraction_ || !this.requireInteraction_) {
-
- this.setVisible(false);
- if (!this.isVisible()) {
- this.positionAndShow_(el, opt_pos);
- }
- } else {
- this.anchor = undefined;
- }
- }
- this.showTimer = undefined;
- };
- goog.ui.Tooltip.prototype.getElements = function() {
- return this.elements_;
- };
- goog.ui.Tooltip.prototype.getActiveElement = function() {
- return this.activeEl_;
- };
- goog.ui.Tooltip.prototype.setActiveElement = function(activeEl) {
- this.activeEl_ = activeEl;
- };
- goog.ui.Tooltip.prototype.showForElement = function(el, opt_pos) {
- this.attach(el);
- this.activeEl_ = el;
- this.positionAndShow_(el, opt_pos);
- };
- goog.ui.Tooltip.prototype.positionAndShow_ = function(el, opt_pos) {
- this.anchor = el;
- this.setPosition(
- opt_pos ||
- this.getPositioningStrategy(goog.ui.Tooltip.Activation.CURSOR));
- this.setVisible(true);
- };
- goog.ui.Tooltip.prototype.maybeHide = function(el) {
- this.hideTimer = undefined;
- if (el == this.anchor) {
- var dom = this.getDomHelper();
- var focusedEl = dom.getActiveElement();
-
- var tooltipContentFocused = focusedEl && this.getElement() &&
- dom.contains(this.getElement(), focusedEl);
- if ((this.activeEl_ == null ||
- (this.activeEl_ != this.getElement() &&
- !this.elements_.contains(this.activeEl_))) &&
- !tooltipContentFocused && !this.hasActiveChild()) {
- this.setVisible(false);
- }
- }
- };
- goog.ui.Tooltip.prototype.hasActiveChild = function() {
- return !!(this.childTooltip_ && this.childTooltip_.activeEl_);
- };
- goog.ui.Tooltip.prototype.saveCursorPosition_ = function(event) {
- var scroll = this.dom_.getDocumentScroll();
- this.cursorPosition.x = event.clientX + scroll.x;
- this.cursorPosition.y = event.clientY + scroll.y;
- };
- goog.ui.Tooltip.prototype.handleMouseOver = function(event) {
- var el = this.getAnchorFromElement( (event.target));
- this.activeEl_ = el;
- this.clearHideTimer();
- if (el != this.anchor) {
- this.anchor = el;
- this.startShowTimer(el);
- this.checkForParentTooltip_();
- this.saveCursorPosition_(event);
- }
- };
- goog.ui.Tooltip.prototype.getAnchorFromElement = function(el) {
-
-
-
-
-
-
-
- try {
- while (el && !this.elements_.contains(el)) {
- el = (el.parentNode);
- }
- return el;
- } catch (e) {
- return null;
- }
- };
- goog.ui.Tooltip.prototype.handleMouseMove = function(event) {
- this.saveCursorPosition_(event);
- this.seenInteraction_ = true;
- };
- goog.ui.Tooltip.prototype.handleFocus = function(event) {
- var el = this.getAnchorFromElement( (event.target));
- this.activeEl_ = el;
- this.seenInteraction_ = true;
- if (this.anchor != el) {
- this.anchor = el;
- var pos = this.getPositioningStrategy(goog.ui.Tooltip.Activation.FOCUS);
- this.clearHideTimer();
- this.startShowTimer(el, pos);
- this.checkForParentTooltip_();
- }
- };
- goog.ui.Tooltip.prototype.getPositioningStrategy = function(activationType) {
- if (activationType == goog.ui.Tooltip.Activation.CURSOR) {
- var coord = this.cursorPosition.clone();
- return new goog.ui.Tooltip.CursorTooltipPosition(coord);
- }
- return new goog.ui.Tooltip.ElementTooltipPosition(this.activeEl_);
- };
- goog.ui.Tooltip.prototype.checkForParentTooltip_ = function() {
- if (this.anchor) {
- for (var tt, i = 0; tt = goog.ui.Tooltip.activeInstances_[i]; i++) {
- if (goog.dom.contains(tt.getElement(), this.anchor)) {
- tt.childTooltip_ = this;
- this.parentTooltip_ = tt;
- }
- }
- }
- };
- goog.ui.Tooltip.prototype.handleMouseOutAndBlur = function(event) {
- var el = this.getAnchorFromElement( (event.target));
- var elTo = this.getAnchorFromElement(
- (event.relatedTarget));
- if (el == elTo) {
-
-
- return;
- }
- if (el == this.activeEl_) {
- this.activeEl_ = null;
- }
- this.clearShowTimer();
- this.seenInteraction_ = false;
- if (this.isVisible() &&
- (!event.relatedTarget ||
- !goog.dom.contains(this.getElement(), event.relatedTarget))) {
- this.startHideTimer();
- } else {
- this.anchor = undefined;
- }
- };
- goog.ui.Tooltip.prototype.handleTooltipMouseOver = function(event) {
- var element = this.getElement();
- if (this.activeEl_ != element) {
- this.clearHideTimer();
- this.activeEl_ = element;
- }
- };
- goog.ui.Tooltip.prototype.handleTooltipMouseOut = function(event) {
- var element = this.getElement();
- if (this.activeEl_ == element &&
- (!event.relatedTarget ||
- !goog.dom.contains(element, event.relatedTarget))) {
- this.activeEl_ = null;
- this.startHideTimer();
- }
- };
- goog.ui.Tooltip.prototype.startShowTimer = function(el, opt_pos) {
- if (!this.showTimer) {
- this.showTimer = goog.Timer.callOnce(
- goog.bind(this.maybeShow, this, el, opt_pos), this.showDelayMs_);
- }
- };
- goog.ui.Tooltip.prototype.clearShowTimer = function() {
- if (this.showTimer) {
- goog.Timer.clear(this.showTimer);
- this.showTimer = undefined;
- }
- };
- goog.ui.Tooltip.prototype.startHideTimer = function() {
- if (this.getState() == goog.ui.Tooltip.State.SHOWING) {
- this.hideTimer = goog.Timer.callOnce(
- goog.bind(this.maybeHide, this, this.anchor), this.getHideDelayMs());
- }
- };
- goog.ui.Tooltip.prototype.clearHideTimer = function() {
- if (this.hideTimer) {
- goog.Timer.clear(this.hideTimer);
- this.hideTimer = undefined;
- }
- };
- goog.ui.Tooltip.prototype.disposeInternal = function() {
- this.setVisible(false);
- this.clearShowTimer();
- this.detach();
- if (this.getElement()) {
- goog.dom.removeNode(this.getElement());
- }
- this.activeEl_ = null;
- delete this.dom_;
- goog.ui.Tooltip.superClass_.disposeInternal.call(this);
- };
- goog.ui.Tooltip.CursorTooltipPosition = function(arg1, opt_arg2) {
- goog.positioning.ViewportPosition.call(this, arg1, opt_arg2);
- };
- goog.inherits(
- goog.ui.Tooltip.CursorTooltipPosition, goog.positioning.ViewportPosition);
- goog.ui.Tooltip.CursorTooltipPosition.prototype.reposition = function(
- element, popupCorner, opt_margin) {
- var viewportElt = goog.style.getClientViewportElement(element);
- var viewport = goog.style.getVisibleRectForElement(viewportElt);
- var margin = opt_margin ?
- new goog.math.Box(
- opt_margin.top + 10, opt_margin.right, opt_margin.bottom,
- opt_margin.left + 10) :
- new goog.math.Box(10, 0, 0, 10);
- if (goog.positioning.positionAtCoordinate(
- this.coordinate, element, goog.positioning.Corner.TOP_START, margin,
- viewport, goog.positioning.Overflow.ADJUST_X |
- goog.positioning.Overflow.FAIL_Y) &
- goog.positioning.OverflowStatus.FAILED) {
- goog.positioning.positionAtCoordinate(
- this.coordinate, element, goog.positioning.Corner.TOP_START, margin,
- viewport, goog.positioning.Overflow.ADJUST_X |
- goog.positioning.Overflow.ADJUST_Y);
- }
- };
- goog.ui.Tooltip.ElementTooltipPosition = function(element) {
- goog.positioning.AnchoredPosition.call(
- this, element, goog.positioning.Corner.BOTTOM_RIGHT);
- };
- goog.inherits(
- goog.ui.Tooltip.ElementTooltipPosition, goog.positioning.AnchoredPosition);
- goog.ui.Tooltip.ElementTooltipPosition.prototype.reposition = function(
- element, popupCorner, opt_margin) {
- var offset = new goog.math.Coordinate(10, 0);
- if (goog.positioning.positionAtAnchor(
- this.element, this.corner, element, popupCorner, offset, opt_margin,
- goog.positioning.Overflow.ADJUST_X |
- goog.positioning.Overflow.FAIL_Y) &
- goog.positioning.OverflowStatus.FAILED) {
- goog.positioning.positionAtAnchor(
- this.element, goog.positioning.Corner.TOP_RIGHT, element,
- goog.positioning.Corner.BOTTOM_LEFT, offset, opt_margin,
- goog.positioning.Overflow.ADJUST_X |
- goog.positioning.Overflow.ADJUST_Y);
- }
- };
|