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         //Roo.log(['enter',dom]);
49         var el = Roo.fly(dom);
50         if (this.currentEl) {
51             //Roo.log(dom);
52             //Roo.log(this.currentEl);
53             //Roo.log(this.currentEl.contains(dom));
54             if (this.currentEl == el) {
55                 return;
56             }
57             if (dom != this.currentEl.dom && this.currentEl.contains(dom)) {
58                 return;
59             }
60
61         }
62         
63         
64         
65         if (this.currentTip.el) {
66             this.currentTip.el.hide(); // force hiding...
67         }    
68         Roo.log(el);
69         if (!el.attr('tooltip')) { // parents who have tip?
70             return;
71         }
72         this.currentEl = el;
73         this.currentTip.bind(el);
74         this.currentRegion = Roo.lib.Region.getRegion(dom);
75         this.currentTip.enter();
76         
77     },
78     leave : function(ev)
79     {
80         var dom = ev.getTarget();
81         //Roo.log(['leave',dom]);
82         if (!this.currentEl) {
83             return;
84         }
85         
86         
87         if (dom != this.currentEl.dom) {
88             return;
89         }
90         var xy = ev.getXY();
91         if (this.currentRegion.contains( new Roo.lib.Region( xy[1], xy[0] ,xy[1], xy[0]  ))) {
92             return;
93         }
94         // only activate leave if mouse cursor is outside... bounding box..
95         
96         
97         
98         
99         if (this.currentTip) {
100             this.currentTip.leave();
101         }
102         //Roo.log('clear currentEl');
103         this.currentEl = false;
104         
105         
106     },
107     alignment : {
108         'left' : ['r-l', [-2,0], 'right'],
109         'right' : ['l-r', [2,0], 'left'],
110         'bottom' : ['t-b', [0,2], 'top'],
111         'top' : [ 'b-t', [0,-2], 'bottom']
112     }
113     
114 });
115
116
117 Roo.extend(Roo.bootstrap.Tooltip, Roo.bootstrap.Component,  {
118     
119     
120     bindEl : false,
121     
122     delay : null, // can be { show : 300 , hide: 500}
123     
124     timeout : null,
125     
126     hoverState : null, //???
127     
128     placement : 'bottom', 
129     
130     getAutoCreate : function(){
131     
132         var cfg = {
133            cls : 'tooltip',
134            role : 'tooltip',
135            cn : [
136                 {
137                     cls : 'tooltip-arrow'
138                 },
139                 {
140                     cls : 'tooltip-inner'
141                 }
142            ]
143         };
144         
145         return cfg;
146     },
147     bind : function(el)
148     {
149         this.bindEl = el;
150     },
151       
152     
153     enter : function () {
154        
155         if (this.timeout != null) {
156             clearTimeout(this.timeout);
157         }
158         
159         this.hoverState = 'in';
160          //Roo.log("enter - show");
161         if (!this.delay || !this.delay.show) {
162             this.show();
163             return;
164         }
165         var _t = this;
166         this.timeout = setTimeout(function () {
167             if (_t.hoverState == 'in') {
168                 _t.show();
169             }
170         }, this.delay.show);
171     },
172     leave : function()
173     {
174         clearTimeout(this.timeout);
175     
176         this.hoverState = 'out';
177          if (!this.delay || !this.delay.hide) {
178             this.hide();
179             return 
180         }
181        
182         var _t = this;
183         this.timeout = setTimeout(function () {
184             //Roo.log("leave - timeout");
185             
186             if (_t.hoverState == 'out') {
187                 _t.hide();
188                 Roo.bootstrap.Tooltip.currentEl = false;
189             }
190         }, delay)
191     },
192     
193     show : function ()
194     {
195         if (!this.el) {
196             this.render(document.body);
197         }
198         // set content.
199         //Roo.log([this.bindEl, this.bindEl.attr('tooltip')]);
200         this.el.select('.tooltip-inner',true).first().dom.innerHTML = this.bindEl.attr('tooltip');
201         
202         this.el.removeClass(['fade','top','bottom', 'left', 'right','in']);
203         
204         var placement = typeof this.placement == 'function' ?
205             this.placement.call(this, this.el, on_el) :
206             this.placement;
207             
208         var autoToken = /\s?auto?\s?/i;
209         var autoPlace = autoToken.test(placement);
210         if (autoPlace) {
211             placement = placement.replace(autoToken, '') || 'top';
212         }
213         
214         //this.el.detach()
215         //this.el.setXY([0,0]);
216         this.el.show();
217         //this.el.dom.style.display='block';
218         this.el.addClass(placement);
219         
220         //this.el.appendTo(on_el);
221         
222         var p = this.getPosition();
223         var box = this.el.getBox();
224         
225         if (autoPlace) {
226             // fixme..
227         }
228         var align = Roo.bootstrap.Tooltip.alignment[placement];
229         this.el.alignTo(this.bindEl, align[0],align[1]);
230         //var arrow = this.el.select('.arrow',true).first();
231         //arrow.set(align[2], 
232         
233         this.el.addClass('in fade');
234         this.hoverState = null;
235         
236         if (this.el.hasClass('fade')) {
237             // fade it?
238         }
239         
240     },
241     hide : function()
242     {
243          
244         if (!this.el) {
245             return;
246         }
247         //this.el.setXY([0,0]);
248         this.el.removeClass('in');
249         //this.el.hide();
250         
251     }
252     
253 });
254  
255
256