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