Discussioni MediaWiki:Gadget-CronoAvanzataBase.js

Ultimo commento: 8 anni fa, lasciato da Andyrom75 in merito all'argomento Istruzioni obsolete

Istruzioni obsolete modifica

La funzione utilizza codice obsoleto. Sostituiscigli il seguente:

if (!window.histCombTalk) histCombTalk = 'd';
if (!window.histCombContrib) histCombContrib = 'c';
if (!window.histCombMyBg) histCombMyBg = '#F8D3F4';
 
var pagehis, histRow, histSize, isCombEdits = false;
 
function histCombonLoad(){
	pagehis = document.getElementById('pagehistory');
	if (!pagehis) return;
	histRow = pagehis.getElementsByTagName('li');
	histSize = new Array (histRow.length);
	var row, spans, i, aa, author, author_p = '', size_digits;
	var sameAuthor = 0, thisMinor, allMinor = true;
	 
	for (var rr=histRow.length-1; rr>=0; rr--){ //main loop: all rows bottom to top
		row = histRow[rr];
		thisMinor = false;
		spans = row.getElementsByTagName('span');
		//check all spans
		for (i=0; i<spans.length; i++)
			switch (spans[i].className){
				case 'history-size':
					size_digits = spans[i].innerHTML.match(/\d+/g);
					histSize[rr] = size_digits ? size_digits.join('') : '0';
					if (histSize[rr+1]){ 
						spans[i].title = spans[i].innerHTML.replace(/&nbsp;/g,' ');
						insertDiffValue(spans[i], histSize[rr] - histSize[rr+1]);
					}
					break;
				case 'minor':
					thisMinor = true;
					break;
				case 'history-user': // get user name and shorten talk/contribs
					aa = spans[i].getElementsByTagName('a');
					author = aa[1].title.split(':')[1];
					aa[1].innerHTML = histCombTalk;
					if (aa.length >= 3 && (aa[2].title.indexOf(':Contributi/') != -1)) //for anons this could be 'block'
						aa[2].innerHTML = histCombContrib;
					break;
				case 'mw-history-undo':
					aa = spans[i].getElementsByTagName('a')[0];
					aa.title = aa.innerHTML;
					aa.innerHTML = '×';
					break;
				//case 'comment': histSumm = spans[i].innerHTML break 
			}//end switch inside for

			if (!histSize[rr] && histSize[rr+1])
				histSize[rr] = histSize[rr+1]; //assume it was '(un)protect' edit

			if (mw.config.get('wgUserName') == author)
				histRow[rr].style.backgroundColor = histCombMyBg;
			//check how many consequent edits
			if (author == author_p) 
				sameAuthor++;
			else {
				if (sameAuthor > 1)
					createCombineEdit(rr+1, sameAuthor, allMinor);
				sameAuthor = 1;
				author_p = author;
				allMinor = thisMinor;
			}
			if (!thisMinor)
				allMinor = false;
	}//main loop

	//collapse top editor too
	if (sameAuthor>1) {
		createCombineEdit(0, sameAuthor, allMinor);
		if (histRow.length > sameAuthor && !window.histCombNoCollapse) //if collapsing top row - make next row selected
			histRow[sameAuthor].getElementsByTagName('input')[0].click();
	}

	//add  "expand/collapse all" link
	if (isCombEdits) { 
		var togAll = document.createElement('a');
		togAll.appendChild(document.createTextNode('[±]'));
		togAll.href = 'javascript:mw.histCombtoggleAll()';
		//togAll.setAttribute("onclick", "mw.histCombtoggleAll()");
		pagehis.parentNode.insertBefore(togAll, pagehis.previousSibling);
	}
}

function createCombineEdit(m, len, isMinor){ //creates collapsing link for row m down to row m+len
	var row = histRow[m];
	row.style.listStyle = 'none none';
	row.style.position = 'relative';
	var plus = document.createElement('a');
	plus.className = 'collapsedEdits';
	plus.appendChild(document.createTextNode('[–]'));
	plus.style.position = 'absolute';
	plus.style.left = '-22px';
	plus.style.top = '2px';
	plus.href = 'javascript:mw.histCombtoggle('+m+','+len+','+isMinor+')';
	//plus.setAttribute("onclick", 'mw.histCombtoggle('+m+','+len+','+isMinor+')');
	row.appendChild(plus);
	if (!window.histCombNoCollapse) mw.histCombtoggle(m, len, isMinor); //and collapse
	isCombEdits = true;

	//unexplained IE bug: in row just under collapsed edit radios were jumping to the left, links were unclickable
	if (navigator.userAgent.indexOf('MSIE') != -1 && m+len < histRow.length){ //make all elements 'relative'
		var els = histRow[m+len].getElementsByTagName('*');
		for (i=0; i<els.length; i++)
			if (els[i].style && !els[i].style.position) els[i].style.position = 'relative';
	}
}

function insertDiffValue(span, value){
	var html, class2;
	if (value>0) {
		html = '(+' + value + ')';
		class2 = ' mw-plusminus-pos';
	} else if (value<0) {
		html = '(' + value + ')';
		class2 = ' mw-plusminus-neg';
	} else {
		html = '(0)';
		class2 = ' mw-plusminus-null';
	}
	span.style.fontWeight = (value < -500) ? 'bold' : 'normal';
	span.innerHTML = html;
	span.className = span.className.split(' ')[0] + class2;
}

mw.histCombtoggle = function (m, len, isMinor) { //handler for collapsing link
	var row = histRow[m];
	var spans = row.getElementsByTagName('span');
	var plus = row.lastChild;
	var radios = row.getElementsByTagName('input');
	var topDiff = row.getElementsByTagName('a')[ m > 0 ? 1 : 0]; // "diff to last" in the combined edit
	var i, clss, bottomDiff, oldid;
	var isHiding = (plus.innerHTML == '[–]');
	if (isHiding) {//need to collapse −
		plus.innerHTML = '[' + len + ']';
		row.style.borderLeft = '1px solid transparent';
		//hide other rows
		for (i=m+1; i<m+len; i++) histRow[i].style.display = 'none';
		//"diff from last" link: get oldid from the lowest collapsed edit
		bottomDiff = histRow[m+len-1].getElementsByTagName('a')[1].href;
		if (bottomDiff.indexOf('&diff=') == -1)
			topDiff.style.visibility = 'hidden'; //the very 1st edit
		oldid = bottomDiff.match(/oldid=(\d+)/)[1];
		topDiff['old'] = topDiff.href;
		topDiff.href = topDiff.href.replace(/&oldid=\d+/, '&oldid='+oldid);
	}else{ //need to expand
		plus.innerHTML = '[–]';
		//show other rows and visually reference the whole group
		row.style.borderLeft = '1px dotted gray';
		for (i=m+1; i<m+len; i++){
			histRow[i].style.display = 'block';
			histRow[i].style.borderLeft = '1px dotted gray';
			histRow[i].style.listStyle = 'none none';
		}
		histRow[m+len-1].style.borderBottom = '1px dotted gray';
		topDiff.href = topDiff['old'];
		topDiff.style.visibility = 'visible';
	}
	//hide/show/modify spans in this row
	for (i=0; i<spans.length; i++)
		switch (spans[i].className.split(' ')[0]){
		case 'minor':
			spans[i].style.display = (isHiding && !isMinor)?'none':'inline';
			break;
		case 'history-size': //switch to  combined diff size and back
			insertDiffValue(spans[i], histSize[m] - (isHiding ? histSize[m+len] : histSize[m+1]));
			break;
		case 'comment': //hide/show
			spans[i].style.display = (isHiding && !window.histCombLeaveComment)?'none':'inline';
			break;
		case 'mw-history-undo':
			var undoLnk = spans[i].getElementsByTagName('a')[0];
			if (isHiding) {
				undoLnk['old'] = undoLnk.href;
				undoLnk.href = undoLnk.href.replace(/&undoafter=\d+/, '&undoafter='+oldid);
			}else
				undoLnk.href = undoLnk['old'];
			break;
		//case 'history-user':
	}
};

mw.histCombtoggleAll = function () {
	var links = $('a.collapsedEdits');
	var state = (links[0].innerHTML == '[–]');
	for (var i=0; i<links.length; i++)
		if (state == (links[i].innerHTML == '[–]'))
			eval(links[i].href);
};

if (mw.config.get('wgAction') == 'history')
	mw.loader.using( 'mediawiki.util' ).done( function () {
		histCombonLoad();
	} );

PS Ho un dubbio sull'istruzione finale, ma penso che vada bene. --Andyrom75 (discussioni) 08:59, 22 ago 2015 (CEST)Rispondi

Chiedo venia, avevo fatto un copia e incolla errato. Aggiorna lo script. --Andyrom75 (discussioni) 12:09, 22 ago 2015 (CEST)Rispondi
Wim, ho modificato l'ultima istruzione nell'ottica di forzare la sincronizzazione tra script e pagina e altre piccole modifiche di cosmesi per ridurre drasticamente il numero di warning presenti nella pagina wiki. Aggiorna lo script con questa e vedi le mie perplessità sull'altro script. --Andyrom75 (discussioni) 13:14, 22 ago 2015 (CEST)Rispondi
Wim, ho tolto un pezzo di codice che creava un link ad un tool non più esistente. Aggiorna lo script. --Andyrom75 (discussioni) 00:01, 23 ago 2015 (CEST)Rispondi
Deo gratia! Ora te l'ho fatto funzionare. Dopo averci studiato un po' ho scoperto che il ResourceLoader impedisce la chiamata a funzioni JS dichiarate nello script e che l'unica interfaccia utilizzabile è quella di mediawiki (i.e. mw).
Durante le prove ho modificato un po' il codice (forse non era necessario) quindi ti ho messo qui sopra la mia ultima versione. In linea teorica andrebbero sostituite le chiamate javascript da href con le più pulite onclick, ma questo richiederebbe una riscrittura (seppur minima) del codice. Ammetto di non averne voglia :-P Quindi ho lasciato commentate le istruzioni di partenza lasciando ai posteri la riscrittura del resto (i.e. eliminazione dei riferimenti ad href). Oltre a copiare questo script, ti invito a cancellare l'altro che è inutile. --Andyrom75 (discussioni) 10:35, 23 ago 2015 (CEST)Rispondi
Ritorna alla pagina "Gadget-CronoAvanzataBase.js".