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