MediaWiki:Gadget-corrections.js

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
/* taken from //wikisource.org/wiki/MediaWiki:Corrections.js
 * Author: ThomasV
 */

/**************
 * corrections 
 **************/

$(document).ready(function() {
	corr_list = "Correzioni apportate alla pagina",
	corr_link = "Correzioni",
	corr_one = "Un errore di stampa </a> è stato corretto.",
	corr_many = " errori di stampa</a> sono stati corretti.";
	
	var closeCorrSummary = function() {
		var summary = document.getElementById("corr_summary");
		if(!summary) return;
		summary.innerHTML = "";
		summary.parentNode.removeChild(summary);
	
		//tolgo la sottolineatura
		$("span.errata").css("border-bottom", "");
	};
	
	var highlightCorr = function(index) {
		$("span.errata")[index].style.backgroundColor = "yellow";
	};
	var dehighlightCorr = function(index) {
		$("span.errata")[index].style.backgroundColor = "";
	};
	
	var scrollToCorrection = function(k) {
		$(window).scrollTop( $('span.errata').eq( (k-1) ).offset().top);
	};
	
	var prPopup = function(){
		var corr_container = document.getElementById("corr_summary");
		if(corr_container) { close_corr_summary(); return; }
	
		//sottolineo di rosso
		$("span.errata").css("border-bottom", "1px solid red");
	
		corr_container = document.createElement("div");
		corr_container.setAttribute("id", "corr_summary");
		corr_container.setAttribute("class", "box");
	
		var content= document.getElementById("text");
		corr_container.innerHTML = self.corr_list;
		content = document.getElementById("bodyContent");
		content.insertBefore(corr_container,content.firstChild);
		$("#corr_summary").draggable({
			create: function( event, ui ) {
		        $(this).css({
		            right: "auto",
		            top: $(this).position().top,
		            left: $(this).position().left
		        });
		    }
	   	});
	   	$('#corr_summary').draggable('option', 'cancel', '.box-main');
	   	
   		$('.correctionScrollLink').click(function(e) {
			e.preventDefault();
			num = $(this).data('correction-number');
			scrollToCorrection(num);
		});
		$('.correctionScrollLink').mouseover(function(e) {
			e.preventDefault();
			num = $(this).data('correction-number');
			highlightCorr(num-1);
		});
		$('.correctionScrollLink').mouseout(function(e) {
			e.preventDefault();
			num = $(this).data('correction-number');
			dehighlightCorr(num+1);
		});
		$('#correction-popup-close').click(function(e) {
			e.preventDefault();
			closeCorrSummary();
		});
	};
	
	var prTypos = function() {
	
		if( ! ( mw.config.get("wgNamespaceNumber")===0 || self.proofreadPageURL ) ) return;
	
		var str = '<div class="box-title">' + corr_list + '<span class="icon-close" id="correction-popup-close"></span></div>'
			+'<div class="box-main"><ul>'; 
		var j = 0;
		$('span.errata').each( function(index) {
			var item = this;
			j++; 
			w1 = w2 = '';
			if (item.previousSibling && item.previousSibling.data) { 
				context1 = item.previousSibling.data.split(' ');
				if (context1.length > 1) {
					w1 = context1[context1.length - 2].replace(/[\s]*/g, '') + ' ' + context1[context1.length - 1].replace(/[\s]*/g, '');
				}
			}
			if (item.nextSibling && item.nextSibling.data) { 
				context2 = item.nextSibling.data.split(' ');
				if (context2.length > 1) {
					w2 = context2[0].replace(/[\s]*/g, '') + ' ' + context2[1].replace(/[\s]*/g, '');
				}
			}
	
			//hidden "m" char is to avoid rtl languages firefox bug
			var newline = '<li>'
				+'<a class="correctionScrollLink" style="color:#000000" data-correction-number="' + j + '" href="#"> « '
			+ w1 + '<span style="color:#ff0000">#pre#</span>'
			+ w2 + ' » → « '
			+ w1 + '<span style="color:#00A000">#post#</span>'
			+ w2 + ' »</a> #eco#</li>'; 
	
			//recupera link a errata corrige originale (se presente)
			var innerText = item.innerHTML;
			var eco = "";
			if (innerText.indexOf('<span class="eco">') != -1) {
				eco = "(" + innerText.substring(innerText.indexOf('<span class="eco">')) + ")";
				innerText = innerText.substring(0, innerText.indexOf('<span class="eco">'));
			}
			newline = newline.replace("#eco#", eco);
	
			if (mw.config.get("wgCanonicalNamespace") == 'Page') {
				newline = newline.replace("#pre#", innerText);
				newline = newline.replace("#post#",item.title);
			}
			else {
				newline = newline.replace("#pre#",item.title);
				newline = newline.replace("#post#", innerText);
			}
			str += newline; 
		});
		str += '</ul></div>';
		self.corr_list = str;
	
		if ($('span.errata').length) {
			mw.util.addPortletLink('p-tb', '#', corr_link + " (" + j + ")", 'option-corrections', 'Correzioni applicate al testo della pagina (errata corrige)' );
			cs = document.getElementById("corr-info");
			if (cs) {
				if (j == 1)
					cs.innerHTML = " <a href='#'>" + corr_one;
				else
					cs.innerHTML = " <a href='#'>" + j + corr_many; 
			}
		}
		$('#option-corrections').click(function(e) {
			e.preventDefault();
			prPopup();
		});
	};
	
	prTypos();
});