User:AzaToth/test/morebits-test.js

// Script depends on jQuery dialog widget mw.loader.using( 'jquery.ui', function {	// Construct object (to prevent namespace conflicts)	Twinkle.morebitsTest = {

launchDialog: function( userInterface ) { userInterface.dialog('open'); },		initSimpleWindow: function { var Window = new SimpleWindow( 600, 400 ); Window.setTitle( "Test morebits.js" ); Window.display; var form = new QuickForm( null ); var main_group = form.append( {				type:'select',				name:'main_group',				event:null			} ); var result = form.render; Window.setContent( result ); Window.display; result.main_group.root = result; Status.init( result ); Wikipedia.actionCompleted.redirect = wgPageName; Wikipedia.actionCompleted.notice = "Test complete, reloading talk page in a few seconds"; },

setPageOptions: function(page) { page.setEditSummary( $('#editSummary').val ); if ( $('#runTestForm input[name="followRedirect"]').attr('checked') ) { page.setFollowRedirect(true); }			if ( $('#runTestForm input[name="minorEdit"]').attr('checked') ) { page.setMinorEdit(true); }			if ( $('#runTestForm input[name="watchlist"]').attr('checked') ) { page.setWatchlist(true); }			if ( $('#runTestForm input[name="watchlistFromPreferences"]').attr('checked') ) { page.setWatchlistFromPreferences(true); }			if ( $('#runTestForm input[name="noRetries"]').attr('checked') ) { page.setMaxConflictRetries(0); page.setMaxRetries(0); }			var section = $('#runTestForm input[name="sectionNumber"]').val; if ( section != "" ) { page.setPageSection( Number( section ) ); }					page.setCreateOption(morebits_test_createOption);

if ( $('#runTestForm input[name="lookupCreator"]').attr('checked') ) { page.lookupCreator(Twinkle.morebitsTest.lookupCreatorCallback); }		},		loadCallbackInsert: function(page) { var params = page.getCallbackParameters; var text = page.getPageText; var pos = text.indexOf(params['beforeText']); if (pos == -1) { alert('Search text "' + params['beforeText'] + '" not found!'); return; }			page.setPageText(text.substr(0, pos) + params['newText'] + text.substr(pos)); page.save(Twinkle.morebitsTest.finalSaveCallback); },		loadCallbackReplace: function(page) { var params = page.getCallbackParameters; page.setPageText(params['newText']); page.save(Twinkle.morebitsTest.finalSaveCallback); },

lookupCreatorCallback: function(page) { alert("Page was created by: " + page.getCreator); },

finalSaveCallback: function(page) { Wikipedia.actionCompleted.redirect = page.getPageName; // get result of redirects },

initialize: function {

// Define runTest interface // Can also use alternative syntax new to jQuery 1.4: //   $(' ').html( 'Text to be added:' ) // → $(' ', { css: { 'margin-top': '0.4em' }, text: 'Text to be added:' } )

morebits_test_createOption = null;

Twinkle.morebitsTest.$runTests = $(' ') .append( $(' ').html( 'Text to be added: ' ).append( $(' ') ) ) .append( $(' ').html( 'Insert text before (for insert mode only): ' ).append( $(' ') ) ) .append( $(' ').html( 'Edit summary: ' ).append( $(' ') ) ) .append( $(' ').html( 'Section number: ' ) ) .append( $(' ').html(' Follow redirect') ) .append( $(' ').html(' Minor edit') ) .append( $(' ').html(' Add to watchlist') ) .append( $(' ').html(' Add to watchlist based on preference settings') ) .append( $(' ').html(' Disable retries') ) .append( $(' ').html(' Lookup page creator ') ) .append( $(' ').html(' Create page if needed, unless deleted since loaded ') ) .append( $(' ').html(' Create page if needed ') ) .append( $(' ').html(' Only create a new page ') ) .append( $(' ').html(' Do not create a new page ') ) .dialog({					width: 500,					autoOpen: false,					title: 'Test Wikipedia.page class',					modal: true,					buttons: { 						"Append": function { 							$(this).dialog('close');							Twinkle.morebitsTest.initSimpleWindow;							var page = new Wikipedia.page(wgPageName);							page.setAppendText( $('#message').val );							Twinkle.morebitsTest.setPageOptions(page);							page.append(Twinkle.morebitsTest.finalSaveCallback);						},						"Prepend": function(e) { 							$(this).dialog('close');							Twinkle.morebitsTest.initSimpleWindow;							var page = new Wikipedia.page(wgPageName);							page.setPrependText( $('#message').val );							Twinkle.morebitsTest.setPageOptions(page);							page.prepend(Twinkle.morebitsTest.finalSaveCallback);						},						"Insert": function(e) { 							if ( $('#beforeText').val == "" ) {								alert ("Text to insert before must be specified!"); return; }							$(this).dialog('close'); Twinkle.morebitsTest.initSimpleWindow; var page = new Wikipedia.page(wgPageName); page.setCallbackParameters( {								beforeText: $('#beforeText').val, 								newText: $('#message').val								}); Twinkle.morebitsTest.setPageOptions(page); page.load(Twinkle.morebitsTest.loadCallbackInsert); },						"Replace": function(e) { $(this).dialog('close'); Twinkle.morebitsTest.initSimpleWindow; var page = new Wikipedia.page(wgPageName); page.setCallbackParameters( {								newText: $('#message').val								}); Twinkle.morebitsTest.setPageOptions(page); page.load(Twinkle.morebitsTest.loadCallbackReplace); }					}				}); // close .dialog

} // close initialize function

} // close Twinkle.morebitsTest object Twinkle.morebitsTest.initialize; }); // close mw.loader

Twinkle.morebitsTestInit = function { if ( wgAction == 'view' && skin == 'vector' && wgNamespaceNumber >= 0 ) { twAddPortletLink( ("javascript:Twinkle.morebitsTest.launchDialog(Twinkle.morebitsTest.$runTests)"), "Test", "tw-test", "Test morebits.js", ""); } }

// register initialization callback Twinkle.init.moduleReady( "morebits-test", Twinkle.morebitsTestInit );