MediaWiki:Template:MainPageInfopush.js: Difference between revisions

From VRChat Wiki
No edit summary
m (Removed protection from "MediaWiki:Template:MainPageInfopush.js": No longer needed)
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
$(
$(
(function () {
(function () {
const visibleContentIds = [];
mw.loader.using(["oojs-ui-core", "oojs-ui-windows"]).done(() => {
const visibleContent = new Map();
const [pageName, pageLanguage] = mw.config.values.wgPageName.split("/");
for (const infopush of document.querySelectorAll(".tpl-infopush")) {
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");
// Restrict component rendering to the Main Page (or translated versions of itself!)
// as we'll run arbitrary code based on the content.
if (pageName !== "Main_Page" && (!pageLanguage || mw.config.values.wgPageContentLanguage != pageLanguage)) return;
const action = navigation
var windowManager = new OO.ui.WindowManager();
.querySelector(".tpl-infopush-action")
$(document.body).append(windowManager.$element);
 
function showArticle(element) {
var { articleTitle, articleContent } = element.dataset;


let currentOffset = 0;
function ArticleDialog(config) {
function showSlide(index) {
ArticleDialog.super.call(this, config);
if (index < 0) {
this.config = config || {};
currentOffset = content.children.length - 1;
} else if (index >= content.children.length) {
currentOffset = 0;
}
}


content.style.transform = `translateX(-${currentOffset * 100}%)`;
OO.inheritClass(ArticleDialog, OO.ui.ProcessDialog);
action.classList.add("tpl-infopush-action-none");
 
ArticleDialog.static.name = "ArticleDialog";
const { url } = visibleContent.get(visibleContentIds[currentOffset]);
ArticleDialog.static.title = articleTitle;
if (url) {
ArticleDialog.static.size = "large";
action.classList.remove("tpl-infopush-action-none");
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 autoAdvanceEvery = 5000;
const infopushes = document.querySelectorAll(".tpl-infopush");
console.log("Template:MainPageInfopush", infopushes);
 
for (const infopush of infopushes) {
try {
const content = infopush.querySelector(".tpl-infopush-content");


let autoAdvanceInterval;
const items = content.querySelectorAll(".tpl-infopush-item");
function resetAutoAdvance() {
const total = items.length;
if (autoAdvanceInterval) clearInterval(autoAdvanceInterval);
 
autoAdvanceInterval = setInterval(function () {
console.log("Template:MainPageInfopush", infopush, items);
showSlide(currentOffset++);
 
}, autoAdvanceEvery);
for (const item of items) {
}
const { articleId } = item.dataset;
// If we've given this item an link override, but it previously had an article, we'll ignore it.
action.addEventListener("click", () => {
if (!articleId || !!item.querySelector("a")) continue;
const { url } = visibleContent.get(visibleContentIds[currentOffset]);
 
if (url) open(url)
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
navigation
.querySelector(".tpl-infopush-navigation-previous")
.querySelector(".tpl-infopush-navigation-next")
.addEventListener("click", () => {
.addEventListener("click", () => {
showSlide(currentOffset--);
showSlide(currentOffset + 1);
resetAutoAdvance();
resetAutoAdvance();
});
});


navigation
.querySelector(".tpl-infopush-navigation-next")
.addEventListener("click", () => {
showSlide(currentOffset++);
resetAutoAdvance();
resetAutoAdvance();
});
showSlide(0);
 
} catch (reason) {
resetAutoAdvance();
console.warn(
showSlide(0)
"Template:MainPageInfopush",
}
"Couldn't initialize infopush.",
reason
);
}
}
});
})()
})()
);
);

Latest revision as of 11:48, 1 March 2025

$(
	(function () {
		mw.loader.using(["oojs-ui-core", "oojs-ui-windows"]).done(() => {
			const [pageName, pageLanguage] = mw.config.values.wgPageName.split("/");
			
			// Restrict component rendering to the Main Page (or translated versions of itself!)
			// as we'll run arbitrary code based on the content.
			if (pageName !== "Main_Page" && (!pageLanguage || mw.config.values.wgPageContentLanguage != pageLanguage)) return;
			
			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 we've given this item an link override, but it previously had an article, we'll ignore it.
						if (!articleId || !!item.querySelector("a")) 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
					);
				}
			}
		});
	})()
);