4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-tip-ToolTip'>/**
19 </span> * ToolTip is a {@link Ext.tip.Tip} implementation that handles the common case of displaying a
20 * tooltip when hovering over a certain element or elements on the page. It allows fine-grained
21 * control over the tooltip's alignment relative to the target element or mouse, and the timing
22 * of when it is automatically shown and hidden.
24 * This implementation does **not** have a built-in method of automatically populating the tooltip's
25 * text based on the target element; you must either configure a fixed {@link #html} value for each
26 * ToolTip instance, or implement custom logic (e.g. in a {@link #beforeshow} event listener) to
27 * generate the appropriate tooltip content on the fly. See {@link Ext.tip.QuickTip} for a more
28 * convenient way of automatically populating and configuring a tooltip based on specific DOM
29 * attributes of each target element.
33 * var tip = Ext.create('Ext.tip.ToolTip', {
34 * target: 'clearButton',
35 * html: 'Press this button to clear the form'
38 * {@img Ext.tip.ToolTip/Ext.tip.ToolTip1.png Basic Ext.tip.ToolTip}
42 * In addition to attaching a ToolTip to a single element, you can also use delegation to attach
43 * one ToolTip to many elements under a common parent. This is more efficient than creating many
44 * ToolTip instances. To do this, point the {@link #target} config to a common ancestor of all the
45 * elements, and then set the {@link #delegate} config to a CSS selector that will select all the
46 * appropriate sub-elements.
48 * When using delegation, it is likely that you will want to programmatically change the content
49 * of the ToolTip based on each delegate element; you can do this by implementing a custom
50 * listener for the {@link #beforeshow} event. Example:
52 * var store = Ext.create('Ext.data.ArrayStore', {
53 * fields: ['company', 'price', 'change'],
55 * ['3m Co', 71.72, 0.02],
56 * ['Alcoa Inc', 29.01, 0.42],
57 * ['Altria Group Inc', 83.81, 0.28],
58 * ['American Express Company', 52.55, 0.01],
59 * ['American International Group, Inc.', 64.13, 0.31],
60 * ['AT&T Inc.', 31.61, -0.48]
64 * var grid = Ext.create('Ext.grid.Panel', {
65 * title: 'Array Grid',
68 * {text: 'Company', flex: 1, dataIndex: 'company'},
69 * {text: 'Price', width: 75, dataIndex: 'price'},
70 * {text: 'Change', width: 75, dataIndex: 'change'}
74 * renderTo: Ext.getBody()
77 * grid.getView().on('render', function(view) {
78 * view.tip = Ext.create('Ext.tip.ToolTip', {
79 * // The overall target element.
81 * // Each grid row causes its own seperate show and hide.
82 * delegate: view.itemSelector,
83 * // Moving within the row should not hide the tip.
85 * // Render immediately so that tip.body can be referenced prior to the first show.
86 * renderTo: Ext.getBody(),
88 * // Change content dynamically depending on which element triggered the show.
89 * beforeshow: function updateTipBody(tip) {
90 * tip.update('Over company "' + view.getRecord(tip.triggerElement).get('company') + '"');
96 * {@img Ext.tip.ToolTip/Ext.tip.ToolTip2.png Ext.tip.ToolTip with delegation}
100 * The following configuration properties allow control over how the ToolTip is aligned relative to
101 * the target element and/or mouse pointer:
104 * - {@link #anchorToTarget}
105 * - {@link #anchorOffset}
106 * - {@link #trackMouse}
107 * - {@link #mouseOffset}
111 * The following configuration properties allow control over how and when the ToolTip is automatically
114 * - {@link #autoHide}
115 * - {@link #showDelay}
116 * - {@link #hideDelay}
117 * - {@link #dismissDelay}
119 * @docauthor Jason Johnston <jason@sencha.com>
121 Ext.define('Ext.tip.ToolTip', {
122 extend: 'Ext.tip.Tip',
123 alias: 'widget.tooltip',
124 alternateClassName: 'Ext.ToolTip',
125 <span id='Ext-tip-ToolTip-property-triggerElement'> /**
126 </span> * @property {HTMLElement} triggerElement
127 * When a ToolTip is configured with the `{@link #delegate}`
128 * option to cause selected child elements of the `{@link #target}`
129 * Element to each trigger a seperate show event, this property is set to
130 * the DOM element which triggered the show.
132 <span id='Ext-tip-ToolTip-cfg-target'> /**
133 </span> * @cfg {HTMLElement/Ext.Element/String} target
134 * The target element or string id to monitor for mouseover events to trigger
135 * showing this ToolTip.
137 <span id='Ext-tip-ToolTip-cfg-autoHide'> /**
138 </span> * @cfg {Boolean} [autoHide=true]
139 * True to automatically hide the tooltip after the
140 * mouse exits the target element or after the `{@link #dismissDelay}`
141 * has expired if set. If `{@link #closable} = true`
142 * a close tool button will be rendered into the tooltip header.
144 <span id='Ext-tip-ToolTip-cfg-showDelay'> /**
145 </span> * @cfg {Number} showDelay
146 * Delay in milliseconds before the tooltip displays after the mouse enters the target element.
149 <span id='Ext-tip-ToolTip-cfg-hideDelay'> /**
150 </span> * @cfg {Number} hideDelay
151 * Delay in milliseconds after the mouse exits the target element but before the tooltip actually hides.
152 * Set to 0 for the tooltip to hide immediately.
155 <span id='Ext-tip-ToolTip-cfg-dismissDelay'> /**
156 </span> * @cfg {Number} dismissDelay
157 * Delay in milliseconds before the tooltip automatically hides. To disable automatic hiding, set
161 <span id='Ext-tip-ToolTip-cfg-mouseOffset'> /**
162 </span> * @cfg {Number[]} [mouseOffset=[15,18]]
163 * An XY offset from the mouse position where the tooltip should be shown.
165 <span id='Ext-tip-ToolTip-cfg-trackMouse'> /**
166 </span> * @cfg {Boolean} trackMouse
167 * True to have the tooltip follow the mouse as it moves over the target element.
170 <span id='Ext-tip-ToolTip-cfg-anchor'> /**
171 </span> * @cfg {String} anchor
172 * If specified, indicates that the tip should be anchored to a
173 * particular side of the target element or mouse pointer ("top", "right", "bottom",
174 * or "left"), with an arrow pointing back at the target or mouse pointer. If
175 * {@link #constrainPosition} is enabled, this will be used as a preferred value
176 * only and may be flipped as needed.
178 <span id='Ext-tip-ToolTip-cfg-anchorToTarget'> /**
179 </span> * @cfg {Boolean} anchorToTarget
180 * True to anchor the tooltip to the target element, false to anchor it relative to the mouse coordinates.
181 * When `anchorToTarget` is true, use `{@link #defaultAlign}` to control tooltip alignment to the
182 * target element. When `anchorToTarget` is false, use `{@link #anchor}` instead to control alignment.
184 anchorToTarget: true,
185 <span id='Ext-tip-ToolTip-cfg-anchorOffset'> /**
186 </span> * @cfg {Number} anchorOffset
187 * A numeric pixel value used to offset the default position of the anchor arrow. When the anchor
188 * position is on the top or bottom of the tooltip, `anchorOffset` will be used as a horizontal offset.
189 * Likewise, when the anchor position is on the left or right side, `anchorOffset` will be used as
193 <span id='Ext-tip-ToolTip-cfg-delegate'> /**
194 </span> * @cfg {String} delegate
196 * A {@link Ext.DomQuery DomQuery} selector which allows selection of individual elements within the
197 * `{@link #target}` element to trigger showing and hiding the ToolTip as the mouse moves within the
200 * When specified, the child element of the target which caused a show event is placed into the
201 * `{@link #triggerElement}` property before the ToolTip is shown.
203 * This may be useful when a Component has regular, repeating elements in it, each of which need a
204 * ToolTip which contains information specific to that element.
206 * See the delegate example in class documentation of {@link Ext.tip.ToolTip}.
211 quickShowInterval: 250,
214 initComponent: function() {
216 me.callParent(arguments);
217 me.lastActive = new Date();
218 me.setTarget(me.target);
219 me.origAnchor = me.anchor;
223 onRender: function(ct, position) {
225 me.callParent(arguments);
226 me.anchorCls = Ext.baseCSSPrefix + 'tip-anchor-' + me.getAnchorPosition();
227 me.anchorEl = me.el.createChild({
228 cls: Ext.baseCSSPrefix + 'tip-anchor ' + me.anchorCls
233 afterRender: function() {
237 me.callParent(arguments);
238 zIndex = parseInt(me.el.getZIndex(), 10) || 0;
239 me.anchorEl.setStyle('z-index', zIndex + 1).setVisibilityMode(Ext.Element.DISPLAY);
242 <span id='Ext-tip-ToolTip-method-setTarget'> /**
243 </span> * Binds this ToolTip to the specified element. The tooltip will be displayed when the mouse moves over the element.
244 * @param {String/HTMLElement/Ext.Element} t The Element, HtmlElement, or ID of an element to bind to
246 setTarget: function(target) {
252 tg = Ext.get(me.target);
253 me.mun(tg, 'mouseover', me.onTargetOver, me);
254 me.mun(tg, 'mouseout', me.onTargetOut, me);
255 me.mun(tg, 'mousemove', me.onMouseMove, me);
262 // TODO - investigate why IE6/7 seem to fire recursive resize in e.getXY
263 // breaking QuickTip#onTargetOver (EXTJSIV-1608)
266 mouseover: me.onTargetOver,
267 mouseout: me.onTargetOut,
268 mousemove: me.onMouseMove,
273 me.anchorTarget = me.target;
278 onMouseMove: function(e) {
280 t = me.delegate ? e.getTarget(me.delegate) : me.triggerElement = true,
283 me.targetXY = e.getXY();
284 if (t === me.triggerElement) {
285 if (!me.hidden && me.trackMouse) {
286 xy = me.getTargetXY();
287 if (me.constrainPosition) {
288 xy = me.el.adjustForConstraints(xy, me.el.getScopeParent());
290 me.setPagePosition(xy);
294 me.lastActive = new Date(0);
297 } else if ((!me.closable && me.isVisible()) && me.autoHide !== false) {
303 getTargetXY: function() {
307 me.anchorTarget = me.triggerElement;
311 var offsets = me.getOffsets(),
312 xy = (me.anchorToTarget && !me.trackMouse) ? me.el.getAlignToXY(me.anchorTarget, me.getAnchorAlign()) : me.targetXY,
313 dw = Ext.Element.getViewWidth() - 5,
314 dh = Ext.Element.getViewHeight() - 5,
315 de = document.documentElement,
317 scrollX = (de.scrollLeft || bd.scrollLeft || 0) + 5,
318 scrollY = (de.scrollTop || bd.scrollTop || 0) + 5,
319 axy = [xy[0] + offsets[0], xy[1] + offsets[1]],
321 constrainPosition = me.constrainPosition;
323 me.anchorEl.removeCls(me.anchorCls);
325 if (me.targetCounter < 2 && constrainPosition) {
326 if (axy[0] < scrollX) {
327 if (me.anchorToTarget) {
328 me.defaultAlign = 'l-r';
329 if (me.mouseOffset) {
330 me.mouseOffset[0] *= -1;
334 return me.getTargetXY();
336 if (axy[0] + sz.width > dw) {
337 if (me.anchorToTarget) {
338 me.defaultAlign = 'r-l';
339 if (me.mouseOffset) {
340 me.mouseOffset[0] *= -1;
344 return me.getTargetXY();
346 if (axy[1] < scrollY) {
347 if (me.anchorToTarget) {
348 me.defaultAlign = 't-b';
349 if (me.mouseOffset) {
350 me.mouseOffset[1] *= -1;
354 return me.getTargetXY();
356 if (axy[1] + sz.height > dh) {
357 if (me.anchorToTarget) {
358 me.defaultAlign = 'b-t';
359 if (me.mouseOffset) {
360 me.mouseOffset[1] *= -1;
363 me.anchor = 'bottom';
364 return me.getTargetXY();
368 me.anchorCls = Ext.baseCSSPrefix + 'tip-anchor-' + me.getAnchorPosition();
369 me.anchorEl.addCls(me.anchorCls);
370 me.targetCounter = 0;
373 mouseOffset = me.getMouseOffset();
374 return (me.targetXY) ? [me.targetXY[0] + mouseOffset[0], me.targetXY[1] + mouseOffset[1]] : mouseOffset;
378 getMouseOffset: function() {
380 offset = me.anchor ? [0, 0] : [15, 18];
381 if (me.mouseOffset) {
382 offset[0] += me.mouseOffset[0];
383 offset[1] += me.mouseOffset[1];
389 getAnchorPosition: function() {
393 me.tipAnchor = me.anchor.charAt(0);
395 m = me.defaultAlign.match(/^([a-z]+)-([a-z]+)(\?)?$/);
398 Ext.Error.raise('The AnchorTip.defaultAlign value "' + me.defaultAlign + '" is invalid.');
401 me.tipAnchor = m[1].charAt(0);
404 switch (me.tipAnchor) {
416 getAnchorAlign: function() {
417 switch (this.anchor) {
430 getOffsets: function() {
434 ap = me.getAnchorPosition().charAt(0);
435 if (me.anchorToTarget && !me.trackMouse) {
444 offsets = [ - 13, 0];
453 offsets = [ - 15 - me.anchorOffset, 30];
456 offsets = [ - 19 - me.anchorOffset, -13 - me.el.dom.offsetHeight];
459 offsets = [ - 15 - me.el.dom.offsetWidth, -13 - me.anchorOffset];
462 offsets = [25, -13 - me.anchorOffset];
466 mouseOffset = me.getMouseOffset();
467 offsets[0] += mouseOffset[0];
468 offsets[1] += mouseOffset[1];
474 onTargetOver: function(e) {
478 if (me.disabled || e.within(me.target.dom, true)) {
481 t = e.getTarget(me.delegate);
483 me.triggerElement = t;
484 me.clearTimer('hide');
485 me.targetXY = e.getXY();
491 delayShow: function() {
493 if (me.hidden && !me.showTimer) {
494 if (Ext.Date.getElapsed(me.lastActive) < me.quickShowInterval) {
497 me.showTimer = Ext.defer(me.show, me.showDelay, me);
500 else if (!me.hidden && me.autoHide !== false) {
506 onTargetOut: function(e) {
508 if (me.disabled || e.within(me.target.dom, true)) {
511 me.clearTimer('show');
512 if (me.autoHide !== false) {
518 delayHide: function() {
520 if (!me.hidden && !me.hideTimer) {
521 me.hideTimer = Ext.defer(me.hide, me.hideDelay, me);
525 <span id='Ext-tip-ToolTip-method-hide'> /**
526 </span> * Hides this tooltip if visible.
530 me.clearTimer('dismiss');
531 me.lastActive = new Date();
535 me.callParent(arguments);
536 delete me.triggerElement;
539 <span id='Ext-tip-ToolTip-method-show'> /**
540 </span> * Shows this tooltip at the current event target XY position.
545 // Show this Component first, so that sizing can be calculated
546 // pre-show it off screen so that the el will have dimensions
548 if (this.hidden === false) {
549 me.setPagePosition(-10000, -10000);
552 me.anchor = me.origAnchor;
554 me.showAt(me.getTargetXY());
566 showAt: function(xy) {
568 me.lastActive = new Date();
571 // Only call if this is hidden. May have been called from show above.
572 if (!me.isVisible()) {
573 this.callParent(arguments);
576 // Show may have been vetoed.
577 if (me.isVisible()) {
578 me.setPagePosition(xy[0], xy[1]);
579 if (me.constrainPosition || me.constrain) {
585 if (me.dismissDelay && me.autoHide !== false) {
586 me.dismissTimer = Ext.defer(me.hide, me.dismissDelay, me);
590 if (!me.anchorEl.isVisible()) {
599 syncAnchor: function() {
604 switch (me.tipAnchor.charAt(0)) {
608 offset = [20 + me.anchorOffset, 1];
613 offset = [ - 1, 12 + me.anchorOffset];
618 offset = [20 + me.anchorOffset, -1];
623 offset = [1, 12 + me.anchorOffset];
626 me.anchorEl.alignTo(me.el, anchorPos + '-' + targetPos, offset);
630 setPagePosition: function(x, y) {
632 me.callParent(arguments);
639 clearTimer: function(name) {
640 name = name + 'Timer';
641 clearTimeout(this[name]);
646 clearTimers: function() {
648 me.clearTimer('show');
649 me.clearTimer('dismiss');
650 me.clearTimer('hide');
657 me.mon(Ext.getDoc(), 'mousedown', me.onDocMouseDown, me);
664 me.mun(Ext.getDoc(), 'mousedown', me.onDocMouseDown, me);
668 onDocMouseDown: function(e) {
670 if (me.autoHide !== true && !me.closable && !e.within(me.el.dom)) {
672 Ext.defer(me.doEnable, 100, me);
677 doEnable: function() {
678 if (!this.isDestroyed) {
684 onDisable: function() {
690 beforeDestroy: function() {
693 Ext.destroy(me.anchorEl);
696 delete me.anchorTarget;
697 delete me.triggerElement;
702 onDestroy: function() {
703 Ext.getDoc().un('mousedown', this.onDocMouseDown, this);