Sixtest Wiki:API4/Item

(function {	"use strict";	var $extension = $API.create.extension("Item"),		init;	/* Item extension	 * Purpose:	 * Downloads & parses data files @ %API%/Item/Data%n%	 * Sets $API.data.item = [ the_parsed_data ];	 */	$extension.loadData = function  {		var datafiles, datafileslen, loaderCallback, data = [], datafilesloaded = 0, i = 0, listElementMeta, twEdit, twMove;		datafiles = [			"/wiki/Sixtest_Wiki:API/Item/Data1"		];		twEdit = function (p, v) {			var item = this;			$API.user.changed = $API.user.changed || {};			if (item[p] !== undefined && item[p] !== v) {				$API.user.changed[item.name] = $API.user.changed[item.name] || {};				$API.user.changed[item.name][p] = v;				if (p === 'name' && v) {					this.twMove(v);				}				return true;			}		};		twMove = function (title) {			var item = this;			$API.user.moved = $API.user.moved || {};			$API.user.moved[item.name] = title;		};		listElementMeta = { dataType: 'Item', twEdit: twEdit, twMove: twMove };		datafileslen = datafiles.length; loaderCallback = function (i) { // Used by $.ajax to put response text to an array. Should be done // like this because of the asynchronous nature of ajax; another // version of i is created in this scope so that variables are // assigned property when working asynchronously. return function (text) { var j = 0, len = datafileslen, datastr = ''; data[i] = text; datafilesloaded += 1; if (datafilesloaded === datafileslen) { for (j; j < len; j += 1) { datastr += data[j]; }					$extension.assign($API.ext.Parser.parse(datastr, listElementMeta)); }			};		};		for (i; i < datafileslen; i += 1) { $.ajax({				url: datafiles[i] + "?action=raw",				dataType: 'text',				success: loaderCallback(i)			}); }	};	$extension.assign = function (data) { $extension.data = $API.data.item = $API.create.list("item", data); return $extension.data; };	if (init !== false) { $extension.loadData; }	/* Layout for items */ $extension.layout = function (data, container) { var $cont = container, params = { 'name': '0', 'flavortext': '0', 'implemented': '0', 'range': '1', 'levelrequired': '1', 'vocrequired': '1', 'attack': '1', 'defense': '1', 'defensemod': '1', 'elementattack': '1', 'resist': '1', 'armor': '1', 'weight': '1', 'volume': '1', 'itemclass': '2', 'primarytype': '2', 'secondarytype': '2', 'hands': '2', 'type': '2', 'attrib': '3', 'stackable': '3', 'writable': '3', 'writechars': '3', 'enchantable': '3', 'edible': '3', 'regenseconds': '3', 'npcvalue': '4', 'npcvaluerook': '4', 'npcprice': '4', 'npcpricerook': '4', 'buyfrom': '4', 'sellto': '4', 'droppedby': '5'}, elements = {}; container.ele.className = 'SearchSuggest_Display' + ' ' + 'item' + ' ' + ($API.user.edittools ? 'edittools-enabled' : 'edittools-disabled'); (function init {			var groups, funcGeneric, property, i, len, objprop, base;			// generic function for all params			funcGeneric = function (base, data, param, func) {				var obj = base.child('div');				func = func || function (obj) {					// main function here					return obj;				};				base.ele.className += (base.ele.className ? ' ' : '') + 'p-' + param;				obj.ele.className = param;				obj.ele.appendChild(document.createTextNode(data[param]));				obj = func(obj);				return obj;			};			// list of grouped parameters			if ($API.user && $API.user.ext && $API.user.ext.Item && $API.user.ext.Item.layout) {				// allows for a customized grouping - just a test!				groups = $API.user.ext.Item.layout;			} else {				groups = {					"general": [ "name", "flavortext", "implemented" ],					"data": [ "range", "levelrequired", "vocrequired", "attack", "defense", "defensemod", "elementattack", "resist", "armor", "weight", "volume" ],					"classification": [ "itemclass", "primarytype", "secondarytype", "hands", "type" ],					"additional": [ "attrib", "stackable", "writable", "writechars", "enchantable", "edible" ],					"trades": [ "npcvalue", "npcvaluerook", "npcprice", "npcpricerook", "buyfrom", "sellto" ],					"droppedby": [ "droppedby" ] };			}			// iterate over the data's properties for (property in data) { if (data.hasOwnProperty(property)) { for (objprop in groups) { if (groups.hasOwnProperty(objprop)) { i = 0; len = groups[objprop].length; for (i; i < len; i += 1) { if (groups[objprop][i] === property) { if (!elements[objprop]) { elements[objprop] = container.child('div'); }									base = elements[objprop]; funcGeneric(base, data, property); }							}						}					}				}			}		});	}; });