User:Sgeo/monobook.js (toolbox links)

function addlink(name, url) {

 var na = document.createElement('a');
 na.setAttribute('href', '/wiki/' + url);  
 var txt = document.createTextNode(name);
 na.appendChild(txt);
 var nli = document.createElement('li');
 nli.setAttribute('class', 'p-newlinks');
 nli.appendChild(na);
 return nli;

}

function morelinks() {

 var ndivportlet = document.createElement('div');
 var ndivpbody = document.createElement('div');
 var colone = document.getElementById('column-one');
 ndivportlet.setAttribute('class', 'portlet');
 ndivportlet.setAttribute('id', 'p-newlinks');
 ndivpbody.setAttribute('class', 'pBody');
 var nul = document.createElement('ul');
 nul.setAttribute('id', 'p-newlinks');
 nul.appendChild(addlink('RfA', 'WP:RFA'));
 nul.appendChild(addlink('VfD', 'WP:VFD'));
 nul.appendChild(addlink('FAC', 'WP:FAC'));
 nul.appendChild(addlink('RfC', 'WP:RFC'));
 nul.appendChild(addlink('RfAr', 'WP:RFAR'));
 nul.appendChild(addlink('MoS', 'WP:MOS'));
 nul.appendChild(addlink('ViP', 'WP:VIP'));
 nul.appendChild(addlink('VP', 'WP:VP'));
 var title=document.createElement('h5');
 title.appendChild(document.createTextNode('Shortcuts'));
 ndivportlet.appendChild(title);
 ndivpbody.appendChild(nul);
 ndivportlet.appendChild(ndivpbody);
 colone.appendChild(ndivportlet);

}

function do_onload() {

 morelinks();

}

if (window.addEventListener)

 window.addEventListener("load", do_onload, false);

else if (window.attachEvent)

 window.attachEvent("onload", do_onload);

// Milliseconds before fetching results var typePause = 500;

var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP"); var searchCache = {}; var searchStr; var searchEl; var searchTimeout; var resultCount; var resultsEl;

function getResults() {

 var encStr = escape(searchStr.replace(/ /g, '_'));
 xmlHttp.open("GET", "http://en.wikipedia.org/wiki/Special:Search?gen=titlematch&ns0=0&limit=10&search=" + encStr, true);
 xmlHttp.onreadystatechange = parseResults;
 xmlHttp.send(null);  

}

function parseResults() {

 if (xmlHttp.readyState > 3)
 {
   var resultArr = xmlHttp.responseText.replace(/^$/gm, ).split("\n");
   searchCache[searchStr.toLowerCase()] = resultArr;
   showResults(resultArr);
 }

}

function showResults(resultArr) {

 var returnStr = "";
 var resultsEl = searchEl.suggestList;
 if (resultArr.length < 2)
   resultsEl.style.display = "none";
 else
 {
   resultsEl.innerHTML = "";
   for (var i=0; i < resultArr.length-1; i++)
   {  
     var linkEl = document.createElement("a");
     linkEl.style.display = "block";
     linkEl.style.position = "relative";
     linkEl.style.textDecoration = "none";
     linkEl.style.color = "WindowText";
     linkEl.style.padding = "1px 3px";
     linkEl.href = "/wiki/" + resultArr[i];
     linkEl.onmouseover = function(e)
     {
       var srcEl = e ? e.target : event.srcElement;
       srcEl.style.backgroundColor = "Highlight";
       srcEl.style.color = "HighlightText";
     }
     linkEl.onmouseout = function(e)
     {
       var srcEl = e ? e.target : event.srcElement;
       srcEl.style.backgroundColor = "Window";
       srcEl.style.color = "WindowText";
     }
     if (resultsEl.curSel == i)
     {
       linkEl.style.backgroundColor = "Highlight";
       linkEl.style.color = "HighlightText";
     }


     var textEl = document.createTextNode(resultArr[i].replace(/_/g, ' '));
     var listItemEl = document.createElement("li");
     listItemEl.style.padding = 0;
     listItemEl.style.margin = 0;
     listItemEl.appendChild(linkEl);
     linkEl.appendChild(textEl);
     resultsEl.appendChild(listItemEl);
   }


   var offLeft = 0;
   var offTop = 0;
   var offEl = searchEl;
   for (; offEl && offEl != document.documentElement; offEl = offEl.offsetParent)
   {
     offTop += offEl.offsetTop;
     offLeft += offEl.offsetLeft;
   }
   resultsEl.style.left = offLeft + "px";
   resultsEl.style.top = offTop + searchEl.offsetHeight + "px";
   resultsEl.style.position = "absolute";
   resultsEl.style.display = "block";
   searchEl.onblur = function()
   {
     setTimeout(function()
     {
       searchEl.suggestList.style.display = "none";
       //searchEl.suggestList.parentNode.style.position = "static";
     }, 1000);
   }
 }
 // set width, top, left

}

function resultType() {

 if (!searchEl) return;
 searchStr = searchEl.value;
 if (searchTimeout) clearTimeout(searchTimeout);
 if (searchStr != "")
 {
   if (searchCache[searchStr.toLowerCase()])
     showResults(searchCache[searchStr.toLowerCase()])
   else
     searchTimeout = setTimeout(getResults, typePause);
 }  
 else
 {
   searchEl.suggestList.style.display = "none";
 }

}

document.onkeyup = function(e) {

 var srcEl = e ? e.target : event.srcElement;
 if (srcEl.tagName.toLowerCase() == "input" && srcEl.name == "search" && srcEl.type == "text")
 {
   if (!srcEl.wikiTransform)
   {
     srcEl.autocomplete = "off";
     srcEl.wikiTransform = true;
     var listEl = document.createElement("ul");
     listEl.style.margin = 0;
     listEl.style.padding = 0;
     listEl.style.listStyleType = "none";
     listEl.style.listStyleImage = "none";
     listEl.style.display = "none";
     listEl.style.position = "absolute";
     listEl.style.border = "1px solid #000";
     listEl.style.font = "menu";
     listEl.style.color = "WindowText";
     listEl.style.whiteSpace = "nowrap";
     listEl.style.cursor = "default";
     listEl.style.backgroundColor = "window";
     listEl.style.zIndex = 500;
     listEl.curSel = -1;
     document.body.appendChild(listEl);
     
     //srcEl.parentNode.insertBefore(listEl, srcEl.nextSibling);
     srcEl.suggestList = listEl;
     
     /*var posEl = document.createElement("div");
     posEl.style.position = "relative";
     posEl.appendChild(srcEl);
     srcEl.parentNode.replaceChild(posEl, srcEl);*/
   }
   searchEl = srcEl;
   resultType();    
 }

}

document.onkeydown = function(e) {

 var srcEl = e ? e.target : event.srcElement;
 var keyCode = e ? e.charCode : event.keyCode;
 if (srcEl.tagName.toLowerCase() == "input" && srcEl.name == "search" && srcEl.type == "text")
 {
   switch (keyCode)
   {
     case 40 : // down
       var allLinks = srcEl.suggestList.getElementsByTagName("a");
       if (srcEl.suggestList.curSel < 0)
       {
         srcEl.suggestList.curSel = 0;
         allLinks[0].style.backgroundColor = "Highlight";
         allLinks[0].style.color = "HighlightText";
         break;
       }
       else if (srcEl.suggestList.curSel + 1 < allLinks.length)
       {
         allLinks[srcEl.suggestList.curSel].style.backgroundColor = "";
         allLinks[srcEl.suggestList.curSel].style.color = "";
         allLinks[++srcEl.suggestList.curSel].style.backgroundColor = "Highlight";
         allLinks[srcEl.suggestList.curSel].style.color = "HighlightText";
       }


       break;
     case 38 : // up
       var allLinks = srcEl.suggestList.getElementsByTagName("a");
       if (srcEl.suggestList.curSel < 0)
       {
         srcEl.suggestList.curSel = allLinks.length - 1;
         allLinks[allLinks.length - 1].style.backgroundColor = "Highlight";
         allLinks[allLinks.length - 1].style.color = "HighlightText";
         break;
       }
       else if (srcEl.suggestList.curSel - 1 >= 0)
       {
         allLinks[srcEl.suggestList.curSel].style.backgroundColor = "";
         allLinks[--srcEl.suggestList.curSel].style.backgroundColor = "Highlight";
         allLinks[srcEl.suggestList.curSel].style.color = "HighlightText";
       }


       break;
     case 13 : // return
       var allLinks = srcEl.suggestList.getElementsByTagName("a");
       if (srcEl.suggestList.curSel > -1)
         srcEl.value = allLinks[srcEl.suggestList.curSel].firstChild.nodeValue;
     case 37 : // left
       break;
     case 39 : // right
       break;
    }
 }

}