MediaWiki:Gadget-QuickAccept.js

Nga Wikipedia, enciklopedia e lirë

Shënim: Pas ruajtjes së parapëlqimeve ose kryerjes së ndryshimet, për t'ju shfaqur dallimet duhet të pastroni ruajtësin (cache) e shfletuesit. Pastrimi i ruajtësit të shfletuesve bëhet duke shtypur në të njëjten kohë Ctrl+Shift+Reload për Mozilla/Safari/Konqueror ose ctrl+shift+r për IE ose Ctrl+f5 për Opera: F5.

// Author: Nardog
(mw.config.get('wgNamespaceNumber') === -1 ||
mw.config.get('wgAction') === 'history') &&
mw.loader.using(['mediawiki.api', 'mediawiki.util'], function quickAccept() {
	[
		['accept', 'prano'],
		['unaccept', 'rikthe pranimin'],
		['accepting', 'duke pranuar'],
		['unaccepting', 'duke rikthyer pranimin'],
		['accept-tooltip', 'Prano këtë ndryshim në pritje'],
		['unaccept-tooltip', 'Vije këtë ndryshim në pritje sërish'],
		['error-accept', 'Ndryshimi nuk mund të pranohej.'],
		['error-unaccept', 'Pranimi nuk mund të rikthehej.'],
		['error-get-id', 'Nuk mund të gjendej ID e ndryshimit.'],
		['error-find-id', 'Nuk mund të gjendet ID e ndryshimit.'],
		['error-find-title', 'Nuk mund të gjendet titulli i faqes.'],
	].forEach(function (entry) {
		var key = 'quickaccept-' + entry[0];
		if (!mw.messages.exists(key)) mw.messages.set(key, entry[1]);
	});
	var isPc = ['PendingChanges', 'UnreviewedPages'].indexOf(mw.config.get('wgCanonicalSpecialPageName')) !== -1;
	function handler(e) {
		e.preventDefault();
		var $link = $(this);
		var $wrapper = $link.parent();
		var isUndo = $wrapper.hasClass('quickaccept-undo');
		var id = $wrapper.data('quickacceptId') ||
			!isPc && $wrapper.closest('[data-mw-revid]').data('mwRevid');
		if (id) {
			$link.detach();
			$wrapper.addClass('quickaccept-doing')
				.text(mw.msg('quickaccept-' + (isUndo ? 'unaccepting' : 'accepting')));
			accept(id, isUndo, $link, $wrapper);
		} else if (isPc) {
			var title;
			$wrapper.parent().prevAll('a').each(function () {
				if (mw.util.getParamValue('action', this.search) === 'history') {
					title = mw.util.getParamValue('title', this.search);
					return false;
				}
			});
			if (!title) {
				showError('find-title');
				return;
			}
			$link.detach();
			$wrapper.addClass('quickaccept-doing')
				.text(mw.msg('quickaccept-' + (isUndo ? 'unaccepting' : 'accepting')));
			new mw.Api().get({
				action: 'query',
				titles: title,
				prop: 'info',
				formatversion: 2
			}).always(function (response) {
				id = ((((response || {}).query || {}).pages || [])[0] || {}).lastrevid;
				if (id) {
					accept(id, isUndo, $link, $wrapper);
				} else {
					showError('get-id');
					$wrapper.removeClass('quickaccept-doing').html($link);
				}
			});
		} else {
			showError('find-id');
			return;
		}
		mw.requestIdleCallback(function () {
			var notif = $('.mw-notification-tag-quickaccept').data('mw-notification');
			if (notif) notif.close();
		});
	}
	function accept(id, isUndo, $link, $wrapper) {
		new mw.Api().postWithEditToken({
			action: 'review',
			revid: id,
			unapprove: isUndo ? 1 : undefined,
			errorformat: 'html',
			formatversion: 2
		}).always(function (response, error) {
			if (((response || {}).review || {}).result === 'Success') {
				$link.text(mw.msg('quickaccept-' + (isUndo ? 'accept' : 'unaccept')))
					.attr('title', mw.msg(
						'quickaccept-' + (isUndo ? 'accept' : 'unaccept') +
						'-tooltip'
					));
				$wrapper.toggleClass('quickaccept-undo', !isUndo)
					.attr('data-quickaccept-id', id);
			} else {
				var msg = (((error || {}).errors || [])[0] || {}).html;
				showError(isUndo ? 'unaccept' : 'accept', msg && $.parseHTML(msg));
			}
			$wrapper.removeClass('quickaccept-doing').html($link);
		});
	}
	function showError(key, msg) {
		mw.notify(msg || mw.msg('quickaccept-error-' + key), {
			tag: 'quickaccept',
			type: 'error'
		});
	}
	mw.hook('wikipage.content').add(function ($content) {
		var $lis;
		if (isPc) {
			$lis = $content.find('form[name="pendingchanges"] ~ ul > li, form[name="unreviewedpages"] ~ ul > li');
		} else if (mw.config.get('wgAction') === 'history') {
			$lis = $content.find('.flaggedrevs-pending');
		} else {
			$lis = $content.find('.mw-contributions-list > .flaggedrevs-pending, .mw-contributions-list > .flaggedrevs-unreviewed');
			if (!$lis.length) {
				$lis = $content.find('.mw-changeslist-edit.mw-changeslist-last .mw-changeslist-line-inner[data-target-page]')
					.has('.mw-fr-reviewlink');
				$lis = $lis.add(
					$content.find('table.mw-changeslist-edit').has('.mw-fr-reviewlink')
						.find('.mw-changeslist-last > .mw-enhanced-rc-nested[data-target-page]')
				);
			}
		}
		if (!$lis.length) return;
		var $tools = $lis.children('.mw-changeslist-links.mw-pager-tools');
		$tools = $tools.add(
			$lis.not($tools.parent())
				.append(' ', $('<span>').addClass('mw-changeslist-links mw-pager-tools'))
				.children('.mw-changeslist-links.mw-pager-tools')
		);
		$('<span>').addClass('quickaccept').append(
			$('<a>').attr({
				href: '#',
				role: 'button',
				title: mw.msg('quickaccept-accept-tooltip')
			}).text(mw.msg('quickaccept-accept')).click(handler)
		).appendTo($tools);
	});
});