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 * @param {Roo.Element|string|Boolean} - element to align and point to. (set align to [ pos, offset ])
271 * @param {string} (left|right|top|bottom) position
273 show : function (on_el, placement)
275 this.placement = typeof(placement) == 'undefined' ? this.placement : placement;
276 on_el = on_el || false; // default to false
279 if (this.parent() && (this.over == 'parent' || (this.over === false))) {
280 on_el = this.parent().el;
281 } else if (this.over) {
282 on_el = Roo.get(this.over);
287 this.alignEl = Roo.get( on_el );
290 this.render(document.body);
296 if (this.title === false) {
297 this.headerEl.hide();
302 this.el.dom.style.display = 'block';
306 this.updatePosition(this.placement, true);
309 // this is usually just done by the builder = to show the popoup in the middle of the scren.
310 var es = this.el.getSize();
311 var x = Roo.lib.Dom.getViewWidth()/2;
312 var y = Roo.lib.Dom.getViewHeight()/2;
313 this.el.setXY([ x-(es.width/2), y-(es.height/2)] );
318 //var arrow = this.el.select('.arrow',true).first();
319 //arrow.set(align[2],
321 this.el.addClass('in');
325 this.hoverState = 'in';
328 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
329 this.maskEl.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
330 this.maskEl.dom.style.display = 'block';
331 this.maskEl.addClass('show');
333 this.el.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
335 this.fireEvent('show', this);
339 * fire this manually after loading a grid in the table for example
340 * @param {string} (left|right|top|bottom) where to try and put it (use false to use the last one)
341 * @param {Boolean} try and move it if we cant get right position.
343 updatePosition : function(placement, try_move)
345 // allow for calling with no parameters
346 placement = placement ? placement : this.placement;
347 try_move = typeof(try_move) == 'undefined' ? true : try_move;
349 this.el.removeClass([
350 'fade','top','bottom', 'left', 'right','in',
351 'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
353 this.el.addClass(placement + ' bs-popover-' + placement);
355 if (!this.alignEl ) {
361 var exact = this.el.getAlignToXY(this.alignEl, 'tl-tr', [10,0]);
362 var offset = this.el.getAlignToXY(this.alignEl, 'tl-tr?',[10,0]);
363 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
364 //normal display... or moved up/down.
365 this.el.setXY(offset);
366 var xy = this.alignEl.getAnchorXY('tr', false);
368 this.arrowEl.setXY(xy);
371 // continue through...
372 return this.updatePosition('left', false);
376 var exact = this.el.getAlignToXY(this.alignEl, 'tr-tl', [-10,0]);
377 var offset = this.el.getAlignToXY(this.alignEl, 'tr-tl?',[-10,0]);
378 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
379 //normal display... or moved up/down.
380 this.el.setXY(offset);
381 var xy = this.alignEl.getAnchorXY('tl', false);
382 xy[0]-=10;xy[1]+=5; // << fix me
383 this.arrowEl.setXY(xy);
387 return this.updatePosition('right', false);
390 var exact = this.el.getAlignToXY(this.alignEl, 'b-t', [0,-10]);
391 var offset = this.el.getAlignToXY(this.alignEl, 'b-t?',[0,-10]);
392 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
393 //normal display... or moved up/down.
394 this.el.setXY(offset);
395 var xy = this.alignEl.getAnchorXY('t', false);
396 xy[1]-=10; // << fix me
397 this.arrowEl.setXY(xy);
401 return this.updatePosition('bottom', false);
404 var exact = this.el.getAlignToXY(this.alignEl, 't-b', [0,10]);
405 var offset = this.el.getAlignToXY(this.alignEl, 't-b?',[0,10]);
406 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
407 //normal display... or moved up/down.
408 this.el.setXY(offset);
409 var xy = this.alignEl.getAnchorXY('b', false);
410 xy[1]+=2; // << fix me
411 this.arrowEl.setXY(xy);
415 return this.updatePosition('top', false);
426 this.el.setXY([0,0]);
427 this.el.removeClass('in');
429 this.hoverState = null;
430 this.maskEl.hide(); // always..
431 this.fireEvent('hide', this);
437 Roo.apply(Roo.bootstrap.Popover, {
440 'left' : ['r-l', [-10,0], 'left bs-popover-left'],
441 'right' : ['l-br', [10,0], 'right bs-popover-right'],
442 'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
443 'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
452 onMouseDown : function(e)
454 if (this.popups.length && !e.getTarget(".roo-popover")) {
455 /// what is nothing is showing..
464 register : function(popup)
466 if (!Roo.bootstrap.Popover.clickHandler) {
467 Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
469 // hide other popups.
470 popup.on('show', Roo.bootstrap.Popover.onShow, popup);
471 popup.on('hide', Roo.bootstrap.Popover.onHide, popup);
472 this.hideAll(); //<< why?
473 //this.popups.push(popup);
477 this.popups.forEach(function(p) {
481 onShow : function() {
482 Roo.bootstrap.Popover.popups.push(this);
484 onHide : function() {
485 Roo.bootstrap.Popover.popups.remove(this);