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