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