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