Użytkownik:Expert3222/quickPatrol.js: Różnice pomiędzy wersjami

Z Nondanych
Linia 106: Linia 106:
$patrolLink = $tbody.find(".patrollink-range a"),
$patrolLink = $tbody.find(".patrollink-range a"),
$buttons = $tbody.find(".patrollink-single"),
$buttons = $tbody.find(".patrollink-single"),
buttonsCount = $buttons.length,
$spinner = $.createSpinner({ size: 'small', type: 'inline' });
$spinner = $.createSpinner({ size: 'small', type: 'inline' });
if ($buttons.length === 0)
{
return $.when();
}
console.dir($patrolLink);
console.dir($patrolLink);

Wersja z 13:37, 20 maj 2020

/*
 * gadżet do szybkiego oznaczania wersji jako sprawdzonych na OZ, nowych stronach i widoku diffów
 * autor: Expert3222
 * drobne modyfikacje, backend: Ostrzyciel
 */

// TODO: $(this) na $this (do zmiennej), lepsza wydajność będzie

mw.loader.load('/wiki/Użytkownik:Expert3222/patrolModule.js?action=raw&ctype=text/javascript');

var spinnerCode = "<div class='mw-spinner mw-spinner-small mw-spinner-inline' title='...'></div>";

var RCPatrolSuccessCode = "<span style='color: green;' class='patrolButtonClicked'><img src='https://nonsa.pl/images/2/26/Za.svg.png' width='15px' height='15px' />&nbsp;zrobione</span>";
var RCPatrolFailureCode = "<span style='color: red;' class='patrolButtonClicked'><img src='https://nonsa.pl/images/a/a8/Przeciw.svg.png' width='15px' height='15px' />&nbsp;błąd</span>";

var RCMultiplePatrolSuccessCode = "<span style='color: green;' class='multiplePatrolButtonClicked'><img src='https://nonsa.pl/images/2/26/Za.svg.png' width='15px' height='15px' />&nbsp;zrobione</span>";
var RCMultiplePatrolFailureCode = "<span style='color: red;' class='multiplePatrolButtonClicked'><img src='https://nonsa.pl/images/a/a8/Przeciw.svg.png' width='15px' height='15px' />&nbsp;błąd</span>";

var debugActive = false; //zmieniane z pomocą konsoli

$(document).ready(function()
{
	mw.loader.using(["jquery.spinner", "mediawiki.notification"], function()
	{
		if (mw.config.get("wgCanonicalSpecialPageName") === "Recentchanges")
		{
			makePatrolButtons();
			if (!!$('.mw-rcfilters-enabled').length) //wzięte od Polskacafe z gadżetu RollWithReason
			{
				setInterval(makePatrolButtons, 2000);
			}
		}
		else if (mw.config.get("wgCanonicalSpecialPageName") == "Newpages")
		{
			var $buttons = $(".patrollink-page a");
			$buttons.click(function()
			{
				processPagePatrol($(this).parent());
			});
		}
		else if (mw.util.getParamValue("diff") !== null)
		{
			makeDiffPagePatrolButtons();
			
			setInterval(makeDiffPagePatrolButtons, 2000); //"przeglądaj historię interaktywnie" na górze
		}
		else if ($(".patrollink").length > 0) //footer
		{
			$(".patrollink-page a").off("click").click(function() //remove default click handler and attach our own
			{
				processPagePatrol($(this).parent());
			});
		}
		else if (mw.config.get("wgCanonicalSpecialPageName") == "MobileDiff")
		{
			//TODO
			
			/*var oldid = mw.util.getParamValue("oldid", $("#mw-mf-diff-info a").attr("href"));
			alert("oldid = " + oldid);
			var urlArray = window.location.href.split("/");
			var curid = urlArray[urlArray.length - 1];
			alert("curid = " + curid);
			
			var title = $("#mw-mf-diff-info a").text();
			makeDiffViewRevidsList(oldid, curid, title);*/
		}
	});
});

function makePatrolButtons()
{
	$(".patrollink-range a").each(function()
	{
		var $tbody = goUpUntilTag($(this), "tbody");
		var $link = $(this);
		var lines = $tbody.find("tr");
		var firstLine = $(lines).first();
		if ($(firstLine).find(".multiplePatrolButtonClicked").length === 0)
		{
			if (!isEventBound($link))
			{
				$link.on("click", function()
				{
					processMultipleChangeRCPatrol($tbody);
				});
			}
		}
	});

	var $button = $(".patrollink-single a");
	if (!isEventBound($button))
	{
		$button.on("click", function()
		{
			processRCChangePatrol($(this).parent());
		});
	}
}

//helper function to process multiple change RC patrol, including spinner etc.
function processMultipleChangeRCPatrol($tbody)
{
	var deferred = $.Deferred(),
		failureCount = 0,
		successCount = 0,
		$patrolLink = $tbody.find(".patrollink-range a"),
		$buttons = $tbody.find(".patrollink-single"),
		$spinner = $.createSpinner({ size: 'small', type: 'inline' });
	
	if ($buttons.length === 0)
	{
		return $.when();
	}
	
	console.dir($patrolLink);
	$patrolLink.hide();
	$patrolLink.after($spinner);
	$patrolLink.parent().find(".multiplePatrolButtonClicked").remove();
	
	$buttons.each(function()
	{
		processRCChangePatrol($(this))
		.done(function(data)
		{
			++successCount;
		}).fail(function()
		{
			++failureCount;
		}).always(function(data)
		{
			$spinner.remove();
			if (successCount == buttonsCount && !debugActive) //wszystkie wywołania AJAX do patrolowania zakończyły się pomyślnie
			{
				deferred.resolve(successCount, failureCount);
				
				$patrolLink.after(RCMultiplePatrolSuccessCode);
				$patrolLink.remove();
				$tbody.find("tr:first-child abbr.unpatrolled").replaceWith("&nbsp;");
				
				var title = new mw.Title(data["fmod-patrol"].title);
				mw.notify( mw.msg( 'markedaspatrollednotifymulti', title.toText() ) );
			}
			else if (successCount + failureCount == buttonsCount) //wszystkie zakończyły się, niekoniecznie pomyślnie
			{
				deferred.reject(successCount, failureCount);
				
				$patrolLink.after(RCMultiplePatrolFailureCode);
				$patrolLink.show();
				
				mw.notify( mw.msg( 'markedaspatrollederrornotify' ), { type: 'error' } );
			}
		});
	});
	
	/*if (buttonsCount == 0)
	{
		multiplePatrolButton.replaceWith("<span><img src='https://nonsa.pl/images/2/26/Za.svg.png' width='15px' height='15px' />&nbsp;<span style='color: green;' class='multiplePatrolButtonClicked'>zrobione</span></span>");
		$tbody.find("tr:first-child abbr.unpatrolled").replaceWith("&nbsp;");
	}*/
	
	return deferred.promise();
}

//helper function to process one RC link including spinner etc.
function processRCChangePatrol($button)
{
	//nazwa komunikatu z failem: Markedaspatrollederrornotifychange
	var deferred = $.Deferred();
	var revid = $button.find("a").attr("data-revision");
	$button.find("a").replaceWith(spinnerCode);
	patrolModule.resetLastClick();
	patrolModule.patrolChange(revid).done(function(data)
	{
		$button.find("div").replaceWith(RCPatrolSuccessCode);
		goUpUntilTag($button, "tr").find("abbr.unpatrolled").replaceWith("&nbsp;");
		deferred.resolve(data);
	}).fail(function()
	{
		$button.find("div").replaceWith(RCPatrolFailureCode);
		deferred.reject();
	});
	return deferred.promise();
}

function processPagePatrol($button)
{
	var pageid = $button.find("a").attr("data-pageid");
	$button.find("a").replaceWith(spinnerCode);
	
	patrolModule.resetLastClick();
	patrolModule.patrolPage(pageid).done(function()
	{
		$button.find("div").replaceWith(RCMultiplePatrolSuccessCode);
	}).fail(function(error)
	{
		console.log(error);
		$button.find("div").replaceWith(RCMultipleFailureSuccessCode);
	});
}

function makeDiffPagePatrolButtons()
{	
	var $button = $(".patrollink-range a");
	if (!isEventBound($button))
	{
		$button.click(function()
		{
			processDiffPageRangePatrol();
		});
	}
	
	$button = $(".patrollink-page a");
	if (!isEventBound($button))
	{
		$button.click(function()
		{
			processDiffPageFullPatrol();
		});
	}
}

//helper function to process diff page range patrol including spinner etc.
function processDiffPageRangePatrol()
{
	var $button = $(".patrollink-range");
	var to = $button.find("a").attr("data-revision-max");
	var from = $button.find("a").attr("data-revision-min");
	
	$button.find("a").replaceWith(spinnerCode);
	
	patrolModule.resetLastClick();
	patrolModule.patrolRangeOfChanges(to, from).done(function()
	{
		$button.find("div").replaceWith(RCMultiplePatrolSuccessCode);
	}).fail(function(error)
	{
		console.log(error);
		$button.find("div").replaceWith(RCMultipleFailureSuccessCode);
	});
}

function processDiffPageFullPatrol()
{
	var $button = $(".patrollink-page");
	
	var pageid = $button.find("a").attr("data-pageid");
	$button.find("a").replaceWith(spinnerCode);
	
	patrolModule.resetLastClick();
	patrolModule.patrolPage(pageid).done(function()
	{
		$button.find("div").replaceWith(RCMultiplePatrolSuccessCode);
	}).fail(function(error)
	{
		console.log(error);
		$button.find("div").replaceWith(RCMultipleFailureSuccessCode);
	});
}

function processGroupMarksAndButtons($anchor) //usuwa grupowy wykrzyknik i przycisk patrolowania, jak zajdzie potrzeba
{
	var $parent = $anchor;
	while ($parent.prop("tagName") != "TABLE" && $parent.prop("tagName") != "BODY")
	{
		$parent = $parent.parent();
	}
	
	//wszystkie rozwijalne zmiany/pliki spatrolowane, drugi warunek po to,
	//zeby nie ignorowalo sytuacji, gdy zmiany nie udalo sie spatrolowac
	if ($parent.find(".patrolButton").length === 0 && $parent.find("abbr.unpatrolled").length == 1)
	{
		$parent.find("abbr.unpatrolled").replaceWith("&nbsp;");
		$parent.find(".multiplePatrolButton").replaceWith("<span><img src='https://nonsa.pl/images/2/26/Za.svg.png' width='15px' height='15px' />&nbsp;<span style='color: green;' class='multiplePatrolButtonClicked'>zrobione</span></span>");
	}
}

function goUpUntilTag($anchor, tag)
{
	tag = tag.toUpperCase();
	var $a = $anchor;
	while ($a.prop("tagName") != tag && $a.prop("tagName") != "BODY")
	{
		$a = $a.parent();
	}
	return $a;
}

function isEventBound($element)
{
	var events = $._data($element.get(0), "events");
	return events != null;
}