9 * @class Roo.bootstrap.Tooltip
10 * Bootstrap Tooltip class
11 * This is basic at present - all componets support it by default, however they should add tooltipEl() method
12 * to determine which dom element triggers the tooltip.
14 * It needs to add support for additional attributes like tooltip-position
18 * @param {Object} config The config object
21 Roo.bootstrap.Tooltip = function(config){
22 Roo.bootstrap.Tooltip.superclass.constructor.call(this, config);
24 this.alignment = Roo.bootstrap.Tooltip.alignment;
26 if(typeof(config) != 'undefined' && typeof(config.alignment) != 'undefined'){
27 this.alignment = config.alignment;
32 Roo.apply(Roo.bootstrap.Tooltip, {
34 * @function init initialize tooltip monitoring.
39 currentRegion : false,
45 Roo.get(document).on('mouseover', this.enter ,this);
46 Roo.get(document).on('mouseout', this.leave, this);
49 this.currentTip = new Roo.bootstrap.Tooltip();
54 var dom = ev.getTarget();
56 //Roo.log(['enter',dom]);
57 var el = Roo.fly(dom);
60 //Roo.log(this.currentEl);
61 //Roo.log(this.currentEl.contains(dom));
62 if (this.currentEl == el) {
65 if (dom != this.currentEl.dom && this.currentEl.contains(dom)) {
71 if (this.currentTip.el) {
72 this.currentTip.el.setVisibilityMode(Roo.Element.DISPLAY).hide(); // force hiding...
76 if(!el || el.dom == document){
82 if (!el.attr('tooltip')) {
83 pel = el.findParent("[tooltip]");
85 bindEl = Roo.get(pel);
91 // you can not look for children, as if el is the body.. then everythign is the child..
92 if (!pel && !el.attr('tooltip')) { //
93 if (!el.select("[tooltip]").elements.length) {
96 // is the mouse over this child...?
97 bindEl = el.select("[tooltip]").first();
99 if (!bindEl.getRegion().contains( { top : xy[1] ,right : xy[0] , bottom : xy[1], left : xy[0]})) {
100 //Roo.log("not in region.");
103 //Roo.log("child element over..");
107 this.currentTip.bind(bindEl);
108 this.currentRegion = Roo.lib.Region.getRegion(dom);
109 this.currentTip.enter();
114 var dom = ev.getTarget();
115 //Roo.log(['leave',dom]);
116 if (!this.currentEl) {
121 if (dom != this.currentEl.dom) {
125 if (this.currentRegion.contains( new Roo.lib.Region( xy[1], xy[0] ,xy[1], xy[0] ))) {
128 // only activate leave if mouse cursor is outside... bounding box..
133 if (this.currentTip) {
134 this.currentTip.leave();
136 //Roo.log('clear currentEl');
137 this.currentEl = false;
142 'left' : ['r-l', [-2,0], 'right'],
143 'right' : ['l-r', [2,0], 'left'],
144 'bottom' : ['t-b', [0,2], 'top'],
145 'top' : [ 'b-t', [0,-2], 'bottom']
151 Roo.extend(Roo.bootstrap.Tooltip, Roo.bootstrap.Component, {
156 delay : null, // can be { show : 300 , hide: 500}
160 hoverState : null, //???
162 placement : 'bottom',
166 getAutoCreate : function(){
173 cls : 'tooltip-arrow arrow'
176 cls : 'tooltip-inner'
188 initEvents : function()
190 this.arrowEl = this.el.select('.arrow', true).first();
191 this.innerEl = this.el.select('.tooltip-inner', true).first();
194 enter : function () {
196 if (this.timeout != null) {
197 clearTimeout(this.timeout);
200 this.hoverState = 'in';
201 //Roo.log("enter - show");
202 if (!this.delay || !this.delay.show) {
207 this.timeout = setTimeout(function () {
208 if (_t.hoverState == 'in') {
215 clearTimeout(this.timeout);
217 this.hoverState = 'out';
218 if (!this.delay || !this.delay.hide) {
224 this.timeout = setTimeout(function () {
225 //Roo.log("leave - timeout");
227 if (_t.hoverState == 'out') {
229 Roo.bootstrap.Tooltip.currentEl = false;
234 show : function (msg)
237 this.render(document.body);
240 //Roo.log([this.bindEl, this.bindEl.attr('tooltip')]);
242 var tip = msg || this.bindEl.attr('tooltip') || this.bindEl.select("[tooltip]").first().attr('tooltip');
244 this.el.select('.tooltip-inner',true).first().dom.innerHTML = tip;
246 this.el.removeClass(['fade','top','bottom', 'left', 'right','in',
247 'bs-tooltip-top','bs-tooltip-bottom', 'bs-tooltip-left', 'bs-tooltip-right']);
249 var placement = typeof this.placement == 'function' ?
250 this.placement.call(this, this.el, on_el) :
253 var autoToken = /\s?auto?\s?/i;
254 var autoPlace = autoToken.test(placement);
256 placement = placement.replace(autoToken, '') || 'top';
260 //this.el.setXY([0,0]);
262 //this.el.dom.style.display='block';
264 //this.el.appendTo(on_el);
266 var p = this.getPosition();
267 var box = this.el.getBox();
273 var align = this.alignment[placement];
275 var xy = this.el.getAlignToXY(this.bindEl, align[0], align[1]);
277 if(placement == 'top' || placement == 'bottom'){
282 if(xy[0] + this.el.getWidth() > Roo.lib.Dom.getViewWidth()){
286 var scroll = Roo.select('body', true).first().getScroll();
288 if(xy[1] > Roo.lib.Dom.getViewHeight() + scroll.top - this.el.getHeight()){
292 align = this.alignment[placement];
294 this.arrowEl.setLeft((this.innerEl.getWidth()/2) - 5);
298 var elems = document.getElementsByTagName('div');
299 var highest = Number.MIN_SAFE_INTEGER || -(Math.pow(2, 53) - 1);
300 for (var i = 0; i < elems.length; i++) {
301 var zindex = Number.parseInt(
302 document.defaultView.getComputedStyle(elems[i], null).getPropertyValue("z-index"),
305 if (zindex > highest) {
312 this.el.dom.style.zIndex = highest;
314 this.el.alignTo(this.bindEl, align[0],align[1]);
315 //var arrow = this.el.select('.arrow',true).first();
316 //arrow.set(align[2],
318 this.el.addClass(placement);
319 this.el.addClass("bs-tooltip-"+ placement);
321 this.el.addClass('in fade show');
323 this.hoverState = null;
325 if (this.el.hasClass('fade')) {
340 //this.el.setXY([0,0]);
341 this.el.removeClass(['show', 'in']);