3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.ToolTip"></div>/**
\r
15 * @class Ext.ToolTip
\r
17 * A standard tooltip implementation for providing additional information when hovering over a target element.
\r
20 * Create a new Tooltip
\r
21 * @param {Object} config The configuration options
\r
23 Ext.ToolTip = Ext.extend(Ext.Tip, {
\r
24 <div id="prop-Ext.ToolTip-triggerElement"></div>/**
\r
25 * When a Tooltip is configured with the <code>{@link #delegate}</code>
\r
26 * option to cause selected child elements of the <code>{@link #target}</code>
\r
27 * Element to each trigger a seperate show event, this property is set to
\r
28 * the DOM element which triggered the show.
\r
30 * @property triggerElement
\r
32 <div id="cfg-Ext.ToolTip-target"></div>/**
\r
33 * @cfg {Mixed} target The target HTMLElement, Ext.Element or id to monitor
\r
34 * for mouseover events to trigger showing this ToolTip.
\r
36 <div id="cfg-Ext.ToolTip-autoHide"></div>/**
\r
37 * @cfg {Boolean} autoHide True to automatically hide the tooltip after the
\r
38 * mouse exits the target element or after the <code>{@link #dismissDelay}</code>
\r
39 * has expired if set (defaults to true). If <code>{@link closable} = true</code>
\r
40 * a close tool button will be rendered into the tooltip header.
\r
42 <div id="cfg-Ext.ToolTip-showDelay"></div>/**
\r
43 * @cfg {Number} showDelay Delay in milliseconds before the tooltip displays
\r
44 * after the mouse enters the target element (defaults to 500)
\r
47 <div id="cfg-Ext.ToolTip-hideDelay"></div>/**
\r
48 * @cfg {Number} hideDelay Delay in milliseconds after the mouse exits the
\r
49 * target element but before the tooltip actually hides (defaults to 200).
\r
50 * Set to 0 for the tooltip to hide immediately.
\r
53 <div id="cfg-Ext.ToolTip-dismissDelay"></div>/**
\r
54 * @cfg {Number} dismissDelay Delay in milliseconds before the tooltip
\r
55 * automatically hides (defaults to 5000). To disable automatic hiding, set
\r
58 dismissDelay : 5000,
\r
59 <div id="cfg-Ext.ToolTip-mouseOffset"></div>/**
\r
60 * @cfg {Array} mouseOffset An XY offset from the mouse position where the
\r
61 * tooltip should be shown (defaults to [15,18]).
\r
63 <div id="cfg-Ext.ToolTip-trackMouse"></div>/**
\r
64 * @cfg {Boolean} trackMouse True to have the tooltip follow the mouse as it
\r
65 * moves over the target element (defaults to false).
\r
68 <div id="cfg-Ext.ToolTip-anchorToTarget"></div>/**
\r
69 * @cfg {Boolean} anchorToTarget True to anchor the tooltip to the target
\r
70 * element, false to anchor it relative to the mouse coordinates (defaults
\r
71 * to true). When <code>anchorToTarget</code> is true, use
\r
72 * <code>{@link #defaultAlign}</code> to control tooltip alignment to the
\r
73 * target element. When <code>anchorToTarget</code> is false, use
\r
74 * <code>{@link #anchorPosition}</code> instead to control alignment.
\r
76 anchorToTarget : true,
\r
77 <div id="cfg-Ext.ToolTip-anchorOffset"></div>/**
\r
78 * @cfg {Number} anchorOffset A numeric pixel value used to offset the
\r
79 * default position of the anchor arrow (defaults to 0). When the anchor
\r
80 * position is on the top or bottom of the tooltip, <code>anchorOffset</code>
\r
81 * will be used as a horizontal offset. Likewise, when the anchor position
\r
82 * is on the left or right side, <code>anchorOffset</code> will be used as
\r
83 * a vertical offset.
\r
86 <div id="cfg-Ext.ToolTip-delegate"></div>/**
\r
87 * @cfg {String} delegate <p>Optional. A {@link Ext.DomQuery DomQuery}
\r
88 * selector which allows selection of individual elements within the
\r
89 * <code>{@link #target}</code> element to trigger showing and hiding the
\r
90 * ToolTip as the mouse moves within the target.</p>
\r
91 * <p>When specified, the child element of the target which caused a show
\r
92 * event is placed into the <code>{@link #triggerElement}</code> property
\r
93 * before the ToolTip is shown.</p>
\r
94 * <p>This may be useful when a Component has regular, repeating elements
\r
95 * in it, each of which need a Tooltip which contains information specific
\r
96 * to that element. For example:</p><pre><code>
\r
97 var myGrid = new Ext.grid.gridPanel(gridConfig);
\r
98 myGrid.on('render', function(grid) {
\r
99 var store = grid.getStore(); // Capture the Store.
\r
100 var view = grid.getView(); // Capture the GridView.
\r
101 myGrid.tip = new Ext.ToolTip({
\r
102 target: view.mainBody, // The overall target element.
\r
103 delegate: '.x-grid3-row', // Each grid row causes its own seperate show and hide.
\r
104 trackMouse: true, // Moving within the row should not hide the tip.
\r
105 renderTo: document.body, // Render immediately so that tip.body can be
\r
106 // referenced prior to the first show.
\r
107 listeners: { // Change content dynamically depending on which element
\r
108 // triggered the show.
\r
109 beforeshow: function updateTipBody(tip) {
\r
110 var rowIndex = view.findRowIndex(tip.triggerElement);
\r
111 tip.body.dom.innerHTML = 'Over Record ID ' + store.getAt(rowIndex).id;
\r
122 constrainPosition : false,
\r
125 initComponent : function(){
\r
126 Ext.ToolTip.superclass.initComponent.call(this);
\r
127 this.lastActive = new Date();
\r
128 this.initTarget(this.target);
\r
129 this.origAnchor = this.anchor;
\r
133 onRender : function(ct, position){
\r
134 Ext.ToolTip.superclass.onRender.call(this, ct, position);
\r
135 this.anchorCls = 'x-tip-anchor-' + this.getAnchorPosition();
\r
136 this.anchorEl = this.el.createChild({
\r
137 cls: 'x-tip-anchor ' + this.anchorCls
\r
142 afterRender : function(){
\r
143 Ext.ToolTip.superclass.afterRender.call(this);
\r
144 this.anchorEl.setStyle('z-index', this.el.getZIndex() + 1);
\r
147 <div id="method-Ext.ToolTip-initTarget"></div>/**
\r
148 * Binds this ToolTip to the specified element. The tooltip will be displayed when the mouse moves over the element.
\r
149 * @param {Mixed} t The Element, HtmlElement, or ID of an element to bind to
\r
151 initTarget : function(target){
\r
153 if((t = Ext.get(target))){
\r
155 var tg = Ext.get(this.target);
\r
156 this.mun(tg, 'mouseover', this.onTargetOver, this);
\r
157 this.mun(tg, 'mouseout', this.onTargetOut, this);
\r
158 this.mun(tg, 'mousemove', this.onMouseMove, this);
\r
161 mouseover: this.onTargetOver,
\r
162 mouseout: this.onTargetOut,
\r
163 mousemove: this.onMouseMove,
\r
169 this.anchorTarget = this.target;
\r
174 onMouseMove : function(e){
\r
175 var t = this.delegate ? e.getTarget(this.delegate) : this.triggerElement = true;
\r
177 this.targetXY = e.getXY();
\r
178 if (t === this.triggerElement) {
\r
179 if(!this.hidden && this.trackMouse){
\r
180 this.setPagePosition(this.getTargetXY());
\r
184 this.lastActive = new Date(0);
\r
185 this.onTargetOver(e);
\r
187 } else if (!this.closable && this.isVisible()) {
\r
193 getTargetXY : function(){
\r
195 this.anchorTarget = this.triggerElement;
\r
198 this.targetCounter++;
\r
199 var offsets = this.getOffsets();
\r
200 var xy = (this.anchorToTarget && !this.trackMouse) ?
\r
201 this.el.getAlignToXY(this.anchorTarget, this.getAnchorAlign()) :
\r
204 var dw = Ext.lib.Dom.getViewWidth()-5;
\r
205 var dh = Ext.lib.Dom.getViewHeight()-5;
\r
206 var scrollX = (document.documentElement.scrollLeft || document.body.scrollLeft || 0)+5;
\r
207 var scrollY = (document.documentElement.scrollTop || document.body.scrollTop || 0)+5;
\r
209 var axy = [xy[0] + offsets[0], xy[1] + offsets[1]];
\r
210 var sz = this.getSize();
\r
211 this.anchorEl.removeClass(this.anchorCls);
\r
213 if(this.targetCounter < 2){
\r
214 if(axy[0] < scrollX){
\r
215 if(this.anchorToTarget){
\r
216 this.defaultAlign = 'l-r';
\r
217 if(this.mouseOffset){this.mouseOffset[0] *= -1;}
\r
219 this.anchor = 'left';
\r
220 return this.getTargetXY();
\r
222 if(axy[0]+sz.width > dw){
\r
223 if(this.anchorToTarget){
\r
224 this.defaultAlign = 'r-l';
\r
225 if(this.mouseOffset){this.mouseOffset[0] *= -1;}
\r
227 this.anchor = 'right';
\r
228 return this.getTargetXY();
\r
230 if(axy[1] < scrollY){
\r
231 if(this.anchorToTarget){
\r
232 this.defaultAlign = 't-b';
\r
233 if(this.mouseOffset){this.mouseOffset[1] *= -1;}
\r
235 this.anchor = 'top';
\r
236 return this.getTargetXY();
\r
238 if(axy[1]+sz.height > dh){
\r
239 if(this.anchorToTarget){
\r
240 this.defaultAlign = 'b-t';
\r
241 if(this.mouseOffset){this.mouseOffset[1] *= -1;}
\r
243 this.anchor = 'bottom';
\r
244 return this.getTargetXY();
\r
248 this.anchorCls = 'x-tip-anchor-'+this.getAnchorPosition();
\r
249 this.anchorEl.addClass(this.anchorCls);
\r
250 this.targetCounter = 0;
\r
253 var mouseOffset = this.getMouseOffset();
\r
254 return [this.targetXY[0]+mouseOffset[0], this.targetXY[1]+mouseOffset[1]];
\r
258 getMouseOffset : function(){
\r
259 var offset = this.anchor ? [0,0] : [15,18];
\r
260 if(this.mouseOffset){
\r
261 offset[0] += this.mouseOffset[0];
\r
262 offset[1] += this.mouseOffset[1];
\r
268 getAnchorPosition : function(){
\r
270 this.tipAnchor = this.anchor.charAt(0);
\r
272 var m = this.defaultAlign.match(/^([a-z]+)-([a-z]+)(\?)?$/);
\r
274 throw 'AnchorTip.defaultAlign is invalid';
\r
276 this.tipAnchor = m[1].charAt(0);
\r
279 switch(this.tipAnchor){
\r
280 case 't': return 'top';
\r
281 case 'b': return 'bottom';
\r
282 case 'r': return 'right';
\r
288 getAnchorAlign : function(){
\r
289 switch(this.anchor){
\r
290 case 'top' : return 'tl-bl';
\r
291 case 'left' : return 'tl-tr';
\r
292 case 'right': return 'tr-tl';
\r
293 default : return 'bl-tl';
\r
298 getOffsets : function(){
\r
299 var offsets, ap = this.getAnchorPosition().charAt(0);
\r
300 if(this.anchorToTarget && !this.trackMouse){
\r
306 offsets = [0, -13];
\r
309 offsets = [-13, 0];
\r
318 offsets = [-15-this.anchorOffset, 30];
\r
321 offsets = [-19-this.anchorOffset, -13-this.el.dom.offsetHeight];
\r
324 offsets = [-15-this.el.dom.offsetWidth, -13-this.anchorOffset];
\r
327 offsets = [25, -13-this.anchorOffset];
\r
331 var mouseOffset = this.getMouseOffset();
\r
332 offsets[0] += mouseOffset[0];
\r
333 offsets[1] += mouseOffset[1];
\r
339 onTargetOver : function(e){
\r
340 if(this.disabled || e.within(this.target.dom, true)){
\r
343 var t = e.getTarget(this.delegate);
\r
345 this.triggerElement = t;
\r
346 this.clearTimer('hide');
\r
347 this.targetXY = e.getXY();
\r
353 delayShow : function(){
\r
354 if(this.hidden && !this.showTimer){
\r
355 if(this.lastActive.getElapsed() < this.quickShowInterval){
\r
358 this.showTimer = this.show.defer(this.showDelay, this);
\r
360 }else if(!this.hidden && this.autoHide !== false){
\r
366 onTargetOut : function(e){
\r
367 if(this.disabled || e.within(this.target.dom, true)){
\r
370 this.clearTimer('show');
\r
371 if(this.autoHide !== false){
\r
377 delayHide : function(){
\r
378 if(!this.hidden && !this.hideTimer){
\r
379 this.hideTimer = this.hide.defer(this.hideDelay, this);
\r
383 <div id="method-Ext.ToolTip-hide"></div>/**
\r
384 * Hides this tooltip if visible.
\r
387 this.clearTimer('dismiss');
\r
388 this.lastActive = new Date();
\r
390 this.anchorEl.hide();
\r
392 Ext.ToolTip.superclass.hide.call(this);
\r
393 delete this.triggerElement;
\r
396 <div id="method-Ext.ToolTip-show"></div>/**
\r
397 * Shows this tooltip at the current event target XY position.
\r
401 // pre-show it off screen so that the el will have dimensions
\r
402 // for positioning calcs when getting xy next
\r
403 this.showAt([-1000,-1000]);
\r
404 this.origConstrainPosition = this.constrainPosition;
\r
405 this.constrainPosition = false;
\r
406 this.anchor = this.origAnchor;
\r
408 this.showAt(this.getTargetXY());
\r
412 this.anchorEl.show();
\r
413 this.constrainPosition = this.origConstrainPosition;
\r
415 this.anchorEl.hide();
\r
420 showAt : function(xy){
\r
421 this.lastActive = new Date();
\r
422 this.clearTimers();
\r
423 Ext.ToolTip.superclass.showAt.call(this, xy);
\r
424 if(this.dismissDelay && this.autoHide !== false){
\r
425 this.dismissTimer = this.hide.defer(this.dismissDelay, this);
\r
427 if(this.anchor && !this.anchorEl.isVisible()){
\r
429 this.anchorEl.show();
\r
434 syncAnchor : function(){
\r
435 var anchorPos, targetPos, offset;
\r
436 switch(this.tipAnchor.charAt(0)){
\r
440 offset = [20+this.anchorOffset, 2];
\r
445 offset = [-2, 11+this.anchorOffset];
\r
450 offset = [20+this.anchorOffset, -2];
\r
455 offset = [2, 11+this.anchorOffset];
\r
458 this.anchorEl.alignTo(this.el, anchorPos+'-'+targetPos, offset);
\r
462 setPagePosition : function(x, y){
\r
463 Ext.ToolTip.superclass.setPagePosition.call(this, x, y);
\r
470 clearTimer : function(name){
\r
471 name = name + 'Timer';
\r
472 clearTimeout(this[name]);
\r
477 clearTimers : function(){
\r
478 this.clearTimer('show');
\r
479 this.clearTimer('dismiss');
\r
480 this.clearTimer('hide');
\r
484 onShow : function(){
\r
485 Ext.ToolTip.superclass.onShow.call(this);
\r
486 Ext.getDoc().on('mousedown', this.onDocMouseDown, this);
\r
490 onHide : function(){
\r
491 Ext.ToolTip.superclass.onHide.call(this);
\r
492 Ext.getDoc().un('mousedown', this.onDocMouseDown, this);
\r
496 onDocMouseDown : function(e){
\r
497 if(this.autoHide !== true && !this.closable && !e.within(this.el.dom)){
\r
499 this.enable.defer(100, this);
\r
504 onDisable : function(){
\r
505 this.clearTimers();
\r
510 adjustPosition : function(x, y){
\r
511 if(this.contstrainPosition){
\r
512 var ay = this.targetXY[1], h = this.getSize().height;
\r
513 if(y <= ay && (y+h) >= ay){
\r
517 return {x : x, y: y};
\r
521 onDestroy : function(){
\r
522 Ext.getDoc().un('mousedown', this.onDocMouseDown, this);
\r
523 Ext.ToolTip.superclass.onDestroy.call(this);
\r
527 Ext.reg('tooltip', Ext.ToolTip);</pre>