roojs-bootstrap.js
[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  * @parent none builder
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     /**
269      * update the position of the dialog
270      * normally this is needed if the popover get's bigger - due to a Table reload etc..
271      * 
272      *
273      */
274     
275     doAlign : function()
276     {
277         
278         if (this.alignEl) {
279             this.updatePosition(this.placement, true);
280              
281         } else {
282             // this is usually just done by the builder = to show the popoup in the middle of the scren.
283             var es = this.el.getSize();
284             var x = Roo.lib.Dom.getViewWidth()/2;
285             var y = Roo.lib.Dom.getViewHeight()/2;
286             this.el.setXY([ x-(es.width/2),  y-(es.height/2)] );
287             
288         }
289
290          
291          
292         
293         
294     },
295     
296     /**
297      * Show the popover
298      * @param {Roo.Element|string|Boolean} - element to align and point to. (set align to [ pos, offset ])
299      * @param {string} (left|right|top|bottom) position
300      */
301     show : function (on_el, placement)
302     {
303         this.placement = typeof(placement) == 'undefined' ?  this.placement   : placement;
304         on_el = on_el || false; // default to false
305          
306         if (!on_el) {
307             if (this.parent() && (this.over == 'parent' || (this.over === false))) {
308                 on_el = this.parent().el;
309             } else if (this.over) {
310                 on_el = Roo.get(this.over);
311             }
312             
313         }
314         
315         this.alignEl = Roo.get( on_el );
316
317         if (!this.el) {
318             this.render(document.body);
319         }
320         
321         
322          
323         
324         if (this.title === false) {
325             this.headerEl.hide();
326         }
327         
328        
329         this.el.show();
330         this.el.dom.style.display = 'block';
331          
332         this.doAlign();
333         
334         //var arrow = this.el.select('.arrow',true).first();
335         //arrow.set(align[2], 
336         
337         this.el.addClass('in');
338         
339          
340         
341         this.hoverState = 'in';
342         
343         if (this.modal) {
344             this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
345             this.maskEl.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
346             this.maskEl.dom.style.display = 'block';
347             this.maskEl.addClass('show');
348         }
349         this.el.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
350  
351         this.fireEvent('show', this);
352         
353     },
354     /**
355      * fire this manually after loading a grid in the table for example
356      * @param {string} (left|right|top|bottom) where to try and put it (use false to use the last one)
357      * @param {Boolean} try and move it if we cant get right position.
358      */
359     updatePosition : function(placement, try_move)
360     {
361         // allow for calling with no parameters
362         placement = placement   ? placement :  this.placement;
363         try_move = typeof(try_move) == 'undefined' ? true : try_move;
364         
365         this.el.removeClass([
366             'fade','top','bottom', 'left', 'right','in',
367             'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
368         ]);
369         this.el.addClass(placement + ' bs-popover-' + placement);
370         
371         if (!this.alignEl ) {
372             return false;
373         }
374         
375         switch (placement) {
376             case 'right':
377                 var exact = this.el.getAlignToXY(this.alignEl, 'tl-tr', [10,0]);
378                 var offset = this.el.getAlignToXY(this.alignEl, 'tl-tr?',[10,0]);
379                 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
380                     //normal display... or moved up/down.
381                     this.el.setXY(offset);
382                     var xy = this.alignEl.getAnchorXY('tr', false);
383                     xy[0]+=2;xy[1]+=5;
384                     this.arrowEl.setXY(xy);
385                     return true;
386                 }
387                 // continue through...
388                 return this.updatePosition('left', false);
389                 
390             
391             case 'left':
392                 var exact = this.el.getAlignToXY(this.alignEl, 'tr-tl', [-10,0]);
393                 var offset = this.el.getAlignToXY(this.alignEl, 'tr-tl?',[-10,0]);
394                 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
395                     //normal display... or moved up/down.
396                     this.el.setXY(offset);
397                     var xy = this.alignEl.getAnchorXY('tl', false);
398                     xy[0]-=10;xy[1]+=5; // << fix me
399                     this.arrowEl.setXY(xy);
400                     return true;
401                 }
402                 // call self...
403                 return this.updatePosition('right', false);
404             
405             case 'top':
406                 var exact = this.el.getAlignToXY(this.alignEl, 'b-t', [0,-10]);
407                 var offset = this.el.getAlignToXY(this.alignEl, 'b-t?',[0,-10]);
408                 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
409                     //normal display... or moved up/down.
410                     this.el.setXY(offset);
411                     var xy = this.alignEl.getAnchorXY('t', false);
412                     xy[1]-=10; // << fix me
413                     this.arrowEl.setXY(xy);
414                     return true;
415                 }
416                 // fall through
417                return this.updatePosition('bottom', false);
418             
419             case 'bottom':
420                  var exact = this.el.getAlignToXY(this.alignEl, 't-b', [0,10]);
421                 var offset = this.el.getAlignToXY(this.alignEl, 't-b?',[0,10]);
422                 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
423                     //normal display... or moved up/down.
424                     this.el.setXY(offset);
425                     var xy = this.alignEl.getAnchorXY('b', false);
426                      xy[1]+=2; // << fix me
427                     this.arrowEl.setXY(xy);
428                     return true;
429                 }
430                 // fall through
431                 return this.updatePosition('top', false);
432                 
433             
434         }
435         
436         
437         return false;
438     },
439     
440     hide : function()
441     {
442         this.el.setXY([0,0]);
443         this.el.removeClass('in');
444         this.el.hide();
445         this.hoverState = null;
446         this.maskEl.hide(); // always..
447         this.fireEvent('hide', this);
448     }
449     
450 });
451
452
453 Roo.apply(Roo.bootstrap.Popover, {
454
455     alignment : {
456         'left' : ['r-l', [-10,0], 'left bs-popover-left'],
457         'right' : ['l-br', [10,0], 'right bs-popover-right'],
458         'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
459         'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
460     },
461     
462     zIndex : 20001,
463
464     clickHander : false,
465     
466     
467
468     onMouseDown : function(e)
469     {
470         if (this.popups.length &&  !e.getTarget(".roo-popover")) {
471             /// what is nothing is showing..
472             this.hideAll();
473         }
474          
475     },
476     
477     
478     popups : [],
479     
480     register : function(popup)
481     {
482         if (!Roo.bootstrap.Popover.clickHandler) {
483             Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
484         }
485         // hide other popups.
486         popup.on('show', Roo.bootstrap.Popover.onShow,  popup);
487         popup.on('hide', Roo.bootstrap.Popover.onHide,  popup);
488         this.hideAll(); //<< why?
489         //this.popups.push(popup);
490     },
491     hideAll : function()
492     {
493         this.popups.forEach(function(p) {
494             p.hide();
495         });
496     },
497     onShow : function() {
498         Roo.bootstrap.Popover.popups.push(this);
499     },
500     onHide : function() {
501         Roo.bootstrap.Popover.popups.remove(this);
502     } 
503
504 });