MediaWiki:Gadget-resumsperdefecte.js

De la Viquipèdia, l'enciclopèdia lliure

Nota: Després de desar, heu de netejar la memòria cau del navegador per veure els canvis. En la majoria de navegadors amb Windows o Linux, premeu Ctrl+F5 o bé premeu Shift i cliqueu el botó "Actualitza" (Ctrl i "Actualitza" amb Internet Explorer). Vegeu més informació i instruccions per a cada navegador a Viquipèdia:Neteja de la memòria cau.

/* 
 * Implementa llistes desplegables amb resums d'edició per defecte
 * Importat l'11 de gener de 2019 de https://en.wikipedia.org/w/index.php?title=MediaWiki:Gadget-defaultsummaries.js&oldid=877886403
 */

/* global mediaWiki, ve */

( function ( $, mw ) { // Wrap with anonymous function
	var $summaryBox = $( '#wpSummary' ),
		minorSummaries = [
			"Correcció lingüística",
			"Correcció d'estil o de disseny",
			"Reversió de [[Viquipèdia:Vandalisme|vandalisme]] o d'edició de prova",
			"Reversió d'eliminació de contingut sense explicació",
			"Retocs de format menors"
		],
		articleSummaries = [
			'Expansió del contingut',
			'Referències afegides/millorades',
			'Categories afegides/eliminades',
			'Enllaços externs afegits/eliminats',
			'Enllaços interns afegits/eliminats',
			'Eliminació de contingut sense referenciar',
			'Eliminació d\'enllaços externs abusius',
			'Neteja',
			'Revisió general de l\'article'
		],
		nonArticleSummaries = [
			'Resposta',
			'Comentari',
			'Suggeriment'
		],
		talkPageSummaries = [
			//'[[Wikipedia:WikiProject|WikiProject]] tagging',
			//'[[Wikipedia:WikiProject|WikiProject]] assessment'
		];

	function addOptionsToDropdown( dropdown, optionTexts ) {
		dropdown.menu.addItems( optionTexts.map( function ( optionText ) {
			return new OO.ui.MenuOptionWidget( { label: optionText } );
		} ) );
	}

	function onSummarySelect( option ) {
		// Save the original value of the edit summary field
		var editsummOriginalSummary = $summaryBox.val(),
			canned = option.getLabel(),
			newSummary = editsummOriginalSummary;

		// Append old edit summary with space, if exists,
		// and last character != space
		if ( newSummary.length !== 0 && newSummary.charAt( newSummary.length - 1 ) !== ' ' ) {
			newSummary += ' ';
		}
		newSummary += canned;
		$summaryBox.val( newSummary ).trigger( 'change' );
	}

	function insertSummaryOptions( $insertBeforeThis, dropdownWidth ) {
		// For convenience, add a dropdown box with some canned edit
		// summaries to the form.
		var namespace = mw.config.get( 'wgNamespaceNumber' ),
			dropdown = new OO.ui.DropdownWidget( {
				label: 'Resums d\'edició més comuns'
			} ),
			minorDropdown = new OO.ui.DropdownWidget( {
				label: 'Resums d\'edició menor més comuns'
			} );

		dropdown.$element.css( 'width', dropdownWidth );
		dropdown.menu.on( 'select', onSummarySelect );

		minorDropdown.$element.css( 'width', dropdownWidth );
		minorDropdown.menu.on( 'select', onSummarySelect );

		addOptionsToDropdown( minorDropdown, minorSummaries );

		if ( namespace === 0 ) {
			addOptionsToDropdown( dropdown, articleSummaries );
		} else {
			addOptionsToDropdown( dropdown, nonArticleSummaries );
			if ( namespace % 2 !== 0 && namespace !== 3 ) {
				addOptionsToDropdown( dropdown, talkPageSummaries );
			}
		}

		$insertBeforeThis.before( dropdown.$element );
		$insertBeforeThis.before( minorDropdown.$element );
	}
	// VisualEditor
	mw.hook( 've.saveDialog.stateChanged' ).add( function () {
		var target, $saveOptions;
		// .ve-init-mw-viewPageTarget-saveDialog-checkboxes
		if ( $( 'body' ).data( 'wppresent' ) ) { return; }
		$( 'body' ).data( 'wppresent', 'true' );

		target = ve.init.target;
		$saveOptions = target.saveDialog.$saveOptions;
		$summaryBox = target.saveDialog.editSummaryInput.$input;
		if ( !$saveOptions.length ) {
			return;
		}
		insertSummaryOptions( $saveOptions );
	} );
	// WikiEditor
	$.when( mw.loader.using( 'oojs-ui-core' ), $.ready ).then( function () {
		var $editCheckboxes = $( '.editCheckboxes' );

		// If we failed to find the editCheckboxes class
		if ( !$editCheckboxes.length ) {
			return;
		}
		insertSummaryOptions( $editCheckboxes, '48%' );
		$( "#wpSummary" ).on( "keydown", function ( e ) {
			if( e.key === "Enter" || ( e.keyCode || e.which ) === 13 ) $( "#wpSave" ).trigger( "click" );
		} );
	} );
} )( jQuery, mediaWiki ); // End wrap with anonymous function