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