Sixtest Wiki:API2/Code

function API(name, add) { "use strict"; var API_obj = this, API_property, inArray, readCookie, setCookie, autoSetCookies, scopeIndex = [], xyz = true, currentNamespace = , currentScope = ; API_obj.UI = {}; API_obj.uiElements = {}; for (API_property in add) { if (add.hasOwnProperty(API_property)) { this[API_property] = add[API_property]; }	}	inArray = function (needle, haystack) { var i = 0, len = haystack.length; for (i; i < len; i += 1) { if (haystack[i] === needle) { return true; }		}		return false; };	readCookie = function (name) { var cookieArray = document.cookie.split(';'), cookieChar, i = 0; name = name + "="; for (i; i < cookieArray.length; i += 1) { cookieChar = cookieArray[i]; while (cookieChar.charAt(0) === ' ') { cookieChar = cookieChar.substring(1, cookieChar.length); }			if (cookieChar.indexOf(name) === 0) { return JSON.parse(cookieChar.substring(name.length, cookieChar.length)); }		}		return null; };	setCookie = function (name, value, days) { var expiryString, expiry; if (days !== undefined && days !== 0) { expiry = new Date; // 86400000 ms in 1 day expiry.setTime(expiry.getTime + (days * 86400000)); expiryString = "; expires=" + expiry.toGMTString; } else if (days === 0) { expiryString = ""; } else { //alert('Error: invalid days'); return false; }		return (document.cookie = name + '=' + JSON.stringify(value) + expiryString + '; path=/'); };	API_obj.readCookie = readCookie; API_obj.setCookie = setCookie; function toggle(p, v, obj) { if (obj === undefined) { obj = API_obj; }		if (v === undefined) { obj[p] = !obj[p]; } else { obj[p] = !!v; }		return obj[p]; }	autoSetCookies = (function (obj) {		API_obj = obj; // can't see another way to determine main API's object name ...		return function {			var API = API_obj, out = '', ext;			for (ext in API) {				if (API.hasOwnProperty(ext) && API[ext].savedStates !== undefined) {					out += JSON.stringify(API[ext].savedStates);				}			}			//return obj.setCookie(out);			return null;		};	}(this)); window.onbeforeunload = autoSetCookies; this.remember = function (obj) { var out = readCookie(obj.name), rtn; if (!out) { rtn = out; } else { rtn = setCookie(obj.name, obj, 30); }		return rtn; };	this.name = name; scopeIndex[scopeIndex.length] = this.name; this.version = '1.00'; function API_Extension(name) { /* Create new API extension */ this.toggle = toggle; this.init = function (obj) { var prop; for (prop in obj) { if (obj.hasOwnProperty(prop)) { this.set(prop, obj[prop], false, null, false); }			}		};		this.addDocs = function (obj) { var param; for (param in obj) { if (obj.hasOwnProperty(param)) { this[param] = obj[param]; }			}		};		/* UI */ function API_Interface(name) { this.allowDuplicates = false; this.persist = false; this.uiElements = {}; this.add = function (obj, jsname) { var e, styleprop, jsproperty; if (obj.element !== undefined) { e = document.createElement(obj.element); if (obj.style !== undefined) { for (styleprop in obj.style) { if (obj.style.hasOwnProperty(styleprop)) { e.style[styleprop] = obj.style[styleprop]; }						}					}					if (obj.properties !== undefined) { /* inf loop? */						for (jsproperty in obj.properties) { if (obj.properties.hasOwnProperty(jsproperty)) { e[jsproperty] = obj.properties[jsproperty]; }						}					}					if (API_obj.uiElements[obj.parent] !== undefined && obj.parent !== '') { obj.parent = API_obj.uiElements[obj.parent]; } else { obj.parent = document.getElementById("API-Interface"); }					API_obj.uiElements[jsname] = e;				} obj.parent.appendChild(e); if (API_obj.UI[jsname] === undefined) { API_obj.UI[jsname] = e;				} };		}		this.addUI = function (obj) { var ui = new API_Interface(name), special_vars = [ "UI_Config" ], uielement, sindex = 0, slength = special_vars.length, subject, rule; for (sindex; sindex < slength; sindex += 1) { if (obj[special_vars[sindex]] !== undefined) { subject = obj[special_vars[sindex]]; for (rule in subject) { if (subject.hasOwnProperty(rule)) { ui[rule] = subject[rule]; }					}					delete (obj[special_vars[sindex]]); }			}			for (uielement in obj) { if (obj.hasOwnProperty(uielement)) { ui.add(obj[uielement], uielement); }			}		};		this.statesToSave = []; this.name = name; this.savedStates = {}; this.remember = function (cookie) { var item; for (item in cookie) { if (cookie.hasOwnProperty(item)) { this[item] = cookie[item]; }			}		};		this.scopeIndex = (function (name) {			var cloned_array = scopeIndex.slice(0);			cloned_array[cloned_array.length] = name;			return cloned_array;		}(name)); window[this.scopeIndex[0]].remember(this); //this[scopeIndex[0]].remember(this); this.setNs = function (obj, scope, namespace) { var focus = this.name; if (namespace === undefined || namespace === '') { if ((currentNamespace === undefined || scope === undefined || scope === ) || currentNamespace === ) { currentNamespace = "Default"; }				namespace = currentNamespace; }			currentNamespace = namespace; if (this[namespace] === undefined) { this[namespace] = {}; }			focus = this[namespace]; if (scope === undefined || scope === '') { if (!(currentScope === undefined || currentScope === '')) { scope = currentScope; }			}			if (!(scope === undefined || scope === '')) { if (this[namespace][scope] === undefined) { this[namespace][scope] = {}; }				focus = this[namespace][scope]; }			window.focus = focus; };		this.addFunc = function (obj) { var func; for (func in obj) { if (obj.hasOwnProperty(func)) { this[func] = obj[func]; }			}		};		this.set = function (p, v, addTo, key, save) { if (save === undefined) { save = true; }			var namespace = (function (thisobj) {				var subj = thisobj.scopeIndex, len = subj.length, i = 0, out;				for (i; i < len; i += 1) {					if (out !== undefined) {						out = out[thisobj.scopeIndex[i]];					} else {						out = window[thisobj.scopeIndex[i]];					}				}				return out;			}(this)); if (addTo === 'array' || addTo === true) { if (key === undefined) { if (namespace[p] === undefined) { namespace[p] = []; }					namespace[p][namespace[p].length] = v;				} else { if (namespace[p] === undefined) { namespace[p] = {}; }					namespace[p][key] = v;				} } else { namespace[p] = v;			} if (save) { this.statesToSave[this.statesToSave.length] = p;			} return namespace[p]; };		this.saveState = function { var toSave = this, saved = {}, item; for (item in toSave) { if (toSave.hasOwnProperty(item) && inArray(item, this.statesToSave)) { saved[item] = toSave[item]; }			}			this.savedStates = saved; };	}	this.addExt = function (ext) { var cookie; API_obj[ext] = (API_obj[ext] !== undefined) ? API_obj[ext] : new API_Extension(ext); //alert(this.readCookie); cookie = readCookie(ext); if (cookie) { if (API_obj[ext].remember === undefined) { alert('Cookie has been identified, but the extension cannot handle them.'); } else { API_obj[ext].remember(cookie); }		}		return API_obj[ext]; };	this.enable = function (name, raw) { var head = document.getElementsByTagName('head')[0], baseURL = 'http://sixtest.wikia.com/wiki/Sixtest_Wiki:API2/', e; raw = (raw === true || raw === undefined) ? "?action=raw" : "?action=render"; $.ajax({			'url': baseURL + name + raw,			async: false,			dataType: 'text',			success: function (text) {				e = document.createElement('script');				e.type = 'text/javascript';				e.text = text;				head.appendChild(e);			}		}); }; } window.TWAPI = new API('TWAPI'); window.TWAPI.enable("Interface"); window.TWAPI.enable("Item"); window.TWAPI.enable("SearchSuggest");