MediaWiki:Gadget-DeleteSection.js

Da Semi del Verbo, l'enciclopedia dell'influenza del Vangelo sulla cultura

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.
/**
 * Gadget-DeleteSection.js
 * Aggiunge il link "cancella" a ogni sezione di una pagina,
 * di fianco a "modifica" o "modifica sorgente", per cancellare l'intera sezione.
 * Questa è una riscrittura da zero di:
 * http://it.wikipedia.org/w/index.php?title=Wikipedia:Monobook.js/deledesection.js&oldid=58651956
 *
 * @author https://it.wikipedia.org/wiki/Utente:Rotpunkt
 */
/* global mediaWiki, jQuery, OO */

( function ( mw, $ ) {
	'use strict';

	/**
	 * Cancella una sezione della pagina corrente.
	 * 
	 * @param {string} section - La sezione da rimuovere
	 * @param {string} summary - L'oggetto della modifica
	 */
	function removeSection( section, summary ) {
		new mw.Api().postWithEditToken( {
			action: 'edit',
			format: 'json',
			title: mw.config.get( 'wgPageName' ),
			section: section,
			text: '',
			summary: summary
		} ).done ( function ( data ) {
			location.reload();
		} ).fail ( function ( code, data ) {
			OO.ui.alert( 'Errore nel cancellare la sezione: ' + code );
		} );
	}

	/**
	 * Crea la finestra di dialogo per la richiesta dell'oggetto della modifica.
	 * 
	 * @return {Object} L'oggetto derivato da OO.ui.Dialog che rappresenta la finestra
	 */
	function buildSummaryDialog() {
		var style = '.gds-container { height: 30px }' + 
					'.gds-container-button { width: 100%; text-align: center }';
		$( '<style>' ).text( style ).appendTo( 'head' );
		function SummaryDialog( config ) {
			SummaryDialog.parent.call( this, config );
		}
		OO.inheritClass( SummaryDialog, OO.ui.Dialog );
		SummaryDialog.static.name = 'summaryDialog';
		SummaryDialog.prototype.initialize = function () {
			var self = this;
			SummaryDialog.parent.prototype.initialize.call( this );
			var resultLabel = new OO.ui.LabelWidget( {
				classes: [ 'gds-container' ],
				label: ' '
			} );
			var textInput = new OO.ui.TextInputWidget();
			var textInputLayout = new OO.ui.FieldLayout( textInput, {
				label: 'Oggetto per l\'edit di cancellazione:',
				align: 'top' 
			} );
			var okButton = new OO.ui.ButtonWidget( {
				label: 'Ok',
			} ).on( 'click', function () {
				var summary = $.trim( textInput.getValue() );
				if ( !summary ) {
					resultLabel.setLabel( $( '<p>L\'oggetto &egrave; obbligatorio.</p>' ) );
				} else {
					resultLabel.setLabel( $( '<p>Cancellazione sezione in corso...</p>' ) );
					removeSection( self.currSection, summary );
				}
			} );
			var cancelButton = new OO.ui.ButtonWidget( {
				label: 'Annulla'
			} ).on( 'click', function () {
				self.close();
			} );
			var buttons = new OO.ui.HorizontalLayout( {
				items: [ okButton, cancelButton ],
				classes: [ 'gds-container-button' ]
			} );
			this.textInput = textInput;
			this.panelLayout = new OO.ui.PanelLayout( { padded: true, expanded: false } );
			this.panelLayout.$element.append( textInputLayout.$element, resultLabel.$element, buttons.$element );
			this.$body.append( this.panelLayout.$element );
		};
		SummaryDialog.prototype.getBodyHeight = function () {
			return this.panelLayout.$element.outerHeight( true );
		};
		return new SummaryDialog( {
			size: 'small'
		} );
	}

	/**
	 * Aggiunge il link "cancella" a ogni sezione della pagina corrente.
	 */
	function addDeleteLinks() {
		var summaryDialog, windowManager;
		$( 'span.mw-editsection > a:last-of-type' ).each( function ( i, el ) {
			var $link, sectiontitle;
			sectiontitle = $( el ).attr( 'title' ).split( 'Modifica la sezione ' )[ 1 ];
			$link = $( '<a>' )
				.attr( 'href', '#' )
				.attr( 'title', 'Cancella la sezione ' + sectiontitle )
				.text( 'cancella' )
				.click( function ( event ) {
					mw.loader.using( [ 'mediawiki.api', 'oojs-ui-core',
						'oojs-ui-widgets', 'oojs-ui-windows' ] )
						.done( function () {
							if ( !summaryDialog ) {
								summaryDialog = buildSummaryDialog();
								windowManager = new OO.ui.WindowManager();
								$( 'body' ).append( windowManager.$element );
								windowManager.addWindows( [ summaryDialog ] );
							}
							summaryDialog.textInput.setValue ( 'Sezione ' + sectiontitle + ' eliminata' );
							summaryDialog.currSection = i + 1;
							windowManager.openWindow( summaryDialog );
						} )
						.fail( function () {
							console.error( 'Impossibile avviare l\'accessorio DeleteSection.' );
						} );
					return false;
				} );
			$( el ).after( '<span> | </span>', $link );
		} );
	}

	$( function () {
		if ( mw.config.get( 'wgAction' ) === 'view' && mw.config.get( 'wgNamespaceNumber' ) % 2 === 0 ) {
			addDeleteLinks();
		}
	} );
}( mediaWiki, jQuery ) );