9 * @class Roo.bootstrap.Popover
10 * @extends Roo.bootstrap.Component
11 * @parent none builder
12 * @children Roo.bootstrap.Component
13 * Bootstrap Popover class
14 * @cfg {String} html contents of the popover (or false to use children..)
15 * @cfg {String} title of popover (or false to hide)
16 * @cfg {String|function} (right|top|bottom|left|auto) placement how it is placed
17 * @cfg {String} trigger click || hover (or false to trigger manually)
18 * @cfg {Boolean} modal - popovers that are modal will mask the screen, and must be closed with another event.
19 * @cfg {String|Boolean|Roo.Element} add click hander to trigger show over what element
20 * - if false and it has a 'parent' then it will be automatically added to that element
21 * - if string - Roo.get will be called
22 * @cfg {Number} delay - delay before showing
25 * Create a new Popover
26 * @param {Object} config The config object
29 Roo.bootstrap.Popover = function(config){
30 Roo.bootstrap.Popover.superclass.constructor.call(this, config);
36 * After the popover show
38 * @param {Roo.bootstrap.Popover} this
43 * After the popover hide
45 * @param {Roo.bootstrap.Popover} this
51 Roo.extend(Roo.bootstrap.Popover, Roo.bootstrap.Component, {
57 trigger : 'hover', // hover
63 can_build_overlaid : false,
65 maskEl : false, // the mask element
68 alignEl : false, // when show is called with an element - this get's stored.
70 getChildContainer : function()
72 return this.contentEl;
75 getPopoverHeader : function()
77 this.title = true; // flag not to hide it..
78 this.headerEl.addClass('p-0');
83 getAutoCreate : function(){
86 cls : 'popover roo-dynamic shadow roo-popover' + (this.modal ? '-modal' : ''),
87 style: 'display:block',
93 cls : 'popover-inner ',
97 cls: 'popover-title popover-header',
98 html : this.title === false ? '' : this.title
101 cls : 'popover-content popover-body ' + (this.cls || ''),
102 html : this.html || ''
113 * @param {string} the title
115 setTitle: function(str)
119 this.headerEl.dom.innerHTML = str;
124 * @param {string} the body content
126 setContent: function(str)
129 if (this.contentEl) {
130 this.contentEl.dom.innerHTML = str;
134 // as it get's added to the bottom of the page.
135 onRender : function(ct, position)
137 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
142 var cfg = Roo.apply({}, this.getAutoCreate());
146 cfg.cls += ' ' + this.cls;
149 cfg.style = this.style;
151 //Roo.log("adding to ");
152 this.el = Roo.get(document.body).createChild(cfg, position);
156 this.contentEl = this.el.select('.popover-content',true).first();
157 this.headerEl = this.el.select('.popover-title',true).first();
160 if(typeof(this.items) != 'undefined'){
161 var items = this.items;
164 for(var i =0;i < items.length;i++) {
165 nitems.push(this.addxtype(Roo.apply({}, items[i])));
171 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
172 Roo.EventManager.onWindowResize(this.resizeMask, this, true);
179 resizeMask : function()
182 Roo.lib.Dom.getViewWidth(true),
183 Roo.lib.Dom.getViewHeight(true)
187 initEvents : function()
191 Roo.bootstrap.Popover.register(this);
194 this.arrowEl = this.el.select('.arrow',true).first();
195 this.headerEl.setVisibilityMode(Roo.Element.DISPLAY); // probably not needed as it's default in BS4
196 this.el.enableDisplayMode('block');
200 if (this.over === false && !this.parent()) {
203 if (this.triggers === false) {
208 var on_el = (this.over == 'parent' || this.over === false) ? this.parent().el : Roo.get(this.over);
209 var triggers = this.trigger ? this.trigger.split(' ') : [];
210 Roo.each(triggers, function(trigger) {
212 if (trigger == 'click') {
213 on_el.on('click', this.toggle, this);
214 } else if (trigger != 'manual') {
215 var eventIn = trigger == 'hover' ? 'mouseenter' : 'focusin';
216 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
218 on_el.on(eventIn ,this.enter, this);
219 on_el.on(eventOut, this.leave, this);
229 toggle : function () {
230 this.hoverState == 'in' ? this.leave() : this.enter();
233 enter : function () {
235 clearTimeout(this.timeout);
237 this.hoverState = 'in';
239 if (!this.delay || !this.delay.show) {
244 this.timeout = setTimeout(function () {
245 if (_t.hoverState == 'in') {
252 clearTimeout(this.timeout);
254 this.hoverState = 'out';
256 if (!this.delay || !this.delay.hide) {
261 this.timeout = setTimeout(function () {
262 if (_t.hoverState == 'out') {
269 * update the position of the dialog
270 * normally this is needed if the popover get's bigger - due to a Table reload etc..
279 this.updatePosition(this.placement, true);
282 // this is usually just done by the builder = to show the popoup in the middle of the scren.
283 var es = this.el.getSize();
284 var x = Roo.lib.Dom.getViewWidth()/2;
285 var y = Roo.lib.Dom.getViewHeight()/2;
286 this.el.setXY([ x-(es.width/2), y-(es.height/2)] );
298 * @param {Roo.Element|string|Boolean} - element to align and point to. (set align to [ pos, offset ])
299 * @param {string} (left|right|top|bottom) position
301 show : function (on_el, placement)
303 this.placement = typeof(placement) == 'undefined' ? this.placement : placement;
304 on_el = on_el || false; // default to false
307 if (this.parent() && (this.over == 'parent' || (this.over === false))) {
308 on_el = this.parent().el;
309 } else if (this.over) {
310 on_el = Roo.get(this.over);
315 this.alignEl = Roo.get( on_el );
318 this.render(document.body);
324 if (this.title === false) {
325 this.headerEl.hide();
330 this.el.dom.style.display = 'block';
334 //var arrow = this.el.select('.arrow',true).first();
335 //arrow.set(align[2],
337 this.el.addClass('in');
341 this.hoverState = 'in';
344 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
345 this.maskEl.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
346 this.maskEl.dom.style.display = 'block';
347 this.maskEl.addClass('show');
349 this.el.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
351 this.fireEvent('show', this);
355 * fire this manually after loading a grid in the table for example
356 * @param {string} (left|right|top|bottom) where to try and put it (use false to use the last one)
357 * @param {Boolean} try and move it if we cant get right position.
359 updatePosition : function(placement, try_move)
361 // allow for calling with no parameters
362 placement = placement ? placement : this.placement;
363 try_move = typeof(try_move) == 'undefined' ? true : try_move;
365 this.el.removeClass([
366 'fade','top','bottom', 'left', 'right','in',
367 'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
369 this.el.addClass(placement + ' bs-popover-' + placement);
371 if (!this.alignEl ) {
377 var exact = this.el.getAlignToXY(this.alignEl, 'tl-tr', [10,0]);
378 var offset = this.el.getAlignToXY(this.alignEl, 'tl-tr?',[10,0]);
379 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
380 //normal display... or moved up/down.
381 this.el.setXY(offset);
382 var xy = this.alignEl.getAnchorXY('tr', false);
384 this.arrowEl.setXY(xy);
387 // continue through...
388 return this.updatePosition('left', false);
392 var exact = this.el.getAlignToXY(this.alignEl, 'tr-tl', [-10,0]);
393 var offset = this.el.getAlignToXY(this.alignEl, 'tr-tl?',[-10,0]);
394 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
395 //normal display... or moved up/down.
396 this.el.setXY(offset);
397 var xy = this.alignEl.getAnchorXY('tl', false);
398 xy[0]-=10;xy[1]+=5; // << fix me
399 this.arrowEl.setXY(xy);
403 return this.updatePosition('right', false);
406 var exact = this.el.getAlignToXY(this.alignEl, 'b-t', [0,-10]);
407 var offset = this.el.getAlignToXY(this.alignEl, 'b-t?',[0,-10]);
408 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
409 //normal display... or moved up/down.
410 this.el.setXY(offset);
411 var xy = this.alignEl.getAnchorXY('t', false);
412 xy[1]-=10; // << fix me
413 this.arrowEl.setXY(xy);
417 return this.updatePosition('bottom', false);
420 var exact = this.el.getAlignToXY(this.alignEl, 't-b', [0,10]);
421 var offset = this.el.getAlignToXY(this.alignEl, 't-b?',[0,10]);
422 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
423 //normal display... or moved up/down.
424 this.el.setXY(offset);
425 var xy = this.alignEl.getAnchorXY('b', false);
426 xy[1]+=2; // << fix me
427 this.arrowEl.setXY(xy);
431 return this.updatePosition('top', false);
442 this.el.setXY([0,0]);
443 this.el.removeClass('in');
445 this.hoverState = null;
446 this.maskEl.hide(); // always..
447 this.fireEvent('hide', this);
453 Roo.apply(Roo.bootstrap.Popover, {
456 'left' : ['r-l', [-10,0], 'left bs-popover-left'],
457 'right' : ['l-br', [10,0], 'right bs-popover-right'],
458 'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
459 'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
468 onMouseDown : function(e)
470 if (this.popups.length && !e.getTarget(".roo-popover")) {
471 /// what is nothing is showing..
480 register : function(popup)
482 if (!Roo.bootstrap.Popover.clickHandler) {
483 Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
485 // hide other popups.
486 popup.on('show', Roo.bootstrap.Popover.onShow, popup);
487 popup.on('hide', Roo.bootstrap.Popover.onHide, popup);
488 this.hideAll(); //<< why?
489 //this.popups.push(popup);
493 this.popups.forEach(function(p) {
497 onShow : function() {
498 Roo.bootstrap.Popover.popups.push(this);
500 onHide : function() {
501 Roo.bootstrap.Popover.popups.remove(this);