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