Roo/bootstrap/Popover.js
[roojs1] / Roo / bootstrap / Popover.js
1 /*
2  * - LGPL
3  *
4  * element
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.Popover
10  * @extends Roo.bootstrap.Component
11  * Bootstrap Popover class
12  * @cfg {String} html contents of the popover   (or false to use children..)
13  * @cfg {String} title of popover (or false to hide)
14  * @cfg {String} placement how it is placed
15  * @cfg {String} trigger click || hover (or false to trigger manually)
16  * @cfg {String} over what (parent or false to trigger manually.)
17  * @cfg {Number} delay - delay before showing
18  
19  * @constructor
20  * Create a new Popover
21  * @param {Object} config The config object
22  */
23
24 Roo.bootstrap.Popover = function(config){
25     Roo.bootstrap.Popover.superclass.constructor.call(this, config);
26 };
27
28 Roo.extend(Roo.bootstrap.Popover, Roo.bootstrap.Component,  {
29     
30     title: 'Fill in a title',
31     html: false,
32     
33     placement : 'right',
34     trigger : 'hover', // hover
35     
36     delay : 0,
37     
38     over: 'parent',
39     
40     can_build_overlaid : false,
41     
42     getChildContainer : function()
43     {
44         return this.el.select('.popover-content',true).first();
45     },
46     
47     getAutoCreate : function(){
48          Roo.log('make popover?');
49         var cfg = {
50            cls : 'popover roo-dynamic',
51            style: 'display:block',
52            cn : [
53                 {
54                     cls : 'arrow'
55                 },
56                 {
57                     cls : 'popover-inner',
58                     cn : [
59                         {
60                             tag: 'h3',
61                             cls: 'popover-title',
62                             html : this.title
63                         },
64                         {
65                             cls : 'popover-content',
66                             html : this.html
67                         }
68                     ]
69                     
70                 }
71            ]
72         };
73         
74         return cfg;
75     },
76     setTitle: function(str)
77     {
78         this.title = str;
79         this.el.select('.popover-title',true).first().dom.innerHTML = str;
80     },
81     setContent: function(str)
82     {
83         this.html = str;
84         this.el.select('.popover-content',true).first().dom.innerHTML = str;
85     },
86     // as it get's added to the bottom of the page.
87     onRender : function(ct, position)
88     {
89         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
90         if(!this.el){
91             var cfg = Roo.apply({},  this.getAutoCreate());
92             cfg.id = Roo.id();
93             
94             if (this.cls) {
95                 cfg.cls += ' ' + this.cls;
96             }
97             if (this.style) {
98                 cfg.style = this.style;
99             }
100             //Roo.log("adding to ");
101             this.el = Roo.get(document.body).createChild(cfg, position);
102 //            Roo.log(this.el);
103         }
104         this.initEvents();
105     },
106     
107     initEvents : function()
108     {
109         this.el.select('.popover-title',true).setVisibilityMode(Roo.Element.DISPLAY);
110         this.el.enableDisplayMode('block');
111         this.el.hide();
112         if (this.over === false) {
113             return; 
114         }
115         if (this.triggers === false) {
116             return;
117         }
118         var on_el = (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
119         var triggers = this.trigger ? this.trigger.split(' ') : [];
120         Roo.each(triggers, function(trigger) {
121         
122             if (trigger == 'click') {
123                 on_el.on('click', this.toggle, this);
124             } else if (trigger != 'manual') {
125                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin';
126                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
127       
128                 on_el.on(eventIn  ,this.enter, this);
129                 on_el.on(eventOut, this.leave, this);
130             }
131         }, this);
132         
133     },
134     
135     
136     // private
137     timeout : null,
138     hoverState : null,
139     
140     toggle : function () {
141         this.hoverState == 'in' ? this.leave() : this.enter();
142     },
143     
144     enter : function () {
145        
146     
147         clearTimeout(this.timeout);
148     
149         this.hoverState = 'in';
150     
151         if (!this.delay || !this.delay.show) {
152             this.show();
153             return;
154         }
155         var _t = this;
156         this.timeout = setTimeout(function () {
157             if (_t.hoverState == 'in') {
158                 _t.show();
159             }
160         }, this.delay.show)
161     },
162     leave : function() {
163         clearTimeout(this.timeout);
164     
165         this.hoverState = 'out';
166     
167         if (!this.delay || !this.delay.hide) {
168             this.hide();
169             return;
170         }
171         var _t = this;
172         this.timeout = setTimeout(function () {
173             if (_t.hoverState == 'out') {
174                 _t.hide();
175             }
176         }, this.delay.hide)
177     },
178     
179     show : function (on_el)
180     {
181         if (!on_el) {
182             on_el= (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
183         }
184         // set content.
185         this.el.select('.popover-title',true).first().dom.innerHtml = this.title;
186         if (this.html !== false) {
187             this.el.select('.popover-content',true).first().dom.innerHtml = this.html;
188         }
189         this.el.removeClass(['fade','top','bottom', 'left', 'right','in']);
190         if (!this.title.length) {
191             this.el.select('.popover-title',true).hide();
192         }
193         
194         var placement = typeof this.placement == 'function' ?
195             this.placement.call(this, this.el, on_el) :
196             this.placement;
197             
198         var autoToken = /\s?auto?\s?/i;
199         var autoPlace = autoToken.test(placement);
200         if (autoPlace) {
201             placement = placement.replace(autoToken, '') || 'top';
202         }
203         
204         //this.el.detach()
205         //this.el.setXY([0,0]);
206         this.el.show();
207         this.el.dom.style.display='block';
208         this.el.addClass(placement);
209         
210         //this.el.appendTo(on_el);
211         
212         var p = this.getPosition();
213         var box = this.el.getBox();
214         
215         if (autoPlace) {
216             // fixme..
217         }
218         var align = Roo.bootstrap.Popover.alignment[placement];
219         this.el.alignTo(on_el, align[0],align[1]);
220         //var arrow = this.el.select('.arrow',true).first();
221         //arrow.set(align[2], 
222         
223         this.el.addClass('in');
224         
225         
226         if (this.el.hasClass('fade')) {
227             // fade it?
228         }
229         
230     },
231     hide : function()
232     {
233         this.el.setXY([0,0]);
234         this.el.removeClass('in');
235         this.el.hide();
236         this.hoverState = null;
237         
238     }
239     
240 });
241
242 Roo.bootstrap.Popover.alignment = {
243     'left' : ['r-l', [-10,0], 'right'],
244     'right' : ['l-r', [10,0], 'left'],
245     'bottom' : ['t-b', [0,10], 'top'],
246     'top' : [ 'b-t', [0,-10], 'bottom']
247 };
248
249