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

Z Nondanych
Linia 9: Linia 9:
var spinnerCode = "<div class='mw-spinner mw-spinner-small mw-spinner-inline' title='...'></div>";
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 patrolSuccessCode = "<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 patrolFailureCode = "<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
var debugActive = false; //zmieniane z pomocą konsoli
Linia 94: Linia 91:
function processMultipleChangeRCPatrol($tbody)
function processMultipleChangeRCPatrol($tbody)
{
{
var deferred = $.Deferred(),
var $button = $tbody.find(".patrollink-range"),
$button = $tbody.find(".patrollink-range"),
$patrolLink = $button.find("a"),
$patrolLink = $button.find("a"),
$buttons = $tbody.find(".patrollink-single"),
buttonsCount = $buttons.length,
$spinner = $.createSpinner({ size: 'small', type: 'inline' }),
$spinner = $.createSpinner({ size: 'small', type: 'inline' }),
$patrolAgainBrackets = $patrolLink.parent().find(".patrolAgainBracket");
$patrolAgainBrackets = $patrolLink.parent().find(".patrolAgainBracket");
if (buttonsCount === 0)
{
return $.when();
}
$patrolLink.hide();
$patrolLink.hide();
$patrolAgainBrackets.hide();
$patrolAgainBrackets.hide();
$patrolLink.after($spinner);
$patrolLink.after($spinner);
$patrolLink.parent().find(".multiplePatrolButtonClicked").remove();
$patrolLink.parent().find(".patrolButtonClicked").remove();
patrolModule.patrolRangeOfChanges($patrolLink.attr("data-revision-max")).done(function(data)
patrolModule.patrolRangeOfChanges($patrolLink.attr("data-revision-max")).done(function(data)
{
{
removeSpinner($button);
removeSpinner($button);
deferred.resolve();
$patrolLink.after(RCMultiplePatrolSuccessCode);
$patrolLink.after(patrolSuccessCode);
$patrolLink.remove();
$patrolLink.remove();
$patrolAgainBrackets.remove();
$patrolAgainBrackets.remove();
Linia 125: Linia 113:
//replace single buttons with success text
//replace single buttons with success text
$buttons.replaceWith(RCPatrolSuccessCode);
$buttons.replaceWith(patrolSuccessCode);
//remove patrol exclamation marks
//remove patrol exclamation marks
Linia 131: Linia 119:
}).fail(function() {
}).fail(function() {
removeSpinner($button);
removeSpinner($button);
deferred.reject();
if ($patrolAgainBrackets.length === 0)
if ($patrolAgainBrackets.length === 0)
{
{
$patrolLink.before(RCMultiplePatrolFailureCode);
$patrolLink.before(patrolFailureCode);
$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
Linia 141: Linia 128:
else
else
{
{
$patrolAgainBrackets.first().before(RCMultiplePatrolFailureCode);
$patrolAgainBrackets.first().before(patrolFailureCode);
$patrolAgainBrackets.show();
$patrolAgainBrackets.show();
}
}
Linia 149: Linia 136:
mw.notify( mw.msg( 'markedaspatrollederrornotify' ), { type: 'error' } );
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.
//helper function to process one RC link including spinner etc.
function processRCChangePatrol($button, showMsg)
function processRCChangePatrol($button)
{
{
var deferred = $.Deferred(),
var $patrolLink = $button.find("a");
$patrolLink = $button.find("a");
revid = $patrolLink.attr("data-revision"),
revid = $patrolLink.attr("data-revision"),
$spinner = $.createSpinner({ size: 'small', type: 'inline' });
$spinner = $.createSpinner({ size: 'small', type: 'inline' });
Linia 178: Linia 156:
removeSpinner($button);
removeSpinner($button);
$patrolLink.after(RCPatrolSuccessCode);
$patrolLink.after(patrolSuccessCode);
$patrolLink.remove();
$patrolLink.remove();
$patrolAgainBrackets.remove();
$patrolAgainBrackets.remove();
Linia 184: Linia 162:
goUpUntilTag($button, "tr").find("abbr.unpatrolled").replaceWith("&nbsp;");
goUpUntilTag($button, "tr").find("abbr.unpatrolled").replaceWith("&nbsp;");
var title = new mw.Title(data["fmod-patrol"].title);
if (showMsg)
mw.notify( mw.msg( 'markedaspatrollednotify', title.toText() ) );
{
var title = new mw.Title(data["fmod-patrol"].title);
mw.notify( mw.msg( 'markedaspatrollednotify', title.toText() ) );
}
deferred.resolve(data);
}).fail(function()
}).fail(function()
{
{
Linia 197: Linia 170:
if ($patrolAgainBrackets.length === 0)
if ($patrolAgainBrackets.length === 0)
{
{
$patrolLink.before(RCPatrolFailureCode);
$patrolLink.before(patrolFailureCode);
$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
Linia 203: Linia 176:
else
else
{
{
$patrolAgainBrackets.first().before(RCPatrolFailureCode);
$patrolAgainBrackets.first().before(patrolFailureCode);
$patrolAgainBrackets.show();
$patrolAgainBrackets.show();
}
}
Linia 209: Linia 182:
$patrolLink.show();
$patrolLink.show();
mw.notify( mw.msg( 'markedaspatrollederrornotifychange' ), { type: 'error' } );
if (showMsg)
{
mw.notify( mw.msg( 'markedaspatrollederrornotifychange' ), { type: 'error' } );
}
deferred.reject();
});
});
return deferred.promise();
}
}


Linia 236: Linia 204:
removeSpinner($button);
removeSpinner($button);
$patrolLink.after(RCPatrolSuccessCode);
$patrolLink.after(patrolSuccessCode);
$patrolLink.remove();
$patrolLink.remove();
$patrolAgainBrackets.remove();
$patrolAgainBrackets.remove();
// if we are on diff page remove range patrol button, because we just patrolled all page revisions
// if we are on diff page remove range patrol button, because we just patrolled all page revisions
$(".patrollink-range a").replaceWith(RCPatrolSuccessCode);
$(".patrollink-range a").replaceWith(patrolSuccessCode);
//if we are on Special:Newpages change background to match patrolled
//if we are on Special:Newpages change background to match patrolled
Linia 257: Linia 225:
if ($patrolAgainBrackets.length === 0)
if ($patrolAgainBrackets.length === 0)
{
{
$patrolLink.before(RCPatrolFailureCode);
$patrolLink.before(patrolFailureCode);
$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
Linia 263: Linia 231:
else
else
{
{
$patrolAgainBrackets.first().before(RCPatrolFailureCode);
$patrolAgainBrackets.first().before(patrolFailureCode);
$patrolAgainBrackets.show();
$patrolAgainBrackets.show();
}
}
Linia 314: Linia 282:
removeSpinner($button);
removeSpinner($button);
$patrolLink.after(RCPatrolSuccessCode);
$patrolLink.after(patrolSuccessCode);
$patrolLink.remove();
$patrolLink.remove();
$patrolAgainBrackets.remove();
$patrolAgainBrackets.remove();
Linia 326: Linia 294:
if ($patrolAgainBrackets.length === 0)
if ($patrolAgainBrackets.length === 0)
{
{
$patrolLink.before(RCPatrolFailureCode);
$patrolLink.before(patrolFailureCode);
$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
Linia 332: Linia 300:
else
else
{
{
$patrolAgainBrackets.first().before(RCPatrolFailureCode);
$patrolAgainBrackets.first().before(patrolFailureCode);
$patrolAgainBrackets.show();
$patrolAgainBrackets.show();
}
}

Wersja z 16:50, 28 maj 2020

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

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 patrolSuccessCode = "<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 patrolFailureCode = "<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 debugActive = false; //zmieniane z pomocą konsoli

$(document).ready(function()
{
	mw.loader.using(["jquery.spinner", "mediawiki.notification"], function()
	{
		if (mw.config.get("wgCanonicalSpecialPageName") === "Recentchanges")
		{
			makePatrolLinks();
			if (!!$('.mw-rcfilters-enabled').length) //wzięte od Polskacafe z gadżetu RollWithReason
			{
				setInterval(makePatrolLinks, 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)
		{
			makeDiffPagePatrolLinks();
			
			setInterval(makeDiffPagePatrolLinks, 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 makePatrolLinks()
{
	$(".patrollink-range a").each(function()
	{
		var $link = $(this);
		var $tbody = goUpUntilTag($link, "tbody");
		if (!isEventBound($link))
		{
			$link.on("click", function()
			{
				processMultipleChangeRCPatrol($tbody);
			});
		}
	});

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

//helper function to process multiple change RC patrol, including spinner etc.
function processMultipleChangeRCPatrol($tbody)
{
	var $button = $tbody.find(".patrollink-range"),
		$patrolLink = $button.find("a"),
		$spinner = $.createSpinner({ size: 'small', type: 'inline' }),
		$patrolAgainBrackets = $patrolLink.parent().find(".patrolAgainBracket");
	
	$patrolLink.hide();
	$patrolAgainBrackets.hide();
	$patrolLink.after($spinner);
	$patrolLink.parent().find(".patrolButtonClicked").remove();
	
	patrolModule.patrolRangeOfChanges($patrolLink.attr("data-revision-max")).done(function(data)
	{
		removeSpinner($button);
		
		$patrolLink.after(patrolSuccessCode);
		$patrolLink.remove();
		$patrolAgainBrackets.remove();
		
		var title = new mw.Title(data["fmod-patrol"].title);
		mw.notify( mw.msg( 'markedaspatrollednotifymulti', title.toText() ) );
		
		//replace single buttons with success text
		$buttons.replaceWith(patrolSuccessCode);
		
		//remove patrol exclamation marks
		$tbody.find("abbr.unpatrolled").replaceWith("&nbsp;");
	}).fail(function() {
		removeSpinner($button);
		
		if ($patrolAgainBrackets.length === 0)
		{
			$patrolLink.before(patrolFailureCode);
			$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
			$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
		}
		else
		{
			$patrolAgainBrackets.first().before(patrolFailureCode);
			$patrolAgainBrackets.show();
		}
		$patrolLink.text("spróbuj ponownie");
		$patrolLink.show();
		
		mw.notify( mw.msg( 'markedaspatrollederrornotify' ), { type: 'error' } );
	});
}

//helper function to process one RC link including spinner etc.
function processRCChangePatrol($button)
{
	var $patrolLink = $button.find("a");
		revid = $patrolLink.attr("data-revision"),
		$spinner = $.createSpinner({ size: 'small', type: 'inline' });
		$patrolAgainBrackets = $button.find(".patrolAgainBracket");
		
	$patrolLink.hide();
	$patrolAgainBrackets.hide();
	$patrolLink.after($spinner);
	$patrolLink.parent().find(".patrolButtonClicked").remove();
	
	patrolModule.resetLastClick();
	patrolModule.patrolChange(revid).done(function(data)
	{
		removeSpinner($button);
		
		$patrolLink.after(patrolSuccessCode);
		$patrolLink.remove();
		$patrolAgainBrackets.remove();
		
		goUpUntilTag($button, "tr").find("abbr.unpatrolled").replaceWith("&nbsp;");
		
		var title = new mw.Title(data["fmod-patrol"].title);
		mw.notify( mw.msg( 'markedaspatrollednotify', title.toText() ) );
	}).fail(function()
	{
		removeSpinner($button);
		
		if ($patrolAgainBrackets.length === 0)
		{
			$patrolLink.before(patrolFailureCode);
			$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
			$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
		}
		else
		{
			$patrolAgainBrackets.first().before(patrolFailureCode);
			$patrolAgainBrackets.show();
		}
		$patrolLink.text("spróbuj ponownie");
		$patrolLink.show();
		
		mw.notify( mw.msg( 'markedaspatrollederrornotifychange' ), { type: 'error' } );
	});
}

function processPagePatrol($button)
{
	//diff page, new pages and any new page on the bottom
	var pageid = $button.find("a").attr("data-pageid"),
		$patrolLink = $button.find("a");
		$spinner = $.createSpinner({ size: 'small', type: 'inline' });
		$patrolAgainBrackets = $button.find(".patrolAgainBracket");
	
	$patrolLink.hide();
	$patrolAgainBrackets.hide();
	$patrolLink.after($spinner);
	$patrolLink.parent().find(".patrolButtonClicked").remove();
	
	patrolModule.resetLastClick();
	patrolModule.patrolPage(pageid).done(function(data)
	{
		removeSpinner($button);
		
		$patrolLink.after(patrolSuccessCode);
		$patrolLink.remove();
		$patrolAgainBrackets.remove();
		
		// if we are on diff page remove range patrol button, because we just patrolled all page revisions
		$(".patrollink-range a").replaceWith(patrolSuccessCode);
		
		//if we are on Special:Newpages change background to match patrolled
		if (mw.config.get("wgCanonicalSpecialPageName") == "Newpages")
		{
			goUpUntilTag($button, "li").removeClass("not-patrolled");
		}
		
		var title = new mw.Title(data["fmod-patrol"].title);
		mw.notify( mw.msg( 'markedaspatrollednotifypage', title.toText() ) );
	}).fail(function()
	{
		removeSpinner($button);
		
		if ($patrolAgainBrackets.length === 0)
		{
			$patrolLink.before(patrolFailureCode);
			$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
			$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
		}
		else
		{
			$patrolAgainBrackets.first().before(patrolFailureCode);
			$patrolAgainBrackets.show();
		}
		$patrolLink.text("spróbuj ponownie");
		$patrolLink.show();
		
		mw.notify( mw.msg( 'markedaspatrollederrornotifypage' ), { type: 'error' } );
	});
}

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

//helper function to process diff page range patrol including spinner etc.
function processDiffPageRangePatrol()
{
	var $button = $(".patrollink-range"),
		$patrolLink = $button.find("a");
		$spinner = $.createSpinner({ size: 'small', type: 'inline' });
		$patrolAgainBrackets = $button.find(".patrolAgainBracket"),
		to = $patrolLink.attr("data-revision-max"),
		from = $patrolLink.attr("data-revision-min");
	
	$patrolLink.hide();
	$patrolAgainBrackets.hide();
	$patrolLink.after($spinner);
	$patrolLink.parent().find(".patrolButtonClicked").remove();
	
	patrolModule.resetLastClick();
	patrolModule.patrolRangeOfChanges(to, from).done(function(data)
	{
		removeSpinner($button);
		
		$patrolLink.after(patrolSuccessCode);
		$patrolLink.remove();
		$patrolAgainBrackets.remove();
		
		var title = new mw.Title(data["fmod-patrol"].title);
		mw.notify( mw.msg( 'markedaspatrollednotifymulti', title.toText() ) );
	}).fail(function()
	{
		removeSpinner($button);
		
		if ($patrolAgainBrackets.length === 0)
		{
			$patrolLink.before(patrolFailureCode);
			$patrolLink.before("<span class='patrolAgainBracket'> (</span>");
			$patrolLink.after("<span class='patrolAgainBracket'>)</span>");
		}
		else
		{
			$patrolAgainBrackets.first().before(patrolFailureCode);
			$patrolAgainBrackets.show();
		}
		$patrolLink.text("spróbuj ponownie");
		$patrolLink.show();
		
		mw.notify( mw.msg( 'markedaspatrollederrornotifypage' ), { type: 'error' } );
	});
}

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)
{
	if ($element == null || $element.length == 0)
	{
		return false;
	}
	var events = $._data($element.get(0), "events");
	return events != null;
}

function removeSpinner($node)
{
	$node.find("div.mw-spinner").remove();
}