User:DannyS712/Scripts writer.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>
$( function () {

var ScriptsWriter = {};
window.ScriptsWriter = ScriptsWriter;

// ********** BEGIN UserScript class ********
// Represents a single script that is included in the issue
function UserScript( userName, scriptName, linkTarget, descriptionText ) {
	this.userName = userName;
	this.scriptName = scriptName;
	this.linkTarget = linkTarget;
	this.descriptionText = descriptionText;
}
UserScript.prototype.getDisplay = function () {
	var $link = $( '<a>' )
		.attr( 'href', mw.util.getUrl( this.linkTarget ) )
		.attr( 'target', '_blank' )
		.text( userName + '\'s ' + scriptName );
	var $display = $( '<span>' )
		.append( $link )
		.append( ' ' + this.descriptionText );
	return $display;
};
UserScript.prototype.getWikitext = function () {
	var link = '[[' + linkTarget + '|' + userName + '\'s' + scriptName + ']]';
	var wikitext = link + ' ' + this.descriptionText;
	return wikitext;
};
// ********** END UserScript class ********

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

ScriptsWriter.run = function () {
	// TODO fill in Widgets
	// Featured script
	var featuredScriptInput = new OO.ui.TextInputWidget( {
		value: ''
	} );
	var featuredScriptLayout = new OO.ui.FieldLayout(
		featuredScriptInput,
		{ label: 'Featured script' }
	);


	var submit = new OO.ui.ButtonInputWidget( { 
		label: 'Publish',
		flags: [
			'primary',
			'progressive'
		]
	} );
	submit.on( 'click', function () {
		// CONVERT YOUR WIDGETS TO INPUT
		var submittedValues = {};
		console.log( submittedValues );
		ScriptsWriter.onSubmit( submittedValues );
	} );
	$( window ).on( 'keypress', function ( e ) {
		// press enter to start
		if ( e.which == 13 ) {
			submit.simulateLabelClick();
		}
	} );
	var fieldSet = new OO.ui.FieldsetLayout( { 
		label: 'Scripts writer'
	} );
	fieldSet.addItems( [
		featuredScriptLayout,
		new OO.ui.FieldLayout( submit )
	] );
	
	var $results = $( '<div>' )
		.attr( 'id', 'ScriptsWriter-results' );
	$( '#mw-content-text' ).empty().append(
		fieldSet.$element,
		$( '<hr>' ),
		$results
	);
};

ScriptsWriter.onSubmit = function ( inputs ) {
	$( '#ScriptsWriter-results' ).empty();
	console.log( inputs );
};
	
} );

$( document ).ready( function () {
	if ( mw.config.get( 'wgNamespaceNumber' ) === -1 ) {
		var page = mw.config.get( 'wgCanonicalSpecialPageName' );
		if ( page === 'Blankpage' ) {
			const page2 = mw.config.get( 'wgTitle' ).split( '/' );
			if ( page2[1] && page2[1] === 'ScriptsWriter' ) {
				window.ScriptsWriter.init();
			}
		}
	}
} );

// </nowiki>