User:Grunny/ajaxcloakrequest.js

/** * Ajax Wookieepedia Cloak Request * * Creates a form to submit cloak requests * * Please let me know of any suggested improvements * * @author Grunny * @version 0.0.1 * */

( function( $ ) {

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

window.AjaxCloakRequestScript = { version: '0.0.1',

/* Initialize the script */ init: function {

if ( wgPageName === 'User:Darth_Culator/cloaksignupprototype' ) {

$( '' ).text( 'Make Request' ).attr( 'href', '#' ).click( function {

AjaxCloakRequestScript.createPopUpForm;

} ).appendTo( '#request-button' );			}

},

/* Create the pop-up form */ createPopUpForm: function { var popupHTML = '' + ' ' + ' Wookieepedia Cloak Request ' + '' + ' ' +				'' + ' ' +				' ' + 'Format or abbreviate username as:  ' + ' IRC nick:  ' + ' Existing cloak to be kept or replaced: ' + ' None Keep  Replace  ' + ' Cloak type: ' + ' Plain administrator bureaucrat  ' + ' ' +				'<input type="button" id="acr-startbutton" value="Submit" />' + ' ' +				' ' +				' ' +				' ' +				' ';			$( 'body' ).append( popupHTML ); if ( skin !== 'oasis' ) { appendCSS( '.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%; }' +							'.modalWrapper .close { float: right; margin: 0 18px 0 0; }' ); }			$( '#acr-kor' ).change( function {				if ( $( this ).val === 'keep' || $( this ).val === 'replace' ) {					$( this ).parent.after( ' <label for="acr-oldc">Your current cloak: <input type="text" style="width: 20em;" id="acr-oldc" /> ' );					if ( $( this ).val === 'keep' ) {						$( '#acr-oldc' ).after( ' <label for="acr-otherc">New cloak format: <input type="text" style="width: 20em;" id="acr-otherc" /> (e.g. @wookieepedia/wikipedia.Grunny) ' );					}				}			} ); $( '#acr-closebutt' ).click( function {				AjaxCloakRequestScript.closePopUpForm;			} ); $( '#acr-startbutton' ).click( function {				AjaxCloakRequestScript.getEditToken;			} ); },

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

/* API Edit functions */ getEditToken: function { var	nick = $( '#acr-nick' ).val; if( !nick ) { return; }			var url = wgServer + 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;				AjaxCloakRequestScript.getPageText( editToken );			} ); },

getPageText: function ( editToken ) { var	abbrUserName = $( '#acr-abbrusername' ).val, nick = $( '#acr-nick' ).val, keepOrReplace = $( '#acr-kor' ).val, oldCFormat = 'uncloaked', newCFormat = '', cloakType = $( '#acr-type' ).val, requestText = '', summary = 'Requesting Wookieepedia cloak';

if ( keepOrReplace === 'keep' || keepOrReplace === 'replace' ) { oldCFormat = $( '#acr-oldc' ).val; if ( keepOrReplace === 'keep' ) { newCFormat = $( '#acr-otherc' ).val; }			}

requestText = '\n*\n**Format or abbreviate username as: ' + ( ( abbrUserName === '' ) ? wgUserName : abbrUserName ) + '\n**IRC nick: ' + nick + '\n***Existing cloak to be kept or replaced: "' + oldCFormat + ( newCFormat !== '' ? '" replace with "' + newCFormat : '"' ) + '\n**Cloak type: ' + cloakType + '\n**' + '';

AjaxCloakRequestScript.makeAPIEdit( summary, requestText, editToken ); },

makeAPIEdit: function ( summary, content, editToken ) { $.ajax( {				url: wgScriptPath + '/api.php?',				data: 'action=edit&title=' + encodeURIComponent( wgPageName ) + '&summary=' + encodeURIComponent( summary ) + '&section=1&appendtext=' + 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( AjaxCloakRequestScript.init );

} )( jQuery );