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            cn : [
47                 {
48                     cls : 'arrow'
49                 },
50                 {
51                     cls : 'popover-inner',
52                     cn : [
53                         {
54                             tag: 'h3',
55                             cls: 'popover-title',
56                             html : this.title
57                         },
58                         {
59                             cls : 'popover-content',
60                             html : this.html
61                         }
62                     ]
63                     
64                 }
65            ]
66         };
67         
68         return cfg;
69     },
70     // as it get's added to the bottom of the page.
71     onRender : function(ct, position)
72     {
73         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
74         if(!this.el){
75             var cfg = Roo.apply({},  this.getAutoCreate());
76             cfg.id = Roo.id();
77             
78             if (this.cls) {
79                 cfg.cls += ' ' + this.cls;
80             }
81             if (this.style) {
82                 cfg.style = this.style;
83             }
84             this.el = Roo.get(document.body).createChild(cfg, position);
85         }
86         this.initEvents();
87     },
88     
89     initEvents : function()
90     {
91         this.el.select('.popover-title',true).setVisibilityMode(Roo.Element.DISPLAY);
92         this.el.setVisibilityMode(Roo.Element.DISPLAY);
93         this.el.hide();
94         if (over === false) {
95             return; 
96         }
97         if (this.triggers === false) {
98             return;
99         }
100         var on_el = (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
101         var triggers = this.trigger.split(' ');
102         Roo.each(triggers, function(trigger) {
103         
104             if (trigger == 'click') {
105                 this.on_el.on('click', this.toggle, this);
106             } else if (trigger != 'manual') {
107                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin'
108                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout'
109       
110                 this.on_el.on(eventIn  ,this.enter, this);
111               this.$element.on(eventOut, this.leave, this);
112             }
113         }, this);
114         
115     },
116     
117     
118     // private
119     timeout : null,
120     hoverState : null,
121     
122     toggle : function () {
123         this.hoverState == 'in' ? this.leave() : this.enter();
124     },
125     
126     enter : function () {
127        
128     
129         clearTimeout(this.timeout);
130     
131         this.hoverState = 'in'
132     
133         if (!this.delay || !this.delay.show) {
134             this.show();
135             return 
136         }
137         var _t = this;
138         this.timeout = setTimeout(function () {
139             if (_t.hoverState == 'in') {
140                 _t.show();
141             }
142         }, this.delay.show)
143     },
144     leave : function() {
145         clearTimeout(this.timeout);
146     
147         this.hoverState = 'out'
148     
149         if (!this.delay || !this.delay.hide) {
150             this.hide();
151             return 
152         }
153         var _t = this;
154         this.timeout = setTimeout(function () {
155             if (_t.hoverState == 'out') {
156                 _t.hide();
157             }
158         }, this.delay.hide)
159     },
160     
161     show : function (on_el)
162     {
163         if (!on_el) {
164             on_el= (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
165         }
166         // set content.
167         this.el.select('.popover-title').dom.innerHtml = this.title;
168         if (this.html !== false) {
169             this.el.select('.popover-content',true).dom.innerHtml = this.title;
170         }
171         this.el.removeClass('fade top bottom left right in');
172         if (!this.title.length) {
173             this.el.select('.popover-title',true).hide();
174         }
175         
176         var placement = typeof this.placement == 'function' ?
177             this.placement.call(this, this.el, on_el) :
178             this.placement;
179             
180         var autoToken = /\s?auto?\s?/i;
181         var autoPlace = autoToken.test(placement);
182         if (autoPlace) {
183             placement = placement.replace(autoToken, '') || 'top';
184         }
185         
186         //this.el.detach()
187         this.el.setXY([0,0]);
188         this.el.show();
189
190         
191         
192         
193         
194     },
195     
196 });
197
198  
199
200