MediaWiki:FasiAvanzamento.js
Wikibooks, manuali e libri di testo liberi.
Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti. Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menu Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.
/* <pre> */ // Stato avanzamento function setupAvzRipiani() { var blocchi = getElementsByClassName(document, 'div', 'avanzamenti'); var i, j; for(i=0; i<blocchi.length; i++) { var punti = blocchi[i].childNodes[1].childNodes; for(j=0; j<punti.length; j++) { if ( punti[j].nodeType != 1 ) continue; var link = punti[j].firstChild; var avz = document.createElement('span'); avz.setAttribute('class','avanzamento'); avz.style.fontSize = 'small'; avz.style.marginLeft = '3px'; var placeholder = document.createElement('span'); placeholder.setAttribute('class', 'avz_placeholder'); var bookTitle = document.createTextNode(link.title); placeholder.appendChild(bookTitle); avz.appendChild(placeholder); if(wgNamespaceNumber != 0) { var dataAvz = document.createElement('span'); dataAvz.setAttribute('class', 'dataAvanzamento'); dataAvz.style.marginLeft = '3px'; dataAvz.style.fontSize = 'smaller'; avz.appendChild(dataAvz); } punti[j].appendChild(avz); } } } function cercaAvz() { cercatoreAvanzamento = new avanzoCatcher(); cercatoreAvanzamento.onAvanzoCatched = inserisciAvz; var avanzamenti = getElementsByClassName(document, 'span', 'avanzamento'); // Prende tutti gli elementi che hanno come classe "avanzamento" var i; for(i in avanzamenti) { if(avanzamenti[i].firstChild.className == 'avz_placeholder') { var page = avanzamenti[i].firstChild.innerHTML; avanzamenti[i].firstChild.className='avz_' + encodeURI(page); avanzamenti[i].firstChild.innerHTML=''; cercatoreAvanzamento.aggiungiPagina(page); } } cercatoreAvanzamento.avanzaCoda(); } function inserisciAvz(page, avanzamento, data) { var elms = getElementsByClass('avz_' + encodeURI(page), 'span', document); // Becca gli elementi corrispondenti a quel libro var elm; var i; for(i=0; i<elms.length; i++) { elm = elms[i]; var statusImg = document.createElement('img'); var image = ""; var imagePath = ""; if(avanzamento == "0" || avanzamento == "00" ) { // Switcha tra le fasi di avanzamento imagePath = 'http://upload.wikimedia.org/wikipedia/commons/8/8f/00%25.png'; } if(avanzamento == "25") { imagePath = 'http://upload.wikimedia.org/wikipedia/commons/5/5b/25%25.png'; } if(avanzamento == "50") { imagePath = 'http://upload.wikimedia.org/wikipedia/commons/3/3a/50%25.png'; } if(avanzamento == "75") { imagePath = 'http://upload.wikimedia.org/wikipedia/commons/c/cd/75%25.png'; } if(avanzamento == "100") { imagePath = 'http://upload.wikimedia.org/wikipedia/commons/6/64/100%25.png'; } statusImg.setAttribute('src', imagePath); statusImg.setAttribute('alt', 'Avanzamento: ' + avanzamento + '%'); elm.appendChild(statusImg); if(!elm.nextSibling) continue; // Se non c'è niente, vuol dire che la data non va messa. if(data != null && data != '' && elm.nextSibling.className == 'dataAvanzamento') { dataText = document.createTextNode('(' + data + ')'); elm.nextSibling.appendChild(dataText) } } } function avanzoCatcherStatoCambiato() { cercatoreAvanzamento.processa(); } function avanzoCatcher() { this.coda = new Array(); this.xhttpreq = null; this.currentPage = null; this.createRequest = createRequest; this.aggiungiPagina = addPage; this.avanzaCoda = initRequest; this.processa = processRequest; this.onAvanzoCatched = null; function createRequest() { try { this.xhttpreq = new XMLHttpRequest(); } catch(exc) { try { this.xhttpreq = new ActiveXObject('Msxml2.XMLHTTP'); } catch(exc) { try { this.xhttpreq = new ActiveXObject('Microsoft.XMLHTTP'); } catch(exc) { this.xhttpreq = null; } } } } function addPage(inputPage) { if(arrayContains(inputPage, this.coda)) return; // Se l'ho già messa, la salto! this.coda.push(inputPage); } function initRequest() { if(this.coda.length == 0) return; this.createRequest(); this.xhttpreq.open('GET', '/w/index.php?title=' + encodeURIComponent(this.coda[0]) + '&action=raw&ctype=text/x-wiki', true); this.xhttpreq.onreadystatechange = avanzoCatcherStatoCambiato; this.xhttpreq.send(''); this.currentPage = this.coda[0]; this.coda.shift(); } function processRequest() { if(this.xhttpreq.readyState == 4) { var txtresp = this.xhttpreq.responseText; var avanzoregexp = /\{\{[Aa]vanzamento\|(\d{1,3})%\|?(.*)\}\}/; var match = avanzoregexp.exec(txtresp); if(match) { var avanzamento = match[1]; var data = null; if(match.length == 3) { data = match[2]; } this.onAvanzoCatched(this.currentPage, avanzamento, data); } this.avanzaCoda(); } } } addLoadEvent(setupAvzRipiani); addLoadEvent(cercaAvz); /* </pre> */