Sixtest Wiki:API4/NPC

(function {	"use strict";	// TODO: convert as much as possible to settings[var]	var extension = $API.create.extension("NPC"),		settings = {},		init;	$API.require('Edit');	settings.locale = [ "/wiki/Sixtest_Wiki:API/NPC/Data1" ];	settings.groups = { "general": [ "name", "implemented" ], "data": [ ], "classification": [ "job", "city" ], "additional": [ "location" ], "sounds": [ "sounds" ], "trades": [ "buysell", "buys", "sells" ] };	settings.edittools = ($API.isEnabled('Edit') ? ($API.ext.Edit.edittools !== false) : false);	settings.layout = ($API.user && $API.user.ext && $API.user.ext.NPC && $API.user.ext.NPC.layout) ? $API.user.ext.NPC.layout : undefined;	extension.load = function (callback) {		// TODO: cleanup ... rename vars more appropriately		var dataFiles = (settings.locale || []).length,			data = [],			dataLoaded = 0,			i = 0;		$API.require('Parser');		callback = callback || 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 = dataFiles,					datastr = '';				data[i] = text;				dataLoaded += 1;				if (dataLoaded === dataFiles) {					for (j; j < len; j += 1) {						datastr += data[j];					}					extension.data = $API.data.npc = $API.create.list("npc", $API.ext.Parser.parse(datastr, { dataType: 'NPC' })); }			};		};		for (i; i < dataFiles; i += 1) { $.ajax({				url: settings.locale[i] + "?action=raw",				dataType: 'text',				success: callback(i)			}); }	};	extension.layout = function (data, container) { // TODO: cleanup .... var layout, layoutArr, layoutArrObj, layoutStr; container.addClass('SearchSuggest_Display'); container.addClass('npc'); //container.replaceClass((settings.edittools ? 'edittools-disabled' : 'edittools-enabled'), (settings.edittools ? 'edittools-enabled' : 'edittools-disabled')); layoutArrObj = function (obj, data) { var len = data.length, i = 0, $cdiv; for (i; i < len; i += 1) { $cdiv = obj.child('div'); $cdiv.addClass('list-' + i); $cdiv.text(data[i].name); if (data[i].value !== undefined) { $cdiv.addClass('bad'); }			}			return $cdiv; };		layoutArr = function (obj, data) { var $cdiv, i = 0, len = data.length; for (i; i < len; i += 1) { $cdiv = obj.child('div'); $cdiv.addClass('list-' + i); $cdiv.text(data[i]); }			return $cdiv; };		layoutStr = function (obj, data) { obj.text(data); };		layout = function (base, data, param) { var obj = base.child('div'), isArray = (typeof data[param] === 'object'), arrObj = (isArray ? (typeof data[param][0] === 'object') : false), func; base.addClass('p-' + param); obj.addClass(param); func = (isArray ? (arrObj ? layoutArrObj : layoutArr) : layoutStr); obj = func(obj, data[param]); return obj; };		(function init {			var i, len, elements = {}, group;			for (group in settings.groups) {				if (settings.groups.hasOwnProperty(group)) {					if (elements[group] === undefined) {						elements[group] = container.child('div');					}					i = 0;					len = settings.groups[group].length;					for (i; i < len; i += 1) {						if (data[settings.groups[group][i]] !== undefined) {							layout(elements[group], data, settings.groups[group][i]);						}					}				}			}		}); };	if (init !== false) { extension.load; } });