MediaWiki:Tooltips.js

var is_IE=!!window.ActiveXObject,EvtHandler={listen_to:function(t,e,i,o){o=EvtHandler.make_listener(t,o);EvtHandler.attach(e,i,o)},attach:function(t,e,i){t.attachEvent?t.attachEvent("on"+e,i):t.addEventListener?t.addEventListener(e,i,!1):t["on"+e]=i},remove:function(t,e,i){t.detachEvent?t.detachEvent("on"+e,i):t.removeEventListener?t.removeEventListener(e,i,!1):t["on"+e]=null},make_listener:function(t,e){var i=t,o=e;return function(t){return o.apply(i,[t||window.event])}},mouse_offset:function{if(is_IE){var t=document.documentElement;if(t){if("function"!=typeof t.getBoundingClientRect)return{x:t.clientLeft,y:t.clientTop};t=t.getBoundingClientRect;return{x:t.left,y:t.top}}}return null},killEvt:function(t){return"function"==typeof t.preventDefault?(t.stopPropagation,t.preventDefault):void 0!==t.cancelBubble&&(t.cancelBubble=!0),!1}},Buttons={buttonClasses:{},createCSS:function(t,e,i){var o=t[0].getAttribute("width"),s=t[0].getAttribute("height");try{var n="a"+e+i,p="border:0; text-decoration:none; background-color:transparent; width:"+o+"px; height:"+s+"px; display:inline-block; background-position:left; background-repeat:no-repeat; background-image:url("+t[0].src+");",l=n+" {"+p+"}\n";1<t.length&&t[1]&&(l=l+("a"+e+i+":hover")+" {"+("background-image:url("+t[1].src+");")+"}\n"),2<t.length&&t[2]&&(l=l+("a"+e+i+":active")+" {"+("background-image:url("+t[2].src+");")+"}\n");var h=document.createElement("style");h.setAttribute("type","text/css");try{h.appendChild(document.createTextNode(l)),document.getElementsByTagName("head")[0].appendChild(h)}catch(t){document.getElementsByTagName("head")[0].appendChild(h),h.styleSheet.cssText=l}}catch(t){return null}return"."==e&&(Buttons.buttonClasses[i]=t[0]),i},createClass:function(t,e){return Buttons.createCSS(t,".",e)},makeButton:function(t,e,i,o){var s=!1,n=null,p=null;if("string"==typeof t?(n=t,s=null!=(p=Buttons.buttonClasses[t])):(s=null!=Buttons.createCSS(t,"#",e),p=t[0]),s){s=document.createElement("a");return s.setAttribute("title",o||p.getAttribute("alt")||p.getAttribute("title")||""),s.id=e,n&&(s.className=n),"string"==typeof i?s.href=i:(s.href="#",s.onclick=function(t){var e=t||window.event;try{i(e)}catch(t){}return EvtHandler.killEvt(e)}),(p=p.cloneNode(!0)).style.visibility="hidden",s.appendChild(p),s}return null}},Tooltips={debug:!1,top_tip:null,nof_tips:0,new_id:function{return Tooltips.nof_tips++,"tooltip_"+Tooltips.nof_tips},register:function(t){Tooltips.top_tip&&Tooltips.top_tip!=t&&Tooltips.top_tip.hide_now,Tooltips.top_tip=t},deregister:function(t){Tooltips.top_tip==t&&(Tooltips.top_tip=null)},close:function{Tooltips.top_tip&&(Tooltips.top_tip.hide_now,Tooltips.top_tip=null)}},Tooltip=function{this.initialize.apply(this,arguments)};Tooltip.MOUSE=0,Tooltip.TRACK=1,Tooltip.FIXED=2,Tooltip.TOP_LEFT=1,Tooltip.TOP_RIGHT=2,Tooltip.BOTTOM_LEFT=3,Tooltip.BOTTOM_RIGHT=4,Tooltip.NONE=-1,Tooltip.HOVER=1,Tooltip.FOCUS=2,Tooltip.CLICK=4,Tooltip.ALL_ACTIVATIONS=7,Tooltip.MOUSE_LEAVE=1,Tooltip.LOSE_FOCUS=2,Tooltip.CLICK_ELEM=4,Tooltip.CLICK_TIP=8,Tooltip.ESCAPE=16,Tooltip.ALL_DEACTIVATIONS=31,Tooltip.LEAVE=Tooltip.MOUSE_LEAVE|Tooltip.LOSE_FOCUS,Tooltip.mouse_in=is_IE?"mouseenter":"mouseover",Tooltip.mouse_out=is_IE?"mouseleave":"mouseout",Tooltip.prototype={initialize:function(t,e,i,o){if(t&&e){this.tip_id=Tooltips.new_id,this.event_state=-1,this.tracks=!1,"function"==typeof e?(this.tip_creator=e,this.css=o,this.content=null):(this.tip_creator=null,this.css=null,e.parentNode&&(e=e.ownerDocument!=document?document.importNode(e,!0):e.cloneNode(!0)),e.id=this.tip_id,this.content=e);e=document.createElement("div");if(e.className="tooltipContent",this.content&&e.appendChild(this.content),this.popup=document.createElement("div"),this.popup.style.display="none",this.popup.style.position="absolute",this.popup.style.top="0px",this.popup.style.left="0px",this.popup.appendChild(e),this.options={mode:Tooltip.TRACK,activate:Tooltip.HOVER,deactivate:Tooltip.LEAVE|Tooltip.CLICK_ELEM|Tooltip.ESCAPE,mouse_offset:{x:5,y:5,dx:1,dy:1},fixed_offset:{x:10,y:5,dx:1,dy:1},anchor:Tooltip.BOTTOM_LEFT,target:null,max_width:.6,max_pixels:0,z_index:1e3,open_delay:500,hide_delay:1e3,close_button:null,onclose:null,onopen:null},i)for(var s in i)if("mouse_offset"==s||"fixed_offset"==s)try{for(var n in i[s])this.options[s][n]=i[s][n]}catch(t){}else this.options[s]=i[s];this.eventShow=EvtHandler.make_listener(this,this.show),this.eventToggle=EvtHandler.make_listener(this,this.toggle),this.eventFocus=EvtHandler.make_listener(this,this.show_focus),this.eventClick=EvtHandler.make_listener(this,this.show_click),this.eventHide=EvtHandler.make_listener(this,this.hide),this.eventTrack=EvtHandler.make_listener(this,this.track),this.eventClose=EvtHandler.make_listener(this,this.hide_now),this.eventKey=EvtHandler.make_listener(this,this.key_handler),this.close_button=null,this.close_button_width=0,this.options.close_button&&(this.makeCloseButton,this.close_button&&(this.options.deactivate=this.options.deactivate&~Tooltip.CLICK_TIP,this.options.deactivate=this.options.deactivate|Tooltip.ESCAPE,this.options.mode==Tooltip.TRACK&&(this.options.mode=Tooltip.MOUSE),this.has_links=!0)),this.options.activate==Tooltip.NONE?this.options.activate=0:0==(this.options.activate&Tooltip.ALL_ACTIVATIONS)&&("a"==t.nodeName.toLowerCase?this.options.activate=Tooltip.CLICK:this.options.activate=Tooltip.HOVER),0!=(this.options.deactivate&Tooltip.ALL_DEACTIVATIONS)||this.close_button||(this.options.deactivate=Tooltip.LEAVE|Tooltip.CLICK_ELEM|Tooltip.ESCAPE),document.body.appendChild(this.popup),this.content&&this.apply_styles(this.content,o),this.content&&this.options.mode==Tooltip.TRACK&&(this.setHasLinks,this.has_links&&(this.options.mode=Tooltip.MOUSE)),this.popup.style.zIndex=""+this.options.z_index,this.target=t,this.open_timeout_id=null,this.hide_timeout_id=null,this.size={width:0,height:0},this.setupEvents(EvtHandler.attach,0),this.ieFix=null,is_IE&&(this.ieFix=document.createElement("iframe"),this.ieFix.style.position="absolute",this.ieFix.style.border="0",this.ieFix.style.margin="0",this.ieFix.style.padding="0",this.ieFix.style.zIndex=""+(this.options.z_index-1),this.ieFix.tabIndex=-1,this.ieFix.frameBorder="0",this.ieFix.style.display="none",document.body.appendChild(this.ieFix),this.ieFix.style.filter="alpha(Opacity=0)")}},apply_styles:function(t,e){if(e)for(var i in e)t.style[i]=e[i];this.close_button&&(t.style.opacity="1.0"),"none"==t.style.display&&(t.style.display="")},setHasLinks:function{if(this.close_button)this.has_links=!0;else{var t=this.content.getElementsByTagName("a");this.has_links=!1;for(var e=0;e<t.length;e++){var i=t[e].getAttribute("href");if(i&&0<i.length)return void(this.has_links=!0)}this.has_links=function(t,e){if(e)for(var i=0;i<e.length;i++){var o=t.getElementsByTagName(e[i]);if(o&&0<o.length)return!0}return!1}(this.content,["form","textarea","input","button","select"])}},setupEvents:function(t,e){(e<0||0==e&&this.event_state<e)&&(this.options.activate&Tooltip.HOVER&&t(this.target,Tooltip.mouse_in,this.eventShow),this.options.activate&Tooltip.FOCUS&&t(this.target,"focus",this.eventFocus),this.options.activate&Tooltip.CLICK&&this.options.deactivate&Tooltip.CLICK_ELEM?t(this.target,"click",this.eventToggle):(this.options.activate&Tooltip.CLICK&&t(this.target,"click",this.eventClick),this.options.deactivate&Tooltip.CLICK_ELEM&&t(this.target,"click",this.eventClose)),this.event_state=e),(e<0||1==e&&this.event_state<e)&&(this.options.deactivate&Tooltip.MOUSE_LEAVE&&(t(this.target,Tooltip.mouse_out,this.eventHide),t(this.popup,Tooltip.mouse_out,this.eventHide),this.options.target&&t(this.options.target,Tooltip.mouse_out,this.eventHide)),this.options.deactivate&Tooltip.LOSE_FOCUS&&t(this.target,"blur",this.eventHide),this.options.deactivate&Tooltip.CLICK_TIP&&this.options.mode!=Tooltip.TRACK&&t(this.popup,"click",this.eventClose),0<this.hide_delay&&(this.options.activate&Tooltip.HOVER||t(this.popup,Tooltip.mouse_in,this.eventShow),t(this.popup,"mousemove",this.eventShow)),this.event_state=e),e<0&&this.tracks&&t(this.target,"mousemove",this.eventTrack)},remove:function{this.hide_now,this.setupEvents(EvtHandler.remove,-1),this.tip_creator=null,document.body.removeElement(this.popup),this.ieFix&&document.body.removeElement(this.ieFix)},show:function(t){this.show_tip(t,!0)},show_focus:function(t){this.show_tip(t,!1)},show_click:function(t){return this.show_tip(t,!1),"a"==this.target.nodeName.toLowerCase&&EvtHandler.killEvt(t)},toggle:function(t){return"none"!=this.popup.style.display&&null!=this.popup.style.display?this.hide_now(t):this.show_tip(t,!0),"a"==this.target.nodeName.toLowerCase&&EvtHandler.killEvt(t)},show_tip:function(t,e){if(null!=this.hide_timeout_id&&window.clearTimeout(this.hide_timeout_id),this.hide_timeout_id=null,"none"==this.popup.style.display||null==this.popup.style.display){if(this.tip_creator){try{this.content=this.tip_creator(t)}catch(t){var i=document.createElement("div");i.appendChild(document.createElement("b").appendChild(document.createTextNode("Exception: "+t.name))),i.appendChild(document.createElement("br")),i.appendChild(document.createTextNode(t.message)),void 0!==t.fileName&&void 0!==t.lineNumber&&(i.appendChild(document.createElement("br")),i.appendChild(document.createTextNode("File "+t.fileName)),i.appendChild(document.createElement("br")),i.appendChild(document.createTextNode("Line "+t.lineNumber))),this.content=i}this.popup.firstChild.lastChild&&this.popup.firstChild.lastChild!=this.close_button&&this.popup.firstChild.removeChild(this.popup.firstChild.lastChild),this.popup.firstChild.appendChild(this.content),this.apply_styles(this.content,this.css),this.options.mode==Tooltip.TRACK&&this.setHasLinks}var o;this.position_tip(t,e),Tooltips.debug&&alert("Position: x = "+this.popup.style.left+" y = "+this.popup.style.top),this.setupEvents(EvtHandler.attach,1),this.options.mode==Tooltip.TRACK&&(this.has_links?(this.tracks&&EvtHandler.remove(this.target,"mousemove",this.eventTrack),this.tracks=!1):(this.tracks||EvtHandler.attach(this.target,"mousemove",this.eventTrack),this.tracks=!0)),0<this.options.open_delay?(o=this).open_timout_id=window.setTimeout(function{o.show_now(o)},this.options.open_delay):this.show_now(this)}},show_now:function(t){"none"!=t.popup.style.display&&null!=t.popup.style.display||(Tooltips.register(t),t.ieFix&&(t.ieFix.style.top=t.popup.style.top,t.ieFix.style.left=t.popup.style.left,t.ieFix.style.width=t.size.width+"px",t.ieFix.style.height=t.size.height+"px",t.ieFix.style.display=""),t.popup.style.display="",t.options.deactivate&Tooltip.ESCAPE&&"function"==typeof t.popup.focus&&EvtHandler.attach(document,"keydown",t.eventKey),t.open_timeout_id=null,"function"==typeof t.options.onopen&&t.options.onopen(t))},track:function(t){this.position_tip(t,!0),this.ieFix&&(this.ieFix.style.top=this.popup.style.top,this.ieFix.style.left=this.popup.style.left,this.ieFix.style.width=this.size.width+"px",this.ieFix.style.height=this.size.height+"px")},size_change:function{"none"!=this.popup.style.display&&null!=this.popup.style.display&&(this.size={width:this.popup.offsetWidth,height:this.popup.offsetHeight},this.ieFix&&(this.ieFix.style.top=this.popup.style.top,this.ieFix.style.left=this.popup.style.left,this.ieFix.style.width=this.size.width+"px",this.ieFix.style.height=this.size.height+"px"))},position_tip:function(t,e){var i=this.viewport("Width"),o=this.viewport("Height"),s={left:this.scroll_offset("Left"),top:this.scroll_offset("Top")},n=0,p=0,l=null;if(e&&this.options.mode!=Tooltip.FIXED){e=EvtHandler.mouse_offset;Tooltips.debug&&e&&alert("Mouse offsets: x = "+e.x+", y = "+e.y),n=t.pageX||t.clientX+s.left-(e?e.x:0),p=t.pageY||t.clientY+s.top-(e?e.y:0),l="mouse_offset"}else{var h=this.options.target||this.target,d=this.position(h);switch(this.options.anchor){default:case Tooltip.BOTTOM_LEFT:n=d.x,p=d.y+h.offsetHeight;break;case Tooltip.BOTTOM_RIGHT:n=d.x+h.offsetWidth,p=d.y+h.offsetHeight;break;case Tooltip.TOP_LEFT:n=d.x,p=d.y;break;case Tooltip.TOP_RIGHT:n=d.x+h.offsetWidth,p=d.y}l="fixed_offset"}n+=this.options[l].x*this.options[l].dx,p+=this.options[l].y*this.options[l].dy,this.size=this.calculate_dimension,this.options[l].dx<0&&(n-=this.size.width),this.options[l].dy<0&&(p-=this.size.height),n+this.size.width>s.left+i&&(n=s.left+i-this.size.width),ns.top+o&&(p=s.top+o-this.size.height),p<s.top&&(p=s.top),this.popup.style.top=p+"px",this.popup.style.left=n+"px"},hide:function(t){var e,i,o,s,n,p,l;"none"!=this.popup.style.display&&(e=EvtHandler.mouse_offset,i=t.pageX||t.clientX+this.scroll_offset("Left")-(e?e.x:0),o=t.pageY||t.clientY+this.scroll_offset("Top")-(e?e.y:0),Tooltips.debug&&(s=this.position(this.target),n=this.position(this.popup),alert("x = "+i+" y = "+o+"\nt: "+s.x+"/"+s.y+"/"+this.target.offsetWidth+"/"+this.target.offsetHeight+"\n"+(s.n?"t.m = "+s.n.nodeName+"/"+s.n.getAttribute("margin")+"/"+s.n.getAttribute("marginTop")+"/"+s.n.getAttribute("border")+"\n":"")+"p: "+n.x+"/"+n.y+"/"+this.popup.offsetWidth+"/"+this.popup.offsetHeight+"\n"+(n.n?"p.m = "+n.n.nodeName+"/"+n.n.getAttribute("margin")+"/"+n.n.getAttribute("marginTop")+"/"+n.n.getAttribute("border")+"\n":"")+"e: "+t.pageX+"/"+t.pageY+" "+t.clientX+"/"+this.scroll_offset("Left")+" "+t.clientY+"/"+this.scroll_offset("Top")+"\n"+(e?"m : "+e.x+"/"+e.y+"\n":""))),this.within(this.target,i,o)||this.within(this.popup,i,o)||this.options.target&&this.within(this.options.target,i,o)||(null!=this.open_timeout_id&&window.clearTimeout(this.open_timeout_id),this.open_timeout_id=null,p=t,0<this.options.hide_delay?(l=this).hide_timeout_id=window.setTimeout(function{l.hide_popup(l,p)},this.options.hide_delay):this.hide_popup(this,p)))},hide_popup:function(t,e){"none"!=t.popup.style.display&&(t.popup.style.display="none",t.ieFix&&(t.ieFix.style.display="none"),t.hide_timeout_id=null,Tooltips.deregister(t),t.options.deactivate&Tooltip.ESCAPE&&EvtHandler.remove(document,"keydown",t.eventKey),"function"==typeof t.options.onclose&&t.options.onclose(t,e))},hide_now:function(t){null!=this.open_timeout_id&&window.clearTimeout(this.open_timeout_id),this.open_timeout_id=null;var e=t||null;return this.hide_popup(this,e),!(!t||"a"!=this.target.nodeName.toLowerCase)&&EvtHandler.killEvt(t)},key_handler:function(t){return Tooltips.debug&&alert("key evt "+t.keyCode),(t.DOM_VK_ESCAPE&&t.keyCode==t.DOM_VK_ESCAPE||27==t.keyCode)&&this.hide_now(t),!0},setZIndex:function(t){null===t||isNaN(t)||t<2||(t=Math.floor(t))!=this.options.z_index&&(this.ieFix?t>this.options.z_index?(this.popup.style.zIndex=t,this.ieFix.style.zIndex=""+(t-1)):(this.ieFix.style.zIndex=""+(t-1),this.popup.style.zIndex=t):this.popup.style.zIndex=t,this.options.z_index=t)},makeCloseButton:function{var t,e,i;this.close_button=null,this.options.close_button&&(i=null,!(i=void 0!==this.options.close_button.length?this.options.close_button:[this.options.close_button])||0==i.length||(t=Buttons.makeButton(i,this.tip_id+"_button",this.eventClose))&&(e=t.firstChild.getAttribute("width"),is_IE?((i=document.createElement("div")).style.display="inline",i.style.styleFloat="right",i.appendChild(t),t=i):t.style.cssFloat="right",t.style.paddingTop="2px",t.style.paddingRight="2px",this.popup.firstChild.insertBefore(t,this.popup.firstChild.firstChild),this.close_button=t,this.close_button_width=parseInt(""+e,10)))},within:function(t,e,i){if(!t)return!1;var o=this.position(t);return(null==e||e>o.x&&eo.y&&i  ',t.insertBefore(e,t.firstChild);var i=e.firstChild,o=i.firstChild,s=i.nextSibling.firstChild.firstChild;r.doesNotAddBorder=5!==o.offsetTop,r.doesAddBorderForTableAndCells=5===s.offsetTop,i.style.overflow="hidden",i.style.position="relative",r.subtractsBorderForOverflowNotVisible=-5===o.offsetTop;o=t.style.marginTop;t.style.marginTop="1px",r.doesNotIncludeMarginInBodyOffset=0===t.offsetTop,t.style.marginTop=o,t.removeChild(e)}return function(t){if(t===t.ownerDocument.body)return function(s){r||u;var t=s.offsetTop,e=s.offsetLeft;{function i(t,e){var i,o;return!/^\d+(px)?$/i.test(e)&&/^\d/.test(e)&&s.runtimeStyle&&(i=t.style.left,o=t.runtimeStyle.left,t.runtimeStyle.left=t.currentStyle.left,t.style.left=result||0,e=elem.style.pixelLeft+"px",t.style.left=i,t.runtimeStyle.left=o),e}r.doesNotIncludeMarginInBodyOffset&&(s.ownerDocument.defaultView&&s.ownerDocument.defaultView.getComputedStyle?(s.ownerDocument.defaultView.getComputedStyle(s,null),t+=parseInt(style.getPropertyValue("margin-top"),10)||0,e+=parseInt(style.getPropertyValue("margin-left"),10)||0):(style=s.currentStyle||s.style,t+=parseInt(i(s,style.marginTop),10)||0,e+=parseInt(i(s,style.marginleft),10)||0))}return{x:e,y:t}}(t);if(t.getBoundingClientRect){var e=t.getBoundingClientRect,i=this.scroll_offset("Left"),o=this.scroll_offset("Top");return{x:e.left+i,y:e.top+o}}r||u;for(var s,n=t,p=n.offsetParent,l=n.ownerDocument,h=l.defaultView.getComputedStyle(n,null),d=n.offsetTop,a=n.offsetLeft;(n=n.parentNode)&&n!==l.body&&n!==l.documentElement;)s=l.defaultView.getComputedStyle(n,null),d-=n.scrollTop,a-=n.scrollLeft,n===p&&(d+=n.offsetTop,a+=n.offsetLeft,!r.doesNotAddBorder||r.doesAddBorderForTableAndCells&&/^t(able|d|h)$/i.test(n.tagName)||(d+=parseInt(s.borderTopWidth,10)||0,a+=parseInt(s.borderLeftWidth,10)||0),p=n.offsetParent),r.subtractsBorderForOverflowNotVisible&&"visible"!==s.overflow&&(d+=parseInt(s.borderTopWidth,10)||0,a+=parseInt(s.borderLeftWidth,10)||0),h=s;return"relative"!==h.position&&"static"!==h.position||(d+=l.body.offsetTop,a+=l.body.offsetLeft),"fixed"===h.position&&(d+=Math.max(l.documentElement.scrollTop,l.body.scrollTop),a+=Math.max(l.documentElement.scrollLeft,l.body.scrollLeft)),{x:a,y:d}}},scroll_offset:function(t){t="scroll"+t;return(document.documentElement?document.documentElement[t]:0)||document.body[t]||0},viewport:function(t){t="client"+t;return(document.documentElement?document.documentElement[t]:0)||document.body[t]||0},calculate_dimension:function{if("none"!=this.popup.style.display&&null!=this.popup.style.display)return{width:this.popup.offsetWidth,height:this.popup.offsetHeight};var t=this.viewport("Width");this.popup.style.top="0px",this.popup.style.left="0px",this.popup.style.width="",this.popup.style.maxWidth="",this.popup.style.overflow="hidden",this.popup.style.visibility="hidden",this.close_button&&this.popup.firstChild.removeChild(this.close_button),this.popup.style.display="";var e=this.popup.offsetWidth,i=this.popup.offsetHeight,t=Math.round(t*this.options.max_width);0<this.options.max_pixels&&this.options.max_pixels<t&&(t=this.options.max_pixels),t<e?(e=t,this.popup.style.width=e+"px",this.popup.style.maxWidth=this.popup.style.width,this.close_button&&this.popup.firstChild.insertBefore(this.close_button,this.popup.firstChild.firstChild)):(this.popup.style.width=e+"px",this.popup.style.maxWidth=this.popup.style.width,this.close_button&&this.popup.firstChild.insertBefore(this.close_button,this.popup.firstChild.firstChild),i!=this.popup.offsetHeight&&(e+=this.close_button_width,this.popup.style.width=e+"px",this.popup.style.maxWidth=this.popup.style.width));e={width:this.popup.offsetWidth,height:this.popup.offsetHeight};return this.popup.style.display="none",this.popup.style.visibility="",e}};