Roo/bootstrap/Tooltip.js
[roojs1] / Roo / bootstrap / Tooltip.js
1 /*
2  * - LGPL
3  *
4  * Tooltip
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.Tooltip
10  * Bootstrap Tooltip class
11  * This is basic at present - all componets support it by default, however they should add tooltipEl() method
12  * to determine which dom element triggers the tooltip.
13  * 
14  * It needs to add support for additional attributes like tooltip-position
15  * 
16  * @constructor
17  * Create a new Toolti
18  * @param {Object} config The config object
19  */
20
21 Roo.bootstrap.Tooltip = function(config){
22     Roo.bootstrap.Tooltip.superclass.constructor.call(this, config);
23 };
24
25 Roo.apply(Roo.bootstrap.Tooltip, {
26     /**
27      * @function init initialize tooltip monitoring.
28      * @static
29      */
30     currentEl : false,
31     currentTip : false,
32     currentRegion : false,
33     
34     //  init : delay?
35     
36     init : function()
37     {
38         Roo.get(document).on('mouseover', this.enter ,this);
39         Roo.get(document).on('mouseout', this.leave, this);
40          
41         
42         this.currentTip = new Roo.bootstrap.Tooltip();
43     },
44     
45     enter : function(ev)
46     {
47         var dom = ev.getTarget();
48         
49         //Roo.log(['enter',dom]);
50         var el = Roo.fly(dom);
51         if (this.currentEl) {
52             //Roo.log(dom);
53             //Roo.log(this.currentEl);
54             //Roo.log(this.currentEl.contains(dom));
55             if (this.currentEl == el) {
56                 return;
57             }
58             if (dom != this.currentEl.dom && this.currentEl.contains(dom)) {
59                 return;
60             }
61
62         }
63         
64         if (this.currentTip.el) {
65             this.currentTip.el.setVisibilityMode(Roo.Element.DISPLAY).hide(); // force hiding...
66         }    
67         //Roo.log(ev);
68         
69         if(!el || el.dom == document){
70             return;
71         }
72         
73         var bindEl = el;
74         
75         // you can not look for children, as if el is the body.. then everythign is the child..
76         if (!el.attr('tooltip')) { //
77             if (!el.select("[tooltip]").elements.length) {
78                 return;
79             }
80             // is the mouse over this child...?
81             bindEl = el.select("[tooltip]").first();
82             var xy = ev.getXY();
83             if (!bindEl.getRegion().contains( { top : xy[1] ,right : xy[0] , bottom : xy[1], left : xy[0]})) {
84                 //Roo.log("not in region.");
85                 return;
86             }
87             //Roo.log("child element over..");
88             
89         }
90         this.currentEl = bindEl;
91         this.currentTip.bind(bindEl);
92         this.currentRegion = Roo.lib.Region.getRegion(dom);
93         this.currentTip.enter();
94         
95     },
96     leave : function(ev)
97     {
98         var dom = ev.getTarget();
99         //Roo.log(['leave',dom]);
100         if (!this.currentEl) {
101             return;
102         }
103         
104         
105         if (dom != this.currentEl.dom) {
106             return;
107         }
108         var xy = ev.getXY();
109         if (this.currentRegion.contains( new Roo.lib.Region( xy[1], xy[0] ,xy[1], xy[0]  ))) {
110             return;
111         }
112         // only activate leave if mouse cursor is outside... bounding box..
113         
114         
115         
116         
117         if (this.currentTip) {
118             this.currentTip.leave();
119         }
120         //Roo.log('clear currentEl');
121         this.currentEl = false;
122         
123         
124     },
125     alignment : {
126         'left' : ['r-l', [-2,0], 'right'],
127         'right' : ['l-r', [2,0], 'left'],
128         'bottom' : ['t-b', [0,2], 'top'],
129         'top' : [ 'b-t', [0,-2], 'bottom']
130     }
131     
132 });
133
134
135 Roo.extend(Roo.bootstrap.Tooltip, Roo.bootstrap.Component,  {
136     
137     
138     bindEl : false,
139     
140     delay : null, // can be { show : 300 , hide: 500}
141     
142     timeout : null,
143     
144     hoverState : null, //???
145     
146     placement : 'bottom', 
147     
148     getAutoCreate : function(){
149     
150         var cfg = {
151            cls : 'tooltip',
152            role : 'tooltip',
153            cn : [
154                 {
155                     cls : 'tooltip-arrow'
156                 },
157                 {
158                     cls : 'tooltip-inner'
159                 }
160            ]
161         };
162         
163         return cfg;
164     },
165     bind : function(el)
166     {
167         this.bindEl = el;
168     },
169       
170     
171     enter : function () {
172        
173        Roo.log('tooltip enter??');
174        
175         if (this.timeout != null) {
176             clearTimeout(this.timeout);
177         }
178         
179         this.hoverState = 'in';
180          //Roo.log("enter - show");
181         if (!this.delay || !this.delay.show) {
182             this.show();
183             return;
184         }
185         var _t = this;
186         this.timeout = setTimeout(function () {
187             if (_t.hoverState == 'in') {
188                 _t.show();
189             }
190         }, this.delay.show);
191     },
192     leave : function()
193     {
194         clearTimeout(this.timeout);
195     
196         this.hoverState = 'out';
197          if (!this.delay || !this.delay.hide) {
198             this.hide();
199             return;
200         }
201        
202         var _t = this;
203         this.timeout = setTimeout(function () {
204             //Roo.log("leave - timeout");
205             
206             if (_t.hoverState == 'out') {
207                 _t.hide();
208                 Roo.bootstrap.Tooltip.currentEl = false;
209             }
210         }, delay);
211     },
212     
213     show : function ()
214     {
215         Roo.log('tooltip show???????');
216         if (!this.el) {
217             this.render(document.body);
218         }
219         // set content.
220         //Roo.log([this.bindEl, this.bindEl.attr('tooltip')]);
221         
222         var tip = this.bindEl.attr('tooltip') || this.bindEl.select("[tooltip]").first().attr('tooltip');
223         
224         this.el.select('.tooltip-inner',true).first().dom.innerHTML = tip;
225         
226         this.el.removeClass(['fade','top','bottom', 'left', 'right','in']);
227         
228         var placement = typeof this.placement == 'function' ?
229             this.placement.call(this, this.el, on_el) :
230             this.placement;
231             
232         var autoToken = /\s?auto?\s?/i;
233         var autoPlace = autoToken.test(placement);
234         if (autoPlace) {
235             placement = placement.replace(autoToken, '') || 'top';
236         }
237         
238         //this.el.detach()
239         //this.el.setXY([0,0]);
240         this.el.show();
241         //this.el.dom.style.display='block';
242         
243         //this.el.appendTo(on_el);
244         
245         var p = this.getPosition();
246         var box = this.el.getBox();
247         
248         if (autoPlace) {
249             // fixme..
250         }
251         
252         var align = Roo.bootstrap.Tooltip.alignment[placement];
253         
254         var xy = this.el.getAlignToXY(this.bindEl, align[0], align[1]);
255         
256         if(placement == 'top' || placement == 'bottom'){
257             if(xy[0] < 0){
258                 placement = 'right';
259             }
260             
261             if(xy[0] + this.el.getWidth() > Roo.lib.Dom.getViewWidth()){
262                 placement = 'left';
263             }
264             
265             var scroll = Roo.select('body', true).first().getScroll();
266             
267             if(xy[1] > Roo.lib.Dom.getViewHeight() + scroll.top - this.el.getHeight()){
268                 placement = 'top';
269             }
270             
271         }
272         
273         align = Roo.bootstrap.Tooltip.alignment[placement];
274         
275         this.el.alignTo(this.bindEl, align[0],align[1]);
276         //var arrow = this.el.select('.arrow',true).first();
277         //arrow.set(align[2], 
278         
279         this.el.addClass(placement);
280         
281         this.el.addClass('in fade');
282         
283         this.hoverState = null;
284         
285         if (this.el.hasClass('fade')) {
286             // fade it?
287         }
288         
289     },
290     hide : function()
291     {
292          
293         if (!this.el) {
294             return;
295         }
296         //this.el.setXY([0,0]);
297         this.el.removeClass('in');
298         //this.el.hide();
299         
300     }
301     
302 });
303  
304
305