User:Enterprisey/CustomSummaryPresets.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* * CustomSummaryPresets.js
 *
 * Displays a highly configurable menu of custom edit summaries.
 * See documentation at [[User:Enterprisey/CustomSummaryPresets]].
 *
 * Mostly copied on 2018-07-24 from https://en.wikipedia.org/w/index.php?title=&oldid=807346245,
 * some code also taken from https://en.wikipedia.org/w/index.php?title=&oldid=498931785
 *
 * Older history:
 * Imported as of 09/06/2011 from [[User:ErrantX/defaultsummaries.js]]
 * Edited version from [[User:MC10/defaultsummaries.js]]
 * Implements default edit summary dropdown boxes
 */

var $summaryBox = $( '#wpSummary' );
if( typeof customGeneralSummaries === "undefined" ) {
        customGeneralSummaries = [];
        if (typeof customsum1 != "undefined") customGeneralSummaries.push( customsum1 );
        if (typeof customsum2 != "undefined") customGeneralSummaries.push( customsum2 );
        if (typeof customsum3 != "undefined") customGeneralSummaries.push( customsum3 );
        if (typeof customsum4 != "undefined") customGeneralSummaries.push( customsum4 );
        if (typeof customsum5 != "undefined") customGeneralSummaries.push( customsum5 );
        if (typeof customsum6 != "undefined") customGeneralSummaries.push( customsum6 );
        if (typeof customsum7 != "undefined") customGeneralSummaries.push( customsum7 );
        if (typeof customsum8 != "undefined") customGeneralSummaries.push( customsum8 );
        if (typeof customsum9 != "undefined") customGeneralSummaries.push( customsum9 );
        if (typeof customsum10 != "undefined") customGeneralSummaries.push( customsum10 );
        if (typeof customsum11 != "undefined") customGeneralSummaries.push( customsum11 );
        if (typeof customsum12 != "undefined") customGeneralSummaries.push( customsum12 );
        if (typeof customsum13 != "undefined") customGeneralSummaries.push( customsum13 );
        if (typeof customsum14 != "undefined") customGeneralSummaries.push( customsum14 );
        if (typeof customsum15 != "undefined") customGeneralSummaries.push( customsum15 );
        if (typeof customsum16 != "undefined") customGeneralSummaries.push( customsum16 );
        if (typeof customsum17 != "undefined") customGeneralSummaries.push( customsum17 );
        if (typeof customsum18 != "undefined") customGeneralSummaries.push( customsum18 );
        if (typeof customsum19 != "undefined") customGeneralSummaries.push( customsum19 );
        if (typeof customsum20 != "undefined") customGeneralSummaries.push( customsum20 );
}

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: 'Custom edit summaries – click to use'
		} );

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

	addOptionsToDropdown( dropdown, customGeneralSummaries );
	if( typeof customNamespaceSummaries != "undefined" && customNamespaceSummaries[namespace + ''] ) {
		addOptionsToDropdown( dropdown, customNamespaceSummaries[namespace + ''] );
	}

	$insertBeforeThis.before( dropdown.$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 (i.e. the normal wikicode editor)
$.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, '38%' );
} );