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