Netzwerk

MediaWiki:AtkTable.js

Aus PokéWiki
Wechseln zu: Navigation, Suche
/* toggler function */
function at_toggle(gen) {
	at_sortvalues(gen);
	/* general toggling of generation-specific data */
	$("*[data-gen]").each(function() {
		$(this).css("display", (""+$(this).data("gen")).split(",").map(x => gen > 0 ? (x == "" + gen || x == -gen || x == "-0") : (x >= 0 && x != "-0")).reduce((acc, cv) => acc || cv, false) ? "" : "none");
	});
	/* toggle rows that are irrelevant for this generation */
	$(".at:not(.at_ew) tbody tr:not(.expand-child)").each(function() {
		$(this).css("display", gen == 0 || $(this).find("td").toArray().filter(x => x.dataset.gen == gen || x.dataset.gen == "+" + gen).map(x => x.innerText.trim() != "—").reduce((acc, cv) => acc || cv, false) ? "" : "none");
	});
	/* hide table if there are no relevant rows */
	$(".at").each(function() {
		var empty = $(this).find("tbody tr:not(.expand-child):not([style*='display'])").length == 0;
		$(this).css("display", empty ? "none": "");
		$(this).next().css("display", empty ? "" : "none");
	});
	/* toggle columns in tmvm and event tables */
	$(".at_t tbody td[data-gen], .at_ev tbody td[data-gen]").each(function() {
		$(this)[0].style.display = (gen == 0 ? "" : "none");
	});
	/* adjust level header */
	$(".at_l").each(function() {
		id = $(this).find("thead tr:nth-child(1) th:nth-child(2)")[0];
		level = $(this).find("thead tr:nth-child(1) th:nth-child(3)")[0];
		if (gen == 0) {
			id.colSpan = level.colSpan;
			$(this).find("thead tr:nth-child(1) th").toArray().slice(3).forEach(x => x.rowSpan = $(this).find("thead tr:nth-child(1) th:nth-child(1)")[0].rowSpan);
		}
		else {
			id.colSpan = $(this).find("thead tr:nth-child(2) th").toArray().filter(x => x.dataset.gen == gen)[0].colSpan;
			$(this).find("thead tr:nth-child(1) th").toArray().slice(3).forEach(x => x.rowSpan = $(this).find("thead tr:nth-child(2) th").toArray().filter(x => x.dataset.gen == gen)[0].colSpan);
		}
		$(this).find("thead tr:nth-child(2)")[0].style.display = (gen == 0 ? "" : "none");
		if ($(this).find("thead tr:nth-child(3)")[0]) {
			$(this).find("thead tr:nth-child(3)")[0].style.display = (gen == 0 ? "" : ($(this).find("thead tr:nth-child(2) th").toArray().filter(x => x.dataset.gen == gen)[0].rowSpan > 1 ? "none" : ""));
		}
		$(this).find("tfoot th")[0].colSpan = id.colSpan + 6;
	});
	/* adjust tmvm and event header */
	$(".at_t thead tr:nth-child(1) th:nth-last-child(7), .at_ev thead tr:nth-child(1) th:nth-last-child(6)").toggleClass("round-topleft", gen != 0);
	/* adjust tmvm footer */
	$(".at_t").each(function() {
		$(this).find("tfoot th")[0].colSpan = 7 + (gen == 0 ? $(this).find("thead tr:nth-child(1) th:nth-child(1)")[0].colSpan : 0);
	});
	/* adjust event footer */
	$(".at_ev").each(function() {
		$(this).find("tfoot th")[0].colSpan = 6 + (gen == 0 ? $(this).find("thead tr:nth-child(1) th:nth-child(1)")[0].colSpan : 0);
	});
	/* adjust breeding header and footer */
	$(".at_z").each(function() {
		var split = gen != 0 && $(this).find("thead tr:nth-child(2) th").toArray().filter(x => x.dataset.gen == "+" + gen).map(x => x.colSpan)[0] > 1;
		$(this).find("thead tr:nth-child(1) th:nth-child(2)")[0].style.display = (split ? "": "none");
		$(this).find("thead tr:nth-child(1) th:nth-last-child(7)").toggleClass("round-topleft", gen != 0 && !split);
		$(this).find("thead tr:nth-child(1) th:nth-last-child(2)").toggleClass("round-topright", gen == 0);
		$(this).find("tfoot th")[0].colSpan = (gen == 0 ? 6 + $(this).find("thead tr:nth-child(1) th:nth-child(1)")[0].colSpan : (split ? 9 : 7));
		$(this).find("tr.expand-child td").attr('colSpan', split ? 9 : 7);
	});
	$(".at_z").toggleClass("at_z_all", gen == 0);
	/* adjust tutor footer */
	$(".at_a").each(function() {
		$(this).find("tfoot th")[0].colSpan = 6 + (gen == 0 ? $(this).find("thead tr:nth-child(1) th:nth-child(1)")[0].colSpan : 1);
	});
	if (!at_parentsloaded) at_loadparents();
}

/* flexible sort values */
function at_sortvalues(gen) {
	$(".at tbody td").each(function() {
		if ($(this).find("> span[data-gen]").length > 0) {
			value = $(this).find("> span").toArray().filter(x => ("" + x.dataset.gen).split(",").map(x => Math.abs(x)).indexOf(gen) > -1).map(x => x.innerText)[0];
			if (value) {
				value = value.replace("(", "Z").replace("TM100", "TM999");
				this.dataset.sortValue = value;
				if (Object.keys(this).length > 0) this[Object.keys(this)[0]].sortValue = value;
			}
	    }
	});
	
	/* table resorting */
	$(".at").each(function() {
		$(this).data('tablesorter').sort($(this)[0][Object.keys($(this)[0]).filter(x => x.startsWith("jQuery"))[0]].tablesorter.config.sortList.map(function(x) { a = {}; a[x[0]] = (x[1] == 0 ? 'asc' : 'desc'); return a; }));
	});
}

function at_loadparents() {
	namenr = {};
	$.get("/Vorlage:Namenr?action=raw", function(data) {
		data.split("\n").slice(1, data.split("\n").length - 1).forEach(x => namenr[x.split("=")[0]] = x.split("=")[1]);
		$(".at_parents").each(function() {
			if (this.innerHTML != "") {
				d = this.dataset;
	            this.innerHTML = this.innerHTML.split(", ").map(function(x) {
	                chain = x.indexOf("*") > -1;
	                transfer = x.indexOf("°") > -1;
	                x = x.replace("°", "").replace("*", "");
                    return "<div style='background:#" + (chain ? (transfer ? d.cu : d.c) : (transfer ? d.u : "fff")) + ";width:30px;height:30px;line-height:30px;border-radius:15px'><a href='/" + x + "' title='" + x + "'><img src='https://greenchu.de/sprites/icons/" + namenr[x] + ".png' /></a></div>";
            	}).join("");
            }
        });
    });
    at_parentsloaded = true;
}

jQuery(document).ready(function($) {
	mw.loader.using('jquery.tablesorter', function() {
		/* initialize generation labels and level sorting across generations */
		$(".at:not(.at_ew)").each(function() {
			coltogen = $(this).find("thead tr:nth-child(2) th").toArray().map((x, i) => [Math.abs(x.dataset.gen), Math.abs(x.dataset.gen)].slice(0, x.colSpan).join(",")).join(",").split(",");
			shift = $(this)[0].classList.contains("at_l") ? 1 : 0;
			if ($(this)[0].classList.contains("at_z")) coltogen = coltogen.map(x => (coltogen.filter(y => y == x).length == 1 ? "+" : "") + x);
			$(this).find("tbody tr:not(.expand-child)").each(function() {
				$(this).find("td").toArray().slice(shift, coltogen.length+shift).forEach((x, i) => x.dataset.gen = coltogen[i]);
			});
		});
		$(".at_l tbody tr td:nth-child(1)").each((i, x) => x.innerHTML = "" + i);
		$(".at_l").each(function() {
			id = $(this).find("thead tr:nth-child(1) th:nth-child(1)")[0];
			level = $(this).find("thead tr:nth-child(1) th:nth-child(2)")[0];
			id2 = document.createElement("th");
			id2.rowSpan = id.rowSpan;
			$(this).find("thead tr:nth-child(1)")[0].insertBefore(id2, id);
			id.colSpan = level.colSpan;
			id.rowSpan = level.rowSpan;
			id.style.display = "";
			level.style.display = "none";
		});
		
		/* initialize toggle buttons */
		$(".at_toggle").each(function(index, object) {
			var toggle = document.createElement("a");
			toggle.className = 'at_togglerlink';
			toggle.setAttribute('href', 'javascript:at_toggle(' + object.getAttribute("data-for") + ');');
			var child = object.firstChild;
			object.removeChild(child);
			toggle.appendChild(child);
			object.insertBefore(toggle, object.firstChild);
		});
		
		at_sortvalues(0);
		at_parentsloaded = false;
	});
});