Jump to content

User:Siddhartha Ghai/MTC.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>
var mtc = {};

mtc.callback = function callback() {
	var dialog;
	mtc.dialog = new SimpleWindow(550, 355 );
	dialog = mtc.dialog;
	dialog.setTitle( 'Create International Membership Templates' );
	dialog.setScriptName( 'MTC' );
	dialog.addFooterLink( 'Project page', 'Wikipedia:International membership templates' );
	dialog.addFooterLink( 'MTC help', 'User:Siddhartha Ghai/MTC' );

	var form = new QuickForm( mtc.evaluate);
	form.append({
		type: 'input',
		name: 'cname',
		label: 'Name of the country',
		value: ''
	});
	form.append({
		type: 'input',
		name: 'clink',
		label: 'Display name of the country, if different from the name (optional)',
		value: ''
	});
	form.append({
		type: 'input',
		name: 'sdate',
		label: 'Start date of the country(yyyy-mm-dd)',
		value: ''
	});
	form.append({
		type: 'input',
		name: 'edate',
		label: 'End date of the country(yyyy-mm-dd)',
		value: ''
	});
	form.append({
		type: 'checkbox',
		name: 'orgs',
		list: mtc.orglist
	});
	form.append( { type:'submit' } );
	var result = form.render();
	dialog.setContent( result );
	dialog.display();
};

mtc.orglist = [
	{label:'African union', value:'AU', name:'AU', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'Arab League', value:'AL', name:'AL', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'Commonwealth of Nations', value:'CON', name:'CON', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'East African Community', value:'EAC', name:'EAC', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'European Union', value:'EU', name:'EU', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'Interpol', value:'IP', name:'IP', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'North American Treaty Organisation', value:'NATO', name:'NATO', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'Organisation of Islamic Cooperation', value:'OIC', name:'OIC', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'Organisation of Petroleum Exporting Countries', value:'OPEC', name:'OPEC', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'United Nations', value:'UN', name:'UN', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	},
	{label:'World Trade Organisation', value:'WTO', name:'WTO', 
		subgroup: {type: 'input', label: 'Enter the joining date', name: 'sdate', value: ''}
	}
];

mtc.create = function create(pageobj) {
	var statelem = pageobj.getStatusElement();
	var i, pname, pdate;

/*	if (pageobj.exists()) {
		statelem.error( 'The template already exists! Please edit it manually.' );
		return;
	}
*/	
	var params = pageobj.getCallbackParameters();
	var code = '<noinclude>{{Membership/Sub/Head}}</noinclude>\n{{Membership/Sub/T\n';
	code+= '|start=' + params.sdate + '\n';
	if(params.edate.value!=='') {
	code+= '|end=' + params.edate + '\n';
	}
	code+= '|CountryNamesup={{{CountryCodesup|}}}\n' + '|year={{{year|{{CURRENTYEAR}}}}}\n|month={{{month|{{CURRENTMONTH2}}}}}\n' + '|day={{{day|{{CURRENTDAY}}}}}\n|Datesup={{{Datesup|}}}\n';
//	alert(code);
	for (i in params.tags) {
//		alert(i);
//		if (params.tags[i].date !== '') {
			pname = params.tags[i].value;
//			ptext = params[i].value + 'sdate';
			pdate = params.tags[i].sdate;
//			alert(pdate);
			if (params.tags[i].checked) {
				if (pdate === '') {
					code+= '|' + pname + 'org={{{' + pname + 'org|}}}';
				}
				else {
					code+= '|' + pname + 'org={{{' + pname + 'org|' + pname + '}}}';
				}
			}
			else {
				code+= '|' + pname + 'org={{{' + pname + 'org|}}}';
			}
			code+= '|' + pname + 'sup={{{' + pname + 'sup|}}}|' + pname + '={{{' + pname + '|Show}}}';
			if (params.tags[i].checked && pdate!== '') {
				code+= '|' + pname + 'membership={{{' + pname + 'membership|' + pdate + '}}}';
			}
			code+= '\n';
//		}
	}
	code+= '|CountryName=' + params.cname + '\n';
	code+= '}}';
	pageobj.setPageText(code);
//	alert(code);
	pageobj.setEditSummary('Creating template using MTC');
	pageobj.setCreateOption('recreate');
	pageobj.save();
};

mtc.evaluate = function evaluate(e) {
	var i;
	var params = {
		sdate: e.target.sdate.value,
		edate: e.target.edate.value,
		cname: e.target.cname.value,
		clink: e.target.clink.value
	};
	
	params.tags = {
		AU: e.target.AU,
		AL: e.target.AL,
		CON: e.target.CON,
		EAC: e.target.EAC,
		EU: e.target.EU,
		IP: e.target.IP,
		NATO: e.target.NATO,
		OIC: e.target.OIC,
		OPEC: e.target.OPEC,
		UN: e.target.UN,
		WTO: e.target.WTO
	};
	for (i in params.tags) {
		if (i.match(/sdate/) === null ) {
			var sdid = params.tags[i].value + '.sdate';
			if (e.target[sdid] !== undefined) {
				params.tags[i].sdate = e.target[sdid].value;
			}
			else {
				params.tags[i].sdate = '';
			}
		}
	}
	
//	params.orgs = $.extend(true, {}, e.target.orgs);
	
	var pname = 'User:Siddhartha Ghai/Sandbox' /*'Template:Membership/test' + params.cname*/;

	SimpleWindow.setButtonsEnabled( false );
	Status.init( e.target );

	Wikipedia.actionCompleted.redirect = pname;
	Wikipedia.actionCompleted.notice = 'Template created';
	var wikipedia_page = new Wikipedia.page(pname);
	wikipedia_page.setCallbackParameters(params);
	wikipedia_page.load(mtc.create);
};

mw.loader.using( ['mediawiki.util','jquery.ui','jquery.tipsy'], function(){
	mw.loader.load('//en.wiki.x.io/w/index.php?title=Mediawiki:Gadget-morebits.js&action=raw&ctype=text/javascript');
	mw.loader.load('//en.wiki.x.io/w/index.php?title=Mediawiki:Gadget-morebits.css&action=raw&ctype=text/css', 'text/css');
	$(document).ready(mw.util.addPortletLink( 'p-variants', 'javascript:mtc.callback()', 'MTC', 'ca-mtc-main', 'Try this!' ));
});
// </nowiki>