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