9 * @class Roo.bootstrap.Popover
10 * @extends Roo.bootstrap.Component
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 * @param {Roo.Element|string|Boolean} - element to align and point to. (set align to [ pos, offset ])
270 * @param {string} (left|right|top|bottom) position
272 show : function (on_el, placement)
274 this.placement = typeof(placement) == 'undefined' ? this.placement : placement;
275 on_el = on_el || false; // default to false
278 if (this.parent() && (this.over == 'parent' || (this.over === false))) {
279 on_el = this.parent().el;
280 } else if (this.over) {
281 on_el = Roo.get(this.over);
286 this.alignEl = Roo.get( on_el );
289 this.render(document.body);
295 if (this.title === false) {
296 this.headerEl.hide();
301 this.el.dom.style.display = 'block';
305 this.updatePosition(this.placement, true);
308 // this is usually just done by the builder = to show the popoup in the middle of the scren.
309 var es = this.el.getSize();
310 var x = Roo.lib.Dom.getViewWidth()/2;
311 var y = Roo.lib.Dom.getViewHeight()/2;
312 this.el.setXY([ x-(es.width/2), y-(es.height/2)] );
317 //var arrow = this.el.select('.arrow',true).first();
318 //arrow.set(align[2],
320 this.el.addClass('in');
324 this.hoverState = 'in';
327 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
328 this.maskEl.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
329 this.maskEl.dom.style.display = 'block';
330 this.maskEl.addClass('show');
332 this.el.setStyle('z-index', Roo.bootstrap.Popover.zIndex++);
334 this.fireEvent('show', this);
338 * fire this manually after loading a grid in the table for example
339 * @param {string} (left|right|top|bottom) where to try and put it (use false to use the last one)
340 * @param {Boolean} try and move it if we cant get right position.
342 updatePosition : function(placement, try_move)
344 // allow for calling with no parameters
345 placement = placement ? placement : this.placement;
346 try_move = typeof(try_move) == 'undefined' ? true : try_move;
348 this.el.removeClass([
349 'fade','top','bottom', 'left', 'right','in',
350 'bs-popover-top','bs-popover-bottom', 'bs-popover-left', 'bs-popover-right'
352 this.el.addClass(placement + ' bs-popover-' + placement);
354 if (!this.alignEl ) {
360 var exact = this.el.getAlignToXY(this.alignEl, 'tl-tr', [10,0]);
361 var offset = this.el.getAlignToXY(this.alignEl, 'tl-tr?',[10,0]);
362 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
363 //normal display... or moved up/down.
364 this.el.setXY(offset);
365 var xy = this.alignEl.getAnchorXY('tr', false);
367 this.arrowEl.setXY(xy);
370 // continue through...
371 return this.updatePosition('left', false);
375 var exact = this.el.getAlignToXY(this.alignEl, 'tr-tl', [-10,0]);
376 var offset = this.el.getAlignToXY(this.alignEl, 'tr-tl?',[-10,0]);
377 if (!try_move || exact.equals(offset) || exact[0] == offset[0] ) {
378 //normal display... or moved up/down.
379 this.el.setXY(offset);
380 var xy = this.alignEl.getAnchorXY('tl', false);
381 xy[0]-=10;xy[1]+=5; // << fix me
382 this.arrowEl.setXY(xy);
386 return this.updatePosition('right', false);
389 var exact = this.el.getAlignToXY(this.alignEl, 'b-t', [0,-10]);
390 var offset = this.el.getAlignToXY(this.alignEl, 'b-t?',[0,-10]);
391 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
392 //normal display... or moved up/down.
393 this.el.setXY(offset);
394 var xy = this.alignEl.getAnchorXY('t', false);
395 xy[1]-=10; // << fix me
396 this.arrowEl.setXY(xy);
400 return this.updatePosition('bottom', false);
403 var exact = this.el.getAlignToXY(this.alignEl, 't-b', [0,10]);
404 var offset = this.el.getAlignToXY(this.alignEl, 't-b?',[0,10]);
405 if (!try_move || exact.equals(offset) || exact[1] == offset[1] ) {
406 //normal display... or moved up/down.
407 this.el.setXY(offset);
408 var xy = this.alignEl.getAnchorXY('b', false);
409 xy[1]+=2; // << fix me
410 this.arrowEl.setXY(xy);
414 return this.updatePosition('top', false);
425 this.el.setXY([0,0]);
426 this.el.removeClass('in');
428 this.hoverState = null;
429 this.maskEl.hide(); // always..
430 this.fireEvent('hide', this);
436 Roo.apply(Roo.bootstrap.Popover, {
439 'left' : ['r-l', [-10,0], 'left bs-popover-left'],
440 'right' : ['l-br', [10,0], 'right bs-popover-right'],
441 'bottom' : ['t-b', [0,10], 'top bs-popover-top'],
442 'top' : [ 'b-t', [0,-10], 'bottom bs-popover-bottom']
451 onMouseDown : function(e)
453 if (this.popups.length && !e.getTarget(".roo-popover")) {
454 /// what is nothing is showing..
463 register : function(popup)
465 if (!Roo.bootstrap.Popover.clickHandler) {
466 Roo.bootstrap.Popover.clickHandler = Roo.get(document).on("mousedown", Roo.bootstrap.Popover.onMouseDown, Roo.bootstrap.Popover);
468 // hide other popups.
469 popup.on('show', Roo.bootstrap.Popover.onShow, popup);
470 popup.on('hide', Roo.bootstrap.Popover.onHide, popup);
471 this.hideAll(); //<< why?
472 //this.popups.push(popup);
476 this.popups.forEach(function(p) {
480 onShow : function() {
481 Roo.bootstrap.Popover.popups.push(this);
483 onHide : function() {
484 Roo.bootstrap.Popover.popups.remove(this);