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