2726bcb973ed362f4254b1d204d8ce8e2b6f7472
[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  * @builder-top
12  * @children Roo.bootstrap.Component
13  * Bootstrap Popover class
14  * @cfg {String} html contents of the popover   (or false to use children..)
15  * @cfg {String} title of popover (or false to hide)
16  * @cfg {String|function} (right|top|bottom|left|auto) placement how it is placed
17  * @cfg {String} trigger click || hover (or false to trigger manually)
18  * @cfg {Boolean} modal - popovers that are modal will mask the screen, and must be closed with another event.
19  * @cfg {String|Boolean|Roo.Element} add click hander to trigger show over what element
20  *      - if false and it has a 'parent' then it will be automatically added to that element
21  *      - if string - Roo.get  will be called 
22  * @cfg {Number} delay - delay before showing
23  
24  * @constructor
25  * Create a new Popover
26  * @param {Object} config The config object
27  */
28
29 Roo.bootstrap.Popover = function(config){
30     Roo.bootstrap.Popover.superclass.constructor.call(this, config);
31     
32     this.addEvents({
33         // raw events
34          /**
35          * @event show
36          * After the popover show
37          * 
38          * @param {Roo.bootstrap.Popover} this
39          */
40         "show" : true,
41         /**
42          * @event hide
43          * After the popover hide
44          * 
45          * @param {Roo.bootstrap.Popover} this
46          */
47         "hide" : true
48     });
49 };
50
51 Roo.extend(Roo.bootstrap.Popover, Roo.bootstrap.Component,  {
52     
53     title: false,
54     html: false,
55     
56     placement : 'right',
57     trigger : 'hover', // hover
58     modal : false,
59     delay : 0,
60     
61     over: false,
62     
63     can_build_overlaid : false,
64     
65     maskEl : false, // the mask element
66     headerEl : false,
67     contentEl : false,
68     alignEl : false, // when show is called with an element - this get's stored.
69     
70     getChildContainer : function()
71     {
72         return this.contentEl;
73         
74     },
75     getPopoverHeader : function()
76     {
77         this.title = true; // flag not to hide it..
78         this.headerEl.addClass('p-0');
79         return this.headerEl
80     },
81     
82     
83     getAutoCreate : function(){
84          
85         var cfg = {
86            cls : 'popover roo-dynamic shadow roo-popover' + (this.modal ? '-modal' : ''),
87            style: 'display:block',
88            cn : [
89                 {
90                     cls : 'arrow'
91                 },
92                 {
93                     cls : 'popover-inner ',
94                     cn : [
95                         {
96                             tag: 'h3',
97                             cls: 'popover-title popover-header',
98                             html : this.title === false ? '' : this.title
99                         },
100                         {
101                             cls : 'popover-content popover-body '  + (this.cls || ''),
102                             html : this.html || ''
103                         }
104                     ]
105                     
106                 }
107            ]
108         };
109         
110         return cfg;
111     },
112     /**
113      * @param {string} the title
114      */
115     setTitle: function(str)
116     {
117         this.title = str;
118         if (this.el) {
119             this.headerEl.dom.innerHTML = str;
120         }
121         
122     },
123     /**
124      * @param {string} the body content
125      */
126     setContent: function(str)
127     {
128         this.html = str;
129         if (this.contentEl) {
130             this.contentEl.dom.innerHTML = str;
131         }
132         
133     },
134     // as it get's added to the bottom of the page.
135     onRender : function(ct, position)
136     {
137         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
138         
139         
140         
141         if(!this.el){
142             var cfg = Roo.apply({},  this.getAutoCreate());
143             cfg.id = Roo.id();
144             
145             if (this.cls) {
146                 cfg.cls += ' ' + this.cls;
147             }
148             if (this.style) {
149                 cfg.style = this.style;
150             }
151             //Roo.log("adding to ");
152             this.el = Roo.get(document.body).createChild(cfg, position);
153 //            Roo.log(this.el);
154         }
155         
156         this.contentEl = this.el.select('.popover-content',true).first();
157         this.headerEl =  this.el.select('.popover-title',true).first();
158         
159         var nitems = [];
160         if(typeof(this.items) != 'undefined'){
161             var items = this.items;
162             delete this.items;
163
164             for(var i =0;i < items.length;i++) {
165                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
166             }
167         }
168
169         this.items = nitems;
170         
171         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
172         Roo.EventManager.onWindowResize(this.resizeMask, this, true);
173         
174         
175         
176         this.initEvents();
177     },
178     
179     resizeMask : function()
180     {
181         this.maskEl.setSize(
182             Roo.lib.Dom.getViewWidth(true),
183             Roo.lib.Dom.getViewHeight(true)
184         );
185     },
186     
187     initEvents : function()
188     {
189         
190         if (!this.modal) { 
191             Roo.bootstrap.Popover.register(this);
192         }
193          
194         this.arrowEl = this.el.select('.arrow',true).first();
195         this.headerEl.setVisibilityMode(Roo.Element.DISPLAY); // probably not needed as it's default in BS4
196         this.el.enableDisplayMode('block');
197         this.el.hide();
198  
199         
200         if (this.over === false && !this.parent()) {
201             return; 
202         }
203         if (this.triggers === false) {
204             return;
205         }
206          
207         // support parent
208         var on_el = (this.over == 'parent' || this.over === false) ? this.parent().el : Roo.get(this.over);
209         var triggers = this.trigger ? this.trigger.split(' ') : [];
210         Roo.each(triggers, function(trigger) {
211         
212             if (trigger == 'click') {
213                 on_el.on('click', this.toggle, this);
214             } else if (trigger != 'manual') {
215                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin';
216                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
217       
218                 on_el.on(eventIn  ,this.enter, this);
219                 on_el.on(eventOut, this.leave, this);
220             }
221         }, this);
222     },
223     
224     
225     // private
226     timeout : null,
227     hoverState : null,
228     
229     toggle : function () {
230         this.hoverState == 'in' ? this.leave() : this.enter();
231     },
232     
233     enter : function () {
234         
235         clearTimeout(this.timeout);
236     
237         this.hoverState = 'in';
238     
239         if (!this.delay || !this.delay.show) {
240             this.show();
241             return;
242         }
243         var _t = this;
244         this.timeout = setTimeout(function () {
245             if (_t.hoverState == 'in') {
246                 _t.show();
247             }
248         }, this.delay.show)
249     },
250     
251     leave : function() {
252         clearTimeout(this.timeout);
253     
254         this.hoverState = 'out';
255     
256         if (!this.delay || !this.delay.hide) {
257             this.hide();
258             return;
259         }
260         var _t = this;
261         this.timeout = setTimeout(function () {
262             if (_t.hoverState == 'out') {
263                 _t.hide();
264             }
265         }, this.delay.hide)
266     },
267     /**
268      * Show the popover
269      * @param {Roo.Element|string|Boolean} - element to align and point to. (set align to [ pos, offset ])
270      * @param {string} (left|right|top|bottom) position
271      */
272     show : function (on_el, placement)
273     {
274         this.placement = typeof(placement) == 'undefined' ?  this.placement   : placement;
275         on_el = on_el || false; // default to false
276          
277         if (!on_el) {
278             if (this.parent() && (this.over == 'parent' || (this.over === false))) {
279                 on_el = this.parent().el;
280             } else if (this.over) {
281                 on_el = Roo.get(this.over);
282             }
283             
284         }
285         
286         this.alignEl = Roo.get( on_el );
287
288         if (!this.el) {
289             this.render(document.body);
290         }
291         
292         
293          
294         
295         if (this.title === false) {
296             this.headerEl.hide();
297         }
298         
299        
300         this.el.show();
301         this.el.dom.style.display = 'block';
302          
303  
304         if (this.alignEl) {
305             this.updatePosition(this.placement, true);
306              
307         } else {
308             // this is usually just done by the builder = to show the popoup in the middle of the scren.
309             var es = this.el.getSize();
310             var x = Roo.lib.Dom.getViewWidth()/2;
311             var y = Roo.lib.Dom.getViewHeight()/2;
312             this.el.setXY([ x-(es.width/2),  y-(es.height/2)] );
313             
314         }
315
316         
317         //var arrow = this.el.select('.arrow',true).first();
318         //arrow.set(align[2], 
319         
320         this.el.addClass('in');
321         
322          
323         
324         this.hoverState = 'in';
325         
326         if (this.modal) {
327             this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
328             this.maskEl.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
329             this.maskEl.dom.style.display = 'block';
330             this.maskEl.addClass('show');
331         }
332         this.el.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
333  
334         this.fireEvent('show', this);
335         
336     },
337     /**
338      * fire this manually after loading a grid in the table for example
339      * @param {string} (left|right|top|bottom) where to try and put it (use false to use the last one)
340      * @param {Boolean} try and move it if we cant get right position.
341      */
342     updatePosition : function(placement, try_move)
343     {
344         // allow for calling with no parameters
345         placement = placement   ? placement :  this.placement;
346         try_move = typeof(try_move) == 'undefined' ? true : try_move;
347         
348         this.el.removeClass([
349             'fade','top','bottom', 'left', 'right','in',
350             'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
351         ]);
352         this.el.addClass(placement + ' bs-popover-' + placement);
353         
354         if (!this.alignEl ) {
355             return false;
356         }
357         
358         switch (placement) {
359             case 'right':
360                 var exact = this.el.getAlignToXY(this.alignEl, 'tl-tr', [10,0]);
361                 var offset = this.el.getAlignToXY(this.alignEl, 'tl-tr?',[10,0]);
362                 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
363                     //normal display... or moved up/down.
364                     this.el.setXY(offset);
365                     var xy = this.alignEl.getAnchorXY('tr', false);
366                     xy[0]+=2;xy[1]+=5;
367                     this.arrowEl.setXY(xy);
368                     return true;
369                 }
370                 // continue through...
371                 return this.updatePosition('left', false);
372                 
373             
374             case 'left':
375                 var exact = this.el.getAlignToXY(this.alignEl, 'tr-tl', [-10,0]);
376                 var offset = this.el.getAlignToXY(this.alignEl, 'tr-tl?',[-10,0]);
377                 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
378                     //normal display... or moved up/down.
379                     this.el.setXY(offset);
380                     var xy = this.alignEl.getAnchorXY('tl', false);
381                     xy[0]-=10;xy[1]+=5; // << fix me
382                     this.arrowEl.setXY(xy);
383                     return true;
384                 }
385                 // call self...
386                 return this.updatePosition('right', false);
387             
388             case 'top':
389                 var exact = this.el.getAlignToXY(this.alignEl, 'b-t', [0,-10]);
390                 var offset = this.el.getAlignToXY(this.alignEl, 'b-t?',[0,-10]);
391                 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
392                     //normal display... or moved up/down.
393                     this.el.setXY(offset);
394                     var xy = this.alignEl.getAnchorXY('t', false);
395                     xy[1]-=10; // << fix me
396                     this.arrowEl.setXY(xy);
397                     return true;
398                 }
399                 // fall through
400                return this.updatePosition('bottom', false);
401             
402             case 'bottom':
403                  var exact = this.el.getAlignToXY(this.alignEl, 't-b', [0,10]);
404                 var offset = this.el.getAlignToXY(this.alignEl, 't-b?',[0,10]);
405                 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
406                     //normal display... or moved up/down.
407                     this.el.setXY(offset);
408                     var xy = this.alignEl.getAnchorXY('b', false);
409                      xy[1]+=2; // << fix me
410                     this.arrowEl.setXY(xy);
411                     return true;
412                 }
413                 // fall through
414                 return this.updatePosition('top', false);
415                 
416             
417         }
418         
419         
420         return false;
421     },
422     
423     hide : function()
424     {
425         this.el.setXY([0,0]);
426         this.el.removeClass('in');
427         this.el.hide();
428         this.hoverState = null;
429         this.maskEl.hide(); // always..
430         this.fireEvent('hide', this);
431     }
432     
433 });
434
435
436 Roo.apply(Roo.bootstrap.Popover, {
437
438     alignment : {
439         'left' : ['r-l', [-10,0], 'left bs-popover-left'],
440         'right' : ['l-br', [10,0], 'right bs-popover-right'],
441         'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
442         'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
443     },
444     
445     zIndex : 20001,
446
447     clickHander : false,
448     
449     
450
451     onMouseDown : function(e)
452     {
453         if (this.popups.length &&  !e.getTarget(".roo-popover")) {
454             /// what is nothing is showing..
455             this.hideAll();
456         }
457          
458     },
459     
460     
461     popups : [],
462     
463     register : function(popup)
464     {
465         if (!Roo.bootstrap.Popover.clickHandler) {
466             Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
467         }
468         // hide other popups.
469         popup.on('show', Roo.bootstrap.Popover.onShow,  popup);
470         popup.on('hide', Roo.bootstrap.Popover.onHide,  popup);
471         this.hideAll(); //<< why?
472         //this.popups.push(popup);
473     },
474     hideAll : function()
475     {
476         this.popups.forEach(function(p) {
477             p.hide();
478         });
479     },
480     onShow : function() {
481         Roo.bootstrap.Popover.popups.push(this);
482     },
483     onHide : function() {
484         Roo.bootstrap.Popover.popups.remove(this);
485     } 
486
487 });