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         var triggers = this.trigger ? this.trigger.split(' ') : [];
118         Roo.each(triggers, function(trigger) {
119         
120             if (trigger == 'click') {
121                 on_el.on('click', this.toggle, this);
122             } else if (trigger != 'manual') {
123                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin';
124                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
125       
126                 on_el.on(eventIn  ,this.enter, this);
127                 on_el.on(eventOut, this.leave, this);
128             }
129         }, this);
130         
131     },
132     
133     
134     // private
135     timeout : null,
136     hoverState : null,
137     
138     toggle : function () {
139         Roo.log(this);
140         Roo.log(this.hoverState);
141         this.hoverState == 'in' ? this.leave() : this.enter();
142     },
143     
144     enter : function () {
145        
146         clearTimeout(this.timeout);
147         
148         Roo.log(this);
149         Roo.log(this.hoverState);
150         this.hoverState = 'in';
151         
152         Roo.log(this.hoverState);
153         if (!this.delay || !this.delay.show) {
154             this.show();
155             return;
156         }
157         var _t = this;
158         this.timeout = setTimeout(function () {
159             if (_t.hoverState == 'in') {
160                 _t.show();
161             }
162         }, this.delay.show)
163     },
164     leave : function() {
165         Roo.log('run????');
166         clearTimeout(this.timeout);
167     
168         this.hoverState = 'out';
169     
170         if (!this.delay || !this.delay.hide) {
171             this.hide();
172             return;
173         }
174         var _t = this;
175         this.timeout = setTimeout(function () {
176             if (_t.hoverState == 'out') {
177                 _t.hide();
178             }
179         }, this.delay.hide)
180     },
181     
182     show : function (on_el)
183     {
184         if (!on_el) {
185             on_el= (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
186         }
187         // set content.
188         this.el.select('.popover-title',true).first().dom.innerHtml = this.title;
189         if (this.html !== false) {
190             this.el.select('.popover-content',true).first().dom.innerHtml = this.title;
191         }
192         this.el.removeClass(['fade','top','bottom', 'left', 'right','in']);
193         if (!this.title.length) {
194             this.el.select('.popover-title',true).hide();
195         }
196         
197         var placement = typeof this.placement == 'function' ?
198             this.placement.call(this, this.el, on_el) :
199             this.placement;
200             
201         var autoToken = /\s?auto?\s?/i;
202         var autoPlace = autoToken.test(placement);
203         if (autoPlace) {
204             placement = placement.replace(autoToken, '') || 'top';
205         }
206         
207         //this.el.detach()
208         //this.el.setXY([0,0]);
209         this.el.show();
210         this.el.dom.style.display='block';
211         this.el.addClass(placement);
212         
213         //this.el.appendTo(on_el);
214         
215         var p = this.getPosition();
216         var box = this.el.getBox();
217         
218         if (autoPlace) {
219             // fixme..
220         }
221         var align = Roo.bootstrap.Popover.alignment[placement];
222         this.el.alignTo(on_el, align[0],align[1]);
223         //var arrow = this.el.select('.arrow',true).first();
224         //arrow.set(align[2], 
225         
226         this.el.addClass('in');
227         this.hoverState = null;
228         
229         if (this.el.hasClass('fade')) {
230             // fade it?
231         }
232         
233     },
234     hide : function()
235     {
236         this.el.setXY([0,0]);
237         this.el.removeClass('in');
238         this.el.hide();
239         
240     }
241     
242 });
243
244 Roo.bootstrap.Popover.alignment = {
245     'left' : ['r-l', [-10,0], 'right'],
246     'right' : ['l-r', [10,0], 'left'],
247     'bottom' : ['t-b', [0,10], 'top'],
248     'top' : [ 'b-t', [0,-10], 'bottom']
249 };
250
251