MediaWiki:Gadget-defaultsummaries.js

From Old School Near-Reality Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
  1 /*
  2 * Modified version of https://en.wikipedia.org/wiki/MediaWiki:Gadget-defaultsummaries.js
  3 */
  4 
  5 $(function() {
  6 	var $summaryBox = $( '#wpSummary' ),
  7 		$saveButton = $( '#wpSave' ),
  8 		lastkey = 'rsw-defsum-last',
  9 		uselastkey = 'rsw-defsum-uselast',
 10 		hasls =  false,
 11 		minorSummaries = [
 12 			'Fixed spelling',
 13 			'Correcting [[RS:LAYOUT|page layout]]',
 14 			'Revert vandalism',
 15 			'Reworded',
 16 			'Remove [[RS:POLICIES|policy]] violation'
 17 		],
 18 		articleSummaries = [
 19 			'Expanding',
 20 			'Adding [[RS:REF|refs]]',
 21 			'Adding links',
 22 			'Maintenance'
 23 		],
 24 		nonArticleSummaries = [
 25 			'Reply',
 26 			'Comment',
 27 			'Support',
 28 			'Oppose'
 29 		];
 30 	
 31 	try {
 32 		localStorage.setItem('test', 'test')
 33 		localStorage.removeItem('test')
 34 		hasls = true;
 35 	} catch (e) {
 36 		mw.warn('Browser does not support localStorage');
 37 		hasls =  false;
 38 	}
 39 
 40 	function addOptionsToDropdown( dropdown, optionTexts ) {
 41 		dropdown.menu.addItems( optionTexts.map( function ( optionText ) {
 42 			return new OO.ui.MenuOptionWidget( { label: optionText } );
 43 		} ) );
 44 	}
 45 
 46 	function onSummarySelect( option ) {
 47 		// Save the original value of the edit summary field
 48 		var editsummOriginalSummary = $summaryBox.val(),
 49 			canned = option.getLabel(),
 50 			newSummary = editsummOriginalSummary;
 51 
 52 		// Append old edit summary with space, if exists,
 53 		// and last character != space
 54 		if ( newSummary.length !== 0 && newSummary.charAt( newSummary.length - 1 ) !== ' ' ) {
 55 			newSummary += ' ';
 56 		}
 57 		newSummary += canned;
 58 		$summaryBox.val( newSummary ).trigger( 'change' );
 59 	}
 60 	
 61 	function useLastChange ( selected ) {
 62 		if (!hasls) {
 63 			return;
 64 		}
 65 		if (!selected) {
 66 			localStorage.setItem(uselastkey, 'false');
 67 			return;
 68 		}
 69 		
 70 		localStorage.setItem(uselastkey, 'true');
 71 		var lastSum = localStorage.getItem( lastkey );
 72 		if (lastSum) {
 73 			$summaryBox.val( lastSum ).trigger( 'change' );
 74 		}
 75 	}
 76 
 77 	function insertSummaryOptions( $insertBeforeThis, dropdownWidth ) {
 78 		// For convenience, add a dropdown box with some canned edit
 79 		// summaries to the form.
 80 		var namespace = mw.config.get( 'wgNamespaceNumber' ),
 81 			dropdown = new OO.ui.DropdownWidget( {
 82 				label: 'Pre-defined summaries'
 83 			} ),
 84 			minorDropdown = new OO.ui.DropdownWidget( {
 85 				label: 'Pre-defined minor summaries'
 86 			} ),
 87 			useLastTog = new OO.ui.CheckboxInputWidget( {
 88 				selected: false
 89 			} ),
 90 			togLay = new OO.ui.FieldLayout( useLastTog, {
 91 				label: 'Use last summary',
 92 				align: 'inline'
 93 			} );
 94 
 95 		dropdown.$element.css( 'width', dropdownWidth );
 96 		dropdown.menu.on( 'select', onSummarySelect );
 97 
 98 		minorDropdown.$element.css( 'width', dropdownWidth );
 99 		minorDropdown.menu.on( 'select', onSummarySelect );
100 
101 		addOptionsToDropdown( minorDropdown, minorSummaries );
102 		
103 		if (hasls) {
104 			var lastSummary = localStorage.getItem( lastkey ),
105 				useLast = localStorage.getItem( uselastkey );
106 			useLastTog.on('change', useLastChange );
107 			if ( useLast == 'true' ) {
108 				useLastTog.setSelected(true);
109 			}
110 			if (lastSummary) {
111 				articleSummaries.unshift(lastSummary);
112 				nonArticleSummaries.unshift(lastSummary);
113 			}
114 		} else {
115 			useLastTog.setDisabled(true);
116 		}
117 
118 		if ( namespace === 0 ) {
119 			addOptionsToDropdown( dropdown, articleSummaries );
120 		} else {
121 			addOptionsToDropdown( dropdown, nonArticleSummaries );
122 		}
123 
124 		togLay.$element.css('margin-top', 0);
125 		$insertBeforeThis.before( togLay.$element );
126 		$insertBeforeThis.before( dropdown.$element );
127 		$insertBeforeThis.before( minorDropdown.$element );
128 	}
129 	
130 	function onSave() {
131 		mw.log('saving article');
132 		var summary = $summaryBox.val();
133 		localStorage.setItem(lastkey, summary);
134 	}
135 	
136 	// VisualEditor
137 	mw.hook( 've.saveDialog.stateChanged' ).add( function () {
138 		var target, $saveOptions;
139 		// .ve-init-mw-viewPageTarget-saveDialog-checkboxes
140 		if ( $( 'body' ).data( 'wppresent' ) ) { return; }
141 		$( 'body' ).data( 'wppresent', 'true' );
142 
143 		target = ve.init.target;
144 		$saveOptions = target.saveDialog.$saveOptions;
145 		$summaryBox = target.saveDialog.editSummaryInput.$input;
146 		$saveButton = target.saveDialog.$primaryActions.find('a');
147 		
148 		if ( !$saveOptions.length ) {
149 			return;
150 		}
151 		$saveButton.on('click', onSave);
152 		
153 		insertSummaryOptions( $saveOptions );
154 	} );
155 	// WikiEditor
156 	$.when( mw.loader.using( 'oojs-ui-core' ), $.ready ).then( function () {
157 		var $editCheckboxes = $( '.editCheckboxes' );
158 		
159 		// If we failed to find the editCheckboxes class
160 		if ( !$editCheckboxes.length ) {
161 			return;
162 		}
163 		
164 		$saveButton.on('click', onSave);
165 		
166 		insertSummaryOptions( $editCheckboxes, '48%' );
167 	} );
168 })