MediaWiki:Gadget-cambusa.js: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m extraPages
utilizzo di OOjs UI al posto di jQuery UI
Riga 4:
* lo spostamento della discussione, usando i template cambusa e cambusada.
*
* @author [[ithttps:User//it.wikipedia.org/wiki/Utente:Rotpunkt]]
*/
/* global mediaWiki, jQuery, OO */
/*jshint unused: false */
/*global mediaWiki, jQuery */
 
( function ( mw, $ ) {
'use strict';
 
// Le pagine di partenza e destinazione per la cambusa
var inited = false,
// possono appartenere solo ai namespace di discussione,
// Le pagine di partenza e destinazione per la cambusa
// più le pagine qui di seguito configurate:
// possono appartenere solo ai namespace di discussione,
var extraPages = {
// più le pagine qui di seguito configurate:
4: [ 'Officina', 'Oracolo' ],
extraPages = {
4: [ 'Officina', 'Oracolo', 'VisualEditor/Commenti' ],
12: [ 'Sportello informazioni' ]
},;
// Per verificare se i link "Cambusa" sono già stati aggiunti
api, $dialog, $alert, $namespace;
var inited = false;
// mw.Api utilizzata durante lo spostamento della discussione
var api;
// La finestra di dialogo per configurare lo spostamento della discussione
var cambusaDialog;
 
/**
* Ottiene il contenuto di una sezione della pagina corrente.
* Alert in jQuery UI.
*/
* @param {string} section - La sezione da leggere
function alert( msg, spinner ) {
* @param {function} readHandler - La funzione da richiamare con il risultato
if ( !$alert ) {
$alert = $( '<div>' ).attr( 'id', 'gc-dialog-alert' ).appendTo( 'body' );
}
$alert.empty().append( $( '<p>' ).text( msg ) );
if ( spinner ) {
$alert.append( '<br/>', $.createSpinner( { size: 'large', type: 'block' } ), '<br/>' );
}
$alert.dialog( {
title: 'Accessorio cambusa',
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( 'close' );
}
}
} );
}
 
/**
* Legge una sezione della pagina corrente.
*/
function getSection( section, readHandler ) {
Riga 56 ⟶ 40:
rvsection: section,
} ).done ( function ( data ) {
var text = data.query.pages[ Object.keys( data.query.pages )[ 0 ] ].revisions[ 0 ][ '*' ];
readHandler( text );
} ).fail ( function ( code, data ) {
OO.ui.alert( 'Errore nel leggere la discussione: ' + code );
} );
}
Riga 65 ⟶ 49:
/**
* Modifica una sezione della pagina corrente.
*
* @param {string} section - La sezione da modificare
* @param {string} text - Il nuovo contenuto
* @param {string} summary - L'oggetto della modifica
*/
function setSection( section, text, summary ) {
Riga 77 ⟶ 65:
___location.reload();
} ).fail ( function ( code, data ) {
OO.ui.alert( 'Errore nel modificare la discussione: ' + code );
} );
}
 
/**
* Aggiunge una sezione a una pagina di discussione.
*
* @param {string} title - Il nome della pagina
* @param {string} sectiontitle - Il titolo della nuova sezione
* @param {string} text - Il contenuto della nuova sezione
* @param {function} successHandler - La funzione da richiamare in caso di successo
*/
function addSection( title, sectiontitle, text, successHandler ) {
Riga 90 ⟶ 83:
} )
.fail ( function ( code, data ) {
OO.ui.alert( 'Errore nel creare la discussione: ' + data.error.infocode );
} );
}
 
/**
* Ottiene l'anchor dell'ultima discussione di una data versione di una pagina.
*
* @param {string} oldid - L'id della versione della pagina
* @param {function} anchorHandler - La funzione da richiamare con il risultato
*/
function getSectionAnchor( oldid, anchorHandler ) {
Riga 104 ⟶ 100:
prop: 'sections',
} ).done ( function ( data ) {
anchorHandler( data.parse.sections[ data.parse.sections.length - 1 ].anchor );
} ).fail ( function ( code, data ) {
OO.ui.alert( 'Errore nel leggere l\'anchor della sezione: ' + code );
} );
}
 
/**
* Gestore del click sul pulsante "Cambusa" della finestra di dialogo.
* Aggiunge l'autocompletamento per i titoli delle pagine.
*/
function addAutocompletecambusaHandler( inputEl ) {
var from, text, error, formData = cambusaDialog.getFormData();
inputEl.autocomplete( {
source: function( request, response ) {
var ns = $namespace.find( 'option:selected' ).data( 'ns' );
if ( ns % 2 === 1 ) {
$.ajax( {
url: mw.util.wikiScript( 'api' ),
data: {
action: 'query',
list: 'allpages',
aplimit: '10',
apprefix: request.term,
apnamespace: ns - 1,
format: 'json'
},
dataType: 'json'
} )
.done( function ( data ) {
var pages = [];
$.each( data.query.allpages, function( i, page ) {
pages.push( page.title.substring( page.title.indexOf( ':' ) + 1 ) );
});
response( pages );
} );
} else if ( extraPages[ns] ) {
response( extraPages[ns] );
}
},
change: function ( e, ui ) {
if ( !ui.item ) {
e.target.value = '';
}
}
} );
}
 
if ( !formData.destTitle ) {
function addInputText( $fieldset, id, size, label , value ) {
error = 'Il campo pagina è obbligatorio.';
var $input = $( '<input/>' ).attr( 'id', id ).attr( 'type', 'text' )
} else if ( ( formData.destNamespace + ':' + formData.destTitle ).replace( / /g, '_' ) === mw.config.get( 'wgPageName' ) ) {
.attr( 'size', size ).val( value );
error = 'La pagina di destinazione deve essere diversa dalla corrente.';
if ( label ) {
} else if ( !formData.destSectionTitle ) {
$( '<label>' ).attr( 'for', id ).text( label ).appendTo( $fieldset );
error = 'Il campo titolo è obbligatorio.';
$fieldset.append( '<br/>' );
} else if ( extraPages[ formData.destNamespaceNum ] &&
$.inArray( formData.destTitle, extraPages[ formData.destNamespaceNum ] ) === -1 ) {
error = 'Le pagine valide nel namespace ' + formData.destNamespace + ' sono: ' +
extraPages[ formData.destNamespaceNum ].join( ', ' );
}
 
if ( error ) {
OO.ui.alert( error );
} else {
cambusaDialog.close();
formData.destTitle = formData.destNamespace + ':' + formData.destTitle;
from = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
text = '{{Cambusada|' + from + '|firma=--~~\~~}}\n' + formData.sourceText;
OO.ui.alert( 'Spostamento in corso...' );
addSection( formData.destTitle, formData.destSectionTitle, text, function ( newrevid ) {
getSectionAnchor( newrevid, function ( anchor ) {
text = '==' + formData.destSectionTitle + '==\n\n' +
'{{Cambusa|' + formData.destIncipit + '|' + formData.destTitle + '#' + anchor + '|firma=--~~\~~}}';
setSection( formData.sourceSection, text, 'cambusata discussione in [[' + formData.destTitle + ']]' );
} );
} );
}
$fieldset.append( $input, '<br/>' );
return $input;
}
 
/**
* VisualizzaCrea la finestra di dialogo per inserireconfigurare ilo datispostamento per ladella cambusadiscussione.
*
* @return {Object} L'oggetto derivato da OO.ui.Dialog che rappresenta la finestra
*/
function showDialogCambusabuildCambusaDialog( section, sectiontitle, text ) {
var style = '.gc-container-button { width: 100%; text-align: center; margin-top: 10px }';
var $fieldset, $dest, $desttitle, $incipit;
$( '<style>' ).text( style ).appendTo( 'head' );
// crea il dialog
iffunction CambusaDialog( !$dialogconfig ) {
CambusaDialog.parent.call( this, config );
$dialog = $( '<div>' ).attr( 'id', 'gc-dialog-cambusa' ).appendTo( 'body' );
}
OO.inheritClass( CambusaDialog, OO.ui.Dialog );
$dialog.html( '' );
CambusaDialog.static.name = 'cambusaDialog';
$fieldset = $( '<fieldset>' ).css( 'border-color', 'gray' ).appendTo( $dialog );
CambusaDialog.prototype.initialize = function () {
$( '<legend>' ).text( 'Cambusa la discussione' ).appendTo( $fieldset );
var self = this;
$namespace = $( '<select>' ).attr( 'id', 'gc-ns' ).css( 'width', '150px' );
CambusaDialog.parent.prototype.initialize.call( this );
$.each( mw.config.get( 'wgFormattedNamespaces' ), function ( key, value ) {
// scelta namespace
if ( key > 0 && ( key % 2 === 1 || extraPages[key] ) ) {
var ns = [];
$( '<option>' ).html( value ).data( 'ns', key ).appendTo( $namespace );
$.each( mw.config.get( 'wgFormattedNamespaces' ), function ( key, value ) {
}
key = parseInt( key );
} );
if ( key > 0 && ( key % 2 === 1 || extraPages[ key ] ) ) {
$( '<label>' ).attr( 'for', 'gc-ns' ).text( 'Namespace e pagina di destinazione:' ).appendTo( $fieldset );
ns.push( new OO.ui.MenuOptionWidget( { data: key, label: value } ) );
$fieldset.append( '<br/>', $namespace, ' <b>:</b> ' );
$dest = addInputText( $fieldset, 'gc-pagina', 40 );
$desttitle = addInputText( $fieldset, 'gc-titolo', 70, 'Titolo nuova discussione:', sectiontitle );
$incipit = addInputText( $fieldset, 'gc-incipit', 70, 'Incipit (facoltativo):' );
addAutocomplete( $dest );
 
// visualizza il dialog
$dialog.dialog( {
title: 'Accessorio cambusa',
width: 500,
resizable: false,
modal: false,
buttons: {
'Ok': function () {
var from, error, incipit = $.trim( $incipit.val() ),
dest = $.trim( $dest.val() ),
desttitle = $.trim( $desttitle.val() );
if ( dest.length === 0 ) {
error = 'Il campo pagina è obbligatorio.';
} else if ( ( $namespace.val() + ':' + dest ).replace( / /g, '_' ) === mw.config.get( 'wgPageName' ) ) {
error = 'La pagina di destinazione deve essere diversa dalla corrente.';
} else if ( desttitle.length === 0 ) {
error = 'Il campo titolo è obbligatorio.';
}
if ( error ) {
alert( error );
} else {
$( this ).dialog( 'close' );
// cambusa
dest = $namespace.val() + ':' + dest;
from = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
text = '{{Cambusada|' + from + '|firma=--~~' + '~~}}\n' + text;
alert( 'Spostamento in corso...', true );
addSection( dest, desttitle, text, function ( newrevid ) {
getSectionAnchor( newrevid, function ( anchor ) {
text = '==' + sectiontitle + '==\n\n' +
'{{Cambusa|' + incipit + '|' + dest + '#' + anchor + '|firma=--~~' + '~~}}';
setSection( section, text, 'cambusata discussione in [[' + dest + ']]' );
} );
} );
}
},
'Annulla': function () {
$( this ).dialog( 'close' );
}
} );
this.namespaceDropdown = new OO.ui.DropdownWidget( {
menu: { items: ns }
} );
this.namespaceDropdown.getMenu().on( 'select', function () {
var ns = self.namespaceDropdown.getMenu().getSelectedItem().getData();
self.destInput.setNamespace( ns );
} );
// pagina di destinazione
this.destInput = new mw.widgets.TitleInputWidget( {
validateTitle: true
} );
// titolo sezione
this.sectionTitleInput = new OO.ui.TextInputWidget();
// incipit
this.incipitInput = new OO.ui.TextInputWidget();
var cambusaButton = new OO.ui.ButtonWidget( {
label: 'Cambusa',
} ).on( 'click', cambusaHandler );
var cancelButton = new OO.ui.ButtonWidget( {
label: 'Annulla'
} ).on( 'click', function () {
self.close();
} );
// FieldsetLayout
var fieldsetLayout = new OO.ui.FieldsetLayout( {
items: [
new OO.ui.FieldLayout( this.namespaceDropdown, {
label: 'Namespace destinazione:',
align: 'top'
} ),
new OO.ui.FieldLayout( this.destInput, {
label: 'Pagina destinazione (senza namespace):',
align: 'top'
} ),
new OO.ui.FieldLayout( this.sectionTitleInput, {
label: 'Titolo nuova discussione:',
align: 'top'
} ),
new OO.ui.FieldLayout( this.incipitInput, {
label: 'Incipit (facoltativo):',
align: 'top'
} ),
new OO.ui.HorizontalLayout( {
items: [ cambusaButton, cancelButton ],
classes: [ 'gc-container-button' ]
} )
]
} );
this.panelLayout = new OO.ui.PanelLayout( { padded: true, expanded: false } );
this.panelLayout.$element.append( fieldsetLayout.$element );
this.$body.append( this.panelLayout.$element );
};
CambusaDialog.prototype.getBodyHeight = function () {
return this.panelLayout.$element.outerHeight( true );
};
CambusaDialog.prototype.setSourceSection = function ( sourceSection, sourceSectionTitle, sourceText ) {
this.sourceSection = sourceSection;
this.sectionTitleInput.setValue( sourceSectionTitle );
this.sourceText = sourceText;
this.destInput.setValue( '' );
this.namespaceDropdown.getMenu().selectItemByData( 1 );
};
CambusaDialog.prototype.getFormData = function () {
return {
destTitle: $.trim( this.destInput.getValue() ),
destSectionTitle: $.trim( this.sectionTitleInput.getValue() ),
destIncipit: $.trim( this.incipitInput.getValue() ),
destNamespace: this.namespaceDropdown.getMenu().getSelectedItem().getLabel(),
destNamespaceNum: this.namespaceDropdown.getMenu().getSelectedItem().getData(),
sourceSection: this.sourceSection,
sourceText: this.sourceText
};
};
return new CambusaDialog( {
size: 'small'
} );
}
 
/**
function addLinks() {
* Aggiunge il link "Cambusa" a ogni sezione della pagina corrente.
*/
function addCambusaLinks() {
var windowManager;
$( 'span.mw-editsection > a:last-of-type' ).each( function ( i, el ) {
var $link, sectiontitle = $( el ).attr( 'title' ).replace( /^(Modifica|Cancella) la sezione /g, '' );
Riga 237 ⟶ 257:
.click( function ( event ) {
event.preventDefault();
mw.loader.using( [ 'jquerymediawiki.uiapi.dialogedit', 'mediawiki.apioojs-ui-core', 'mediawiki.api.editoojs-ui-widgets',
'jquery.oojs-ui.autocomplete-windows', 'jquerymediawiki.spinnerwidgets' ], function () {
api = new mw.Api();
getSection( i + 1, function ( text ) {
var match = text.match( /==\s*(.*?)\s*==\s*\n([\s\S]*)/ );
if ( !cambusaDialog ) {
showDialogCambusa( i + 1, match[1], match[2] );
cambusaDialog = buildCambusaDialog();
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ cambusaDialog ] );
}
cambusaDialog.setSourceSection( i + 1, match[ 1 ], match[ 2 ] );
windowManager.openWindow( cambusaDialog );
} );
} );
Riga 248 ⟶ 275:
$( el ).after( '<span> | </span>', $link );
} );
inited = true;
}
 
/**
* RitornaRestituisce true se la pagina corrente può utilizzare il link "cambusa".
*
* @return {boolean}
*/
function cambusaLinksAllowed() {
Riga 258 ⟶ 286:
title = mw.config.get( 'wgTitle' );
return ns % 2 === 1 ||
( extraPages[ ns ] && $.inArray( title, extraPages[ ns ] ) !== -1 );
}
 
Riga 267 ⟶ 295:
event.preventDefault();
if ( !inited ) {
addLinksaddCambusaLinks();
inited = true;
}
} );