Sixtest Wiki:API4/Spell

(function {	"use strict";	var $extension = $API.create.extension("Spell"),		init;	$extension.loadData = function  {		var datafiles, datafileslen, loaderCallback, data = [], datafilesloaded = 0, i = 0;		datafiles = [			"/wiki/Sixtest_Wiki:API/Spell/Data1"		];		datafileslen = datafiles.length;		loaderCallback = function (i) {			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, { dataType: 'Spell' }));				}			};		};		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.spell = $API.create.list("spell", data);		return $extension.data;	};	$extension.layout = function (data, container) {		// TODO: cleanup ....		var $cont = container;		container.ele.className = 'SearchSuggest_Display' + ' ' + 'spell' + ' ' + ($API.user.edittools ? 'edittools-enabled' : 'edittools-disabled');		(function init { var groups, funcGeneric, property, i, len, objprop, base, elements = {}; // generic function for all params funcGeneric = function (base, data, param, func) { var obj = base.child('div'), isArray = (typeof data[param] === 'object'), arrayOfObjects = (isArray ? (typeof data[param][0] === 'object') : false), propLen = (isArray ? data[param].length : 0), $cdiv, $cdivi, i; base.ele.className += (base.ele.className ? ' ' : '') + 'p-' + param; obj.ele.className = param; //obj.ele.appendChild(document.createTextNode(data[param])); if (isArray) { if (arrayOfObjects) { // dealing with e.g. trades which isn't a simple array (can be [ {name:'Axe',value:'2'} ] for example) // make header text $cdiv = obj.child('div'); $cdiv.ele.className = 'header-' + param; $cdiv.ele.appendChild(document.createTextNode(param.charAt(0).toUpperCase + param.slice(1))); // iterate over the data i = 0; for (i; i < propLen; i += 1) { if (data[param][i].name) { $cdivi = $cdiv.child('div'); $cdivi.ele.className = param + '-' + i;								$cdivi.ele.appendChild(document.createTextNode(data[param][i].name)); if (data[param][i].value) { $cdivi.ele.className += ' bad'; }							}						}					} else { // dealing with single-level array like sounds: [ "A", "B", "C" ]; i = 0; for (i; i < propLen; i += 1) { $cdiv = obj.child('div'); $cdiv.ele.className = param + '-' + i;							$cdiv.ele.appendChild(document.createTextNode(data[param][i])); }					}				} else { // dealing with simple string (e.g. name, location) obj.ele.appendChild(document.createTextNode(data[param])); }				func = func || function (obj) { // main function here return obj; };				obj = func(obj); return obj; };			// list of grouped parameters if ($API.user && $API.user.ext && $API.user.ext.spell && $API.user.ext.spell.layout) { // allows for a customized grouping - just a test! groups = $API.user.ext.spell.layout; } else { cityExceptions = [ "k-abd" ]; groups = { "general": [ "name", "implemented" ], "data": [ "words", "mana", "explvl", "spellcost" ], "classification": [ "subclass", "damagetype" ], "additional": [ "premium", "voc", cityExceptions ] };			}			// 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); }							}						}					}				}			}		});	};	if (init !== false) {		$extension.loadData;	} });