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 Element 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         this.el.select('.popover-title',true).enableDisplayMode('block');
95         this.el.setVisibilityMode(Roo.Element.DISPLAY);
96         this.el.hide();
97         if (this.over === false) {
98             return; 
99         }
100         if (this.triggers === false) {
101             return;
102         }
103         var on_el = (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
104         var triggers = this.trigger.split(' ');
105         Roo.each(triggers, function(trigger) {
106         
107             if (trigger == 'click') {
108                 on_el.on('click', this.toggle, this);
109             } else if (trigger != 'manual') {
110                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin'
111                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout'
112       
113                 on_el.on(eventIn  ,this.enter, this);
114                 on_el.on(eventOut, this.leave, this);
115             }
116         }, this);
117         
118     },
119     
120     
121     // private
122     timeout : null,
123     hoverState : null,
124     
125     toggle : function () {
126         this.hoverState == 'in' ? this.leave() : this.enter();
127     },
128     
129     enter : function () {
130        
131     
132         clearTimeout(this.timeout);
133     
134         this.hoverState = 'in'
135     
136         if (!this.delay || !this.delay.show) {
137             this.show();
138             return 
139         }
140         var _t = this;
141         this.timeout = setTimeout(function () {
142             if (_t.hoverState == 'in') {
143                 _t.show();
144             }
145         }, this.delay.show)
146     },
147     leave : function() {
148         clearTimeout(this.timeout);
149     
150         this.hoverState = 'out'
151     
152         if (!this.delay || !this.delay.hide) {
153             this.hide();
154             return 
155         }
156         var _t = this;
157         this.timeout = setTimeout(function () {
158             if (_t.hoverState == 'out') {
159                 _t.hide();
160             }
161         }, this.delay.hide)
162     },
163     
164     show : function (on_el)
165     {
166         if (!on_el) {
167             on_el= (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
168         }
169         // set content.
170         this.el.select('.popover-title',true).first().dom.innerHtml = this.title;
171         if (this.html !== false) {
172             this.el.select('.popover-content',true).first().dom.innerHtml = this.title;
173         }
174         this.el.removeClass('fade top bottom left right in');
175         if (!this.title.length) {
176             this.el.select('.popover-title',true).hide();
177         }
178         
179         var placement = typeof this.placement == 'function' ?
180             this.placement.call(this, this.el, on_el) :
181             this.placement;
182             
183         var autoToken = /\s?auto?\s?/i;
184         var autoPlace = autoToken.test(placement);
185         if (autoPlace) {
186             placement = placement.replace(autoToken, '') || 'top';
187         }
188         
189         //this.el.detach()
190         //this.el.setXY([0,0]);
191         this.el.show();
192       
193         this.el.addClass(placement);
194         
195         //this.el.appendTo(on_el);
196         
197         var p = this.getPosition();
198         var box = this.el.getBox();
199         
200         if (autoPlace) {
201             // fixme..
202         }
203         
204         this.el.alignTo(on_el, placement[0]);
205         
206         this.el.addClass('in');
207         this.hoverState = null;
208         
209         if (this.el.hasClass('fade')) {
210             // fade it?
211             
212         }
213         
214     },
215     hide : function()
216     {
217         this.el.setXY([0,0]);
218         this.el.removeClass('in');
219         
220     }
221     
222 });
223
224  
225
226