Sixtest Wiki:API5/Core

(function {	"use strict";	// Main API object	var $API;	$API = (function  { var list, loadScripts, extensions, base_url = 'http://sixtest.wikia.com/wiki/', url_suffix = '?action=raw'; list = (function {			// call $API.list to create a new list class			// such as: items, npcs, ajax requests, UI elements, ...			// it creates a new constructor, allowing subclassing by a common List descendent			// i.e. it will have the generic List methods, but can be extended with ListName.extend(object)			function List {}			List.prototype.constructor = List;			List.prototype.extend = function (obj, local) {				var prop, subj = local === true ? this : this.constructor.prototype;				for (prop in obj) {					if (obj.hasOwnProperty(prop)) {						subj[prop] = obj[prop];					}				}				return this;			};			List.prototype.extend({ "append": function (data) { if (data !== undefined) { this.push.apply(this, data); }					return this; },				length: 0, splice: Array.prototype.splice, slice: Array.prototype.slice, push: Array.prototype.push, clone: function { var obj = $API; obj.extend(this); obj.length = 0; return obj; },				sort: function (obj) { var func, sortBy, sortOrder, dataType; obj = obj || {}; sortBy = obj.sortBy || this.sortBy; sortOrder = obj.sortOrder || this.sortOrder || 'ascending'; dataType = obj.dataType || this.dataType || 'string'; func = function { var ret; if (sortOrder === 'ascending') { if (dataType === 'string') { if (sortBy !== undefined) { ret = function (a, b) { return a[sortBy] > b[sortBy]; }; } else { ret = function (a, b) { return a > b; }; }							} else if (dataType === 'number') { if (sortBy !== undefined) { ret = function (a, b) { return a[sortBy] - b[sortBy]; }; } else { ret = function (a, b) { return a - b; }; }							}						} else { if (dataType === 'string') { if (sortBy !== undefined) { ret = function (a, b) { return a[sortBy] < b[sortBy]; }; } else { ret = function (a, b) { return a < b; }; }							} else if (dataType === 'number') { if (sortBy !== undefined) { ret = function (a, b) { return b[sortBy] - a[sortBy]; }; } else { ret = function (a, b) { return b - a; }; }							}						}						return ret; };					return Array.prototype.sort.call(this, func); },				find: function (arr, limit, param) { // iterate over arr, find 1 var i = 0, j, len = arr.length, leny, done, count = 0, resultCount = 0, ret_results = [], results = [], v;					if (typeof arr === 'string') { arr = [arr]; len = 1; }					param = param || this.sortBy; limit = (limit === true ? Infinity : (typeof limit === 'number' ? limit : 1)); for (i; i < len; i += 1) { j = 0; leny = this.length; done = false; for (j; j < leny && !done; j += 1) { v = param ? this[j][param] : this[j]; if (v === arr[i]) { results[i] = this[j]; done = true; }						}					}					return this.clone(results); },				iterate: function (func) { var i = 0, len = this.length, res = [], v;					for (i; i < len; i += 1) { res[res.length] = func(this[i]); }					return this.clone(res); },				indexOf: function (val, prop) { var i = 0, len = this.length, c;					for (i; i < len; i += 1) { c = prop !== undefined ? this[i][prop] : this[i]; if (c === val) { return i;						} }					return -1; },				filterValues: function (val, param) { var i = 0, len = this.length, j, len2, res = [], c; val = val.length !== undefined ? val : [ val ]; for (i; i < len; i += 1) { c = param !== undefined ? this[i][c] : this[i]; if (this.indexOf.call(val, c) === -1) { res[res.length] = this[i]; }					}					return this.clone(res); },				gt: function (index, param) { var i = 0, len = this.length, res; return (this.iterate(function (item) { return item[param] > index ? item : false; })).filterValues(false); },				lt: function (index, param) { var i = 0, len = this.length, res; return (this.iterate(function (item) { return item[param] < index ? item : false; })).filterValues(false); },				match: function (find, limit, param) { var i = 0, len = this.length, count = 0, results = [], type = typeof this[0] === 'object', c; param = param || this.sortBy || false; limit = (limit === true ? Infinity : (typeof limit === 'number' ? limit : 1)); if (find !== undefined) { for (i; i < len && count < limit; i += 1) { c = type ? this[i][param] : this[i]; if (c.match(find) !== null) { results[results.length] = this[i]; }						}					}					return this.clone(results); }			});			return function {				var F = function  {};				F.prototype = new List;				F.prototype.constructor = F;				return function (data) {					var e = new F;					e.extend({ clone: function (data) { return new F.append(data); }					});					return e.clone(data);				};			};		}); extensions = list; extensions.extend({			sortBy: 'name',			create: function (name) {				var extension = list;				extension.extend({ name: name, states: (function {						var states_list = list;						states_list.extend({ change: function (param, value) { this.iterate(function (state) {									state[param] = value;								}); return this; }						});						return function (state, value) {							var i, len, prop, ret = [], states_found, valueIsArray = value && typeof value === 'object' && value.length;							if (typeof state !== 'string') {								if (typeof state === 'object') {									if (state.length !== undefined) {										for (i = 0, len = state.length; i < len; i += 1) {											ret[i] = this.states(state[i], valueIsArray ? state[i] : value);										}										return this.clone(ret);									}									for (prop in state) {										if (state.hasOwnProperty(prop)) {											ret[ret.length] = this.states(prop, state[prop]);										}									}									return this.clone(ret);								}							}							ret = states_list.find(state);							if (!ret.length) {								states_list.append([{ name: state, value: value }]);								return states_list.find(state);							}							if (value !== undefined) {								ret.change('value', value);							}							return ret;						};					}) });				extensions.append([extension]);				return extension;			}		}); loadScripts = function (/* [scripts][, callbacks] */) { var scripts, callbacks = [], i, len, load, runCallbacks, loadedScripts = 0, onLoadFunction, findScripts, createExtension; findScripts = function { var i = arguments.length - 1; while (i && typeof arguments[i] === 'function') { callbacks[callbacks.length] = arguments[i]; i -= 1; }				return Array.prototype.slice.apply(arguments, [0, i+1]); };			scripts = findScripts.apply(this, arguments); len = callbacks.length; createExtension = extensions.create; runCallbacks = function { var i = 0, len = callbacks.length; for (i; i < len; i += 1) { callbacks[i].apply(this, $API.apply(this, scripts)); }				return; };			onLoadFunction = function { // if all scripts are loaded, run the callbacks loadedScripts += 1; if (loadedScripts === len) { runCallbacks; }				return null; };			load = function (name) { var head = document.getElementsByTagName("head")[0], script = document.createElement("script"), done = false; if (script.onload === script.onreadystatechange === undefined) { throw new Error("Could not reliably determine onload script method. Update or change your browser."); }				if (script.onload === undefined) { script.onreadystatechange = function { if (!done && (!this.readyState || this.readyState === "loaded" || this.readystate === "complete")) { done = true; // fix memory leak (IE) script.onreadystatechange = onLoadFunction; return; }						return; };					return; }				script.onload = function { // fix any possible reference loop memory leaks script.onload = onLoadFunction; };				script.src = base_url + name + url_suffix; head.appendChild(script); return; };			for (i = 0, len = scripts.length; i < len; i += 1) { if (!extensions.find(scripts[i]).length) { load(scripts[i]); createExtension(scripts[i]); } else { onLoadFunction; }			}		};		return (function {			return function  {				if (typeof arguments[arguments.length - 1] === 'function') {					return loadScripts.apply(this, arguments);				}				return extensions.find(arguments, true);			};		}); });	window.$API = $API; }); $API("Project:API5/Color2", function (scope) {	"use strict";	var color = new scope.Color("HSV", [274, 0.8, 0.4]);	color.hook("onChangeColor", function { var element = document.getElementById("API-Interface"); function colorEle(e, depth) { var i = 0, len = e.children.length, style = new scope.Color("HSV", color.convert("HSV")); style.shift(0, -depth * 0.1, 0); e.style.backgroundColor = "rgb(" + style.convert("RGB").join(",") + ")"; for (i; i < len; i += 1) { colorEle(e.children[i], depth + 1); }		}		colorEle(element, 0); });	color.slider; });