Sixtest Wiki:API3/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 = {}; /* Event methods */ API_obj.Events = { "onchange": [], // after variable has changed "onload": [], // after function has loaded "onrun": [], // after function has run };	this.E_Clear = function (event, subject) { var i, len, subject, finalArrayLength; subject = this.Events[event][subject]; i = 0; len = subject.length; finalArrayLength = 0; for (i; i < len; i += 1) { if (subject[i] !== undefined && subject[i].persist === true) { subject[finalArrayLength] = subject[i]; } else { delete (subject[i]); }		}		return subject; };	this.E_Fire = function (event, subject, args) { var fns, fnsCount, i, rtns = []; if (this.Events === undefined) { return false; }	   if (this.Events[event] === undefined) { return false; }	   if (this.Events[event][subject] === undefined) { return false; }	   fns = this.Events[event][subject]; fnsCount = fns.length; i = 0 for (i; i < fnsCount; i += 1) { rtns[i] = fns[i](args); }	   return rtns; };	this.E_Listen = function (event, subject, func, persist) { if (persist) { func.persist = true; }		if (this.Events !== undefined) { if (this.Events[event][subject] === undefined) { this.Events[event][subject] = []; }			this.Events[event][subject][this.Events[event][subject].length] = func; }		return func; };	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'; this.API_Extension = function(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 */ this.API_Interface = function (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 this.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) { /* Events */ API_obj.E_Fire("onbeforechange", p); /* Code */ 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;			} /* Events */ API_obj.E_Fire("onchange", p, v); /* Code */ 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 this.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:API3/', 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");