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'>/**
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> * When a ToolTip is configured with the `{@link #delegate}`
127 * option to cause selected child elements of the `{@link #target}`
128 * Element to each trigger a seperate show event, this property is set to
129 * the DOM element which triggered the show.
131 * @property triggerElement
133 <span id='Ext-tip-ToolTip-cfg-target'> /**
134 </span> * @cfg {Mixed} target The target HTMLElement, Ext.core.Element or id to monitor
135 * for mouseover events to trigger showing this ToolTip.
137 <span id='Ext-tip-ToolTip-cfg-autoHide'> /**
138 </span> * @cfg {Boolean} autoHide True to automatically hide the tooltip after the
139 * mouse exits the target element or after the `{@link #dismissDelay}`
140 * has expired if set (defaults to true). If `{@link #closable} = true`
141 * a close tool button will be rendered into the tooltip header.
143 <span id='Ext-tip-ToolTip-cfg-showDelay'> /**
144 </span> * @cfg {Number} showDelay Delay in milliseconds before the tooltip displays
145 * after the mouse enters the target element (defaults to 500)
148 <span id='Ext-tip-ToolTip-cfg-hideDelay'> /**
149 </span> * @cfg {Number} hideDelay Delay in milliseconds after the mouse exits the
150 * target element but before the tooltip actually hides (defaults to 200).
151 * Set to 0 for the tooltip to hide immediately.
154 <span id='Ext-tip-ToolTip-cfg-dismissDelay'> /**
155 </span> * @cfg {Number} dismissDelay Delay in milliseconds before the tooltip
156 * automatically hides (defaults to 5000). To disable automatic hiding, set
160 <span id='Ext-tip-ToolTip-cfg-mouseOffset'> /**
161 </span> * @cfg {Array} mouseOffset An XY offset from the mouse position where the
162 * tooltip should be shown (defaults to [15,18]).
164 <span id='Ext-tip-ToolTip-cfg-trackMouse'> /**
165 </span> * @cfg {Boolean} trackMouse True to have the tooltip follow the mouse as it
166 * moves over the target element (defaults to false).
169 <span id='Ext-tip-ToolTip-cfg-anchor'> /**
170 </span> * @cfg {String} anchor If specified, indicates that the tip should be anchored to a
171 * particular side of the target element or mouse pointer ("top", "right", "bottom",
172 * or "left"), with an arrow pointing back at the target or mouse pointer. If
173 * {@link #constrainPosition} is enabled, this will be used as a preferred value
174 * only and may be flipped as needed.
176 <span id='Ext-tip-ToolTip-cfg-anchorToTarget'> /**
177 </span> * @cfg {Boolean} anchorToTarget True to anchor the tooltip to the target
178 * element, false to anchor it relative to the mouse coordinates (defaults
179 * to true). When `anchorToTarget` is true, use
180 * `{@link #defaultAlign}` to control tooltip alignment to the
181 * target element. When `anchorToTarget` is false, use
182 * `{@link #anchorPosition}` instead to control alignment.
184 anchorToTarget: true,
185 <span id='Ext-tip-ToolTip-cfg-anchorOffset'> /**
186 </span> * @cfg {Number} anchorOffset A numeric pixel value used to offset the
187 * default position of the anchor arrow (defaults to 0). When the anchor
188 * position is on the top or bottom of the tooltip, `anchorOffset`
189 * will be used as a horizontal offset. Likewise, when the anchor position
190 * is on the left or right side, `anchorOffset` will be used as
194 <span id='Ext-tip-ToolTip-cfg-delegate'> /**
195 </span> * @cfg {String} delegate
197 * A {@link Ext.DomQuery DomQuery} selector which allows selection of individual elements within the
198 * `{@link #target}` element to trigger showing and hiding the ToolTip as the mouse moves within the
201 * When specified, the child element of the target which caused a show event is placed into the
202 * `{@link #triggerElement}` property before the ToolTip is shown.
204 * This may be useful when a Component has regular, repeating elements in it, each of which need a
205 * ToolTip which contains information specific to that element.
207 * See the delegate example in class documentation of {@link Ext.tip.ToolTip}.
212 quickShowInterval: 250,
215 initComponent: function() {
217 me.callParent(arguments);
218 me.lastActive = new Date();
219 me.setTarget(me.target);
220 me.origAnchor = me.anchor;
224 onRender: function(ct, position) {
226 me.callParent(arguments);
227 me.anchorCls = Ext.baseCSSPrefix + 'tip-anchor-' + me.getAnchorPosition();
228 me.anchorEl = me.el.createChild({
229 cls: Ext.baseCSSPrefix + 'tip-anchor ' + me.anchorCls
234 afterRender: function() {
238 me.callParent(arguments);
239 zIndex = parseInt(me.el.getZIndex(), 10) || 0;
240 me.anchorEl.setStyle('z-index', zIndex + 1).setVisibilityMode(Ext.core.Element.DISPLAY);
243 <span id='Ext-tip-ToolTip-method-setTarget'> /**
244 </span> * Binds this ToolTip to the specified element. The tooltip will be displayed when the mouse moves over the element.
245 * @param {Mixed} t The Element, HtmlElement, or ID of an element to bind to
247 setTarget: function(target) {
253 tg = Ext.get(me.target);
254 me.mun(tg, 'mouseover', me.onTargetOver, me);
255 me.mun(tg, 'mouseout', me.onTargetOut, me);
256 me.mun(tg, 'mousemove', me.onMouseMove, me);
263 // TODO - investigate why IE6/7 seem to fire recursive resize in e.getXY
264 // breaking QuickTip#onTargetOver (EXTJSIV-1608)
267 mouseover: me.onTargetOver,
268 mouseout: me.onTargetOut,
269 mousemove: me.onMouseMove,
274 me.anchorTarget = me.target;
279 onMouseMove: function(e) {
281 t = me.delegate ? e.getTarget(me.delegate) : me.triggerElement = true,
284 me.targetXY = e.getXY();
285 if (t === me.triggerElement) {
286 if (!me.hidden && me.trackMouse) {
287 xy = me.getTargetXY();
288 if (me.constrainPosition) {
289 xy = me.el.adjustForConstraints(xy, me.el.dom.parentNode);
291 me.setPagePosition(xy);
295 me.lastActive = new Date(0);
298 } else if ((!me.closable && me.isVisible()) && me.autoHide !== false) {
304 getTargetXY: function() {
308 me.anchorTarget = me.triggerElement;
312 var offsets = me.getOffsets(),
313 xy = (me.anchorToTarget && !me.trackMouse) ? me.el.getAlignToXY(me.anchorTarget, me.getAnchorAlign()) : me.targetXY,
314 dw = Ext.core.Element.getViewWidth() - 5,
315 dh = Ext.core.Element.getViewHeight() - 5,
316 de = document.documentElement,
318 scrollX = (de.scrollLeft || bd.scrollLeft || 0) + 5,
319 scrollY = (de.scrollTop || bd.scrollTop || 0) + 5,
320 axy = [xy[0] + offsets[0], xy[1] + offsets[1]],
322 constrainPosition = me.constrainPosition;
324 me.anchorEl.removeCls(me.anchorCls);
326 if (me.targetCounter < 2 && constrainPosition) {
327 if (axy[0] < scrollX) {
328 if (me.anchorToTarget) {
329 me.defaultAlign = 'l-r';
330 if (me.mouseOffset) {
331 me.mouseOffset[0] *= -1;
335 return me.getTargetXY();
337 if (axy[0] + sz.width > dw) {
338 if (me.anchorToTarget) {
339 me.defaultAlign = 'r-l';
340 if (me.mouseOffset) {
341 me.mouseOffset[0] *= -1;
345 return me.getTargetXY();
347 if (axy[1] < scrollY) {
348 if (me.anchorToTarget) {
349 me.defaultAlign = 't-b';
350 if (me.mouseOffset) {
351 me.mouseOffset[1] *= -1;
355 return me.getTargetXY();
357 if (axy[1] + sz.height > dh) {
358 if (me.anchorToTarget) {
359 me.defaultAlign = 'b-t';
360 if (me.mouseOffset) {
361 me.mouseOffset[1] *= -1;
364 me.anchor = 'bottom';
365 return me.getTargetXY();
369 me.anchorCls = Ext.baseCSSPrefix + 'tip-anchor-' + me.getAnchorPosition();
370 me.anchorEl.addCls(me.anchorCls);
371 me.targetCounter = 0;
374 mouseOffset = me.getMouseOffset();
375 return (me.targetXY) ? [me.targetXY[0] + mouseOffset[0], me.targetXY[1] + mouseOffset[1]] : mouseOffset;
379 getMouseOffset: function() {
381 offset = me.anchor ? [0, 0] : [15, 18];
382 if (me.mouseOffset) {
383 offset[0] += me.mouseOffset[0];
384 offset[1] += me.mouseOffset[1];
390 getAnchorPosition: function() {
394 me.tipAnchor = me.anchor.charAt(0);
396 m = me.defaultAlign.match(/^([a-z]+)-([a-z]+)(\?)?$/);
399 Ext.Error.raise('The AnchorTip.defaultAlign value "' + me.defaultAlign + '" is invalid.');
402 me.tipAnchor = m[1].charAt(0);
405 switch (me.tipAnchor) {
417 getAnchorAlign: function() {
418 switch (this.anchor) {
431 getOffsets: function() {
435 ap = me.getAnchorPosition().charAt(0);
436 if (me.anchorToTarget && !me.trackMouse) {
445 offsets = [ - 13, 0];
454 offsets = [ - 15 - me.anchorOffset, 30];
457 offsets = [ - 19 - me.anchorOffset, -13 - me.el.dom.offsetHeight];
460 offsets = [ - 15 - me.el.dom.offsetWidth, -13 - me.anchorOffset];
463 offsets = [25, -13 - me.anchorOffset];
467 mouseOffset = me.getMouseOffset();
468 offsets[0] += mouseOffset[0];
469 offsets[1] += mouseOffset[1];
475 onTargetOver: function(e) {
479 if (me.disabled || e.within(me.target.dom, true)) {
482 t = e.getTarget(me.delegate);
484 me.triggerElement = t;
485 me.clearTimer('hide');
486 me.targetXY = e.getXY();
492 delayShow: function() {
494 if (me.hidden && !me.showTimer) {
495 if (Ext.Date.getElapsed(me.lastActive) < me.quickShowInterval) {
498 me.showTimer = Ext.defer(me.show, me.showDelay, me);
501 else if (!me.hidden && me.autoHide !== false) {
507 onTargetOut: function(e) {
509 if (me.disabled || e.within(me.target.dom, true)) {
512 me.clearTimer('show');
513 if (me.autoHide !== false) {
519 delayHide: function() {
521 if (!me.hidden && !me.hideTimer) {
522 me.hideTimer = Ext.defer(me.hide, me.hideDelay, me);
526 <span id='Ext-tip-ToolTip-method-hide'> /**
527 </span> * Hides this tooltip if visible.
531 me.clearTimer('dismiss');
532 me.lastActive = new Date();
536 me.callParent(arguments);
537 delete me.triggerElement;
540 <span id='Ext-tip-ToolTip-method-show'> /**
541 </span> * Shows this tooltip at the current event target XY position.
546 // Show this Component first, so that sizing can be calculated
547 // pre-show it off screen so that the el will have dimensions
549 if (this.hidden === false) {
550 me.setPagePosition(-10000, -10000);
553 me.anchor = me.origAnchor;
555 me.showAt(me.getTargetXY());
567 showAt: function(xy) {
569 me.lastActive = new Date();
572 // Only call if this is hidden. May have been called from show above.
573 if (!me.isVisible()) {
574 this.callParent(arguments);
577 // Show may have been vetoed.
578 if (me.isVisible()) {
579 me.setPagePosition(xy[0], xy[1]);
580 if (me.constrainPosition || me.constrain) {
586 if (me.dismissDelay && me.autoHide !== false) {
587 me.dismissTimer = Ext.defer(me.hide, me.dismissDelay, me);
591 if (!me.anchorEl.isVisible()) {
600 syncAnchor: function() {
605 switch (me.tipAnchor.charAt(0)) {
609 offset = [20 + me.anchorOffset, 1];
614 offset = [ - 1, 12 + me.anchorOffset];
619 offset = [20 + me.anchorOffset, -1];
624 offset = [1, 12 + me.anchorOffset];
627 me.anchorEl.alignTo(me.el, anchorPos + '-' + targetPos, offset);
631 setPagePosition: function(x, y) {
633 me.callParent(arguments);
640 clearTimer: function(name) {
641 name = name + 'Timer';
642 clearTimeout(this[name]);
647 clearTimers: function() {
649 me.clearTimer('show');
650 me.clearTimer('dismiss');
651 me.clearTimer('hide');
658 me.mon(Ext.getDoc(), 'mousedown', me.onDocMouseDown, me);
665 me.mun(Ext.getDoc(), 'mousedown', me.onDocMouseDown, me);
669 onDocMouseDown: function(e) {
671 if (me.autoHide !== true && !me.closable && !e.within(me.el.dom)) {
673 Ext.defer(me.doEnable, 100, me);
678 doEnable: function() {
679 if (!this.isDestroyed) {
685 onDisable: function() {
691 beforeDestroy: function() {
694 Ext.destroy(me.anchorEl);
697 delete me.anchorTarget;
698 delete me.triggerElement;
703 onDestroy: function() {
704 Ext.getDoc().un('mousedown', this.onDocMouseDown, this);