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} placement how it is placed
15  * @cfg {String} trigger click || hover (or false to trigger manually)
16  * @cfg {String} over what (parent or false to trigger manually.)
17  * @cfg {Number} delay - delay before showing
18  
19  * @constructor
20  * Create a new Popover
21  * @param {Object} config The config object
22  */
23
24 Roo.bootstrap.Popover = function(config){
25     Roo.bootstrap.Popover.superclass.constructor.call(this, config);
26 };
27
28 Roo.extend(Roo.bootstrap.Popover, Roo.bootstrap.Component,  {
29     
30     title: 'Fill in a title',
31     html: false,
32     
33     placement : 'right',
34     trigger : 'hover', // hover
35     
36     delay : 0,
37     
38     over: 'parent',
39     
40     can_build_overlaid : false,
41     
42     getChildContainer : function()
43     {
44         return this.el.select('.popover-content',true).first();
45     },
46     
47     getAutoCreate : function(){
48          Roo.log('make popover?');
49         var cfg = {
50            cls : 'popover roo-dynamic',
51            style: 'display:block',
52            cn : [
53                 {
54                     cls : 'arrow'
55                 },
56                 {
57                     cls : 'popover-inner',
58                     cn : [
59                         {
60                             tag: 'h3',
61                             cls: 'popover-title',
62                             html : this.title
63                         },
64                         {
65                             cls : 'popover-content',
66                             html : this.html
67                         }
68                     ]
69                     
70                 }
71            ]
72         };
73         
74         return cfg;
75     },
76     setTitle: function(str)
77     {
78         this.el.select('.popover-title',true).first().dom.innerHTML = str;
79     },
80     setContent: function(str)
81     {
82         this.el.select('.popover-content',true).first().dom.innerHTML = str;
83     },
84     // as it get's added to the bottom of the page.
85     onRender : function(ct, position)
86     {
87         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
88         if(!this.el){
89             var cfg = Roo.apply({},  this.getAutoCreate());
90             cfg.id = Roo.id();
91             
92             if (this.cls) {
93                 cfg.cls += ' ' + this.cls;
94             }
95             if (this.style) {
96                 cfg.style = this.style;
97             }
98             Roo.log("adding to ")
99             this.el = Roo.get(document.body).createChild(cfg, position);
100             Roo.log(this.el);
101         }
102         this.initEvents();
103     },
104     
105     initEvents : function()
106     {
107         this.el.select('.popover-title',true).setVisibilityMode(Roo.Element.DISPLAY);
108         this.el.enableDisplayMode('block');
109         this.el.hide();
110         if (this.over === false) {
111             return; 
112         }
113         if (this.triggers === false) {
114             return;
115         }
116         var on_el = (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
117         Roo.log('Popover init!!!!');
118         Roo.log(on_el);
119         var triggers = this.trigger ? this.trigger.split(' ') : [];
120         Roo.each(triggers, function(trigger) {
121         
122             if (trigger == 'click') {
123                 on_el.on('click', this.toggle, this);
124             } else if (trigger != 'manual') {
125                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin';
126                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
127       
128                 on_el.on(eventIn  ,this.enter, this);
129                 on_el.on(eventOut, this.leave, this);
130             }
131         }, this);
132         
133     },
134     
135     
136     // private
137     timeout : null,
138     hoverState : null,
139     
140     toggle : function () {
141         this.hoverState == 'in' ? this.leave() : this.enter();
142     },
143     
144     enter : function () {
145        
146     
147         clearTimeout(this.timeout);
148     
149         this.hoverState = 'in';
150     
151         if (!this.delay || !this.delay.show) {
152             this.show();
153             return;
154         }
155         var _t = this;
156         this.timeout = setTimeout(function () {
157             if (_t.hoverState == 'in') {
158                 _t.show();
159             }
160         }, this.delay.show)
161     },
162     leave : function() {
163         clearTimeout(this.timeout);
164     
165         this.hoverState = 'out';
166     
167         if (!this.delay || !this.delay.hide) {
168             this.hide();
169             return;
170         }
171         var _t = this;
172         this.timeout = setTimeout(function () {
173             if (_t.hoverState == 'out') {
174                 _t.hide();
175             }
176         }, this.delay.hide)
177     },
178     
179     show : function (on_el)
180     {
181         if (!on_el) {
182             on_el= (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
183         }
184         // set content.
185         this.el.select('.popover-title',true).first().dom.innerHtml = this.title;
186         if (this.html !== false) {
187             this.el.select('.popover-content',true).first().dom.innerHtml = this.title;
188         }
189         this.el.removeClass(['fade','top','bottom', 'left', 'right','in']);
190         if (!this.title.length) {
191             this.el.select('.popover-title',true).hide();
192         }
193         
194         var placement = typeof this.placement == 'function' ?
195             this.placement.call(this, this.el, on_el) :
196             this.placement;
197             
198         var autoToken = /\s?auto?\s?/i;
199         var autoPlace = autoToken.test(placement);
200         if (autoPlace) {
201             placement = placement.replace(autoToken, '') || 'top';
202         }
203         
204         //this.el.detach()
205         //this.el.setXY([0,0]);
206         this.el.show();
207         this.el.dom.style.display='block';
208         this.el.addClass(placement);
209         
210         //this.el.appendTo(on_el);
211         
212         var p = this.getPosition();
213         var box = this.el.getBox();
214         
215         if (autoPlace) {
216             // fixme..
217         }
218         var align = Roo.bootstrap.Popover.alignment[placement];
219         this.el.alignTo(on_el, align[0],align[1]);
220         //var arrow = this.el.select('.arrow',true).first();
221         //arrow.set(align[2], 
222         
223         this.el.addClass('in');
224         this.hoverState = null;
225         
226         if (this.el.hasClass('fade')) {
227             // fade it?
228         }
229         
230     },
231     hide : function()
232     {
233         this.el.setXY([0,0]);
234         this.el.removeClass('in');
235         this.el.hide();
236         
237     }
238     
239 });
240
241 Roo.bootstrap.Popover.alignment = {
242     'left' : ['r-l', [-10,0], 'right'],
243     'right' : ['l-r', [10,0], 'left'],
244     'bottom' : ['t-b', [0,10], 'top'],
245     'top' : [ 'b-t', [0,-10], 'bottom']
246 };
247
248