Sixtest Wiki:API/SearchSuggest

TWAPI.extInit({	/* documentation */	'name': 'Search Suggest',	'description': 'Search suggest script for the API.',	'author': 'Sixorish',	'dependencies': [ 'Code', 'Interface', 'Item' ],	'jquery': true,	'objName': 'SearchSuggest',	'variables': {		'cache': 'Cached version of all search suggest lookups.',		'timer': 'Timer ID returned by setInterval. Use this to stop checking for input changes.'	},	'functions': {		'init': 'Initialize necessary variables for the script. Runs once upon loading the script.',		'searchSuggest': 'Checks if input field has been changed and runs suggest if so.',		'suggest': 'Iterates through an array of data to list search results for display.',		'display': 'Displays results obtained through suggest.'	},	/* interface */	'interface': {		top: addInterfaceElement('input', 'SS_Search', 'Container', { border: '1px inset', margin: 'auto', textAlign: 'center', width: '14em' }).value = 'Search functionality coming soon.', mid: addInterfaceElement('div', 'SS_Main', 'Container', { }), mid2: addInterfaceElement('div', 'SS_Main2', 'SS_Main', { backgroundColor: '#E0E0E0', borderColor: '#C1C1C1', borderStyle: 'solid', borderWidth: '1px', cssFloat: 'right', margin: '0 0 0 1.2em', padding: '0.2em 0.6em', width: '8em' }).innerHTML = 'Script execution dialog.', mid3: addInterfaceElement('div', 'SS_Main3', 'SS_Main', { backgroundColor: '#E0E0E0', borderColor: '#C1C1C1', borderStyle: 'solid', borderWidth: '1px', margin: '0 10.2em 0 0', padding: '0.2em 0.6em' }), btm: addInterfaceElement('div', 'SS_Log', 'Container'), content1: addInterfaceElement('div', 'SS_Suggest', 'SS_Main3', { cssFloat: 'right', margin: '0', width: '210px' }).innerHTML = 'Loaded data within current running of script.', content2: addInterfaceElement('div', 'SS_Results', 'SS_Main3', { color: '#2E0000', fontWeight: 'bold', width: '14em' }).innerHTML = 'Content.', btmtitle: addInterfaceElement('h2', 'SS_Log_Title', 'SS_Log', { border: 'none', color: '#3A3A3A', margin: '0.5em 0 0', padding: '0 0 0 0.1em', textAlign: 'center' }).innerHTML = 'Log', floatClearer: addInterfaceElement('div', 'SS_Float_Clear', 'SS_Main3', { clear: 'both' }), log: addInterfaceElement('div', 'SS_Log_Content', 'SS_Log', { borderColor: '#9D9D9D', borderStyle: 'solid', borderWidth: '1px 1px 1px 5px', fontSize: '80%', height: '6.6em', lineHeight: '95%', overflowX: 'hidden', padding: '0.1em 0 0.1em 0.5em' }).innerHTML = 'Script will log here (for e.g. debugging purposes).' },	/* code */ init: function { 'use strict'; /* main */ TWAPI.SearchSuggest = { timer: undefined, data: { item: [] },			populateData: function(type) { var data = TWAPI[type].Data, output = [], element; for (element in data) { output[output.length] = element; }				return output; },			cache: [], cacheData: function { var cache = [{}], s = 1, items = TWAPI.SearchSuggest.data.item, len = items.length; for (s; s < 33; s += 1) { var i = 0, activeItem, activeItemLen, j, stringIndex = {}, c, k, str; for (i; i < len; i += 1) { activeItem = items[i].toLowerCase, activeItemLen = activeItem.length, j = 0; for (j; j < activeItemLen; j += 1) { if (j + s <= activeItemLen) { k = 0, str = ''; for (k; k < s; k += 1) { str += activeItem.charAt(j+k); }								if (stringIndex[str] === undefined) { stringIndex[str] = []; }								stringIndex[str][stringIndex[str].length] = items[i]; }						}					}					cache[s] = stringIndex; }				return cache; },			searchSuggest: function { var e = TWAPI.Interface.SS_Search, input_new = e.value; if (TWAPI.SearchSuggest.input === undefined && e !== undefined) { TWAPI.SearchSuggest.input = e.value; } else { if (input_new !== TWAPI.SearchSuggest.input) { TWAPI.SearchSuggest.input = input_new; TWAPI.SearchSuggest.suggest(input_new); }				}			},			suggest: function (term) { var data = TWAPI.SearchSuggest.cache[term.length][term], s = TWAPI.Interface.SS_Results; TWAPI.SearchSuggest.display(s, data); },			display: function (p, item) { while (p.hasChildNodes) { // remove all child elements from suggestions container p.removeChild(p.lastChild); }				var e = document.createElement('div'), a = {}, v;				for (v in item) { if (item.hasOwnProperty(v)) { a[v] = document.createElement('div'); a[v].appendChild(document.createTextNode(item[v])); e.appendChild(a[v]); }				}				p.appendChild(e); }		};		/* css */ var style = document.createElement('style'), at = document.getElementsByTagName('head')[0]; style.setAttribute('type', 'text/css'); style.appendChild(document.createTextNode('div.active{background:none repeat scroll 0 0 #1F1F1F;color:#D1FFFF;font-size:130%;line-height:130%;}')); at.appendChild(style); /* code for css */ TWAPI.Interface.SS_Search.onkeyup = function(event) { if (TWAPI.SearchSuggest.focusItem === undefined) { TWAPI.SearchSuggest.focusItem = 0; }			var diff, s = TWAPI.Interface.SS_Results.childNodes[0], e;			if (s.children !== undefined) { e = s.children.length; }			if (event.keyCode === 40 || event.keyCode === 38) { if (event.keyCode === 40) { diff = 1; } else { diff = -1; }				s.childNodes[TWAPI.SearchSuggest.focusItem].className = "inactive"; TWAPI.SearchSuggest.focusItem = (e + TWAPI.SearchSuggest.focusItem + diff) % e;				s.childNodes[TWAPI.SearchSuggest.focusItem].className = "active"; TWAPI.Interface.SS_Suggest.innerHTML = (function {				   var item = TWAPI.Item.Data[s.childNodes[TWAPI.SearchSuggest.focusItem].innerHTML], out = '', p;				    for (p in item) {				        out += p + " : " + item[p] + " ";				    }				    return out;				}); } else if (event.keyCode === 13) { // submit var index = TWAPI.SearchSuggest.focusItem, target; if (index !== undefined) { target = s.childNodes[index].innerHTML; document.location.href = "http://sixtest.wikia.com/wiki/" + target; }			} else { TWAPI.SearchSuggest.searchSuggest; }		};		TWAPI.SearchSuggest.data.item = TWAPI.SearchSuggest.populateData('Item'); TWAPI.SearchSuggest.cache = TWAPI.SearchSuggest.cacheData; } });