User:Eleazaros/core.js: Difference between revisions

From Lotro-Wiki.com
Jump to navigation Jump to search
m (Changed protection level for "User:Eleazaros/core.js": Changing back, fixing will take place later ([edit=sysop] (indefinite) [move=sysop] (indefinite)) [cascading])
(getRequest.open("GET", link) will return an empty string, if the script is on another domain (lotro-wiki.com) then the link (www.lotro-wiki.com) you want to get. permission problem)
Line 117: Line 117:
//Checks to see if XmlHttpRequest object is ready.
//Checks to see if XmlHttpRequest object is ready.
if (getRequest.readyState == 4 || getRequest.readyState == 0) {
if (getRequest.readyState == 4 || getRequest.readyState == 0) {
getRequest.open("GET", 'http://www.lotro-wiki.com/index.php?action=render&title=' + pagename, true);
getRequest.open("GET", '/index.php?action=render&title=' + pagename, true);
switch (type) {
switch (type) {
case 1:
case 1:

Revision as of 02:03, 19 March 2011

// from http://www.wowwiki.com/User:Pcj/core.js
/*
Defines universal variables and functions for hover tooltip generation.
<pre>
*/

// loot comparison off by default
var ttCompare = false;
// debug mode off by default
var ttDebug = true;

var ttHTMLStart = '<div style="font-size:1em; width: auto; background: transparent url(http://www.lotro-wiki.com/images/8/82/Ttip_Background.png);" class="itemtooltip">';
var crlf = new RegExp("\r\n", "g");

function $A(a) {
var r = [];
for (var i = 0, len = a.length; i < len; ++i) r.push(a[i]);
return r;
}

Function.prototype.bind = function() {
var __method = this, args = $A(arguments), object = args.shift();
return function() { return __method.apply(object, args.concat($A(arguments))) };
}

// Empty variables to hold the mouse position and the window size
var mousePos = null;
var winSize = null;

// Set events to catch mouse position and window size
document.onmousemove = mouseMove;
window.onresize = windowResize;

// The mouseMove and mouseCoords function track the mouse position for us
function mouseMove(ev) {
ev = ev || window.event;
mousePos = mouseCoords(ev);
}

function mouseCoords(ev) {
if(ev.pageX || ev.pageY) return {x:ev.pageX, y:ev.pageY};
if (document.documentElement && document.documentElement.scrollTop) {
// IE6 +4.01 and user has scrolled
dbSleft = document.documentElement.scrollLeft;
dbStop = document.documentElement.scrollTop;
dbCleft = document.documentElement.clientLeft;
dbCtop = document.documentElement.clientTop;
} else {
// IE5 or DTD 3.2
dbSleft = document.body.scrollLeft;
dbStop = document.body.scrollTop;
dbCleft = document.body.clientLeft;
dbCtop = document.body.clientTop;
}
return {x:ev.clientX + dbSleft - dbCleft, y:ev.clientY + dbStop  - dbCtop};
}

function ttError(type) {
extraText = "";
switch(type) {
case "NPC":
  extraText = "<br>A correctly formatted {" + "{npcbox}" + "}<br>must be added to the target<br>NPC page.";
  return ttHTMLStart + "<b>Error</b><br>This " + type + " either has no tooltip<br>or was not intended to have one." + extraText + "</div>";
  break;    
case "item":
  extraText = "<br>A correctly formatted {" + "{itembox}" + "}<br> must be added to the target<br>Item page.";
  return ttHTMLStart + "<b>Error</b><br>This " + type + " either has no tooltip<br>or was not intended to have one." + extraText + "</div>";
  break;
case "quest":
  extraText = "<br>A correctly formatted {" + "{questbox}" + "}<br> must be added to the target<br>Quest page.";
  return ttHTMLStart + "<b>Error</b><br>This " + type + " either has no tooltip<br>or was not intended to have one." + extraText + "</div>";
  break;
default:
 return type;
}
}

// The windowResize function keeps track of the window size for us
function windowResize() {
if (document.documentElement && document.documentElement.clientWidth) {
// IE6 +4.01
dbCwidth = document.documentElement.clientWidth;
dbCheight = document.documentElement.clientHeight;
} else {
// IE5 or DTD 3.2
dbCwidth = document.body.clientWidth;
dbCheight = document.body.clientHeight;
}
winSize = {x:(dbCwidth) ? dbCwidth:window.innerWidth, y:(dbCheight) ? dbCheight:window.innerHeight}
}

//Determine what XmlHttpRequest object we will use.
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest(); //Not Internet Explorer
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP"); //Internet Explorer
} else {
//fail silently
}
}

var getRequest = getXmlHttpRequestObject();

function hideTip() {
var tip = document.getElementById('tfb');
tip.innerHTML = "";
tip.style.display = "none";
if (ttCompare) {
var tip2 = document.getElementById('tfb2');
tip2.innerHTML = "";
tip2.style.display = "none";
}
}

function getInfo(pagename, type) {
//Checks to see if XmlHttpRequest object is ready.
if (getRequest.readyState == 4 || getRequest.readyState == 0) {
getRequest.open("GET", '/index.php?action=render&title=' + pagename, true);
switch (type) {
case 1:
getRequest.onreadystatechange = parseText;
break;
case 2:
getRequest.onreadystatechange = parseSetText;
break;
case 3:
getRequest.onreadystatechange = parseCompText;
break;
case 4:
getRequest.onreadystatechange = parseQuestText;
break;
case 5:
getRequest.onreadystatechange = parseNPCText;
break;
default:
getRequest.onreadystatechange = parseText;
}
//Makes the request.
getRequest.send(null);
}
}

function displayTip() {
var tip = document.getElementById('tfb');
tip.style.position = "absolute";
tip.style.visibility = "hidden";
tip.style.display = "block";
tip.style.zIndex = "999";
moveTip();
tip.style.visibility = "visible";
}

// This function moves the tool-tips when our mouse moves
function moveTip() {
if (document.documentElement && document.documentElement.scrollTop) {
// IE6 +4.01 and user has scrolled
dbSleft = document.documentElement.scrollLeft;
dbStop = document.documentElement.scrollTop;
} else {
// IE5 or DTD 3.2
dbSleft = document.body.scrollLeft;
dbStop = document.body.scrollTop;
}
var tip = document.getElementById('tfb');
var newTop = mousePos.y - (tip.clientHeight + 40);
var newLeft = mousePos.x - ( tip.clientWidth / 2 );
if( newTop < dbStop ) { 
newTop = mousePos.y + 1;
if ( newTop + tip.clientHeight > winSize.y ) newTop = dbStop; 
}
if( newLeft < dbSleft ) newLeft = dbSleft;
if( ( mousePos.x + ( tip.clientWidth / 2 ) ) >= winSize.x - 300 ) newLeft = mousePos.x - ( 1.75 * tip.clientWidth );
tip.style.top = newTop + "px";
tip.style.left = newLeft + "px";
}

windowResize();
ttfdiv = document.createElement("div");
ttfdiv.setAttribute("id", "tfb");
contentstart = document.getElementById("content");
contentstart.insertBefore(ttfdiv , contentstart.childNodes[0]);
document.body.onmouseover = hideTip;
/*
</pre>
*/