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