MediaWiki:Gadget-cambusa.js

Versione del 24 feb 2015 alle 12:45 di Rotpunkt (discussione | contributi) (fix selector)

Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.


Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). 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 Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.

/**
 * Gadget-cambusa.js
 * Aggiunge il link "cambusa" a ogni discussione della pagina, per effettuare
 * lo spostamento della discussione, usando i template cambusa e cambusada.
 *
 * @author [[it:User:Rotpunkt]]
 */
/*jshint unused: false */
/*global mediaWiki, jQuery  */

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

	var $dialog, $alert, $namespace, inited = false, api = new mw.Api();

	/**
	 * Alert in jQuery UI.
	 */
	function alert( msg ) {
		if ( !$alert ) {
			$alert = $( '<div>' ).attr( 'id', 'gc-dialog-alert' ).appendTo( 'body' );
		}
		$alert.html( '<p>' + msg + '</p>' );
		$alert.dialog( {
			title: 'Accessorio cambusa',
			modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( 'close' );
				}
			}
		} );
	}

	/**
	 * Legge una sezione della pagina corrente.
	 */
	function getSection( section, readHandler ) {
		api.get( {
			action: 'query',
			format: 'json',
			prop: 'revisions',
			titles: mw.config.get( 'wgPageName' ),
			rvprop: 'content',
			rvsection: section,
		} ).done ( function ( data ) {
			var text = data.query.pages[Object.keys( data.query.pages )[0]].revisions[0][ '*' ];
			readHandler( text );
		} ).fail ( function ( code, data ) {
			alert( 'Errore nel leggere la discussione: ' + code );
		} );
	}

	/**
	 * Modifica una sezione della pagina corrente.
	 */
	function setSection( section, text, summary ) {
		api.postWithEditToken( {
			action: 'edit',
			format: 'json',
			title: mw.config.get( 'wgPageName' ),
			section: section,
			text: text,
			summary: summary
		} ).done ( function ( data ) {
			___location.reload();
		} ).fail ( function ( code, data ) {
			alert( 'Errore nel modificare la discussione: ' + code );
		} );
	}

	/**
	 * Aggiunge una sezione a una pagina di discussione.
	 */
	function addSection( title, sectiontitle, text, handlerSuccess ) {
		api.newSection( title, sectiontitle, text )
			.done( function ( data ) {
				handlerSuccess();
			} )
			.fail ( function ( code, data ) {
				alert( 'Errore nel creare la discussione: ' + data.error.info );
			} );
	}

	/**
	 * Aggiunge l'autocompletamento per i titoli delle pagine.
	 */
	function addAutocomplete( inputEl ) {
		inputEl.autocomplete( {
			source: function( request, response ) {
				$.ajax( {
					url: mw.util.wikiScript( 'api' ),
					data: {
						action: 'query',
						list: 'allpages',
						aplimit: '10',
						apprefix: request.term,
						apnamespace: $namespace.find( 'option:selected' ).data( '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 );
					} );
			},
			change: function ( e, ui ) {
				if ( !ui.item ) {
					e.target.value = '';
				}
			}
		} );
	}

	function addInputText( $fieldset, id, size, label , value ) {
		var $input = $( '<input/>' ).attr( 'id', id ).attr( 'type', 'text' )
			.attr( 'size', size ).val( value );
		if ( label ) {
			$( '<label>' ).attr( 'for', id ).text( label ).appendTo( $fieldset );
			$fieldset.append( '<br/>' );
		}
		$fieldset.append( $input, '<br/>' );
		return $input;
	}

	/**
	 * Visualizza la finestra di dialogo per inserire i dati per la cambusa.
	 */
	function showDialogCambusa( section, sectiontitle ) {
		var $fieldset, $dest, $desttitle, $incipit;
		// crea il dialog
		if ( !$dialog ) {
			$dialog = $( '<div>' ).attr( 'id', 'gc-dialog-cambusa' ).appendTo( 'body' );
		}
		$dialog.html( '' );
		$fieldset = $( '<fieldset>' ).css( 'border-color', 'gray' ).appendTo( $dialog );
		$( '<legend>' ).text( 'Cambusa la discussione' ).appendTo( $fieldset );
		$namespace = $( '<select>' ).attr( 'id', 'gc-ns' ).css( 'width', '150px' );
		$.each( mw.config.get( 'wgFormattedNamespaces' ), function ( key, value ) {
			if ( key > 0 && key % 2 === 1 ) {
				$( '<option>' ).html( value ).data( 'ns', key ).appendTo( $namespace );
			}
		} );
		$( '<label>' ).attr( 'for', 'gc-ns' ).text( 'Namespace e pagina di destinazione:' ).appendTo( $fieldset );
		$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 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;
						getSection( section, function ( text ) {
							var from = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
							text = text.substring( text.indexOf( '==', 3 ) + 2 );
							text = text.replace( /^\s+/g, '' );
							text = '{{Cambusada|' + from + '|--~~' + '~~}}\n' + text;
							addSection( dest, desttitle, text, function () {
								text = '== ' + sectiontitle + ' ==\n' + '{{Cambusa|' + incipit + '|' + dest + '|--~~' + '~~}}';
								setSection( section, text, 'cambusata discussione in [[' + dest + ']]' );
							} );
						} );
					}
				},
				'Annulla': function () {
					$( this ).dialog( 'close' );
				}
			}
		} );
	}

	function addLinks() {
		$( 'span.mw-editsection > a:first-of-type:not( .mw-editsection-visualeditor )' ).each( function ( i, el ) {
			var $link, sectiontitle = $( this ).attr( 'title' ).split( 'Modifica la sezione ' )[1];
			$link = $( '<a>' )
				.attr( 'href', '#' )
				.attr( 'title', 'Cambusa la discussione' )
				.text( 'cambusa' )
				.click( function ( event ) {
					event.preventDefault();
					mw.loader.using( [ 'jquery.ui.dialog', 'mediawiki.api.edit', 'jquery.ui.autocomplete' ], function () {
						showDialogCambusa( i + 1, sectiontitle );
					} );
				} );
			$( this ).after( '<span> | </span>', $link );
		} );
		inited = true;
	}

	$( function () {
		if ( mw.config.get( 'wgAction' ) === 'view' && mw.config.get( 'wgNamespaceNumber' ) % 2 === 1 ) {
			var portletLink = mw.util.addPortletLink( 'p-tb', '#', 'Cambusa', 't-cambusa', 'Aggiunge il link cambusa a ogni discussione' );
			$( portletLink ).click( function ( event ) {
				event.preventDefault();
				if ( !inited ) {
					addLinks();
				}
			} );
		}
	} );
}( mediaWiki, jQuery ) );