attempt to show popovers really above.
[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     alignEl : false, // when show is called with an element - this get's stored.
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         this.arrowEl = this.el.select('.arrow',true).first();
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  
197         
198         if (this.over === false && !this.parent()) {
199             return; 
200         }
201         if (this.triggers === false) {
202             return;
203         }
204          
205         // support parent
206         var on_el = (this.over == 'parent' || this.over === false) ? this.parent().el : Roo.get(this.over);
207         var triggers = this.trigger ? this.trigger.split(' ') : [];
208         Roo.each(triggers, function(trigger) {
209         
210             if (trigger == 'click') {
211                 on_el.on('click', this.toggle, this);
212             } else if (trigger != 'manual') {
213                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin';
214                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
215       
216                 on_el.on(eventIn  ,this.enter, this);
217                 on_el.on(eventOut, this.leave, this);
218             }
219         }, this);
220     },
221     
222     
223     // private
224     timeout : null,
225     hoverState : null,
226     
227     toggle : function () {
228         this.hoverState == 'in' ? this.leave() : this.enter();
229     },
230     
231     enter : function () {
232         
233         clearTimeout(this.timeout);
234     
235         this.hoverState = 'in';
236     
237         if (!this.delay || !this.delay.show) {
238             this.show();
239             return;
240         }
241         var _t = this;
242         this.timeout = setTimeout(function () {
243             if (_t.hoverState == 'in') {
244                 _t.show();
245             }
246         }, this.delay.show)
247     },
248     
249     leave : function() {
250         clearTimeout(this.timeout);
251     
252         this.hoverState = 'out';
253     
254         if (!this.delay || !this.delay.hide) {
255             this.hide();
256             return;
257         }
258         var _t = this;
259         this.timeout = setTimeout(function () {
260             if (_t.hoverState == 'out') {
261                 _t.hide();
262             }
263         }, this.delay.hide)
264     },
265     /**
266      * Show the popover
267      * @param {Roo.Element|string|Boolean} - element to align and point to. (set align to [ pos, offset ])
268      * @param {string} (left|right|top|bottom) position
269      */
270     show : function (on_el, placement)
271     {
272         this.placement = typeof(placement) == 'undefined' ?  this.placement   : placement;
273         on_el = on_el || false; // default to false
274          
275         if (!on_el) {
276             if (this.parent() && (this.over == 'parent' || (this.over === false))) {
277                 on_el = this.parent().el;
278             } else if (this.over) {
279                 on_el = Roo.get(this.over);
280             }
281             
282         }
283         
284         this.alignEl = Roo.get( on_el );
285
286         if (!this.el) {
287             this.render(document.body);
288         }
289         
290         var x = this.alignEl.dom;
291         while (x && "auto" ==getComputedStyle(x).zIndex) {
292             //Roo.log(getComputedStyle(x).zIndex );
293             x = x.parentNode;
294         }
295         if (x) {
296             this.el.dom.style.zIndex = (1*getComputedStyle(x).zIndex) + 1000;
297         }
298          
299         
300         if (this.title === false) {
301             this.headerEl.hide();
302         }
303         
304        
305         this.el.show();
306         this.el.dom.style.display = 'block';
307          
308  
309         if (this.alignEl) {
310             this.updatePosition(this.placement, true);
311              
312         } else {
313             // this is usually just done by the builder = to show the popoup in the middle of the scren.
314             var es = this.el.getSize();
315             var x = Roo.lib.Dom.getViewWidth()/2;
316             var y = Roo.lib.Dom.getViewHeight()/2;
317             this.el.setXY([ x-(es.width/2),  y-(es.height/2)] );
318             
319         }
320
321         
322         //var arrow = this.el.select('.arrow',true).first();
323         //arrow.set(align[2], 
324         
325         this.el.addClass('in');
326         
327          
328         
329         this.hoverState = 'in';
330         
331         if (this.modal) {
332             this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
333             this.maskEl.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
334             this.maskEl.dom.style.display = 'block';
335             this.maskEl.addClass('show');
336         }
337         this.el.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
338  
339         this.fireEvent('show', this);
340         
341     },
342     /**
343      * fire this manually after loading a grid in the table for example
344      * @param {string} (left|right|top|bottom) where to try and put it (use false to use the last one)
345      * @param {Boolean} try and move it if we cant get right position.
346      */
347     updatePosition : function(placement, try_move)
348     {
349         // allow for calling with no parameters
350         placement = placement   ? placement :  this.placement;
351         try_move = typeof(try_move) == 'undefined' ? true : try_move;
352         
353         this.el.removeClass([
354             'fade','top','bottom', 'left', 'right','in',
355             'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
356         ]);
357         this.el.addClass(placement + ' bs-popover-' + placement);
358         
359         if (!this.alignEl ) {
360             return false;
361         }
362         
363         switch (placement) {
364             case 'right':
365                 var exact = this.el.getAlignToXY(this.alignEl, 'tl-tr', [10,0]);
366                 var offset = this.el.getAlignToXY(this.alignEl, 'tl-tr?',[10,0]);
367                 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
368                     //normal display... or moved up/down.
369                     this.el.setXY(offset);
370                     var xy = this.alignEl.getAnchorXY('tr', false);
371                     xy[0]+=2;xy[1]+=5;
372                     this.arrowEl.setXY(xy);
373                     return true;
374                 }
375                 // continue through...
376                 return this.updatePosition('left', false);
377                 
378             
379             case 'left':
380                 var exact = this.el.getAlignToXY(this.alignEl, 'tr-tl', [-10,0]);
381                 var offset = this.el.getAlignToXY(this.alignEl, 'tr-tl?',[-10,0]);
382                 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
383                     //normal display... or moved up/down.
384                     this.el.setXY(offset);
385                     var xy = this.alignEl.getAnchorXY('tl', false);
386                     xy[0]-=10;xy[1]+=5; // << fix me
387                     this.arrowEl.setXY(xy);
388                     return true;
389                 }
390                 // call self...
391                 return this.updatePosition('right', false);
392             
393             case 'top':
394                 var exact = this.el.getAlignToXY(this.alignEl, 'b-t', [0,-10]);
395                 var offset = this.el.getAlignToXY(this.alignEl, 'b-t?',[0,-10]);
396                 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
397                     //normal display... or moved up/down.
398                     this.el.setXY(offset);
399                     var xy = this.alignEl.getAnchorXY('t', false);
400                     xy[1]-=10; // << fix me
401                     this.arrowEl.setXY(xy);
402                     return true;
403                 }
404                 // fall through
405                return this.updatePosition('bottom', false);
406             
407             case 'bottom':
408                  var exact = this.el.getAlignToXY(this.alignEl, 't-b', [0,10]);
409                 var offset = this.el.getAlignToXY(this.alignEl, 't-b?',[0,10]);
410                 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
411                     //normal display... or moved up/down.
412                     this.el.setXY(offset);
413                     var xy = this.alignEl.getAnchorXY('b', false);
414                      xy[1]+=2; // << fix me
415                     this.arrowEl.setXY(xy);
416                     return true;
417                 }
418                 // fall through
419                 return this.updatePosition('top', false);
420                 
421             
422         }
423         
424         
425         return false;
426     },
427     
428     hide : function()
429     {
430         this.el.setXY([0,0]);
431         this.el.removeClass('in');
432         this.el.hide();
433         this.hoverState = null;
434         this.maskEl.hide(); // always..
435         this.fireEvent('hide', this);
436     }
437     
438 });
439
440
441 Roo.apply(Roo.bootstrap.Popover, {
442
443     alignment : {
444         'left' : ['r-l', [-10,0], 'left bs-popover-left'],
445         'right' : ['l-br', [10,0], 'right bs-popover-right'],
446         'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
447         'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
448     },
449     
450     zIndex : 20001,
451
452     clickHander : false,
453     
454
455     onMouseDown : function(e)
456     {
457         if (!e.getTarget(".roo-popover")) {
458             this.hideAll();
459         }
460          
461     },
462     
463     popups : [],
464     
465     register : function(popup)
466     {
467         if (!Roo.bootstrap.Popover.clickHandler) {
468             Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
469         }
470         // hide other popups.
471         this.hideAll();
472         this.popups.push(popup);
473     },
474     hideAll : function()
475     {
476         this.popups.forEach(function(p) {
477             p.hide();
478         });
479     }
480
481 });