4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../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-method-constructor'><span id='Ext-tip-ToolTip'>/**
19 </span></span> * @class Ext.tip.ToolTip
20 * @extends Ext.tip.Tip
22 * ToolTip is a {@link Ext.tip.Tip} implementation that handles the common case of displaying a
23 * tooltip when hovering over a certain element or elements on the page. It allows fine-grained
24 * control over the tooltip's alignment relative to the target element or mouse, and the timing
25 * of when it is automatically shown and hidden.
27 * This implementation does **not** have a built-in method of automatically populating the tooltip's
28 * text based on the target element; you must either configure a fixed {@link #html} value for each
29 * ToolTip instance, or implement custom logic (e.g. in a {@link #beforeshow} event listener) to
30 * generate the appropriate tooltip content on the fly. See {@link Ext.tip.QuickTip} for a more
31 * convenient way of automatically populating and configuring a tooltip based on specific DOM
32 * attributes of each target element.
36 * var tip = Ext.create('Ext.tip.ToolTip', {
37 * target: 'clearButton',
38 * html: 'Press this button to clear the form'
41 * {@img Ext.tip.ToolTip/Ext.tip.ToolTip1.png Basic Ext.tip.ToolTip}
45 * In addition to attaching a ToolTip to a single element, you can also use delegation to attach
46 * one ToolTip to many elements under a common parent. This is more efficient than creating many
47 * ToolTip instances. To do this, point the {@link #target} config to a common ancestor of all the
48 * elements, and then set the {@link #delegate} config to a CSS selector that will select all the
49 * appropriate sub-elements.
51 * When using delegation, it is likely that you will want to programmatically change the content
52 * of the ToolTip based on each delegate element; you can do this by implementing a custom
53 * listener for the {@link #beforeshow} event. Example:
55 * var myGrid = Ext.create('Ext.grid.GridPanel', gridConfig);
56 * myGrid.on('render', function(grid) {
57 * var view = grid.getView(); // Capture the grid's view.
58 * grid.tip = Ext.create('Ext.tip.ToolTip', {
59 * target: view.el, // The overall target element.
60 * delegate: view.itemSelector, // Each grid row causes its own seperate show and hide.
61 * trackMouse: true, // Moving within the row should not hide the tip.
62 * renderTo: Ext.getBody(), // Render immediately so that tip.body can be referenced prior to the first show.
63 * listeners: { // Change content dynamically depending on which element triggered the show.
64 * beforeshow: function updateTipBody(tip) {
65 * tip.update('Over company "' + view.getRecord(tip.triggerElement).get('company') + '"');
71 * {@img Ext.tip.ToolTip/Ext.tip.ToolTip2.png Ext.tip.ToolTip with delegation}
75 * The following configuration properties allow control over how the ToolTip is aligned relative to
76 * the target element and/or mouse pointer:
79 * - {@link #anchorToTarget}
80 * - {@link #anchorOffset}
81 * - {@link #trackMouse}
82 * - {@link #mouseOffset}
86 * The following configuration properties allow control over how and when the ToolTip is automatically
90 * - {@link #showDelay}
91 * - {@link #hideDelay}
92 * - {@link #dismissDelay}
95 * Create a new ToolTip instance
96 * @param {Object} config The configuration options
99 * @docauthor Jason Johnston <jason@sencha.com>
101 Ext.define('Ext.tip.ToolTip', {
102 extend: 'Ext.tip.Tip',
103 alias: 'widget.tooltip',
104 alternateClassName: 'Ext.ToolTip',
105 <span id='Ext-tip-ToolTip-property-triggerElement'> /**
106 </span> * When a ToolTip is configured with the <code>{@link #delegate}</code>
107 * option to cause selected child elements of the <code>{@link #target}</code>
108 * Element to each trigger a seperate show event, this property is set to
109 * the DOM element which triggered the show.
111 * @property triggerElement
113 <span id='Ext-tip-ToolTip-cfg-target'> /**
114 </span> * @cfg {Mixed} target The target HTMLElement, Ext.core.Element or id to monitor
115 * for mouseover events to trigger showing this ToolTip.
117 <span id='Ext-tip-ToolTip-cfg-autoHide'> /**
118 </span> * @cfg {Boolean} autoHide True to automatically hide the tooltip after the
119 * mouse exits the target element or after the <code>{@link #dismissDelay}</code>
120 * has expired if set (defaults to true). If <code>{@link #closable} = true</code>
121 * a close tool button will be rendered into the tooltip header.
123 <span id='Ext-tip-ToolTip-cfg-showDelay'> /**
124 </span> * @cfg {Number} showDelay Delay in milliseconds before the tooltip displays
125 * after the mouse enters the target element (defaults to 500)
128 <span id='Ext-tip-ToolTip-cfg-hideDelay'> /**
129 </span> * @cfg {Number} hideDelay Delay in milliseconds after the mouse exits the
130 * target element but before the tooltip actually hides (defaults to 200).
131 * Set to 0 for the tooltip to hide immediately.
134 <span id='Ext-tip-ToolTip-cfg-dismissDelay'> /**
135 </span> * @cfg {Number} dismissDelay Delay in milliseconds before the tooltip
136 * automatically hides (defaults to 5000). To disable automatic hiding, set
140 <span id='Ext-tip-ToolTip-cfg-mouseOffset'> /**
141 </span> * @cfg {Array} mouseOffset An XY offset from the mouse position where the
142 * tooltip should be shown (defaults to [15,18]).
144 <span id='Ext-tip-ToolTip-cfg-trackMouse'> /**
145 </span> * @cfg {Boolean} trackMouse True to have the tooltip follow the mouse as it
146 * moves over the target element (defaults to false).
149 <span id='Ext-tip-ToolTip-cfg-anchor'> /**
150 </span> * @cfg {String} anchor If specified, indicates that the tip should be anchored to a
151 * particular side of the target element or mouse pointer ("top", "right", "bottom",
152 * or "left"), with an arrow pointing back at the target or mouse pointer. If
153 * {@link #constrainPosition} is enabled, this will be used as a preferred value
154 * only and may be flipped as needed.
156 <span id='Ext-tip-ToolTip-cfg-anchorToTarget'> /**
157 </span> * @cfg {Boolean} anchorToTarget True to anchor the tooltip to the target
158 * element, false to anchor it relative to the mouse coordinates (defaults
159 * to true). When <code>anchorToTarget</code> is true, use
160 * <code>{@link #defaultAlign}</code> to control tooltip alignment to the
161 * target element. When <code>anchorToTarget</code> is false, use
162 * <code>{@link #anchorPosition}</code> instead to control alignment.
164 anchorToTarget: true,
165 <span id='Ext-tip-ToolTip-cfg-anchorOffset'> /**
166 </span> * @cfg {Number} anchorOffset A numeric pixel value used to offset the
167 * default position of the anchor arrow (defaults to 0). When the anchor
168 * position is on the top or bottom of the tooltip, <code>anchorOffset</code>
169 * will be used as a horizontal offset. Likewise, when the anchor position
170 * is on the left or right side, <code>anchorOffset</code> will be used as
174 <span id='Ext-tip-ToolTip-cfg-delegate'> /**
175 </span> * @cfg {String} delegate <p>Optional. A {@link Ext.DomQuery DomQuery}
176 * selector which allows selection of individual elements within the
177 * <code>{@link #target}</code> element to trigger showing and hiding the
178 * ToolTip as the mouse moves within the target.</p>
179 * <p>When specified, the child element of the target which caused a show
180 * event is placed into the <code>{@link #triggerElement}</code> property
181 * before the ToolTip is shown.</p>
182 * <p>This may be useful when a Component has regular, repeating elements
183 * in it, each of which need a ToolTip which contains information specific
184 * to that element. For example:</p><pre><code>
185 var myGrid = Ext.create('Ext.grid.GridPanel', gridConfig);
186 myGrid.on('render', function(grid) {
187 var view = grid.getView(); // Capture the grid's view.
188 grid.tip = Ext.create('Ext.tip.ToolTip', {
189 target: view.el, // The overall target element.
190 delegate: view.itemSelector, // Each grid row causes its own seperate show and hide.
191 trackMouse: true, // Moving within the row should not hide the tip.
192 renderTo: Ext.getBody(), // Render immediately so that tip.body can be referenced prior to the first show.
193 listeners: { // Change content dynamically depending on which element triggered the show.
194 beforeshow: function(tip) {
195 tip.update('Over Record ID ' + view.getRecord(tip.triggerElement).id);
200 *</code></pre>
205 quickShowInterval: 250,
208 initComponent: function() {
210 me.callParent(arguments);
211 me.lastActive = new Date();
212 me.setTarget(me.target);
213 me.origAnchor = me.anchor;
217 onRender: function(ct, position) {
219 me.callParent(arguments);
220 me.anchorCls = Ext.baseCSSPrefix + 'tip-anchor-' + me.getAnchorPosition();
221 me.anchorEl = me.el.createChild({
222 cls: Ext.baseCSSPrefix + 'tip-anchor ' + me.anchorCls
227 afterRender: function() {
231 me.callParent(arguments);
232 zIndex = parseInt(me.el.getZIndex(), 10) || 0;
233 me.anchorEl.setStyle('z-index', zIndex + 1).setVisibilityMode(Ext.core.Element.DISPLAY);
236 <span id='Ext-tip-ToolTip-method-setTarget'> /**
237 </span> * Binds this ToolTip to the specified element. The tooltip will be displayed when the mouse moves over the element.
238 * @param {Mixed} t The Element, HtmlElement, or ID of an element to bind to
240 setTarget: function(target) {
246 tg = Ext.get(me.target);
247 me.mun(tg, 'mouseover', me.onTargetOver, me);
248 me.mun(tg, 'mouseout', me.onTargetOut, me);
249 me.mun(tg, 'mousemove', me.onMouseMove, me);
256 // TODO - investigate why IE6/7 seem to fire recursive resize in e.getXY
257 // breaking QuickTip#onTargetOver (EXTJSIV-1608)
260 mouseover: me.onTargetOver,
261 mouseout: me.onTargetOut,
262 mousemove: me.onMouseMove,
267 me.anchorTarget = me.target;
272 onMouseMove: function(e) {
274 t = me.delegate ? e.getTarget(me.delegate) : me.triggerElement = true,
277 me.targetXY = e.getXY();
278 if (t === me.triggerElement) {
279 if (!me.hidden && me.trackMouse) {
280 xy = me.getTargetXY();
281 if (me.constrainPosition) {
282 xy = me.el.adjustForConstraints(xy, me.el.dom.parentNode);
284 me.setPagePosition(xy);
288 me.lastActive = new Date(0);
291 } else if ((!me.closable && me.isVisible()) && me.autoHide !== false) {
297 getTargetXY: function() {
301 me.anchorTarget = me.triggerElement;
305 var offsets = me.getOffsets(),
306 xy = (me.anchorToTarget && !me.trackMouse) ? me.el.getAlignToXY(me.anchorTarget, me.getAnchorAlign()) : me.targetXY,
307 dw = Ext.core.Element.getViewWidth() - 5,
308 dh = Ext.core.Element.getViewHeight() - 5,
309 de = document.documentElement,
311 scrollX = (de.scrollLeft || bd.scrollLeft || 0) + 5,
312 scrollY = (de.scrollTop || bd.scrollTop || 0) + 5,
313 axy = [xy[0] + offsets[0], xy[1] + offsets[1]],
315 constrainPosition = me.constrainPosition;
317 me.anchorEl.removeCls(me.anchorCls);
319 if (me.targetCounter < 2 && constrainPosition) {
320 if (axy[0] < scrollX) {
321 if (me.anchorToTarget) {
322 me.defaultAlign = 'l-r';
323 if (me.mouseOffset) {
324 me.mouseOffset[0] *= -1;
328 return me.getTargetXY();
330 if (axy[0] + sz.width > dw) {
331 if (me.anchorToTarget) {
332 me.defaultAlign = 'r-l';
333 if (me.mouseOffset) {
334 me.mouseOffset[0] *= -1;
338 return me.getTargetXY();
340 if (axy[1] < scrollY) {
341 if (me.anchorToTarget) {
342 me.defaultAlign = 't-b';
343 if (me.mouseOffset) {
344 me.mouseOffset[1] *= -1;
348 return me.getTargetXY();
350 if (axy[1] + sz.height > dh) {
351 if (me.anchorToTarget) {
352 me.defaultAlign = 'b-t';
353 if (me.mouseOffset) {
354 me.mouseOffset[1] *= -1;
357 me.anchor = 'bottom';
358 return me.getTargetXY();
362 me.anchorCls = Ext.baseCSSPrefix + 'tip-anchor-' + me.getAnchorPosition();
363 me.anchorEl.addCls(me.anchorCls);
364 me.targetCounter = 0;
367 mouseOffset = me.getMouseOffset();
368 return (me.targetXY) ? [me.targetXY[0] + mouseOffset[0], me.targetXY[1] + mouseOffset[1]] : mouseOffset;
372 getMouseOffset: function() {
374 offset = me.anchor ? [0, 0] : [15, 18];
375 if (me.mouseOffset) {
376 offset[0] += me.mouseOffset[0];
377 offset[1] += me.mouseOffset[1];
383 getAnchorPosition: function() {
387 me.tipAnchor = me.anchor.charAt(0);
389 m = me.defaultAlign.match(/^([a-z]+)-([a-z]+)(\?)?$/);
392 Ext.Error.raise('The AnchorTip.defaultAlign value "' + me.defaultAlign + '" is invalid.');
395 me.tipAnchor = m[1].charAt(0);
398 switch (me.tipAnchor) {
410 getAnchorAlign: function() {
411 switch (this.anchor) {
424 getOffsets: function() {
428 ap = me.getAnchorPosition().charAt(0);
429 if (me.anchorToTarget && !me.trackMouse) {
438 offsets = [ - 13, 0];
447 offsets = [ - 15 - me.anchorOffset, 30];
450 offsets = [ - 19 - me.anchorOffset, -13 - me.el.dom.offsetHeight];
453 offsets = [ - 15 - me.el.dom.offsetWidth, -13 - me.anchorOffset];
456 offsets = [25, -13 - me.anchorOffset];
460 mouseOffset = me.getMouseOffset();
461 offsets[0] += mouseOffset[0];
462 offsets[1] += mouseOffset[1];
468 onTargetOver: function(e) {
472 if (me.disabled || e.within(me.target.dom, true)) {
475 t = e.getTarget(me.delegate);
477 me.triggerElement = t;
478 me.clearTimer('hide');
479 me.targetXY = e.getXY();
485 delayShow: function() {
487 if (me.hidden && !me.showTimer) {
488 if (Ext.Date.getElapsed(me.lastActive) < me.quickShowInterval) {
491 me.showTimer = Ext.defer(me.show, me.showDelay, me);
494 else if (!me.hidden && me.autoHide !== false) {
500 onTargetOut: function(e) {
502 if (me.disabled || e.within(me.target.dom, true)) {
505 me.clearTimer('show');
506 if (me.autoHide !== false) {
512 delayHide: function() {
514 if (!me.hidden && !me.hideTimer) {
515 me.hideTimer = Ext.defer(me.hide, me.hideDelay, me);
519 <span id='Ext-tip-ToolTip-method-hide'> /**
520 </span> * Hides this tooltip if visible.
524 me.clearTimer('dismiss');
525 me.lastActive = new Date();
529 me.callParent(arguments);
530 delete me.triggerElement;
533 <span id='Ext-tip-ToolTip-method-show'> /**
534 </span> * Shows this tooltip at the current event target XY position.
539 // Show this Component first, so that sizing can be calculated
540 // pre-show it off screen so that the el will have dimensions
542 if (this.hidden === false) {
543 me.setPagePosition(-10000, -10000);
546 me.anchor = me.origAnchor;
548 me.showAt(me.getTargetXY());
560 showAt: function(xy) {
562 me.lastActive = new Date();
565 // Only call if this is hidden. May have been called from show above.
566 if (!me.isVisible()) {
567 this.callParent(arguments);
570 // Show may have been vetoed.
571 if (me.isVisible()) {
572 me.setPagePosition(xy[0], xy[1]);
573 if (me.constrainPosition || me.constrain) {
579 if (me.dismissDelay && me.autoHide !== false) {
580 me.dismissTimer = Ext.defer(me.hide, me.dismissDelay, me);
584 if (!me.anchorEl.isVisible()) {
593 syncAnchor: function() {
598 switch (me.tipAnchor.charAt(0)) {
602 offset = [20 + me.anchorOffset, 1];
607 offset = [ - 1, 12 + me.anchorOffset];
612 offset = [20 + me.anchorOffset, -1];
617 offset = [1, 12 + me.anchorOffset];
620 me.anchorEl.alignTo(me.el, anchorPos + '-' + targetPos, offset);
624 setPagePosition: function(x, y) {
626 me.callParent(arguments);
633 clearTimer: function(name) {
634 name = name + 'Timer';
635 clearTimeout(this[name]);
640 clearTimers: function() {
642 me.clearTimer('show');
643 me.clearTimer('dismiss');
644 me.clearTimer('hide');
651 me.mon(Ext.getDoc(), 'mousedown', me.onDocMouseDown, me);
658 me.mun(Ext.getDoc(), 'mousedown', me.onDocMouseDown, me);
662 onDocMouseDown: function(e) {
664 if (me.autoHide !== true && !me.closable && !e.within(me.el.dom)) {
666 Ext.defer(me.doEnable, 100, me);
671 doEnable: function() {
672 if (!this.isDestroyed) {
678 onDisable: function() {
684 beforeDestroy: function() {
687 Ext.destroy(me.anchorEl);
690 delete me.anchorTarget;
691 delete me.triggerElement;
696 onDestroy: function() {
697 Ext.getDoc().un('mousedown', this.onDocMouseDown, this);