MediaWiki:Template:MainPageInfopush.js: Difference between revisions

From VRChat Wiki
No edit summary
No edit summary
Line 3: Line 3:
const visibleContentIds = [];
const visibleContentIds = [];
const visibleContent = new Map();
const visibleContent = new Map();
 
for (const infopush of document.querySelectorAll(".tpl-infopush")) {
for (const infopush of document.querySelectorAll(".tpl-infopush")) {
const entries = Object.entries(JSON.parse(atob(infopush.dataset.entries)));
try {
const content = infopush.querySelector(".tpl-infopush-content");
const entries = Object.entries(
JSON.parse(atob(infopush.dataset.entries))
);
const content = infopush.querySelector(".tpl-infopush-content");
 
entries.map(([id, value], index) => {
const { image: imageUrl, url } = value;
 
const image = document.createElement("img");
 
image.dataset.id = id;
image.src = imageUrl;
image.style.width = "100%";
image.style.flexShrink = "0";
 
content.append(image);
 
visibleContentIds.push(id);
visibleContent.set(id, value);
});


entries.map(([id, value], index) => {
const navigation = infopush.querySelector(".tpl-infopush-navigation");
const { image: imageUrl, url } = value;
const image = document.createElement("img");
image.dataset.id = id;
image.src = imageUrl;
image.style.width = "100%";
image.style.flexShrink = "0";


content.append(image);
const action = navigation.querySelector(".tpl-infopush-action");
visibleContentIds.push(id);
visibleContent.set(id, value);
});
const navigation = infopush.querySelector(".tpl-infopush-navigation");
const action = navigation
.querySelector(".tpl-infopush-action")


let currentOffset = 0;
let currentOffset = 0;
function showSlide(index) {
function showSlide(index) {
if (index < 0) {
if (index < 0) {
currentOffset = content.children.length - 1;
currentOffset = content.children.length - 1;
} else if (index >= content.children.length) {
} else if (index >= content.children.length) {
currentOffset = 0;
currentOffset = 0;
}
}
 
content.style.transform = `translateX(-${currentOffset * 100}%)`;
action.classList.add("tpl-infopush-action-none");


content.style.transform = `translateX(-${currentOffset * 100}%)`;
const { url } = visibleContent.get(visibleContentIds[currentOffset]);
action.classList.add("tpl-infopush-action-none");
if (url) {
action.classList.remove("tpl-infopush-action-none");
const { url } = visibleContent.get(visibleContentIds[currentOffset]);
}
if (url) {
action.classList.remove("tpl-infopush-action-none");
}
}
}


const autoAdvanceEvery = 5000;
const autoAdvanceEvery = 5000;


let autoAdvanceInterval;
let autoAdvanceInterval;
function resetAutoAdvance() {
function resetAutoAdvance() {
if (autoAdvanceInterval) clearInterval(autoAdvanceInterval);
if (autoAdvanceInterval) clearInterval(autoAdvanceInterval);
autoAdvanceInterval = setInterval(function () {
autoAdvanceInterval = setInterval(function () {
showSlide(currentOffset++);
showSlide(currentOffset++);
}, autoAdvanceEvery);
}, autoAdvanceEvery);
}
}
action.addEventListener("click", () => {
const { url } = visibleContent.get(visibleContentIds[currentOffset]);
if (url) open(url)
});


navigation
action.addEventListener("click", () => {
.querySelector(".tpl-infopush-navigation-previous")
const { url } = visibleContent.get(visibleContentIds[currentOffset]);
.addEventListener("click", () => {
if (url) open(url);
showSlide(currentOffset--);
resetAutoAdvance();
});
});


navigation
navigation
.querySelector(".tpl-infopush-navigation-next")
.querySelector(".tpl-infopush-navigation-previous")
.addEventListener("click", () => {
.addEventListener("click", () => {
showSlide(currentOffset++);
showSlide(currentOffset--);
resetAutoAdvance();
resetAutoAdvance();
});
});
 
navigation
.querySelector(".tpl-infopush-navigation-next")
.addEventListener("click", () => {
showSlide(currentOffset++);
resetAutoAdvance();
});


resetAutoAdvance();
resetAutoAdvance();
showSlide(0)
showSlide(0);
} catch (reason) {
console.warn(
"Template:MainPageInfopush",
"Couldn't initialize infopush.",
reason
);
}
}
}
})()
})()
);
);

Revision as of 10:11, 29 July 2024

$(
	(function () {
		const visibleContentIds = [];
		const visibleContent = new Map();

		for (const infopush of document.querySelectorAll(".tpl-infopush")) {
			try {
				const entries = Object.entries(
					JSON.parse(atob(infopush.dataset.entries))
				);
				const content = infopush.querySelector(".tpl-infopush-content");

				entries.map(([id, value], index) => {
					const { image: imageUrl, url } = value;

					const image = document.createElement("img");

					image.dataset.id = id;
					image.src = imageUrl;
					image.style.width = "100%";
					image.style.flexShrink = "0";

					content.append(image);

					visibleContentIds.push(id);
					visibleContent.set(id, value);
				});

				const navigation = infopush.querySelector(".tpl-infopush-navigation");

				const action = navigation.querySelector(".tpl-infopush-action");

				let currentOffset = 0;
				function showSlide(index) {
					if (index < 0) {
						currentOffset = content.children.length - 1;
					} else if (index >= content.children.length) {
						currentOffset = 0;
					}

					content.style.transform = `translateX(-${currentOffset * 100}%)`;
					action.classList.add("tpl-infopush-action-none");

					const { url } = visibleContent.get(visibleContentIds[currentOffset]);
					if (url) {
						action.classList.remove("tpl-infopush-action-none");
					}
				}

				const autoAdvanceEvery = 5000;

				let autoAdvanceInterval;
				function resetAutoAdvance() {
					if (autoAdvanceInterval) clearInterval(autoAdvanceInterval);
					autoAdvanceInterval = setInterval(function () {
						showSlide(currentOffset++);
					}, autoAdvanceEvery);
				}

				action.addEventListener("click", () => {
					const { url } = visibleContent.get(visibleContentIds[currentOffset]);
					if (url) open(url);
				});

				navigation
					.querySelector(".tpl-infopush-navigation-previous")
					.addEventListener("click", () => {
						showSlide(currentOffset--);
						resetAutoAdvance();
					});

				navigation
					.querySelector(".tpl-infopush-navigation-next")
					.addEventListener("click", () => {
						showSlide(currentOffset++);
						resetAutoAdvance();
					});

				resetAutoAdvance();
				showSlide(0);
			} catch (reason) {
				console.warn(
					"Template:MainPageInfopush",
					"Couldn't initialize infopush.",
					reason
				);
			}
		}
	})()
);