MediaWiki:Gadget-UP-Voting.js

// <nowiki>
mw.loader.using(['mediawiki.api', 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']).then(function () {
	const api = new mw.Api();
	const page = mw.config.get("wgPageName");
	const user = mw.config.get("wgUserName");
	if (!user) return;
	const windowManager = new OO.ui.WindowManager();
	$(document.body).append(windowManager.$element);
	async function closed() {
		const res = await api.get({
			action: "parse",
			page: page,
			prop: "wikitext"
		});
		const text = res.parse.wikitext["*"];
		const regex = /\{\{\s*atop\b/i;
		return regex.test(text);
	}
	async function alreadyVoted() {
		const res = await api.get({
			action: "parse",
			page: page,
			prop: "wikitext"
		});
		const text = res.parse.wikitext["*"];
		const safeUser = user.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
		const regex = new RegExp(
			`\\{\\{\\s*v?(hapus|simpan|gabung|pisah)\\b[^}]*\\}\\}[^\\n]*${safeUser}`,
			'i'
		);
		return regex.test(text);
	}
	function createButton() {
		const btn = new OO.ui.ButtonWidget({
			label: "Berikan suara (Usulan Penghapusan)",
			flags: ["primary", "progressive"]
		});
		const wrap = $("<div>")
			.css({
				textAlign: "center",
				margin: "20px 0"
			})
			.append(btn.$element);
		$(".mw-body-content").prepend(wrap);
		btn.on("click", openDialog);
	}
	function openDialog() {
		function Dialog(config) {
			Dialog.super.call(this, config);
		}
		OO.inheritClass(Dialog, OO.ui.ProcessDialog);
		Dialog.static.name = "afdDialog";
		Dialog.static.title = "Usulan Penghapusan";
		Dialog.static.size = "medium";
		Dialog.static.actions = [
			{ action: "submit", label: "Kirim", flags: ["primary", "progressive"] },
			{ action: "cancel", label: "Batal", flags: "safe" }
		];
		Dialog.prototype.initialize = function () {
			Dialog.super.prototype.initialize.call(this);
			this.vote = new OO.ui.RadioSelectWidget({
				items: [
					new OO.ui.RadioOptionWidget({ data: "hapus", label: "Hapus" }),
					new OO.ui.RadioOptionWidget({ data: "simpan", label: "Simpan" }),
					new OO.ui.RadioOptionWidget({ data: "gabung", label: "Gabung" }),
					new OO.ui.RadioOptionWidget({ data: "pisah", label: "Pisah" })
				]
			});
			this.vote.selectItemByData("hapus");
			this.comment = new OO.ui.MultilineTextInputWidget({
				placeholder: "Komentar (opsional)",
				rows: 3
			});
			this.panel = new OO.ui.PanelLayout({
				padded: true,
				expanded: false
			});
			this.panel.$element.append(
				this.vote.$element,
				$("<br>"),
				this.comment.$element
			);
			this.$body.append(this.panel.$element);
		};
		Dialog.prototype.getActionProcess = function (action) {
			if (action === "cancel") {
				return new OO.ui.Process(() => this.close());
			}
			if (action === "submit") {
				return new OO.ui.Process(async () => {
					const selected = this.vote.findSelectedItem();
					const vote = selected ? selected.getData() : null;
					const comment = this.comment.getValue();
					if (!vote) {
						mw.notify("Pilih opsi dulu");
						return;
					}
					if (await alreadyVoted()) {
						mw.notify("⚠️ Anda sudah memberikan suara");
						return;
					}
					await submitVote(vote, comment);
					this.close();
					mw.notify("🟢 Suara berhasil ditambahkan");
				});
			}
			return Dialog.super.prototype.getActionProcess.call(this, action);
		};
		const dialog = new Dialog();
		windowManager.addWindows([dialog]);
		windowManager.openWindow(dialog);
	}
	async function submitVote(type, comment) {
		let template = "";
		let label = "";
		if (type === "hapus") {
			template = "{{VHapus}}";
			label = "Hapus";
		}
		if (type === "simpan") {
			template = "{{VSimpan}}";
			label = "Simpan";
		}
		if (type === "gabung") {
			template = "{{VGabung}}";
			label = "Gabung";
		}
		if (type === "pisah") {
			template = "{{VPisah}}";
			label = "Pisah";
		}
		let text = "\n* " + template + ". ";
		if (comment) {
			text += comment + " ";
		}
		text += "~~~~\n";
		await api.postWithToken("csrf", {
			action: "edit",
			title: page,
			appendtext: text,
			summary: "Memberikan suara (" + label + ")",
			minor: true
		});
	}
	(async function () {
		if (!page.startsWith("Wikipedia:Usulan_penghapusan/")) return;
		if ((await closed())) return;
		if (await alreadyVoted()) {
			$(".mw-body-content").prepend(
				$("<div>")
					.css({
						textAlign: "center",
						margin: "20px",
						fontWeight: "bold"
					})
					.text("🗳️ Anda sudah memberikan suara")
			);
			return;
		}
		createButton();
	})();
});
// </nowiki>

Konten ini disalin dari wikipedia, mohon digunakan dengan bijak.

×
Advertisement