9 * @class Roo.bootstrap.Popover
10 * @extends Roo.bootstrap.Component
11 * Bootstrap Popover 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|function} (right|top|bottom|left|auto) placement how it is placed
15 * @cfg {String} trigger click || hover (or false to trigger manually)
16 * @cfg {Boolean} modal - popovers that are modal will mask the screen, and must be closed with another event.
17 * @cfg {String|Boolean|Roo.Element} add click hander to trigger show over what element
18 * - if false and it has a 'parent' then it will be automatically added to that element
19 * - if string - Roo.get will be called
20 * @cfg {Number} delay - delay before showing
23 * Create a new Popover
24 * @param {Object} config The config object
27 Roo.bootstrap.Popover = function(config){
28 Roo.bootstrap.Popover.superclass.constructor.call(this, config);
34 * After the popover show
36 * @param {Roo.bootstrap.Popover} this
41 * After the popover hide
43 * @param {Roo.bootstrap.Popover} this
49 Roo.extend(Roo.bootstrap.Popover, Roo.bootstrap.Component, {
55 trigger : 'hover', // hover
61 can_build_overlaid : false,
63 maskEl : false, // the mask element
66 alignEl : false, // when show is called with an element - this get's stored.
68 getChildContainer : function()
70 return this.contentEl;
73 getPopoverHeader : function()
75 this.title = true; // flag not to hide it..
76 this.headerEl.addClass('p-0');
81 getAutoCreate : function(){
84 cls : 'popover roo-dynamic shadow roo-popover' + (this.modal ? '-modal' : ''),
85 style: 'display:block',
91 cls : 'popover-inner ',
95 cls: 'popover-title popover-header',
96 html : this.title === false ? '' : this.title
99 cls : 'popover-content popover-body ' + (this.cls || ''),
100 html : this.html || ''
111 * @param {string} the title
113 setTitle: function(str)
117 this.headerEl.dom.innerHTML = str;
122 * @param {string} the body content
124 setContent: function(str)
127 if (this.contentEl) {
128 this.contentEl.dom.innerHTML = str;
132 // as it get's added to the bottom of the page.
133 onRender : function(ct, position)
135 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
140 var cfg = Roo.apply({}, this.getAutoCreate());
144 cfg.cls += ' ' + this.cls;
147 cfg.style = this.style;
149 //Roo.log("adding to ");
150 this.el = Roo.get(document.body).createChild(cfg, position);
154 this.contentEl = this.el.select('.popover-content',true).first();
155 this.headerEl = this.el.select('.popover-title',true).first();
158 if(typeof(this.items) != 'undefined'){
159 var items = this.items;
162 for(var i =0;i < items.length;i++) {
163 nitems.push(this.addxtype(Roo.apply({}, items[i])));
169 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
170 Roo.EventManager.onWindowResize(this.resizeMask, this, true);
177 resizeMask : function()
180 Roo.lib.Dom.getViewWidth(true),
181 Roo.lib.Dom.getViewHeight(true)
185 initEvents : function()
189 Roo.bootstrap.Popover.register(this);
192 this.arrowEl = this.el.select('.arrow',true).first();
193 this.headerEl.setVisibilityMode(Roo.Element.DISPLAY); // probably not needed as it's default in BS4
194 this.el.enableDisplayMode('block');
198 if (this.over === false && !this.parent()) {
201 if (this.triggers === false) {
206 var on_el = (this.over == 'parent' || this.over === false) ? this.parent().el : Roo.get(this.over);
207 var triggers = this.trigger ? this.trigger.split(' ') : [];
208 Roo.each(triggers, function(trigger) {
210 if (trigger == 'click') {
211 on_el.on('click', this.toggle, this);
212 } else if (trigger != 'manual') {
213 var eventIn = trigger == 'hover' ? 'mouseenter' : 'focusin';
214 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
216 on_el.on(eventIn ,this.enter, this);
217 on_el.on(eventOut, this.leave, this);
227 toggle : function () {
228 this.hoverState == 'in' ? this.leave() : this.enter();
231 enter : function () {
233 clearTimeout(this.timeout);
235 this.hoverState = 'in';
237 if (!this.delay || !this.delay.show) {
242 this.timeout = setTimeout(function () {
243 if (_t.hoverState == 'in') {
250 clearTimeout(this.timeout);
252 this.hoverState = 'out';
254 if (!this.delay || !this.delay.hide) {
259 this.timeout = setTimeout(function () {
260 if (_t.hoverState == 'out') {
267 * @param {Roo.Element|string|Boolean} - element to align and point to. (set align to [ pos, offset ])
268 * @param {string} (left|right|top|bottom) position
270 show : function (on_el, placement)
272 this.placement = typeof(placement) == 'undefined' ? this.placement : placement;
273 on_el = on_el || false; // default to false
276 if (this.parent() && (this.over == 'parent' || (this.over === false))) {
277 on_el = this.parent().el;
278 } else if (this.over) {
284 this.alignEl = on_el;
287 this.render(document.body);
293 if (this.title === false) {
294 this.headerEl.hide();
299 this.el.dom.style.display = 'block';
303 this.updatePosition(this.placement, true);
306 // this is usually just done by the builder = to show the popoup in the middle of the scren.
307 var es = this.el.getSize();
308 var x = Roo.lib.Dom.getViewWidth()/2;
309 var y = Roo.lib.Dom.getViewHeight()/2;
310 this.el.setXY([ x-(es.width/2), y-(es.height/2)] );
315 //var arrow = this.el.select('.arrow',true).first();
316 //arrow.set(align[2],
318 this.el.addClass('in');
322 this.hoverState = 'in';
325 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
326 this.maskEl.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
327 this.maskEl.dom.style.display = 'block';
328 this.maskEl.addClass('show');
330 this.el.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
332 this.fireEvent('show', this);
336 * fire this manually after loading a grid in the table for example
337 * @param {string} (left|right|top|bottom) where to try and put it (use false to use the last one)
338 * @param {Boolean} try and move it if we cant get right position.
340 updatePosition : function(placement, try_move)
342 // allow for calling with no parameters
343 placement = placement ? placement : this.placement;
344 try_move = typeof(try_move) == 'undefined' ? true : try_move;
346 this.el.removeClass([
347 'fade','top','bottom', 'left', 'right','in',
348 'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
350 this.el.addClass(placement + ' bs-popover-' + placement);
352 if (!this.alignEl ) {
358 var exact = this.el.getAlignToXY(this.alignEl, 'tl-tr', [10,0]);
359 var offset = this.el.getAlignToXY(this.alignEl, 'tl-tr?',[10,0]);
360 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
361 //normal display... or moved up/down.
362 this.el.setXY(offset);
363 var xy = this.alignEl.getAnchorXY('tr', false);
365 this.arrowEl.setXY(xy);
368 // continue through...
373 var exact = this.el.getAlignToXY(this.alignEl, 'tr-tl', [-10,0]);
374 var offset = this.el.getAlignToXY(this.alignEl, 'tr-tl?',[-10,0]);
375 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
376 //normal display... or moved up/down.
377 this.el.setXY(offset);
378 var xy = this.alignEl.getAnchorXY('tl', false);
379 xy[0]+=2;xy[1]+=5; // << fix me
380 this.arrowEl.setXY(xy);
384 return this.updatePosition('right', false);
387 var exact = this.el.getAlignToXY(this.alignEl, 'b-t', [0,-10]);
388 var offset = this.el.getAlignToXY(this.alignEl, 'b-t?',[0,-10]);
389 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
390 //normal display... or moved up/down.
391 this.el.setXY(offset);
392 var xy = this.alignEl.getAnchorXY('t', false);
393 xy[1]-=10; // << fix me
394 this.arrowEl.setXY(xy);
401 var exact = this.el.getAlignToXY(this.alignEl, 't-b', [0,10]);
402 var offset = this.el.getAlignToXY(this.alignEl, 't-b?',[0,10]);
403 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
404 //normal display... or moved up/down.
405 this.el.setXY(offset);
406 var xy = this.alignEl.getAnchorXY('b', false);
407 xy[1]+=5; // << fix me
408 this.arrowEl.setXY(xy);
412 return this.updatePosition('top', false);
423 this.el.setXY([0,0]);
424 this.el.removeClass('in');
426 this.hoverState = null;
427 this.maskEl.hide(); // always..
428 this.fireEvent('hide', this);
434 Roo.apply(Roo.bootstrap.Popover, {
437 'left' : ['r-l', [-10,0], 'left bs-popover-left'],
438 'right' : ['l-br', [10,0], 'right bs-popover-right'],
439 'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
440 'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
448 onMouseDown : function(e)
450 if (!e.getTarget(".roo-popover")) {
458 register : function(popup)
460 if (!Roo.bootstrap.Popover.clickHandler) {
461 Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
463 // hide other popups.
465 this.popups.push(popup);
469 this.popups.forEach(function(p) {