MediaWiki:Modifiche alla pagina principale.js/test.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.
/*
* [it]
*
* Esegue modifiche dell'HTML
* della Pagina principale
*
* Sezione "Ultimi articoli pubblicati" :
* - corregge le date sbagliate e riordina la lista
* - rimuove vecchi articoli
* - aggiunge un pulsante che raggruppa/separa per data
*
* [en]
*
* Modifies homepage's HTML
*
* Section "Ultimi articoli pubblicati" :
* - fixes wrong dates and re-sorts the list
* - removes old articles
* - adds a button that groups/ungroups the list by date
*
*/
/* jshint
esversion : 3
, browser : true
, undef : true
, unused : true
, eqeqeq : true
, laxcomma : true
, laxbreak : true
*/
/* globals
$ : false
, mw : false
, console : false
*/
$( function() {
window.mw.libs.modUltimiArticoli = {
init : function ( $root ) {
var top = this;
this.isHome = mw.config.get( 'wgPageName' ) === 'Pagina_principale';
this.$root = ( // needed for live-preview
$root
? $root
: $( 'body' )
);
try {
this.changes.init( this );
}
catch ( e ) {
console[ console.error ? 'error' : 'log' ]( e );
top.changes.count = 0;
}
try {
this.origList.init( this );
this.fixedList.init( this );
this.section.init( this );
}
catch ( e ) {
console[ console.error ? 'error' : 'log' ]( e );
this.reset();
return;
}
try {
this.groupedList.init( this );
this.switcher.init( this );
}
catch ( e ) {
console[ console.error ? 'error' : 'log' ]( e );
this.reset( 'fixedList' );
return;
}
try {
this.preview.init( this );
}
catch ( e ) {
console[ console.error ? 'error' : 'log' ]( e );
return;
}
}
, reset : function ( keep ) {
try {
$( this.changes.$table)
.find( 'tr > * + * + *' )
.remove();
if ( keep === 'fixedList' ) {
if ( this.isHome ) {
$( this.origList.$div ).hide();
$( this.section.$h2 ).hide();
}
else {
$( this.origList.$div ).show();
$( this.section.$h2 ).show();
}
$( this.fixedList.$div ).show();
}
else {
$( this.origList.$div ).show();
$( this.section.$h2 ).remove();
$( this.fixedList.$div ).remove();
}
$( this.groupedList.$div ).remove();
$( this.switcher.$div ).remove();
}
catch ( e ) {
console[ console.error ? 'error' : 'log' ]( e );
}
}
//
, $root : null
, isHome : null
, dateUtil : {
shortMonths : [ 'gen' , 'feb' , 'mar' , 'apr' , 'mag' , 'giu' , 'lug' , 'ago' , 'set' , 'ott' , 'nov' , 'dic' ]
, longMonths : [ 'gennaio' , 'febbraio' , 'marzo' , 'aprile' , 'maggio' , 'giugno' , 'luglio' , 'agosto' , 'settembre' , 'ottobre' , 'novembre' , 'dicembre' ]
, weekDays : [ 'domenica' , 'lunedì' , 'martedì' , 'mercoledì' , 'giovedì' , 'venerdì' , 'sabato' ]
, textToDate : function ( text ) {
var
dateEls = text.match( /(\d+) (\w+) (\d+)/ )
, month = dateEls && this.shortMonths.indexOf( dateEls[ 2 ] )
;
if (
month === null
||
month === -1
)
return null;
return new Date( dateEls[ 3 ] , month , dateEls[ 1 ] );
}
, dateToText : function ( date ) {
return (
this.weekDays[ date.getDay() ]
+ ' '
+ date.getDate()
+ ' '
+ this.longMonths[ date.getMonth() ]
+ ' '
+ date.getFullYear()
);
}
, changeDateInLi : function ( $li , dateText ) {
$li
.contents()
.each(
function ( i , el ) {
var oldText = el.textContent;
if ( oldText.match( /\d+ \w+ \d+/ ) )
el.textContent = oldText.replace(
/\d+ \w+ \d+/
, dateText
);
}
);
}
, dateTextFromLi : function ( $li ) {
return $li.text().replace( /:.+/ , '' );
}
}
//
, changes : {
$table : null
, id : '#fix-date-ultimi-articoli'
, titles : {}
, count : 0
, init : function ( top ) {
this.$table = top.$root.find( this.id );
this.count = 0;
var $headerTr = (
this.$table
.find( 'tr:first' )
.append( '<th>nota</th>' )
);
this.$table
.find( 'tr' )
.not( $headerTr )
.append( '<td>' );
this.parse( top );
}
, parse : function ( top ) {
this.titles = {};
var changes = this;
this
.$table
.find( 'tr' )
.each(
function( i , el ) {
var
$el = $( el )
, $tds = $el.find( 'td' )
, title = $tds.eq( 1 ).text()
, dateText = $tds.eq( 0 ).text()
, date = top.dateUtil.textToDate( dateText )
;
if ( title in changes.titles ) {
changes.addNote( $el , '<span class="error">titolo duplicato</span>' );
return;
}
if ( $tds.length > 0 ) {
if (
date
||
dateText === 'rimuovi'
) {
++ changes.count;
changes.titles[ title ] = {
$el : $el
, dateText : dateText
};
}
else
changes.addNote( $el , '<span class="error">data non valida</span>' );
}
}
);
}
, addNote : function ( titleOr$tr , note ) {
(
titleOr$tr.$el
? titleOr$tr.$el
: titleOr$tr
)
.find( 'td' )
.eq( 2 )
.html( note );
}
}
, origList : {
$div : null
, id : '#ultimi-articoli-pubblicati'
, titles : {}
, init : function ( top ) {
this.$div = top.$root.find( this.id );
if ( top.isHome )
this.$div.hide();
}
}
, fixedList : {
$div : null
, id : 'ultimi-articoli-pubblicati-fixed'
, articles : []
, init : function ( top ) {
this.$div = (
top.origList.$div
.clone( true, true )
.attr( 'id' , this.id )
.insertAfter( top.origList.$div )
);
this.parse( top );
if ( top.changes.count > 0 ) {
if ( this.fix( top ) )
this.sort();
}
}
, parse : function ( top ) {
this.articles = [];
var articles = this.articles;
this.$div
.find( 'li' )
.each(
function ( i , el ) {
var
$el = $( el )
, title = $el.find( 'a' ).text()
, dateText = top.dateUtil.dateTextFromLi( $el )
, date = top.dateUtil.textToDate( dateText )
;
articles.push( {
$el : $el
, title : title
, date : date
, dateText : dateText
} );
}
);
}
, fix : function ( top ) {
var
changed = false
, articleChange , article , newDate
;
for ( var i = this.articles.length - 1 ; i >= 0 ; i -- ) {
article = this.articles[ i ];
if ( article.title in top.changes.titles ) {
changed = true;
articleChange = (
top.changes
.titles[ article.title ]
);
newDate = articleChange.dateText;
if ( newDate === 'rimuovi' ) {
article.$el.remove();
this.articles.splice( i , 1 );
top.changes.addNote( articleChange , 'rimosso' );
}
else {
article.oldDate = article.date;
article.oldDateText = article.dateText;
article.date = top.dateUtil.textToDate( newDate );
article.dateText = newDate;
top.dateUtil.changeDateInLi( article.$el , newDate );
top.changes.addNote( articleChange , 'corretto' );
}
}
}
return changed;
}
, sort : function () {
var $ul = this.$div.find( 'ul' );
this.articles
.sort(
function ( a , b ) {
return b.date - a.date;
}
);
$.each(
this.articles
, function ( i , a ) {
$ul.append( a.$el );
}
);
}
}
, groupedList : {
$div : null
, id : 'ultimi-articoli-pubblicati-grouped'
, init : function ( top ) {
this.$div = (
$( '<div>' )
.attr( 'id' , this.id )
.insertAfter( top.fixedList.$div )
);
this.fill( top );
}
, fill : function ( top ) {
var
$dateUl = null
, curDate = null
;
$.each(
top.fixedList.articles
, function ( i , article ) {
// new group -- if needed
if ( article.dateText !== curDate ) {
curDate = article.dateText;
$dateUl = $( '<ul>' );
$( '<div style="margin: .5em 0 1em;">' )
.append( '<p>' + top.dateUtil.dateToText( article.date ) + '</p>' )
.append( $dateUl )
.appendTo( top.groupedList.$div );
}
$( '<li>' )
.append(
article.$el
.find( 'a' )
.clone( true , true )
)
.appendTo( $dateUl );
}
);
}
}
//
, section : {
$h2 : null
, $listType : null
, init : function ( top ) {
if ( top.isHome )
return;
this.$h2 = $(
'\n<h2 id="h2-home-version">\n'
+ ' Versione visualizzata in\n'
+ ' <a href="/wiki/Pagina_principale" title="Pagina principale">Pagina principale</a>\n'
+ '</h2>\n'
);
this.$listType = (
$( ' <span style="font-size: 67%;">' )
.appendTo( this.$h2 )
);
top.fixedList.$div
.before( this.$h2 );
}
}
, switcher : {
top : null
, $div : null
, $button : null
, labels : {
group : '[raggruppa per data]'
, ungroup : '[date ai titoli]'
, grouped : '— lista raggruppata'
, ungrouped : '— lista con correzioni'
}
, init : function ( top ) {
this.top = top;
this.$button = $( '<a id="toggle-ultimi-articoli" href="javascript:void(0);">' );
this.$div = (
$( '<div style="font-size: 85%; text-align: right;">' )
.append( this.$button )
.insertBefore( top.fixedList.$div )
);
this.$button
.click(
function () {
if ( this.textContent === top.switcher.labels.group )
top.switcher.group();
else
top.switcher.ungroup();
}
);
if ( top.isHome )
this.group();
else
this.ungroup();
}
, group : function () {
this.$button.text( this.labels.ungroup );
if ( ! this.top.isHome )
this.top.section.$listType.text( this.labels.grouped );
this.top.fixedList.$div.hide();
this.top.groupedList.$div.show();
}
, ungroup : function () {
this.$button.text( this.labels.group );
if ( ! this.top.isHome )
this.top.section.$listType.text( this.labels.ungrouped );
this.top.fixedList.$div.show();
this.top.groupedList.$div.hide();
}
}
, preview : {
top : null
, hookAdded : false
, init : function ( top ) {
var
notEditPage = $( '#wpTextbox1' ).length === 0
, pageName = mw.config.get( 'wgPageName' )
;
this.top = top;
if( notEditPage )
return;
$( '#wpTemplateSandboxPage' )
.val(
pageName === 'Template:Pagina_principale/Ultimi_articoli_pubblicati'
? 'Pagina principale'
: 'Template:Pagina principale/Ultimi articoli pubblicati'
);
if ( ! this.hookAdded ) {
mw.hook( 'wikipage.content' )
.add( this.hookHandler
.bind( this )
);
this.hookAdded = true;
}
}
, hookHandler : function ( $el ) {
if ( $el.attr( 'id' ) !== 'wikiPreview' )
return;
this.top.reset();
this.top.init( $el );
}
}
};
window.mw.libs.modUltimiArticoli.init();
} );