MediaWiki:Template:MainPageInfopush.js: Difference between revisions

From VRChat Wiki
No edit summary
No edit summary
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
$(
$(
(function () {
(function () {
const visibleContentIds = [];
mw.loader.using(["oojs-ui-core", "oojs-ui-windows"]).done(() => {
const visibleContent = new Map();
var windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);


for (const infopush of document.querySelectorAll(".tpl-infopush")) {
function showArticle(element) {
try {
var { articleTitle, articleContent } = element.dataset;
const entries = Object.entries(
JSON.parse(atob(infopush.dataset.entries))
);
const content = infopush.querySelector(".tpl-infopush-content");


entries.map(([id, value], index) => {
function ArticleDialog(config) {
const { image: imageUrl, url } = value;
ArticleDialog.super.call(this, config);
this.config = config || {};
}


const image = document.createElement("img");
OO.inheritClass(ArticleDialog, OO.ui.ProcessDialog);


image.dataset.id = id;
ArticleDialog.static.name = "ArticleDialog";
image.src = imageUrl;
ArticleDialog.static.title = articleTitle;
image.style.width = "100%";
ArticleDialog.static.size = "large";
image.style.flexShrink = "0";
ArticleDialog.static.actions = [
{
action: "close",
flags: "safe",
icon: "close",
label: "Close"
}
];


content.append(image);
ArticleDialog.prototype.initialize = function () {
ArticleDialog.super.prototype.initialize.apply(this, arguments);
this.content = new OO.ui.PanelLayout({
$content: atob(articleContent),
expanded: false,
padded: true,
scrollable: true
});


visibleContentIds.push(id);
this.$body.append(this.content.$element);
visibleContent.set(id, value);
};
});


const navigation = infopush.querySelector(".tpl-infopush-navigation");
ArticleDialog.prototype.getActionProcess = function (action) {
if (action === "close") {
return new OO.ui.Process(function () {
this.close({ action: "close" });
}, this);
}
return ArticleDialog.super.prototype.getActionProcess.call(
this,
action
);
};


const action = navigation.querySelector(".tpl-infopush-action");
var dialog = new ArticleDialog();


let currentOffset = 0;
windowManager.addWindows([dialog]);
function showSlide(index) {
windowManager.openWindow(dialog);
currentOffset = (index + visibleContentIds.length) % visibleContentIds.length;
}


content.style.transform = `translateX(-${currentOffset * 100}%)`;
const infopushes = document.querySelectorAll(".tpl-infopush");
action.classList.add("tpl-infopush-action-none");
console.log("Template:MainPageInfopush", infopushes);


const { url } = visibleContent.get(visibleContentIds[currentOffset]);
for (const infopush of infopushes) {
if (url) {
try {
action.classList.remove("tpl-infopush-action-none");
const content = infopush.querySelector(".tpl-infopush-content");
 
const items = content.querySelectorAll(".tpl-infopush-item");
const total = items.length;
 
console.log("Template:MainPageInfopush", infopush, items);
 
for (const item of items) {
const { articleId } = item.dataset;
if (!articleId) continue;
 
item.style.cursor = "pointer";
item.addEventListener("click", () => showArticle(item));
}
}
}


const autoAdvanceEvery = 5000;
const navigation = infopush.querySelector(".tpl-infopush-navigation");
 
let currentOffset = 0;
function showSlide(index) {
currentOffset = (index + total) % total;
content.style.transform = `translateX(-${currentOffset * 100}%)`;
}


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


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


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


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


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

Latest revision as of 01:02, 19 August 2024

$(
	(function () {
		mw.loader.using(["oojs-ui-core", "oojs-ui-windows"]).done(() => {
			var windowManager = new OO.ui.WindowManager();
			$(document.body).append(windowManager.$element);

			function showArticle(element) {
				var { articleTitle, articleContent } = element.dataset;

				function ArticleDialog(config) {
					ArticleDialog.super.call(this, config);
					this.config = config || {};
				}

				OO.inheritClass(ArticleDialog, OO.ui.ProcessDialog);

				ArticleDialog.static.name = "ArticleDialog";
				ArticleDialog.static.title = articleTitle;
				ArticleDialog.static.size = "large";
				ArticleDialog.static.actions = [
					{
						action: "close",
						flags: "safe",
						icon: "close",
						label: "Close"
					}
				];

				ArticleDialog.prototype.initialize = function () {
					ArticleDialog.super.prototype.initialize.apply(this, arguments);
					this.content = new OO.ui.PanelLayout({
						$content: atob(articleContent),
						expanded: false,
						padded: true,
						scrollable: true
					});

					this.$body.append(this.content.$element);
				};

				ArticleDialog.prototype.getActionProcess = function (action) {
					if (action === "close") {
						return new OO.ui.Process(function () {
							this.close({ action: "close" });
						}, this);
					}
					return ArticleDialog.super.prototype.getActionProcess.call(
						this,
						action
					);
				};

				var dialog = new ArticleDialog();

				windowManager.addWindows([dialog]);
				windowManager.openWindow(dialog);
			}

			const infopushes = document.querySelectorAll(".tpl-infopush");
			console.log("Template:MainPageInfopush", infopushes);

			for (const infopush of infopushes) {
				try {
					const content = infopush.querySelector(".tpl-infopush-content");

					const items = content.querySelectorAll(".tpl-infopush-item");
					const total = items.length;

					console.log("Template:MainPageInfopush", infopush, items);

					for (const item of items) {
						const { articleId } = item.dataset;
						if (!articleId) continue;

						item.style.cursor = "pointer";
						item.addEventListener("click", () => showArticle(item));
					}

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

					let currentOffset = 0;
					function showSlide(index) {
						currentOffset = (index + total) % total;
						content.style.transform = `translateX(-${currentOffset * 100}%)`;
					}

					const autoAdvanceEvery = 5000;

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

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

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

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