1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.ToolTip |
---|
11 | * @extends Ext.Tip |
---|
12 | * A standard tooltip implementation for providing additional information when hovering over a target element. |
---|
13 | * @constructor |
---|
14 | * Create a new Tooltip |
---|
15 | * @param {Object} config The configuration options |
---|
16 | */ |
---|
17 | Ext.ToolTip = Ext.extend(Ext.Tip, { |
---|
18 | /** |
---|
19 | * @cfg {Mixed} target The target HTMLElement, Ext.Element or id to associate with this tooltip. |
---|
20 | */ |
---|
21 | /** |
---|
22 | * @cfg {Boolean} autoHide True to automatically hide the tooltip after the mouse exits the target element |
---|
23 | * or after the {@link #dismissDelay} has expired if set (defaults to true). If {@link closable} = true a close |
---|
24 | * tool button will be rendered into the tooltip header. |
---|
25 | */ |
---|
26 | /** |
---|
27 | * @cfg {Number} showDelay Delay in milliseconds before the tooltip displays after the mouse enters the |
---|
28 | * target element (defaults to 500) |
---|
29 | */ |
---|
30 | showDelay: 500, |
---|
31 | /** |
---|
32 | * @cfg {Number} hideDelay Delay in milliseconds after the mouse exits the target element but before the |
---|
33 | * tooltip actually hides (defaults to 200). Set to 0 for the tooltip to hide immediately. |
---|
34 | */ |
---|
35 | hideDelay: 200, |
---|
36 | /** |
---|
37 | * @cfg {Number} dismissDelay Delay in milliseconds before the tooltip automatically hides (defaults to 5000). |
---|
38 | * To disable automatic hiding, set dismissDelay = 0. |
---|
39 | */ |
---|
40 | dismissDelay: 5000, |
---|
41 | /** |
---|
42 | * @cfg {Array} mouseOffset An XY offset from the mouse position where the tooltip should be shown (defaults to [15,18]). |
---|
43 | */ |
---|
44 | mouseOffset: [15,18], |
---|
45 | /** |
---|
46 | * @cfg {Boolean} trackMouse True to have the tooltip follow the mouse as it moves over the target element (defaults to false). |
---|
47 | */ |
---|
48 | trackMouse : false, |
---|
49 | constrainPosition: true, |
---|
50 | |
---|
51 | // private |
---|
52 | initComponent: function(){ |
---|
53 | Ext.ToolTip.superclass.initComponent.call(this); |
---|
54 | this.lastActive = new Date(); |
---|
55 | this.initTarget(); |
---|
56 | }, |
---|
57 | |
---|
58 | // private |
---|
59 | initTarget : function(){ |
---|
60 | if(this.target){ |
---|
61 | this.target = Ext.get(this.target); |
---|
62 | this.target.on('mouseover', this.onTargetOver, this); |
---|
63 | this.target.on('mouseout', this.onTargetOut, this); |
---|
64 | this.target.on('mousemove', this.onMouseMove, this); |
---|
65 | } |
---|
66 | }, |
---|
67 | |
---|
68 | // private |
---|
69 | onMouseMove : function(e){ |
---|
70 | this.targetXY = e.getXY(); |
---|
71 | if(!this.hidden && this.trackMouse){ |
---|
72 | this.setPagePosition(this.getTargetXY()); |
---|
73 | } |
---|
74 | }, |
---|
75 | |
---|
76 | // private |
---|
77 | getTargetXY : function(){ |
---|
78 | return [this.targetXY[0]+this.mouseOffset[0], this.targetXY[1]+this.mouseOffset[1]]; |
---|
79 | }, |
---|
80 | |
---|
81 | // private |
---|
82 | onTargetOver : function(e){ |
---|
83 | if(this.disabled || e.within(this.target.dom, true)){ |
---|
84 | return; |
---|
85 | } |
---|
86 | this.clearTimer('hide'); |
---|
87 | this.targetXY = e.getXY(); |
---|
88 | this.delayShow(); |
---|
89 | }, |
---|
90 | |
---|
91 | // private |
---|
92 | delayShow : function(){ |
---|
93 | if(this.hidden && !this.showTimer){ |
---|
94 | if(this.lastActive.getElapsed() < this.quickShowInterval){ |
---|
95 | this.show(); |
---|
96 | }else{ |
---|
97 | this.showTimer = this.show.defer(this.showDelay, this); |
---|
98 | } |
---|
99 | }else if(!this.hidden && this.autoHide !== false){ |
---|
100 | this.show(); |
---|
101 | } |
---|
102 | }, |
---|
103 | |
---|
104 | // private |
---|
105 | onTargetOut : function(e){ |
---|
106 | if(this.disabled || e.within(this.target.dom, true)){ |
---|
107 | return; |
---|
108 | } |
---|
109 | this.clearTimer('show'); |
---|
110 | if(this.autoHide !== false){ |
---|
111 | this.delayHide(); |
---|
112 | } |
---|
113 | }, |
---|
114 | |
---|
115 | // private |
---|
116 | delayHide : function(){ |
---|
117 | if(!this.hidden && !this.hideTimer){ |
---|
118 | this.hideTimer = this.hide.defer(this.hideDelay, this); |
---|
119 | } |
---|
120 | }, |
---|
121 | |
---|
122 | /** |
---|
123 | * Hides this tooltip if visible. |
---|
124 | */ |
---|
125 | hide: function(){ |
---|
126 | this.clearTimer('dismiss'); |
---|
127 | this.lastActive = new Date(); |
---|
128 | Ext.ToolTip.superclass.hide.call(this); |
---|
129 | }, |
---|
130 | |
---|
131 | /** |
---|
132 | * Shows this tooltip at the current event target XY position. |
---|
133 | */ |
---|
134 | show : function(){ |
---|
135 | this.showAt(this.getTargetXY()); |
---|
136 | }, |
---|
137 | |
---|
138 | // inherit docs |
---|
139 | showAt : function(xy){ |
---|
140 | this.lastActive = new Date(); |
---|
141 | this.clearTimers(); |
---|
142 | Ext.ToolTip.superclass.showAt.call(this, xy); |
---|
143 | if(this.dismissDelay && this.autoHide !== false){ |
---|
144 | this.dismissTimer = this.hide.defer(this.dismissDelay, this); |
---|
145 | } |
---|
146 | }, |
---|
147 | |
---|
148 | // private |
---|
149 | clearTimer : function(name){ |
---|
150 | name = name + 'Timer'; |
---|
151 | clearTimeout(this[name]); |
---|
152 | delete this[name]; |
---|
153 | }, |
---|
154 | |
---|
155 | // private |
---|
156 | clearTimers : function(){ |
---|
157 | this.clearTimer('show'); |
---|
158 | this.clearTimer('dismiss'); |
---|
159 | this.clearTimer('hide'); |
---|
160 | }, |
---|
161 | |
---|
162 | // private |
---|
163 | onShow : function(){ |
---|
164 | Ext.ToolTip.superclass.onShow.call(this); |
---|
165 | Ext.getDoc().on('mousedown', this.onDocMouseDown, this); |
---|
166 | }, |
---|
167 | |
---|
168 | // private |
---|
169 | onHide : function(){ |
---|
170 | Ext.ToolTip.superclass.onHide.call(this); |
---|
171 | Ext.getDoc().un('mousedown', this.onDocMouseDown, this); |
---|
172 | }, |
---|
173 | |
---|
174 | // private |
---|
175 | onDocMouseDown : function(e){ |
---|
176 | if(this.autoHide !== false && !e.within(this.el.dom)){ |
---|
177 | this.disable(); |
---|
178 | this.enable.defer(100, this); |
---|
179 | } |
---|
180 | }, |
---|
181 | |
---|
182 | // private |
---|
183 | onDisable : function(){ |
---|
184 | this.clearTimers(); |
---|
185 | this.hide(); |
---|
186 | }, |
---|
187 | |
---|
188 | // private |
---|
189 | adjustPosition : function(x, y){ |
---|
190 | // keep the position from being under the mouse |
---|
191 | var ay = this.targetXY[1], h = this.getSize().height; |
---|
192 | if(this.constrainPosition && y <= ay && (y+h) >= ay){ |
---|
193 | y = ay-h-5; |
---|
194 | } |
---|
195 | return {x : x, y: y}; |
---|
196 | }, |
---|
197 | |
---|
198 | // private |
---|
199 | onDestroy : function(){ |
---|
200 | Ext.ToolTip.superclass.onDestroy.call(this); |
---|
201 | if(this.target){ |
---|
202 | this.target.un('mouseover', this.onTargetOver, this); |
---|
203 | this.target.un('mouseout', this.onTargetOut, this); |
---|
204 | this.target.un('mousemove', this.onMouseMove, this); |
---|
205 | } |
---|
206 | } |
---|
207 | }); |
---|