9 * @class Roo.bootstrap.Popover
10 * @extends Roo.bootstrap.Component
13 * @children Roo.bootstrap.Component
14 * Bootstrap Popover class
15 * @cfg {String} html contents of the popover (or false to use children..)
16 * @cfg {String} title of popover (or false to hide)
17 * @cfg {String|function} (right|top|bottom|left|auto) placement how it is placed
18 * @cfg {String} trigger click || hover (or false to trigger manually)
19 * @cfg {Boolean} modal - popovers that are modal will mask the screen, and must be closed with another event.
20 * @cfg {String|Boolean|Roo.Element} add click hander to trigger show over what element
21 * - if false and it has a 'parent' then it will be automatically added to that element
22 * - if string - Roo.get will be called
23 * @cfg {Number} delay - delay before showing
26 * Create a new Popover
27 * @param {Object} config The config object
30 Roo.bootstrap.Popover = function(config){
31 Roo.bootstrap.Popover.superclass.constructor.call(this, config);
37 * After the popover show
39 * @param {Roo.bootstrap.Popover} this
44 * After the popover hide
46 * @param {Roo.bootstrap.Popover} this
52 Roo.extend(Roo.bootstrap.Popover, Roo.bootstrap.Component, {
58 trigger : 'hover', // hover
64 can_build_overlaid : false,
66 maskEl : false, // the mask element
69 alignEl : false, // when show is called with an element - this get's stored.
71 getChildContainer : function()
73 return this.contentEl;
76 getPopoverHeader : function()
78 this.title = true; // flag not to hide it..
79 this.headerEl.addClass('p-0');
84 getAutoCreate : function(){
87 cls : 'popover roo-dynamic shadow roo-popover' + (this.modal ? '-modal' : ''),
88 style: 'display:block',
94 cls : 'popover-inner ',
98 cls: 'popover-title popover-header',
99 html : this.title === false ? '' : this.title
102 cls : 'popover-content popover-body ' + (this.cls || ''),
103 html : this.html || ''
114 * @param {string} the title
116 setTitle: function(str)
120 this.headerEl.dom.innerHTML = str;
125 * @param {string} the body content
127 setContent: function(str)
130 if (this.contentEl) {
131 this.contentEl.dom.innerHTML = str;
135 // as it get's added to the bottom of the page.
136 onRender : function(ct, position)
138 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
143 var cfg = Roo.apply({}, this.getAutoCreate());
147 cfg.cls += ' ' + this.cls;
150 cfg.style = this.style;
152 //Roo.log("adding to ");
153 this.el = Roo.get(document.body).createChild(cfg, position);
157 this.contentEl = this.el.select('.popover-content',true).first();
158 this.headerEl = this.el.select('.popover-title',true).first();
161 if(typeof(this.items) != 'undefined'){
162 var items = this.items;
165 for(var i =0;i < items.length;i++) {
166 nitems.push(this.addxtype(Roo.apply({}, items[i])));
172 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
173 Roo.EventManager.onWindowResize(this.resizeMask, this, true);
180 resizeMask : function()
183 Roo.lib.Dom.getViewWidth(true),
184 Roo.lib.Dom.getViewHeight(true)
188 initEvents : function()
192 Roo.bootstrap.Popover.register(this);
195 this.arrowEl = this.el.select('.arrow',true).first();
196 this.headerEl.setVisibilityMode(Roo.Element.DISPLAY); // probably not needed as it's default in BS4
197 this.el.enableDisplayMode('block');
201 if (this.over === false && !this.parent()) {
204 if (this.triggers === false) {
209 var on_el = (this.over == 'parent' || this.over === false) ? this.parent().el : Roo.get(this.over);
210 var triggers = this.trigger ? this.trigger.split(' ') : [];
211 Roo.each(triggers, function(trigger) {
213 if (trigger == 'click') {
214 on_el.on('click', this.toggle, this);
215 } else if (trigger != 'manual') {
216 var eventIn = trigger == 'hover' ? 'mouseenter' : 'focusin';
217 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
219 on_el.on(eventIn ,this.enter, this);
220 on_el.on(eventOut, this.leave, this);
230 toggle : function () {
231 this.hoverState == 'in' ? this.leave() : this.enter();
234 enter : function () {
236 clearTimeout(this.timeout);
238 this.hoverState = 'in';
240 if (!this.delay || !this.delay.show) {
245 this.timeout = setTimeout(function () {
246 if (_t.hoverState == 'in') {
253 clearTimeout(this.timeout);
255 this.hoverState = 'out';
257 if (!this.delay || !this.delay.hide) {
262 this.timeout = setTimeout(function () {
263 if (_t.hoverState == 'out') {
270 * update the position of the dialog
271 * normally this is needed if the popover get's bigger - due to a Table reload etc..
280 this.updatePosition(this.placement, true);
283 // this is usually just done by the builder = to show the popoup in the middle of the scren.
284 var es = this.el.getSize();
285 var x = Roo.lib.Dom.getViewWidth()/2;
286 var y = Roo.lib.Dom.getViewHeight()/2;
287 this.el.setXY([ x-(es.width/2), y-(es.height/2)] );
299 * @param {Roo.Element|string|Boolean} - element to align and point to. (set align to [ pos, offset ])
300 * @param {string} (left|right|top|bottom) position
302 show : function (on_el, placement)
304 this.placement = typeof(placement) == 'undefined' ? this.placement : placement;
305 on_el = on_el || false; // default to false
308 if (this.parent() && (this.over == 'parent' || (this.over === false))) {
309 on_el = this.parent().el;
310 } else if (this.over) {
311 on_el = Roo.get(this.over);
316 this.alignEl = Roo.get( on_el );
319 this.render(document.body);
325 if (this.title === false) {
326 this.headerEl.hide();
331 this.el.dom.style.display = 'block';
335 //var arrow = this.el.select('.arrow',true).first();
336 //arrow.set(align[2],
338 this.el.addClass('in');
342 this.hoverState = 'in';
345 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
346 this.maskEl.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
347 this.maskEl.dom.style.display = 'block';
348 this.maskEl.addClass('show');
350 this.el.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
352 this.fireEvent('show', this);
356 * fire this manually after loading a grid in the table for example
357 * @param {string} (left|right|top|bottom) where to try and put it (use false to use the last one)
358 * @param {Boolean} try and move it if we cant get right position.
360 updatePosition : function(placement, try_move)
362 // allow for calling with no parameters
363 placement = placement ? placement : this.placement;
364 try_move = typeof(try_move) == 'undefined' ? true : try_move;
366 this.el.removeClass([
367 'fade','top','bottom', 'left', 'right','in',
368 'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
370 this.el.addClass(placement + ' bs-popover-' + placement);
372 if (!this.alignEl ) {
378 var exact = this.el.getAlignToXY(this.alignEl, 'tl-tr', [10,0]);
379 var offset = this.el.getAlignToXY(this.alignEl, 'tl-tr?',[10,0]);
380 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
381 //normal display... or moved up/down.
382 this.el.setXY(offset);
383 var xy = this.alignEl.getAnchorXY('tr', false);
385 this.arrowEl.setXY(xy);
388 // continue through...
389 return this.updatePosition('left', false);
393 var exact = this.el.getAlignToXY(this.alignEl, 'tr-tl', [-10,0]);
394 var offset = this.el.getAlignToXY(this.alignEl, 'tr-tl?',[-10,0]);
395 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
396 //normal display... or moved up/down.
397 this.el.setXY(offset);
398 var xy = this.alignEl.getAnchorXY('tl', false);
399 xy[0]-=10;xy[1]+=5; // << fix me
400 this.arrowEl.setXY(xy);
404 return this.updatePosition('right', false);
407 var exact = this.el.getAlignToXY(this.alignEl, 'b-t', [0,-10]);
408 var offset = this.el.getAlignToXY(this.alignEl, 'b-t?',[0,-10]);
409 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
410 //normal display... or moved up/down.
411 this.el.setXY(offset);
412 var xy = this.alignEl.getAnchorXY('t', false);
413 xy[1]-=10; // << fix me
414 this.arrowEl.setXY(xy);
418 return this.updatePosition('bottom', false);
421 var exact = this.el.getAlignToXY(this.alignEl, 't-b', [0,10]);
422 var offset = this.el.getAlignToXY(this.alignEl, 't-b?',[0,10]);
423 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
424 //normal display... or moved up/down.
425 this.el.setXY(offset);
426 var xy = this.alignEl.getAnchorXY('b', false);
427 xy[1]+=2; // << fix me
428 this.arrowEl.setXY(xy);
432 return this.updatePosition('top', false);
443 this.el.setXY([0,0]);
444 this.el.removeClass('in');
446 this.hoverState = null;
447 this.maskEl.hide(); // always..
448 this.fireEvent('hide', this);
454 Roo.apply(Roo.bootstrap.Popover, {
457 'left' : ['r-l', [-10,0], 'left bs-popover-left'],
458 'right' : ['l-br', [10,0], 'right bs-popover-right'],
459 'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
460 'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
469 onMouseDown : function(e)
471 if (this.popups.length && !e.getTarget(".roo-popover")) {
472 /// what is nothing is showing..
481 register : function(popup)
483 if (!Roo.bootstrap.Popover.clickHandler) {
484 Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
486 // hide other popups.
487 popup.on('show', Roo.bootstrap.Popover.onShow, popup);
488 popup.on('hide', Roo.bootstrap.Popover.onHide, popup);
489 this.hideAll(); //<< why?
490 //this.popups.push(popup);
494 this.popups.forEach(function(p) {
498 onShow : function() {
499 Roo.bootstrap.Popover.popups.push(this);
501 onHide : function() {
502 Roo.bootstrap.Popover.popups.remove(this);