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