User:AoV2/suggestor.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
if(typeof(aov) == "undefined") aov = {};
 
aov.search = {};
aov.search.path = wgServer + "/w/api.php?action=query&generator=allpages&gaplimit=100&format=json&callback=aov.search.update&gapprefix=";
aov.search.init = function(){
	appendCSS("#aov-suggestor { text-align:left; border:1px solid #ddd; font-size:76%; margin:0px; overflow-x:hidden; overflow-y:auto; max-height:100px; }\n");
	appendCSS("#aov-suggestor li { list-style:none; border-bottom: 1px solid #eee; white-space:nowrap; overflow:hidden; }\n");
	appendCSS("#aov-suggestor li:last-child { border: none; }\n");
	appendCSS("#aov-suggestor li.selected { background-color:#DEF; }\n");
	aov.search.titles = [];
	aov.search.sel = null;
	aov.search.input = document.getElementById("searchInput");
	aov.search.menu = document.createElement("ul");
	aov.search.input.parentNode.appendChild(aov.search.menu);
	aov.search.menu.setAttribute("id", "aov-suggestor");
	aov.search.input.addEventListener("keypress", aov.search.key, false);
	aov.search.listify();
	};
aov.search.use = function(n) {
	li = aov.search.menu.childNodes;
	if(!li.length) return;
	aov.search.sel = Math.max(0, Math.min(n, li.length - 1));
	for(i = 0; i < li.length; i++) li[i].className=null;
	aov.search.input.value = li[aov.search.sel].innerHTML;
	li[aov.search.sel].className = "selected";
	aov.search.input.focus();
	}
aov.search.go = function(n) {
	aov.search.use(n);
	document.getElementById("searchGoButton").click();
	}
aov.search.update = function(o) {
	if(!o || !o.query) return;
	if(aov.search.query != aov.search.input.value) return aov.search.begin();
	aov.search.titles = [];
	for(p in o.query.pages) aov.search.titles.push(o.query.pages[p].title);
	aov.search.titles.sort();
	aov.search.listify();
	};
aov.search.listify = function(){
	aov.search.menu.innerHTML = aov.search.titles.length ? ("<li>" + aov.search.titles.join("</li><li>") + "</li>") : "";
	li = aov.search.menu.childNodes;
	aov.search.menu.setAttribute("style", "display:" + (li.length ? "block" : "none"));
	for(i = 0; i < li.length; i++){ 
		li[i].setAttribute("onClick", "aov.search.use(" + i + ");");
		li[i].setAttribute("onDblClick", "aov.search.go(" + i + ");");
		}
	};
aov.search.key = function(k) {
	if(k.keyCode == 40) return aov.search.use(++aov.search.sel);
	else if(k.keyCode == 38) return aov.search.use(--aov.search.sel);
	else aov.search.begin();	
	};
aov.search.begin = function() {
	aov.search.sel = null;
	pg = aov.search.query = aov.search.input.value;
	ns = "0";
	r = /^/;
	for(n in wgFormattedNamespaces) { // to-do: account for namespace alii such as “WP:”
		r.compile("^" + wgFormattedNamespaces[n] + ":", "i");
		if(aov.search.query.match(r)) {
			pg = aov.search.query.replace(r, "");
			ns = n;
			break;
			}
		}
	url = aov.search.path + encodeURIComponent(pg) + "&gapnamespace=" + ns + "&cur_time_ms=" + Math.round(new Date().getTime());
	if(aov.search.input.value.length) return mw.loader.load(url);
	aov.search.titles = [];
	aov.search.listify();
	}
 
$(aov.search.init);