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