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