Потребител:V111P/js/QuickDiffCommands.js: Разлика между версии

от Уикипедия, свободната енциклопедия
Изтрито е съдържание Добавено е съдържание
Редакция без резюме
мРедакция без резюме
Ред 65: Ред 65:
this.viewWindow = this.buildViewWindow();
this.viewWindow = this.buildViewWindow();


mw.loader.load("mediawiki.action.history.diff", "text/css");
mw.loader.load("mediawiki.diff.styles", "text/css");
this.addCss();
this.addCss();
},
},
Ред 139: Ред 139:
background: url(//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Fileclose.png/16px-Fileclose.png) no-repeat center center;\
background: url(//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Fileclose.png/16px-Fileclose.png) no-repeat center center;\
cursor: pointer;\
cursor: pointer;\
z-index: 1;\
}'
}'
);
);

Версия от 11:54, 23 април 2018

/**
 * Enhance recent changes diff links.
 * Author: Borislav Manolov
 * License: Public domain
 * Documentation: [[МедияУики:Gadget-Quick diff.js/doc]]
 */
var KEY_ESC = 27;

//$(document).off();
//$('a[href*="diff="]').off();
//$('#quickdiff').remove();

var QuickDiff = {

	enable: function()
	{
		jQuery('body').on('click', 'a[href*="diff="]', function (event) {
			var $link = jQuery(this);
			var isInside = $link.closest('#quickdiff').length > 0;
			if (isInside)
				$link = QuickDiff.$currentDiffLink;
			else
				QuickDiff.$currentDiffLink = $link.addClass("working");
			var href = this.href + "&action=render"
				+ ( event.ctrlKey ? "" : "&diffonly=1" );
			jQuery.get(href, function(data){
				QuickDiff.viewDiff(data, $link);
				if (!isInside)
				    $link.removeClass("working").addClass("done");
			});
			return false;
		});
	},

	viewDiff: function(content, $link)
	{
		var $viewWin = this.getViewWindow().css("top", $link.position().top + 30)
			.find("#quickdiff-content").html(content)
			.end().show();
		if (mw.ext && mw.ext.Patroller) {
			new mw.ext.Patroller.bulk(new mw.ext.Patroller.quick()).enable($link[0].href);
			var patrolLink = $viewWin.find('.patrollink a');
			new mw.ext.Patroller.quick().enable(patrolLink, $link[0].href);
		}
		if (mw.ext && mw.ext.QuickRollback) {
			var $rollbackLinkSpan = $('#quickdiff .mw-rollback-link');
			$rollbackLinkSpan.before('<br/>');
			mw.ext.QuickRollback.enable($rollbackLinkSpan.find('a'));
		}
	},

	viewWindow: null,

	getViewWindow: function()
	{
		if ( null === this.viewWindow ) {
			this.prepareViewWindow();
		}

		return this.viewWindow;
	},

	prepareViewWindow: function()
	{
		this.viewWindow = this.buildViewWindow();

		mw.loader.load("mediawiki.diff.styles", "text/css");
		this.addCss();
	},

	closeWin: function() {
		$('#quickdiff').hide();
	},

	buildViewWindow: function()
	{
		var $win = jQuery('<div id="quickdiff"><div id="quickdiff-close"/><div id="quickdiff-content"/></div>');
		var closeWin = function(){
			if ($win) {
				$win.hide();
			}
		};
		$win.on('dblclick', closeWin).appendTo("#bodyContent").find("#quickdiff-close").click(closeWin);
		$(document).keyup(function(e) {
            var keys = QuickDiff.keys;

			if (e.keyCode == KEY_ESC) { closeWin(); }

			if (e.keyCode == KEY_ESC) { QuickDiff.closeWin(); return; }
			if ( !$('#quickdiff').is(':visible') || e.target.nodeName == 'INPUT' ) { return; }

			var key = String.fromCharCode(e.keyCode).toLowerCase();

			if (QuickDiff.userIsPatroller && key == keys.patrol) { QuickDiff.patrolMulti(); }
			else if (key == keys.close) { closeWin(); }
			else if (key == keys.rollback) {
				var rollbkLink = $('#quickdiff .mw-rollback-link a').attr('target', '_blank')[0];
				if (rollbkLink) rollbkLink.click();
			}
			else if (key == keys.rollbackGood) {
				var rollbkGLink = $('#quickdiff a.quickRollback_good')[0];
				if (rollbkGLink) rollbkGLink.click();
			}
			else if (key == keys.rollbackComment) {
				var rollbkCLink = $('#quickdiff a.quickRollback_comm')[0];
	            if (rollbkCLink) rollbkCLink.click();
			}
			else if (key == keys.up || key == keys.down) {
				QuickDiff.next( key == keys.up );
			}
			else if ( QuickDiff.userIsPatroller && ( key == keys.upUnpatrolled || key == keys.downUnpatrolled ) ) {
				QuickDiff.nextUnpatrolled( key == keys.upUnpatrolled );
			}
		});
		$(document.body).on("click", function(event) {
			if ($(event.target).parents('#quickdiff').length === 0) {
				closeWin();
			}
		});
		return $win;
	},

	addCss: function()
	{
		mw.util.addCSS(
			'#quickdiff {\
				position: absolute;\
				width: 100%;\
				border: thin outset silver;\
				box-shadow: 0 0 30px #888888;\
				background-color: white;\
			}\
			#quickdiff-close {\
				position: absolute;\
				top: 0;\
				right: 0;\
				width: 20px;\
				height: 20px;\
				background: url(//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Fileclose.png/16px-Fileclose.png) no-repeat center center;\
				cursor: pointer;\
				z-index: 1;\
			}'
		);
	},

	patrolMulti: function() { // "click" the Patrol-multiple link if exists, otherwise "click" the Patrol link
		var $quickPatrolMultipleLink = $('#quickdiff [href="#executePatrol"]');
		var $standardPatrolLink = $('#quickdiff .patrollink a');
		if ($quickPatrolMultipleLink[0]) $quickPatrolMultipleLink.click();
		else {
			if (mw.ext && mw.ext.Patroller)
				$standardPatrolLink.click();
			else
				$standardPatrolLink[0].click();
		}
		// remove the exclamation point from the line
		QuickDiff.$currentDiffLink.closest('tr').find('abbr.unpatrolled').after('&nbsp;').remove();
	},

	next: function(up) {
		function scrollTo(el) {
			el = (el && el instanceof jQuery ? el[0] : el);
			if (el) el.scrollIntoView();
			return $(el);
		}
		var $lastDiffLink = QuickDiff.$currentDiffLink;
		var $all, index, closeWin = QuickDiff.closeWin;

		if (mw.config.get('wgAction') === "history") {
			$all = $('ul#pagehistory li');
			index = $all.index($lastDiffLink.closest('li'));

			if ( (up && index === 0) || (!up && index == $all.length - 1) ) closeWin();
			else {
				var $diffLinks = $all.eq( index + (up ? -1 : 1) ).find('.mw-history-histlinks a');
				if (up) {
					// if at top and there is no more recent edit, the "prev" link is the first link
					var $lnk = $( $diffLinks[1] || $diffLinks[0] );
					scrollTo( $all[index - 2] || $lnk );
					$lnk.click();
				}
				else { // down
					if ($diffLinks.length == 1) { closeWin(); } // no prev link (no earlier edits)
					else {
						scrollTo($lastDiffLink); // scroll to the link above
						$diffLinks[1].click();
					}
				}
			}
		}
		else { // Recent changes, Watchlist, Contributions
            var $all;
			if (mw.config.get('wgCanonicalSpecialPageName') == 'Contributions') {
				$all = $('.mw-changeslist-date + a[href*="&diff=prev"]');
			}
            else if ($('.mw-enhanced-rc').length > 0) {
                $all = $('.mw-title + a[href*="&diff="]'); // excludes the subdiffs
            }
            else $all = $('a[href*="&diff="]');

			if (up) { $all = $($all.get().reverse()); }
			index = $all.index($lastDiffLink);

			if (index > -1) {
				if ( index == $all.length - 1 ) closeWin();
				else {
					var $el = $all.eq(index + 1);
					// scroll to the element above $el, unless $el is the top element
					scrollTo( $(!up ? $lastDiffLink : $( $all.eq(index + 2)[0] || $el[0] ) ) );
					$el.click();
				}
			}
			else { // Enhanced Recent Changes/Watchlist sublist diff link
				var $sublist = $lastDiffLink.closest('.mw-enhanced-rc').find('.mw-enhanced-rc-time + a + a');
				index = $sublist.index($lastDiffLink);
				if ( (!up && index + 1 == $sublist.length) || (up && index < 1) ) closeWin();
				else if (index > -1) scrollTo( $sublist.eq( index + (up ? -1 : 1) ) ).click();
			}
		}

	},

	nextUnpatrolled: function(up) {
		var namespaceNames = mw.config.get('wgFormattedNamespaces');
		var talkNS = namespaceNames[1], wpNS = namespaceNames[4];
		var excludeNSRegEx = new RegExp('[' + talkNS[0] + talkNS[0].toLowerCase() + ']'
		                                  + talkNS.slice(1) + ':|' + wpNS + ':');
		var grouped = $('table.mw-enhanced-rc').length > 0;
		var $all = $( grouped ? 'table.mw-enhanced-rc' : '.mw-changeslist li' );
		if (up) { $all = $($all.get().reverse()); }
		var index = $all.index(QuickDiff.$currentDiffLink.closest( (grouped ? 'table.mw-enhanced-rc' : 'li' ) ));
		var $nextUnpatrolled, $above, $lnk;

		$all.each(function (i, el) {
			if (i <= index) return true; // continue
			var $el = $(el);
			$lnk = $el.find( ( grouped ? '.mw-title + ' : '' ) + 'a[href*="&diff="]' );

			if ( $lnk[0] && $el.has('.unpatrolled')[0] && !$el.text().match(excludeNSRegEx) ) {
				$nextUnpatrolled = $el;
				$above = (up ? $($all[i + 1]) : $($all[i - 1]));
				return false; // break
			}
			return true; // continue
		});

		if (!$nextUnpatrolled) QuickDiff.closeWin();
		else {
			var $scrollToEl = ($above && $above[0] ? $above : $nextUnpatrolled);
			$scrollToEl[0].scrollIntoView();
			$lnk.click();
		}
	}

};

// prepare for fight
$(function(){
	if ( /^(Recentchanges|Watchlist|Contributions)/.test(mw.config.get('wgCanonicalSpecialPageName'))
			|| mw.config.get('wgAction') === "history"
			|| mw.config.get('wgPageName') === "Уикипедия:Активни_беседи"
	) {
		var keys = {
			close: '4',
			patrol: '3',
			rollback: '5',
			rollbackGood: '6',   // изисква джаджата „Бързо отменяне“
			rollbackComment: '7', // изисква джаджата „Бързо отменяне“
			up: 'r',
			down: 'f',
			upUnpatrolled: 'e',
			downUnpatrolled: 'd'
		};
		var customKeys = window.quickDiffConfig && window.quickDiffConfig.keys;
		QuickDiff.keys = $.extend(keys, customKeys);

		var userGroups = mw.config.get('wgUserGroups');
		QuickDiff.userIsAdmin = $.inArray("sysop", userGroups) > -1;
		QuickDiff.userIsPatroller = $.inArray("patroller", userGroups) > -1
		                            || $.inArray("autopatrolled", userGroups) > -1 || QuickDiff.userIsAdmin;

		QuickDiff.enable();
	}
});