Jump to content

User:SD0001/BDCS.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.
/**
 * Script for the Birthday Committee
 * Rip-off from Twinkle's Welcome module ([[MediaWiki:Gadget-friendlywelcome.js]])
 */

// <nowiki>
$.when(
	mw.loader.using('ext.gadget.morebits'), 
	$.ready
).then(function() {

if (!mw.config.get('wgRelevantUserName')) {
	return;
}

var Wish = {};
window.Wish = Wish;

Wish.advert = ' ([[User:SD0001/BDCS|BDCS]])';

var li = mw.util.addPortletLink('p-cactions', '#', 'BDC wish', 'p-wish', 'Wish user');
li.addEventListener('click', function() {
	Wish.callback(mw.config.get('wgRelevantUserName'));
});


Wish.callback = function (uid) {
	if (uid === mw.config.get('wgUserName') && !confirm('Are you really sure you want to wish yourself?...')) {
		return;
	}
	Morebits.wiki.api.setApiUserAgent('[[w:User:SD0001/BDCS.js]]');

	var Window = new Morebits.simpleWindow(600, 420);
	Window.setTitle('Wish user');
	Window.addFooterLink('Birthday Committee', 'WP:BDC');

	var form = new Morebits.quickForm(Wish.callback.evaluate);

	form.append({
		type: 'select',
		name: 'type',
		label: 'Type of wish: ',
		event: Wish.populateWishList,
		list: [
			{ type: 'option', value: 'birthday', label: 'Happy Birthday', selected: true },
			{ type: 'option', value: 'firsteditday', label: 'Happy First Edit Day' },
			{ type: 'option', value: 'anniv', label: 'Happy Adminship Anniversary' }
		].concat(
			window.BDCS_CustomTemplates && window.BDCS_CustomTemplates.length ?
			[ { type: 'option', value: 'custom', label: 'Custom templates' } ] : []
		)
	});


	form.append({
		type: 'div',
		id: 'wishWorkArea'
	});

	form.append({
		type: 'input',
		name: 'heading',
		label: 'Section heading: ',
		value: 'Happy Birthday!'
	});


	var previewlink = document.createElement('a');
	$(previewlink).click(function() {
		Wish.callbacks.preview(result);  // |result| is defined below
	});
	previewlink.style.cursor = 'pointer';
	previewlink.textContent = 'Preview';
	form.append({ type: 'div', id: 'wishpreview', label: [ previewlink ] });
	form.append({ type: 'div', id: 'wish-previewbox', style: 'display: none' });

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

	var result = form.render();
	Window.setContent(result);
	Window.display();
	result.previewer = new Morebits.wiki.preview($('#wish-previewbox').last()[0]);

	// initialize the wish list
	var evt = document.createEvent('Event');
	evt.initEvent('change', true, true);
	result.type.dispatchEvent(evt);
};

Wish.populateWishList = function(e) {
	var type = e.target.value;
	var form = e.target.form;
	var container = new Morebits.quickForm.element({ type: 'fragment' });

	var appendTemplates = function(list) {
		container.append({
			type: 'radio',
			name: 'template',
			list: list.map(function(obj) {
				return {
					value: obj,
					label: '{{' + obj + '}}'
				}
			})
		});
	};

	switch (type) {
		case 'birthday':
			form.heading.value = 'Happy Birthday!';
			appendTemplates([
				'Happy Birthday',
				'Happy Birthday 2',
				'Happy Birthday 3',
				'Happy Birthday 4',
				'Happy Birthday 5',
				'Happy Birthday 6',
				'Happy Birthday 7',
				'Happy Birthday 8',
				'Happy Birthday 9',
				'Happy Birthday 10',
				'Happy Birthday 11',
				'Happy Birthday 12',
				'Happy Birthday 13'
			]);
			break;

		case 'firsteditday':
			form.heading.value = 'Happy First Edit Day!';
			appendTemplates([
				'First Edit Day',
				'First Edit Day 2',
				'First Edit Day 3',
				'First Edit Day 4',
			]);
			break;

		case 'anniv':
			form.heading.value = 'Happy Adminship Anniversary!';
			appendTemplates([
				'Happy Adminship',
				'Happy Adminship 2',
				'Happy Adminship 3',
				'Happy Adminship 4',
				'Happy Adminship 5',
				'Happy Adminship 6',
				'Happy Adminship 7',
				'Happy Bureaucratship',
				'Happy Bureaucratship 2',
				'Happy Bureaucratship 3',
			]);
			break;

		case 'custom':
			form.heading.value = '';
			appendTemplates(window.BDCS_CustomTemplates);
			break;

		default:
			container.append({ type: 'div', label: 'Wish.populateWishList: something went wrong' });
			break;
	}

	var rendered = container.render();
	$('#wishWorkArea').empty().append(rendered);

	var firstRadio = form.template[0] || form.template;
	firstRadio.checked = true;
};


Wish.getTemplateWikitext = function(template, heading) {
	return '==' + heading + '==\n{{subst:' + template + '}}';
};

Wish.callbacks = {
	preview: function(form) {
		form.previewer.beginRender(Wish.getTemplateWikitext(form.getChecked('template'), form.heading.value), 'User talk:' + mw.config.get('wgRelevantUserName'));
	},
	main: function(pageobj) {
		var params = pageobj.getCallbackParameters();
		var text = pageobj.getPageText();

		var wishText = Wish.getTemplateWikitext(params.value, params.heading);

		text += '\n' + wishText;

		summaryText = (params.heading ? ('/* ' + params.heading + ' */ ') : '') + 'new section';
		
		pageobj.setPageText(text);
		pageobj.setEditSummary(summaryText + Wish.advert);
		pageobj.setCreateOption('nocreate');
		pageobj.save();
	}
};

Wish.callback.evaluate = function wishEvaluate(e) {
	var form = e.target;

	var params = {
		value: form.getChecked('template'),
		heading: form.heading.value
	};

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

	var userTalkPage = mw.config.get('wgFormattedNamespaces')[3] + ':' + mw.config.get('wgRelevantUserName');
	Morebits.wiki.actionCompleted.redirect = userTalkPage;
	Morebits.wiki.actionCompleted.notice = 'Wishing complete, reloading talk page in a few seconds';

	var wikipedia_page = new Morebits.wiki.page(userTalkPage, 'User talk page modification');
	wikipedia_page.setFollowRedirect(true);
	wikipedia_page.setCallbackParameters(params);
	wikipedia_page.load(Wish.callbacks.main);
};

});
// </nowiki>