Jump to content

User:Theleekycauldron/Scripts/PSHAW/protocols/prep builder.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>
Copyright (c) 2023 theleekycauldron

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
$.when(
	mw.loader.using('ext.gadget.morebits'),
	$.ready
).then(async function() {
	console.log("ready!");
	if (mw.config.get('wgPageName').includes("Template:Did_you_know_nominations/")) {
		$(mw.util.addPortletLink(window.DYKH_portlet || 'p-cactions', '#', 'Promote (PSHAW)', 'dyk-portlet', 'Close nomination')).click(promote);
	}
});

promote = async function (e) {
	nomination = await Nomination.create(mw.config.get('wgPageName'));
	if (nomination.isHatted){
		alert("Hang on, you can't promote a closed nomination!");
		return;
	}
	if (nomination.hasBeenEditedByUser()){
		alert("Looks like you've edited this nompage before. Make sure you're not an \"involved user\" before proceeding with a close; if you've nominated or edited the article, reviewed the nomination, or wrote the hook you're about to promote, you are most likely involved.");
	}
	//nomtext = nomtext.replaceAll(/<s>.*?<\/s>/g,"");
	e.preventDefault();
	var Window = new Morebits.simpleWindow(900, 800);
	Window.setTitle("The prep set's half-assed workbench (PSHAW)");
	Window.addFooterLink("DYK promoter's rules", "Wikipedia:Did you know/Prep builder instructions");
	var form = new Morebits.quickForm();
	
	//prep and queue selection
	
	form.append({type: 'select',name: 'area',label: 'Transport hook to:',list: [{ type: 'option', label: "Prep", value: "Template:Did you know/Preparation area "},{ type: 'option', label: "Queue (admins only)", value: "Template:Did you know/Queue/"}]});
	form.append({type: 'number',name: 'pqid',label: 'Prep/queue number:',min: 1,max: 7,step: 1})
	
	// render
	let result;
	function show(){
		result = form.render();
		Window.setContent(result);
		Window.display();
	}
	
	show();
	
	// load up the prep/queue
	
	let firstLoad = true;
	
	form.append({type: 'button',name: 'load', label: 'Load prep/queue', event: load});
	
	async function load(){
		console.log("howdy :)");
		let pagename = result.area.value+result.pqid.value;
		
		let nextpreppage = await get_pages("Template:Did you know/Queue/NextPrep","user|content",null,"older");
		await nextpreppage;
		let nextpreptext = nextpreppage[0].revisions[0].slots.main.content[0];
		let previousprep = (parseInt(nextpreptext[0]-2)%7+1).toString();
		
		let dolastprepmessage = previousprep == result.pqid.value;
		
		console.log(form);
		form.root.childs[2].data.disabled = true; //disable button
		show(); //reload while button is being disabled
		if (firstLoad){ //these should only be done once
			if (!nomination.isApproved){
				alert("Looks like you're trying to promote a nomination that hasn't been approved yet. Make sure the nomination's got a green or grey check mark!")
			}
			
			form.append({type: 'div', name: 'preppagename', label: "<b>[["+pagename+"]]</b>"}) //child 5
			form.append({name: 'prephooks', type: 'radio', label: 'Hooks:', list: []});
			form.append({type: 'div', name: 'hooktopromote', label: '<b>Hook to promote: </b>'})
			form.append({type: 'radio', name: 'nomhooks', list: []});
		} else { //update prepnum
			form.root.childs[3].data.label = "<b>[["+pagename+"]]</b>";
		}
		
		prep = await Prep.create(pagename);

		form.root.childs[4].data.list = prep.morebits();
		console.log(nomination.hooks);
		form.root.childs[6].data.list = nomination.morebits();
		
		if (firstLoad){
			console.log(nomination.files);
			optionsbox = [{type: 'option', label: 'nope', value: "{{main page image/DYK|image=example-serious.jpg|caption=Caption goes here}}"}];
			for (var file of nomination.files){
				optionsbox.push(file.morebits())
			}
			console.log(optionsbox);
			form.append({type: 'div', name: 'label', label: '<b>File: </b>'});
			form.append({type: 'radio', name: 'images', list: optionsbox});
			
			form.append({type: 'div', name: 'lastprepmessage', label: ""});
			form.append({type: 'button', name: 'submit', label: 'submit!', event: function l(){evaluate(e, result)}});
		}
		
		form.root.childs[9].data.label = (dolastprepmessage)?"<i>This appears to be the bottom prep set – per [[WP:DYKSG#J14|J14]], try to leave the image slot, quirky slot, and at least two middle slots open for bumps and replacements!</i>":"";
		
		firstLoad = false;
		form.root.childs[2].data.disabled = false; //re-enable button
		show();
	}
	show();
	
	evaluate = async function (e, result){
		console.log(result)
		let slotnum = JSON.parse(result.prephooks.value).id;
		if (slotnum === ""){
			alert("Which slot is this hook going in?");
			return;
		}
		
		let hook = JSON.parse(result.nomhooks.value);
		if (hook == ""){
			alert("You gotta pick a hook!");
			return;
		}

		let imgfile;
		if (result.images==undefined){
			imgfile="{{main page image/DYK|image=example-serious.jpg|caption=Caption goes here}}";
		} else {
			imgfile=result.images.value;
		}
		console.log(imgfile)
		if (imgfile==""){ //scrub ''(pictured)'' from hook if no image
			hook.content = hook.content.replace(/[^']''\(.*?\)''/,"");
		}
		
		if (slotnum == 0 && imgfile == ""){
			alert("You can't put this in the lead slot unless you've got an image to go along with it!");
			return;
		}
		
		form.root.childs = [];
		
		form.append({type: 'div', name: 'Promoting hook', label: 'promoting hook to [['+prep.title+']]... '})
		form.append({type: 'div', name: 'Closing nomination', label: 'closing [['+nomination.title+']]... '})
		
		show();
		prep.addHook(hook,slotnum,new File(imgfile));
		prep.assemble()
		nomination.hat("yes",prep.title);
	
		await nomination.save("to [[" + prep.title + "]]").done(function(data){
			console.log(data)
			form.root.childs[1].data.label += 'done! :)';
			show();
		});
		
		await prep.save("promoting [[" + nomination.title + "]]").done(function(data){
			form.root.childs[0].data.label += 'done! :)';
			console.log(form.root.childs[0]);
			show();
			location.reload(true);
		});
	}
	
}
// </nowiki>