User:Grunny/ajaxquickrfdclose.js

From Simple English Wikipedia, the free encyclopedia

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.
/**
 * Ajax Quick RFD Closure
 * 
 * Creates an Ajax RFD close tool for use on Simple English Wikipedia
 *
 * Please let me know of any suggested improvements
 * 
 * @author Grunny ([[simple:User:Grunny]])
 * @version 0.0.2
 * 
 * <nowiki>
 */

( function( $ ) {

	// Don't load twice...
	if ( typeof window.SimpleWikiRFDScript !== 'undefined' ) {
		return false;
	}

	window.SimpleWikiRFDScript = {
		version: '0.0.2',

		/* Initialize the script */
		init: function () {

			if( mw.config.get( 'wgNamespaceNumber' ) === 4 && mw.config.get( 'wgTitle' ).match(/Requests for deletion\/Requests\//)
				&& !$( 'a[title="Category:Requests for deletion that succeeded"]' ).length && !$( 'a[title="Category:Requests for deletion that did not succeed"]' ).length ) {

				var menuId = ( skin === 'vector' ? '#p-cactions > .menu > ul' : '#p-cactions > .pBody > ul' );
				$( menuId ).append( '<li id="rfd-quick-close"></li>' );
				$('<a />').text('Quick Close').attr('href', '#').click( function () {

					SimpleWikiRFDScript.createPopUpForm();

				} ).appendTo( '#rfd-quick-close' );
			}

		},

		/* Create the pop-up form */
		createPopUpForm: function () {
			var popupHTML = '<section style="left: 50%; top: 50px; width: 1000px; z-index: 20000002; margin-left: -520px;" class="modalWrapper">'
				+'<span class="quickrfd-closebutton" onclick="SimpleWikiRFDScript.closePopUpForm()"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/65/Crystal_button_cancel.svg/18px-Crystal_button_cancel.svg.png"></span>'
				+'<h1>Quick RFD close</h1>'
				+'<section class="modalContent">'
				+'<div class="AjaxQuickRFDBox" id="AjaxQuickRFDBox">'
				+'<form class="AjaxQuickRFDForm">'
				+'<fieldset>'
				+'<p><label for="aqr-result">Result: </label><select id="aqr-result" style="width: 20em;"><option value="kept">Kept</option><option value="deleted">Deleted</option><option value="other">Other</option></select></p>'
				+'<p><label for="aqr-reason">Reason (optional): </label><input type="text" style="width: 20em;" id="aqr-reason" /></p>'
				+'</fieldset>'
				+'<input type="button" id="aqr-startbutton" value="Submit" onclick="SimpleWikiRFDScript.getEditToken()" />'
				+'</form>'
				+'</div>'
				+'</section>'
				+'</section>'
				+'<div class="blackout" style="height: 6702px; z-index: 20000001; opacity: 0.65;"></div>';
			$( 'body' ).append( popupHTML );
			mw.util.addCSS( '.modalWrapper { -moz-box-shadow: 0 10px 60px #7F7F7F; background: none repeat scroll 0 0 white; border: 5px solid #BACDD8; padding: 25px 15px; position: absolute; }'
						+'section { display: block; }'
						+'.blackout { background: none repeat scroll 0 0 white; left: 0; opacity: 0.8; position: absolute; top: 0; width: 100%; }' );
			$( '#aqr-result' ).change( function () {
				if( $(this).val() === 'other' ) {
					$(this).parent().after( '<p><label for="aqr-otherresult">Other result: </label><input type="text" style="width: 20em;" id="aqr-otherresult" /> (e.g. merged or redirected)</p>' );
				}
			} );
		},

		closePopUpForm: function () {
			$('section.modalWrapper').remove();
			$('div.blackout').remove();
		},

		/* API Edit functions */
		getEditToken: function () {
			var	result = $( '#aqr-result' ).val(),
				reasoning = $( '#aqr-reason' ).val();
			if( !result ) {
				return;
			}
			if( !reasoning ) {
				reasoning = '';
			}
			var url = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?action=query&prop=info|revisions&intoken=edit&titles=Main%20Page&format=json';
			$.getJSON( url, function (data) {
				for ( var p in data.query.pages ) {
					break;
				}
				var editToken = data.query.pages[p].edittoken;
				SimpleWikiRFDScript.getPageText( editToken, result, reasoning );
			} );
		},

		getPageText: function ( editToken, result, reasoning ) {
			var url = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?action=query&format=json&prop=revisions&titles=' + encodeURIComponent( mw.config.get( 'wgPageName' ) ) + '&rvlimit=1&rvprop=content|timestamp';
			$.getJSON( url, function (data) {
				for ( var p in data.query.pages ) {
					break;
				}
				var pageText = data.query.pages[p].revisions[0]["*"];
				pageText = pageText
					.replace( /\<noinclude\>\{\{Template:RfD\/Transclusion Reminder\}\} \<\!-- This is the big red box --\>\<\/noinclude\>\n/, '' )
					.replace( /\<noinclude\>\{\{in progress\}\}\<\/noinclude\>\n/, '' )
					.replace( /\<\!-- Please change it to either \{\{deleted\|optional reason\}\} or \{\{kept\|optional reason\}\}\.\s+NOTE: We are now placing the outcome at the top of the RFD\.\s+Also, the archival templates \{\{subst:archive top\}\} and \{\{subst:archive bottom\}\} are placed at the top and bottom respectively\.--\>\n/, '' );

				if( result === 'other' && $( '#aqr-otherresult' ).length ) {
					var	otherresult = $( '#aqr-otherresult' ).val(),
						summary = 'Closing RFD as ' + otherresult;
					pageText = '{{subst:' + 'Archive-top}}\n{{RfDresult|' + otherresult + '|2=' + reasoning + ' ~~' + '~~}}\n' + pageText;
				} else {
					var summary = 'Closing RFD as ' + result;
					pageText = '{{subst:' + 'Archive-top}}\n{{' + result + '|1=' + reasoning + ' ~~' + '~~}}\n' + pageText;
				}
				pageText += '\n{{subst:' + 'Archive-bottom}}';

				SimpleWikiRFDScript.makeAPIEdit( summary, pageText, editToken );
			} );
		},

		makeAPIEdit: function ( summary, content, editToken ) {
			$.ajax( {
				url: mw.config.get( 'wgScriptPath' ) + '/api.php?',
				data: 'action=edit&title=' + encodeURIComponent( mw.config.get( 'wgPageName' ) ) + '&summary=' + encodeURIComponent( summary ) + '&text=' + encodeURIComponent( content ) + '&format=json&token=' + encodeURIComponent( editToken ),
				dataType: 'json',
				type: 'POST',
				success: function( data ) {
					if ( data.edit.result === "Success" ) {
						window.location.reload(); // reload page if edit was successful
					} else {
						alert( 'Error: Unknown result from API.' );
					}
				},
				error: function( xhr ) {
					alert( 'Error: Edit failed.' );
				}
			} );
		}

	};

	$(document).ready( SimpleWikiRFDScript.init );

} )( jQuery );
// </nowiki>