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