User:SD0001/FAC-helper.js

/** * Script to easily make Feature Article Candidate (FAC) * nominations. */

/* */ /* jshint maxerr: 999 */

var fac = {}; window.fac = fac;

$.when(	mw.loader.using('ext.gadget.morebits'),	$.ready ).then(function {	if ([0, 1].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) {		mw.util.addPortletLink('p-cactions', '#', 'FAC', 'fac-portlet', 'Nominate for FAC');		$('#fac-portlet').click(fac.callback);	} });

fac.advert = ' (FAC-helper)';

fac.callback = function facCallback(e) { if (e) e.preventDefault;

var Window = new Morebits.simpleWindow(600, 600); Window.setTitle( "Nominate article for FAC" ); Window.setScriptName('FAC-helper'); Window.addFooterLink('FAC instructions', 'WP:FAC');

var form = new Morebits.quickForm(fac.evaluate);

var field = form.append({		type: 'field',		label: 'Details',		name: 'details'	});

field.append({		type: 'textarea',		name: 'reason',		label: 'Rationale:',		style: 'height: 130px',		tooltip: 'You can expand the rationale after the page has been saved as well.',		value: 'This article is about ...'	});

var previewlink = Morebits.htmlNode('a', 'Preview'); previewlink.style.cursor = "pointer"; $(previewlink).click(function {		// |result| is defined below		fac.preview(result);	}); form.append( { type: 'div', id: 'facpreview', label: [ previewlink ] } ); form.append( { type: 'div', id: 'fac-previewbox', style: 'display: none' } );

form.append({ type: 'submit', label: 'Submit' });

var result = form.render; Window.setContent(result); Window.display; fac.previewbox = document.getElementById('fac-previewbox'); result.previewer = new Morebits.wiki.preview(fac.previewbox);

};

fac.initParams = function (form) { fac.params = Morebits.quickForm.getInputData(form); var title_obj = mw.Title.newFromText(Morebits.pageNameNorm); fac.title = title_obj.getSubjectPage.toText; fac.talktitle = title_obj.getTalkPage.toText; };

fac.preview = function (form) { fac.initParams(form);

Morebits.status.init(fac.previewbox); $(fac.previewbox).show;

fac.tasks.getNumber.then(function {		form.previewer.beginRender(fac.tasks.getNomPageText, fac.facpage);	}, function  {		form.previewer.beginRender("Unable to render preview", fac.facpage);	}); };

fac.evaluate = function (e) { var form = e.target; fac.initParams(form);

Morebits.simpleWindow.setButtonsEnabled(false); Morebits.status.init(form);

var tm = new Morebits.taskManager; tm.add(fac.tasks.getNumber, []); tm.add(fac.tasks.editTalkPage, []); tm.add(fac.tasks.createNomPage, [ fac.tasks.getNumber ], function {		Morebits.status.printUserText(fac.tasks.getNomPageText, 'Your nomination text is provided below, with which you can manually create the page ' + fac.facpage + '.');	}); tm.add(fac.tasks.transcludeToFacPage, [ fac.tasks.createNomPage ]); tm.execute.then(function {		Morebits.status.actionCompleted('Nomination completed.');	}); };

fac.tasks = { getNumber: function { var query = { action: 'query', format: 'json', formatversion: '2', list: 'allpages', apnamespace: '4', // WP			apprefix: 'Featured article candidates/' + fac.title + '/', aplimit: 'max' };		var api = new Morebits.wiki.api('Getting numbering', query); return api.post.then(function(apiobj) {			var pages = apiobj.response.query.allpages;			fac.num = pages.length + 1; // HACK			fac.facpage = 'Wikipedia:Featured article candidates/' + fac.title + '/archive' + fac.num;			apiobj.getStatusElement.info('Next number is ' + fac.num);		}); },

editTalkPage: function { var def = $.Deferred; var talkpage = new Morebits.wiki.page(fac.talktitle, 'Editing talk page'); talkpage.load(function(talkpage) {			// prepend tag:			var text = '\n' + talkpage.getPageText;			talkpage.setPageText(text);			talkpage.setEditSummary('Nominating to FAC' + fac.advert);			talkpage.save(def.resolve, def.reject);		}, def.reject); return def; },

getNomPageText: function { // XXX: Should this be a template? return '===' + fac.title + '===\n' + '\n' + ' \n' + ' \n' + ':Nominator(s): ~ \n' + '\n' + fac.params.reason.trim + (fac.params.reason.trim.endsWith('~') ? '' : ' ~'); },

createNomPage: function { var def = $.Deferred; var appendtext = fac.tasks.getNomPageText; var pageobj = new Morebits.wiki.page(fac.facpage, 'Creating nomination page'); pageobj.setAppendText(appendtext); pageobj.setCreateOption('createonly'); pageobj.setWatchlist(true); pageobj.setEditSummary('Creating FAC nomination page' + fac.advert); pageobj.append(def.resolve, def.reject); return def; },

transcludeToFacPage: function { var def = $.Deferred; var pageobj = new Morebits.wiki.page('Wikipedia:Featured article candidates', 'Transcluding to FAC page'); pageobj.setPageSection(1); pageobj.load(function (nomspage) {			var text = nomspage.getPageText;			var transclusion = '';			var newText = text.replace(/==\s*Nominations\s*==\n/, '$&' + transclusion + '\n');			if (newText === text) {				pageobj.getStatusElement.error('Failed to add transclusion at WP:FAC page. ' +				'Please manually edit WP:FAC to add  at the top.');				return def.resolve;			}			pageobj.setPageText(newText);			pageobj.setEditSummary('Adding ' + fac.facpage + '' + fac.advert);			pageobj.save(def.resolve, def.reject);		}); return def; }

};

/* */