MediaWiki:QuickCreateMessageWallGreeting/code.js

/** * Ajax Message Wall Greeting Creation * Attempts to create your message wall greeting * Adapted from Grunny's QuickCreateUserPage tool */ ( function( $, mw, window ) {	'use strict';	var QuickCreateMessageWallGreeting = {		init: function {			var $qcLink = $( ' ' ).attr( 'id', 'quick-cup' ).html( $( '' ).attr( 'title', QuickCreateMessageWallGreeting.langMsg( 'button-tooltip', true ) ) .attr( 'style', 'cursor: pointer' ).text( QuickCreateMessageWallGreeting.langMsg( 'button-link-text', true ) ) .click( QuickCreateMessageWallGreeting.createMessageWallGreeting ) );			if ( mw.config.get( 'skin' ) === 'oasis' ) {				$( '#AccountNavigation' ).find( 'li > ul.subnav' ).prepend( $qcLink );			} else {				$( '#column-one' ).find( '#p-personal > .pBody > ul' ).prepend( $qcLink );			}		},		langConfig: {			//English			en: {				'button-link-text': 'Create message wall greeting',				'button-tooltip': 'Automagically create your message wall greeting',				'buttontalk-link-text': 'Create message wall greeting',				'buttontalk-tooltip': 'Create your message wall greeting on this wiki automagically',				'cup-reason': 'Creating message wall greeting',				'cup-success-text': 'Successfully created greeting!',				'cup-error-exists': 'Greeting already exists!',				'cup-error-failed': 'Creating greeting failed!'			}		},		langMsg: function ( name, userLang ) {			if ( userLang && mw.config.get( 'wgUserLanguage' ) in QuickCreateMessageWallGreeting.langConfig && name in QuickCreateMessageWallGreeting.langConfig[mw.config.get( 'wgUserLanguage' )] ) { return QuickCreateMessageWallGreeting.langConfig[mw.config.get( 'wgUserLanguage' )][name]; }			if ( !userLang && mw.config.get( 'wgContentLanguage' ) in QuickCreateMessageWallGreeting.langConfig && name in QuickCreateMessageWallGreeting.langConfig[mw.config.get( 'wgContentLanguage' )] ) { return QuickCreateMessageWallGreeting.langConfig[mw.config.get( 'wgContentLanguage' )][name]; }			return QuickCreateMessageWallGreeting.langConfig.en[name]; },		createMessageWallGreeting: function { var	MessageWallGreetingContent = window.qtMessageWallGreetingTemplate || '', pageName = 'Message_Wall_Greeting:' + mw.config.get( 'wgUserName' ), overwriteMessageWallGreeting = window.qtEnableMessageWallGreetingOverwrite || false; if ( overwriteMessageWallGreeting === true ) { QuickCreateMessageWallGreeting.makeEdit( pageName, MessageWallGreetingContent ); return; }			$.getJSON( mw.util.wikiScript( 'api' ), {				action: 'query',				prop: 'revisions',				titles: pageName,				format: 'json'			} ).done( function ( data ) {				var	pageIds = Object.keys( data.query.pages ),					pageId = pageIds[0];				if ( pageId !== '-1' ) {					QuickCreateMessageWallGreeting.showResult( 'ok', 'cup-error-exists' );				} else {					QuickCreateMessageWallGreeting.makeEdit( pageName, MessageWallGreetingContent );				}			} ); },		makeEdit: function( pageName, MessageWallGreetingContent ) { $.ajax( {				type: 'POST',				url: mw.util.wikiScript( 'api' ),				dataType: 'json',				data: {					action: 'edit',					title: pageName,					summary: QuickCreateMessageWallGreeting.langMsg( 'cup-reason', false ),					text: MessageWallGreetingContent,					format: 'json',					token: mw.user.tokens.get( 'editToken' )				}			} ).done( function ( data ) {				if ( data.edit.result === 'Success' ) {					QuickCreateMessageWallGreeting.showResult( 'ok', 'cup-success-text' );				} else {					QuickCreateMessageWallGreeting.showResult( 'error', 'cup-error-failed' );				}			} ).fail( function ( data ) {				QuickCreateMessageWallGreeting.showResult( 'error', 'cup-error-failed' );			} ); },		showResult: function( result, message ) { if ( mw.config.get( 'skin' ) === 'monobook' ) { mw.util.$content.prepend(					'  ' + QuickCreateMessageWallGreeting.langMsg( message, true ) + ' ' +					' '				); } else { var resultClass = ( result === 'error' ? 'error' : 'confirm' ); window.GlobalNotification.show( QuickCreateMessageWallGreeting.langMsg( message, true ), resultClass ); }		}	};	$( QuickCreateMessageWallGreeting.init ); }( jQuery, mediaWiki, this ) );