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