Benutzer:Shadowtweaker/AtkTable.js: Unterschied zwischen den Versionen

Aus PokéWiki
Zur Navigation springen Zur Suche springen
KKeine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
/* toggler function */
/* toggler function */
function at_toggle(gen) {
function at_toggle(gen) {
at_tmvmsortvalues(gen);
at_sortvalues(gen);
/* general toggling of generation-specific data */
/* general toggling of generation-specific data */
$("*[data-gen]").each(function() {
$("*[data-gen]").each(function() {
if (this.dataset.gen == "-0") this.style.display = (gen == 0 ? "none" : "");
$(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");
else if (gen != 0 && this.dataset.gen.substring(0, 1) == "+") this.style.display = "none";
else if (gen != 0 && Math.abs(this.dataset.gen) != gen) this.style.display = "none";
else if (gen == 0 && this.dataset.gen < 0) this.style.display = "none";
else this.style.display = "";
});
});
/* toggle rows that are irrelevant for this generation */
/* toggle rows that are irrelevant for this generation */
$(".at tbody tr:not(.expand-child)").each(function() {
$(".at:not(.at_ew) tbody tr:not(.expand-child)").each(function() {
$(this)[0].style.display = "";
$(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");
if (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))
$(this)[0].style.display = "none";
});
});
/* hide table if there are no relevant rows */
/* hide table if there are no relevant rows */
Zeile 56: Zeile 50:
/* adjust breeding header and footer */
/* adjust breeding header and footer */
$(".at_z").each(function() {
$(".at_z").each(function() {
var split = gen != 0 && $(this).find("thead tr:nth-child(2) th").toArray().filter(x => x.dataset.gen == "+" + gen)[0].colSpan > 1;
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-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(7)").toggleClass("round-topleft", gen != 0 && !split);
Zeile 67: Zeile 61:
$(".at_a").each(function() {
$(".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);
$(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;
}
    }
});
});
Zeile 75: Zeile 84:
}
}


/* tmvm sorting */
function at_loadparents() {
function at_tmvmsortvalues(gen) {
namenr = {};
$(".at_t tbody td:nth-last-child(7)").each(function() {
$.get("/Vorlage:Namenr?action=raw", function(data) {
value = $(this).find("> span").toArray().filter(x => x.dataset.gen == (gen == 0 ? "7" : gen) || x.dataset.gen == "-" + gen).map(x => x.innerText)[0];
data.split("\n").slice(1, data.split("\n").length - 1).forEach(x => namenr[x.split("=")[0]] = x.split("=")[1]);
if (value == undefined && gen == 0)
$(".at_parents").each(function() {
value = $(this).find("> span").toArray().filter(x => x.dataset.gen == 0).map(x => x.innerText)[0];
if (this.innerHTML != "") {
this.dataset.sortValue = (value == undefined ? "ZZ" : value.replace("(", "Z").replace("TM100", "TM999").split("*")[0]);
d = this.dataset;
if (Object.keys(this).length > 0) this[Object.keys(this)[0]].sortValue = (value == undefined ? "ZZ" : value.replace("(", "Z").replace("TM100", "TM999").split("*")[0]); // jQuery-sortValue-Attribute manipulieren
            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;
}
}


Zeile 89: Zeile 106:
mw.loader.using('jquery.tablesorter', function() {
mw.loader.using('jquery.tablesorter', function() {
/* initialize generation labels and level sorting across generations */
/* initialize generation labels and level sorting across generations */
$(".at").each(function() {
$(".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(",");
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;
shift = $(this)[0].classList.contains("at_l") ? 1 : 0;
Zeile 114: Zeile 131:
var toggle = document.createElement("a");
var toggle = document.createElement("a");
toggle.className = 'at_togglerlink';
toggle.className = 'at_togglerlink';
toggle.setAttribute('href', 'javascript:at_toggle("' + object.getAttribute("data-for") + '");');
toggle.setAttribute('href', 'javascript:at_toggle(' + object.getAttribute("data-for") + ');');
var child = object.firstChild;
var child = object.firstChild;
object.removeChild(child);
object.removeChild(child);
Zeile 121: Zeile 138:
});
});
at_tmvmsortvalues(0);
at_sortvalues(0);
at_parentsloaded = false;
});
});
});
});

Version vom 4. April 2018, 22:25 Uhr

/* 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;
	});
});