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