Roo.bootstrap.Tooltip = function(config){
Roo.bootstrap.Tooltip.superclass.constructor.call(this, config);
this.alignment = Roo.bootstrap.Tooltip.alignment;
if(typeof(config) != 'undefined' && typeof(config.alignment) != 'undefined'){
this.alignment = config.alignment;
}
};
Roo.apply(Roo.bootstrap.Tooltip, {
currentEl : false,
currentTip : false,
currentRegion : false,
init : function()
{
Roo.get(document).on('mouseover', this.enter ,this);
Roo.get(document).on('mouseout', this.leave, this);
this.currentTip = new Roo.bootstrap.Tooltip();
},
enter : function(ev)
{
var dom = ev.getTarget();
var el = Roo.fly(dom);
if (this.currentEl) {
if (this.currentEl == el) {
return;
}
if (dom != this.currentEl.dom && this.currentEl.contains(dom)) {
return;
}
}
if (this.currentTip.el) {
this.currentTip.el.setVisibilityMode(Roo.Element.DISPLAY).hide(); }
if(!el || el.dom == document){
return;
}
var bindEl = el;
var pel = false;
if (!el.attr('tooltip')) {
pel = el.findParent("[tooltip]");
if (pel) {
bindEl = Roo.get(pel);
}
}
if (!pel && !el.attr('tooltip')) { if (!el.select("[tooltip]").elements.length) {
return;
}
bindEl = el.select("[tooltip]").first();
var xy = ev.getXY();
if (!bindEl.getRegion().contains( { top : xy[1] ,right : xy[0] , bottom : xy[1], left : xy[0]})) {
return;
}
}
this.currentEl = el;
this.currentTip.bind(bindEl);
this.currentRegion = Roo.lib.Region.getRegion(dom);
this.currentTip.enter();
},
leave : function(ev)
{
var dom = ev.getTarget();
if (!this.currentEl) {
return;
}
if (dom != this.currentEl.dom) {
return;
}
var xy = ev.getXY();
if (this.currentRegion.contains( new Roo.lib.Region( xy[1], xy[0] ,xy[1], xy[0] ))) {
return;
}
if (this.currentTip) {
this.currentTip.leave();
}
this.currentEl = false;
},
alignment : {
'left' : ['r-l', [-2,0], 'right'],
'right' : ['l-r', [2,0], 'left'],
'bottom' : ['t-b', [0,2], 'top'],
'top' : [ 'b-t', [0,-2], 'bottom']
}
});
Roo.extend(Roo.bootstrap.Tooltip, Roo.bootstrap.Component, {
bindEl : false,
delay : null, timeout : null,
hoverState : null, placement : 'bottom',
alignment : false,
getAutoCreate : function(){
var cfg = {
cls : 'tooltip',
role : 'tooltip',
cn : [
{
cls : 'tooltip-arrow arrow'
},
{
cls : 'tooltip-inner'
}
]
};
return cfg;
},
bind : function(el)
{
this.bindEl = el;
},
initEvents : function()
{
this.arrowEl = this.el.select('.arrow', true).first();
this.innerEl = this.el.select('.tooltip-inner', true).first();
},
enter : function () {
if (this.timeout != null) {
clearTimeout(this.timeout);
}
this.hoverState = 'in';
if (!this.delay || !this.delay.show) {
this.show();
return;
}
var _t = this;
this.timeout = setTimeout(function () {
if (_t.hoverState == 'in') {
_t.show();
}
}, this.delay.show);
},
leave : function()
{
clearTimeout(this.timeout);
this.hoverState = 'out';
if (!this.delay || !this.delay.hide) {
this.hide();
return;
}
var _t = this;
this.timeout = setTimeout(function () {
if (_t.hoverState == 'out') {
_t.hide();
Roo.bootstrap.Tooltip.currentEl = false;
}
}, delay);
},
show : function (msg)
{
if (!this.el) {
this.render(document.body);
}
var tip = msg || this.bindEl.attr('tooltip') || this.bindEl.select("[tooltip]").first().attr('tooltip');
this.el.select('.tooltip-inner',true).first().dom.innerHTML = tip;
this.el.removeClass(['fade','top','bottom', 'left', 'right','in',
'bs-tooltip-top','bs-tooltip-bottom', 'bs-tooltip-left', 'bs-tooltip-right']);
var placement = typeof this.placement == 'function' ?
this.placement.call(this, this.el, on_el) :
this.placement;
var autoToken = /\s?auto?\s?/i;
var autoPlace = autoToken.test(placement);
if (autoPlace) {
placement = placement.replace(autoToken, '') || 'top';
}
this.el.show();
var p = this.getPosition();
var box = this.el.getBox();
if (autoPlace) {
}
var align = this.alignment[placement];
var xy = this.el.getAlignToXY(this.bindEl, align[0], align[1]);
if(placement == 'top' || placement == 'bottom'){
if(xy[0] < 0){
placement = 'right';
}
if(xy[0] + this.el.getWidth() > Roo.lib.Dom.getViewWidth()){
placement = 'left';
}
var scroll = Roo.select('body', true).first().getScroll();
if(xy[1] > Roo.lib.Dom.getViewHeight() + scroll.top - this.el.getHeight()){
placement = 'top';
}
align = this.alignment[placement];
this.arrowEl.setLeft((this.innerEl.getWidth()/2) - 5);
}
var elems = document.getElementsByTagName('div');
var highest = Number.MIN_SAFE_INTEGER || -(Math.pow(2, 53) - 1);
for (var i = 0; i < elems.length; i++) {
var zindex = Number.parseInt(
document.defaultView.getComputedStyle(elems[i], null).getPropertyValue("z-index"),
10
);
if (zindex > highest) {
highest = zindex;
}
}
this.el.dom.style.zIndex = highest;
this.el.alignTo(this.bindEl, align[0],align[1]);
this.el.addClass(placement);
this.el.addClass("bs-tooltip-"+ placement);
this.el.addClass('in fade show');
this.hoverState = null;
if (this.el.hasClass('fade')) {
}
},
hide : function()
{
if (!this.el) {
return;
}
this.el.removeClass(['show', 'in']);
}
});