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         if (over === false) {
93             return; 
94         }
95         if (this.triggers === false) {
96             return;
97         }
98         var on_el = (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
99         var triggers = this.trigger.split(' ');
100         Roo.each(triggers, function(trigger) {
101         
102             if (trigger == 'click') {
103                 this.on_el.on('click', this.toggle, this);
104             } else if (trigger != 'manual') {
105                 var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin'
106                 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout'
107       
108                 this.on_el.on(eventIn  ,this.enter, this);
109               this.$element.on(eventOut, this.leave, this);
110             }
111         }, this);
112         
113     },
114     
115     
116     // private
117     timeout : null,
118     hoverState : null,
119     
120     toggle : function () {
121         this.hoverState == 'in' ? this.leave() : this.enter();
122     },
123     
124     enter : function () {
125        
126     
127         clearTimeout(this.timeout);
128     
129         this.hoverState = 'in'
130     
131         if (!this.delay || !this.delay.show) {
132             this.show();
133             return 
134         }
135         var _t = this;
136         this.timeout = setTimeout(function () {
137             if (_t.hoverState == 'in') {
138                 _t.show();
139             }
140         }, this.delay.show)
141     },
142     leave : function() {
143         clearTimeout(this.timeout);
144     
145         this.hoverState = 'out'
146     
147         if (!this.delay || !this.delay.hide) {
148             this.hide();
149             return 
150         }
151         var _t = this;
152         this.timeout = setTimeout(function () {
153             if (_t.hoverState == 'out') {
154                 _t.hide();
155             }
156         }, this.delay.hide)
157     },
158     
159     show : function (on_el)
160     {
161         if (!on_el) {
162             on_el= (this.over == 'parent') ? this.parent().el : Roo.get(this.over);
163         }
164         
165         this.el.select('.popover-title').dom.innerHtml = this.title;
166         if (this.html !== false) {
167             this.el.select('.popover-content',true).dom.innerHtml = this.title;
168         }
169         this.el.removeClass('fade top bottom left right in');
170         if (!this.title.length) {
171             this.el.select('.popover-title',true).hide();
172         }
173     },
174     
175 });
176
177  
178
179