User:Theleekycauldron/Scripts/PSHAW/protocols/hook mover.js

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) {
	origin = new Prep(mw.config.get('wgPageName'));
	//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
			form.append({type: 'div', name: 'hooktopromote', label: '<b>Hook to move: </b>'})
			form.append({type: 'radio', name: 'nomhooks', list: []});
			form.append({type: 'div', name: 'preppagename', label: "<b>[["+pagename+"]]</b>"})
			form.append({type: 'radio', name: 'prephooks', label: 'Hooks:', list: []});
		} else { //update prepnum
			form.root.childs[5].data.label = "<b>[["+pagename+"]]</b>";
		}
		
		prep = new Prep(pagename);
		await prep.initialize();
		
		form.root.childs[6].data.list = prep.morebits();
		console.log(origin.hooks);
		form.root.childs[4].data.list = origin.morebits();
		
		if (firstLoad){
			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.content == ""){
			alert("You gotta pick a hook!");
			return
		}
		
		if (imgfile==""){ //scrub ''(pictured)'' from hook if no image
			hook.content = hook.content.replace(/[^']''\(.*?\)''/,"");
		}
		
		form.root.childs = [];
		
		form.append({type: 'div', name: 'Promoting hook', label: 'pulling from [['+prep.title+']]... '})
		form.append({type: 'div', name: 'Closing nomination', label: 'moving to [['+nomination.title+']]... '})
		
		show();
		
		if (prep.hooks[slotnum].text != "... that ..."){
			origin.addHook(prep.hooks[slotnum],result.nomhooks.value,)
		}
		
		prep.addHook(hook,slotnum,prep.file);
		prep.assemble()
		nomination.hat("yes",prep.title);
		
		await prep.save("pulling hook from [[" + origin.title + "]]").done(function(data){
			form.root.childs[0].data.label += 'done! :)';
			console.log(form.root.childs[0]);
			show();
		});
	
		await origin.save("moving hook to [[" + prep.title + "]]").done(function(data){
			console.log(data)
			form.root.childs[1].data.label += 'done! :)';
			show();
			location.reload(true);
		});
	}
	
}
// </nowiki>