User:Theleekycauldron/Scripts/PSHAW/protocols/hook puller.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.
*/
pagename = mw.config.get('wgPageName')

$.when(
	mw.loader.using('ext.gadget.morebits'),
	$.ready
).then(async function() {
	console.log("hook puller ready!");
	if (pagename == "Template:Did_you_know" || pagename.includes("Template:Did_you_know/Queue/") || pagename.includes("Template:Did_you_know/Preparation_area")) {
		$(mw.util.addPortletLink(window.DYKH_portlet || 'p-cactions', '#', 'Pull (PSHAW)', 'dyk-portlet', 'Pull hook')).click(pull);
	}
});

pull = async function (e) {
	prep = await Prep.create(pagename);

	var Window = new Morebits.simpleWindow(900, 500);
	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();
	
	// render
	let result;
	function show(){
		result = form.render();
		Window.setContent(result);
		Window.display();
	}
	
	// load up the prep/queue
	
	console.log("howdy :)");
	console.log(form);
	form.append({type: 'div',   name: 'prep',         label: "<b>Hook to pull:</b>"});
	form.append({type: 'radio', name: 'hooksinprep',  label: 'Hooks:', list: []});
	form.append({type: 'button', name: 'submit', label: 'submit!', event: function l(){evaluate(e, result)}});
	form.root.childs[1].data.list = prep.morebits();
	show();
	
	evaluate = async function (e, result){ //jobs: remove the hook
		console.log(result)
		let hook = JSON.parse(result.hooksinprep.value);
		console.log(hook)
		for (var i=0; i<hook.credits.length; i++){
			hook.credits[i] = new Credit(hook.credits[i].content)
		}
		hook = new Hook(hook.id,hook.content,hook.credits);
		let nomination = await hook.subpage();
		console.log(nomination);
		let slotnum = hook.id;
		if (slotnum === ""){
			alert("Which hook is being pulled?");
			return;
		}
		
		let emptyhook = new Hook(slotnum,"... that ...",[new Credit("nullmake")]);
		if (slotnum == prep.hooks.length-1){
			emptyhook.credits.push(new Credit("nullnom"));
		}
		
		form.root.childs = [];
		
		form.append({type: 'div', name: 'Pulling', label: 'pulling hook... '})
		form.append({type: 'div', name: 'Reopening', label: 'reopening nomination... '})
		form.append({type: 'div', name: 'Retranscluding', label: 'retranscluding nomination at [[Template talk:Did you know]]... '})
		form.append({type: 'div', name: 'Note', label: 'opening nomination page in new tab (you will need to make a comment with {{subst:DYK?}} or {{subst:DYK?no}} to prevent the nomination from being moved to [[Template talk:Did you know/Approved|the approved page]])...'})
		
		show();
		prep.addHook(emptyhook,slotnum,new File("{{main page image/DYK|image=example-serious.jpg|caption=Caption goes here}}"));
		prep.assemble()
		nomination.unhat(prep.title);
		
		await prep.save("pulling [[" + nomination.title + "]]").done(function(data){
			form.root.childs[0].data.label += 'done! :)';
			console.log(form.root.childs[0]);
			show();
		});
	
		await nomination.save("pulled from [[" + prep.title + "]]").done(function(data){
			console.log(data)
			form.root.childs[1].data.label += 'done! :)';
			show();
		});
		
		await nomination.retransclude().then(function (data){
			console.log(data);
			form.root.childs[2].data.label += 'done! :)';
			show();
			window.open("https://en.wiki.x.io/wiki/"+nomination.title, '_blank');
			location.reload(true);
		});
		
	}

}

// </nowiki>