da3554092148d2982488e117e0f94396c111383e
[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 popover-header',
80                             html : this.title
81                         },
82                         {
83                             cls : 'popover-content popover-body',
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 () {
159         this.hoverState == 'in' ? this.leave() : this.enter();
160     },
161     
162     enter : function () {
163         
164         clearTimeout(this.timeout);
165     
166         this.hoverState = 'in';
167     
168         if (!this.delay || !this.delay.show) {
169             this.show();
170             return;
171         }
172         var _t = this;
173         this.timeout = setTimeout(function () {
174             if (_t.hoverState == 'in') {
175                 _t.show();
176             }
177         }, this.delay.show)
178     },
179     
180     leave : function() {
181         clearTimeout(this.timeout);
182     
183         this.hoverState = 'out';
184     
185         if (!this.delay || !this.delay.hide) {
186             this.hide();
187             return;
188         }
189         var _t = this;
190         this.timeout = setTimeout(function () {
191             if (_t.hoverState == 'out') {
192                 _t.hide();
193             }
194         }, this.delay.hide)
195     },
196     
197     show : function (on_el)
198     {
199         if (!on_el) {
200             on_el= (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
201         }
202         
203         // set content.
204         this.el.select('.popover-title',true).first().dom.innerHtml = this.title;
205         if (this.html !== false) {
206             this.el.select('.popover-content',true).first().dom.innerHtml = this.html;
207         }
208         this.el.removeClass([
209             'fade','top','bottom', 'left', 'right','in',
210             'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
211         ]);
212         if (!this.title.length) {
213             this.el.select('.popover-title',true).hide();
214         }
215         
216         var placement = typeof this.placement == 'function' ?
217             this.placement.call(this, this.el, on_el) :
218             this.placement;
219             
220         var autoToken = /\s?auto?\s?/i;
221         var autoPlace = autoToken.test(placement);
222         if (autoPlace) {
223             placement = placement.replace(autoToken, '') || 'top';
224         }
225         
226         //this.el.detach()
227         //this.el.setXY([0,0]);
228         this.el.show();
229         this.el.dom.style.display='block';
230         this.el.addClass(placement);
231         
232         //this.el.appendTo(on_el);
233         
234         var p = this.getPosition();
235         var box = this.el.getBox();
236         
237         if (autoPlace) {
238             // fixme..
239         }
240         var align = Roo.bootstrap.Popover.alignment[placement];
241         
242 //        Roo.log(align);
243         this.el.alignTo(on_el, align[0],align[1]);
244         //var arrow = this.el.select('.arrow',true).first();
245         //arrow.set(align[2], 
246         
247         this.el.addClass('in');
248         
249         
250         if (this.el.hasClass('fade')) {
251             // fade it?
252         }
253         
254         this.hoverState = 'in';
255         
256         this.fireEvent('show', this);
257         
258     },
259     hide : function()
260     {
261         this.el.setXY([0,0]);
262         this.el.removeClass('in');
263         this.el.hide();
264         this.hoverState = null;
265         
266         this.fireEvent('hide', this);
267     }
268     
269 });
270
271 Roo.bootstrap.Popover.alignment = {
272     'left' : ['r-l', [-10,0], 'right bs-popover-right'],
273     'right' : ['l-r', [10,0], 'left bs-popover-left'],
274     'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
275     'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
276 };
277
278