Jump to content

User:DannyS712/EasyGAReview.js

From Wikipedia, the free encyclopedia
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.
// <nowiki>
// Quick script to simplify reviewing Good Article nominations
// @author DannyS712
$(() => {
const EasyGAReview = {};
window.EasyGAReview = EasyGAReview;

EasyGAReview.init = function () {
	window.document.title = 'EasyGAReview';
	$( '#firstHeading' ).text( 'EasyGAReview' );
	mw.loader.using(
		[ 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows', 'mediawiki.util', 'mediawiki.api' ],
		EasyGAReview.run
	);
};

EasyGAReview.elements = {};
EasyGAReview.getCheck = function () {
	// Represents input for {{GAList/check}} in different parameters
	// to {{GAList}}
	return new OO.ui.RadioSelectInputWidget( {
		options: [
			{ data: 'unreviewed', label: 'unreviewed' },
			{ data: 'pass', label: 'pass' },
			{ data: 'fail', label: 'fail' },
			{ data: 'hold', label: 'on hold' },
			{ data: 'nuetral', label: 'neutral' }
		]
	} );
};
EasyGAReview.createElements = function () {
	// Using {{GAList}}, matching template inputs
	// TODO add links to the text (labels can be jQuery)
	var e = {};
	
	e.overallComment = new OO.ui.TextInputWidget();
	e.overallCommentLayout = new OO.ui.FieldLayout( e.overallComment, { label: 'Overall comment' } );
	
	e.criteria1 = new OO.ui.LabelWidget( { label: 'Criteria #1: It is reasonably well written' } );
	e.criteria1Layout = new OO.ui.FieldLayout( e.criteria1 );
	e.criteria1a = EasyGAReview.getCheck();
	e.criteria1aLayout = new OO.ui.FieldLayout( e.criteria1a, { label: 'Criteria #1a: prose, spelling, and grammar' } );
	e.criteria1b = EasyGAReview.getCheck();
	e.criteria1bLayout = new OO.ui.FieldLayout( e.criteria1b, { label: 'Criteria #1b: MoS for lead, layout, word choice, fiction, and lists' } );
	e.criteria1Comment = new OO.ui.TextInputWidget();
	e.criteria1CommentLayout = new OO.ui.FieldLayout( e.criteria1Comment, { label: 'Criteria #1: comment' } );
	
	e.criteria2 = new OO.ui.LabelWidget( { label: 'Criteria #2: It is factually accurate and verifiable' } );
	e.criteria2Layout = new OO.ui.FieldLayout( e.criteria2 );
	e.criteria2a = EasyGAReview.getCheck();
	e.criteria2aLayout = new OO.ui.FieldLayout( e.criteria2a, { label: 'Criteria #2a: reference section' } );
	e.criteria2b = EasyGAReview.getCheck();
	e.criteria2bLayout = new OO.ui.FieldLayout( e.criteria2b, { label: 'Criteria #2b: citations to reliable sources' } );
	e.criteria2c = EasyGAReview.getCheck();
	e.criteria2cLayout = new OO.ui.FieldLayout( e.criteria2c, { label: 'Criteria #2c: original research' } );
	e.criteria2d = EasyGAReview.getCheck();
	e.criteria2dLayout = new OO.ui.FieldLayout( e.criteria2d, { label: 'Criteria #2d: copyvio and plagiarist' } );
	e.criteria2Comment = new OO.ui.TextInputWidget();
	e.criteria2CommentLayout = new OO.ui.FieldLayout( e.criteria2Comment, { label: 'Criteria #2: comment' } );
	
	e.criteria3 = new OO.ui.LabelWidget( { label: 'Criteria #3: It is broad in its coverage' } );
	e.criteria3Layout = new OO.ui.FieldLayout( e.criteria3 );
	e.criteria3a = EasyGAReview.getCheck();
	e.criteria3aLayout = new OO.ui.FieldLayout( e.criteria3a, { label: 'Criteria #3a: major aspects' } );
	e.criteria3b = EasyGAReview.getCheck();
	e.criteria3bLayout = new OO.ui.FieldLayout( e.criteria3b, { label: 'Criteria #3b: focused' } );
	e.criteria3Comment = new OO.ui.TextInputWidget();
	e.criteria3CommentLayout = new OO.ui.FieldLayout( e.criteria3Comment, { label: 'Criteria #3: comment' } );
	
	e.criteria4 = EasyGAReview.getCheck();
	e.criteria4Layout = new OO.ui.FieldLayout( e.criteria4, { label: 'Criteria #4: It follows the neutral point of view policy' } );
	e.criteria4Comment = new OO.ui.TextInputWidget();
	e.criteria4CommentLayout = new OO.ui.FieldLayout( e.criteria4Comment, { label: 'Criteria #4: comment' } );
	
	e.criteria5 = EasyGAReview.getCheck();
	e.criteria5Layout = new OO.ui.FieldLayout( e.criteria5, { label: 'Criteria #5: It is stable' } );
	e.criteria5Comment = new OO.ui.TextInputWidget();
	e.criteria5CommentLayout = new OO.ui.FieldLayout( e.criteria5Comment, { label: 'Criteria #5: comment' } );
	
	e.criteria6 = new OO.ui.LabelWidget( { label: 'Criteria #6: It is illustrated by images and other media, where possible and appropriate' } );
	e.criteria6Layout = new OO.ui.FieldLayout( e.criteria6 );
	e.criteria6a = EasyGAReview.getCheck();
	e.criteria6aLayout = new OO.ui.FieldLayout( e.criteria6a, { label: 'Criteria #6a: images are tagged and non-free content have fair use rationales' } );
	e.criteria6b = EasyGAReview.getCheck();
	e.criteria6bLayout = new OO.ui.FieldLayout( e.criteria6b, { label: 'Criteria #6b: appropriate use with suitable captions' } );
	e.criteria6Comment = new OO.ui.TextInputWidget();
	e.criteria6CommentLayout = new OO.ui.FieldLayout( e.criteria6Comment, { label: 'Criteria #6: comment' } );
	
	e.result = EasyGAReview.getCheck();
	e.resultLayout = new OO.ui.FieldLayout( e.result, { label: 'Result' } );
	e.resultComment = new OO.ui.TextInputWidget();
	e.resultCommentLayout = new OO.ui.FieldLayout( e.resultComment, { label: 'Result comment' } );
	
	EasyGAReview.elements = e;
};
EasyGAReview.getExtraSpace = function () {
	return new OO.ui.FieldLayout( new OO.ui.LabelWidget( { label: '' } ) );
};
EasyGAReview.run = function () {
	mw.util.addCSS(`
		.oo-ui-fieldLayout.oo-ui-labelElement {
			margin-top: 0;
		}
		.oo-ui-radioOptionWidget {
			display: inline;
			padding-right: 5px;
		}
	`);
	var submit = new OO.ui.ButtonInputWidget( { 
		label: 'Generate review',
		flags: [ 'primary', 'progressive' ]
	} );
	submit.on( 'click', function () {
		EasyGAReview.onSubmit();
	} );
	$( window ).on( 'keypress', function ( e ) {
		// press enter to start
		if ( e.which == 13 ) {
			submit.simulateLabelClick();
		}
	} );
	
	var fieldSet = new OO.ui.FieldsetLayout( { 
		label: 'Easily go through the GA review checklist'
	} );
	
	EasyGAReview.createElements();
	var e = EasyGAReview.elements;
	fieldSet.addItems( [
		e.overallCommentLayout,
		
		e.criteria1Layout,
		e.criteria1aLayout,
		e.criteria1bLayout,
		e.criteria1CommentLayout,
		
		e.criteria2Layout,
		e.criteria2aLayout,
		e.criteria2bLayout,
		e.criteria2cLayout,
		e.criteria2dLayout,
		e.criteria2CommentLayout,
		
		e.criteria3Layout,
		e.criteria3aLayout,
		e.criteria3bLayout,
		e.criteria3CommentLayout,
		
		EasyGAReview.getExtraSpace(),
		e.criteria4Layout,
		e.criteria4CommentLayout,
		
		EasyGAReview.getExtraSpace(),
		e.criteria5Layout,
		e.criteria5CommentLayout,
		
		e.criteria6Layout,
		e.criteria6aLayout,
		e.criteria6bLayout,
		e.criteria6CommentLayout,
		
		EasyGAReview.getExtraSpace(),
		e.resultLayout,
		e.resultCommentLayout,
		new OO.ui.FieldLayout( submit )
	] );

	var $results = $( '<div>' )
		.attr( 'id', 'EasyGAReview-results' );
	$( '#mw-content-text' ).empty().append(
		fieldSet.$element,
		$( '<hr>' ),
		$results
	);
};

EasyGAReview.onSubmit = function () {
	var $res = $( '#EasyGAReview-results' );
	$res.empty();
	console.log( 'Submitted' );
	
	var generatedTemplate = EasyGAReview.getTemplate();
	$res.append(
		$( '<pre>' ).text( generatedTemplate ),
		$( '<div>' )
			.attr( 'id', 'EasyGAReview-preview' )
			.text( 'Loading preview' )
	);
	EasyGAReview.showPreview( generatedTemplate );
};

EasyGAReview.getTemplate = function () {
	var e = EasyGAReview.elements;
	return '{{subst:GAList' +
		'|overcom=' + e.overallComment.getValue() +
		'|1a=' + e.criteria1a.getValue() +
		'|1b=' + e.criteria1b.getValue() +
		'|1com=' + e.criteria1Comment.getValue() +
		'|2a=' + e.criteria2a.getValue() +
		'|2b=' + e.criteria2b.getValue() +
		'|2c=' + e.criteria2c.getValue() +
		'|2d=' + e.criteria2d.getValue() +
		'|2com=' + e.criteria2Comment.getValue() +
		'|3a=' + e.criteria3a.getValue() +
		'|3b=' + e.criteria3b.getValue() +
		'|3com=' + e.criteria3Comment.getValue() +
		'|4=' + e.criteria4.getValue() +
		'|4com=' + e.criteria4Comment.getValue() +
		'|5=' + e.criteria5.getValue() +
		'|5com=' + e.criteria5Comment.getValue() +
		'|6a=' + e.criteria6a.getValue() +
		'|6b=' + e.criteria6b.getValue() +
		'|7=' + e.result.getValue() +
		'|7com=' + e.resultComment.getValue() +
		'}}';
};

EasyGAReview.onErrHandler = function () {
	// Shared error handler
	alert( 'Something went wrong' );
	console.log( arguments );
};

EasyGAReview.showPreview = function ( generatedTemplate ) {
	new mw.Api().get( {
		action: 'parse',
		text: generatedTemplate,
		prop: 'text',
		pst: true,
		contentmodel: 'wikitext',
		formatversion: 2
	} ).then(
		function ( response ) {
			console.log( response );
			$( '#EasyGAReview-preview' )
				.empty()
				.html( response.parse.text );
		},
		EasyGAReview.onErrHandler
	);
};

});

$( document ).ready( () => {
	mw.loader.using(
		[ 'mediawiki.util' ],
		function () {
			mw.util.addPortletLink(
				'p-tb',
				'/wiki/Special:BlankPage/EasyGAReview',
				'EasyGAReview'
			);
		}
	);
	if ( mw.config.get( 'wgNamespaceNumber' ) === -1 ) {
		const page = mw.config.get( 'wgCanonicalSpecialPageName' );
		if ( page === 'Blankpage' ) {
			const page2 = mw.config.get( 'wgTitle' ).split( '/' );
			if ( page2[1] && page2[1] === 'EasyGAReview' ) {
				window.EasyGAReview.init();
			}
		}
	}
});

// </nowiki>