d1d9d6293eba4ef4dbb9163138756f3d4db755af
[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|function} (right|top|bottom|left|auto) placement how it is placed
15  * @cfg {String} trigger click || hover (or false to trigger manually)
16  * @cfg {Boolean} modal - popovers that are modal will mask the screen, and must be closed with another event.
17  * @cfg {String|Boolean|Roo.Element} add click hander to trigger show over what element
18  *      - if false and it has a 'parent' then it will be automatically added to that element
19  *      - if string - Roo.get  will be called 
20  * @cfg {Number} delay - delay before showing
21  
22  * @constructor
23  * Create a new Popover
24  * @param {Object} config The config object
25  */
26
27 Roo.bootstrap.Popover = function(config){
28     Roo.bootstrap.Popover.superclass.constructor.call(this, config);
29     
30     this.addEvents({
31         // raw events
32          /**
33          * @event show
34          * After the popover show
35          * 
36          * @param {Roo.bootstrap.Popover} this
37          */
38         "show" : true,
39         /**
40          * @event hide
41          * After the popover hide
42          * 
43          * @param {Roo.bootstrap.Popover} this
44          */
45         "hide" : true
46     });
47 };
48
49 Roo.extend(Roo.bootstrap.Popover, Roo.bootstrap.Component,  {
50     
51     title: false,
52     html: false,
53     
54     placement : 'right',
55     trigger : 'hover', // hover
56     
57     delay : 0,
58     
59     over: false,
60     
61     can_build_overlaid : false,
62     
63     getChildContainer : function()
64     {
65         return this.el.select('.popover-content',true).first();
66     },
67     
68     getAutoCreate : function(){
69          
70         var cfg = {
71            cls : 'popover roo-dynamic shadow roo-popover',
72            style: 'display:block',
73            cn : [
74                 {
75                     cls : 'arrow'
76                 },
77                 {
78                     cls : 'popover-inner ',
79                     cn : [
80                         {
81                             tag: 'h3',
82                             cls: 'popover-title popover-header',
83                             html : this.title || ''
84                         },
85                         {
86                             cls : 'popover-content popover-body'  + this.cls,
87                             html : this.html || ''
88                         }
89                     ]
90                     
91                 }
92            ]
93         };
94         
95         return cfg;
96     },
97     /**
98      * @param {string} the title
99      */
100     setTitle: function(str)
101     {
102         this.title = str;
103         if (this.el) {
104             this.el.select('.popover-title',true).first().dom.innerHTML = str;
105         }
106         
107     },
108     /**
109      * @param {string} the body content
110      */
111     setContent: function(str)
112     {
113         this.html = str;
114         if (this.el) {
115             this.el.select('.popover-content',true).first().dom.innerHTML = str;
116         }
117         
118     },
119     // as it get's added to the bottom of the page.
120     onRender : function(ct, position)
121     {
122         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
123         if(!this.el){
124             var cfg = Roo.apply({},  this.getAutoCreate());
125             cfg.id = Roo.id();
126             
127             if (this.cls) {
128                 cfg.cls += ' ' + this.cls;
129             }
130             if (this.style) {
131                 cfg.style = this.style;
132             }
133             //Roo.log("adding to ");
134             this.el = Roo.get(document.body).createChild(cfg, position);
135 //            Roo.log(this.el);
136         }
137         
138         var nitems = [];
139         if(typeof(this.items) != 'undefined'){
140             var items = this.items;
141             delete this.items;
142
143             for(var i =0;i < items.length;i++) {
144                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
145             }
146         }
147
148         this.items = nitems;
149         
150         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
151
152         
153         
154         this.initEvents();
155     },
156     
157     initEvents : function()
158     {
159         
160         Roo.bootstrap.Popover.register(this);
161         this.el.select('.popover-title',true).setVisibilityMode(Roo.Element.DISPLAY);
162         this.el.enableDisplayMode('block');
163         this.el.hide();
164         if (this.over === false && !this.parent()) {
165             return; 
166         }
167         if (this.triggers === false) {
168             return;
169         }
170          
171         // support parent
172         var on_el = (this.over == 'parent' || this.over === false) ? this.parent().el : Roo.get(this.over);
173         var triggers = this.trigger ? this.trigger.split(' ') : [];
174         Roo.each(triggers, function(trigger) {
175         
176             if (trigger == 'click') {
177                 on_el.on('click', this.toggle, this);
178             } else if (trigger != 'manual') {
179                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin';
180                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
181       
182                 on_el.on(eventIn  ,this.enter, this);
183                 on_el.on(eventOut, this.leave, this);
184             }
185         }, this);
186         
187     },
188     
189     
190     // private
191     timeout : null,
192     hoverState : null,
193     
194     toggle : function () {
195         this.hoverState == 'in' ? this.leave() : this.enter();
196     },
197     
198     enter : function () {
199         
200         clearTimeout(this.timeout);
201     
202         this.hoverState = 'in';
203     
204         if (!this.delay || !this.delay.show) {
205             this.show();
206             return;
207         }
208         var _t = this;
209         this.timeout = setTimeout(function () {
210             if (_t.hoverState == 'in') {
211                 _t.show();
212             }
213         }, this.delay.show)
214     },
215     
216     leave : function() {
217         clearTimeout(this.timeout);
218     
219         this.hoverState = 'out';
220     
221         if (!this.delay || !this.delay.hide) {
222             this.hide();
223             return;
224         }
225         var _t = this;
226         this.timeout = setTimeout(function () {
227             if (_t.hoverState == 'out') {
228                 _t.hide();
229             }
230         }, this.delay.hide)
231     },
232     /**
233      * Show the popover
234      * @param {Roo.Element|string|false} - element to align and point to.
235      */
236     show : function (on_el)
237     {
238         
239         on_el = on_el || false; // default to false
240         if (!on_el) {
241             if (this.parent() && (this.over == 'parent' || (this.over === false))) {
242                 on_el = this.parent().el;
243             } else if (this.over) {
244                 Roo.get(this.over);
245             }
246             
247         }
248         
249         if (!this.el) {
250             this.render(document.body);
251         }
252         
253         
254         this.el.removeClass([
255             'fade','top','bottom', 'left', 'right','in',
256             'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
257         ]);
258         
259         if (!this.title.length) {
260             this.el.select('.popover-title',true).hide();
261         }
262         
263         
264         var placement = typeof this.placement == 'function' ?
265             this.placement.call(this, this.el, on_el) :
266             this.placement;
267             
268         /*
269         var autoToken = /\s?auto?\s?/i;   /// not sure how this was supposed to work? right auto ? what?
270         
271         // I think  'auto right' - but 
272         
273         var autoPlace = autoToken.test(placement);
274         if (autoPlace) {
275             placement = placement.replace(autoToken, '') || 'top';
276         }
277         */
278         
279         
280         this.el.show();
281         this.el.dom.style.display='block';
282         
283         //this.el.appendTo(on_el);
284         
285         var p = this.getPosition();
286         var box = this.el.getBox();
287         
288         
289         var align = Roo.bootstrap.Popover.alignment[placement];
290         this.el.addClass(align[2]);
291
292 //        Roo.log(align);
293
294         if (on_el) {
295             this.el.alignTo(on_el, align[0],align[1]);
296         } else {
297             // this is usually just done by the builder = to show the popoup in the middle of the scren.
298             var es = this.el.getSize();
299             var x = Roo.lib.Dom.getViewWidth()/2;
300             var y = Roo.lib.Dom.getViewHeight()/2;
301             this.el.setXY([ x-(es.width/2),  y-(es.height/2)] );
302             
303         }
304
305         
306         //var arrow = this.el.select('.arrow',true).first();
307         //arrow.set(align[2], 
308         
309         this.el.addClass('in');
310         
311         
312         if (this.el.hasClass('fade')) {
313             // fade it?
314         }
315         
316         this.hoverState = 'in';
317         
318         this.fireEvent('show', this);
319         
320     },
321     hide : function()
322     {
323         this.el.setXY([0,0]);
324         this.el.removeClass('in');
325         this.el.hide();
326         this.hoverState = null;
327         
328         this.fireEvent('hide', this);
329     }
330     
331 });
332
333
334 Roo.apply(Roo.bootstrap.Popover, {
335
336     alignment : {
337         'left' : ['r-l', [-10,0], 'left bs-popover-left'],
338         'right' : ['l-br', [10,0], 'right bs-popover-right'],
339         'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
340         'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
341     },
342
343     clickHander : false,
344     
345
346     onMouseDown : function(e)
347     {
348         if (!e.getTarget(".roo-popover")) {
349             this.hideAll();
350         }
351          
352     },
353     
354     popups : [],
355     
356     register : function(popup)
357     {
358         if (!Roo.bootstrap.Popover.clickHandler) {
359             Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
360         }
361         // hide other popups.
362         this.hideAll();
363         this.popups.push(popup);
364     },
365     hideAll : function()
366     {
367         this.popups.forEach(function(p) {
368             p.hide();
369         });
370     }
371
372 });