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