Jump to content

User:Wugapodes/DYK promoter.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.
console.log("A script to automatically promote DYK hooks to prep.");
var masterList = [];

function getHooks(wikiText) {
	var re = /('''ALT.+?)?(\.\.\..*)\?/g;
	var hooks = wikiText.match(re);
	return hooks;
}

function displayHooks() {
	// Called when portlet button is clicked. Gets the hooks from the 
	//   nomination by calling getHooks(), parses the wikitext, and then
	//   calls formatEditNotice() to display them.
	var wikiText = document.getElementById('wpTextbox1').innerHTML;
	var hooks = getHooks(wikiText);
	api.get({
		"action": "parse",
		"format": "json",
		"text": "*".concat(hooks.join("\n*")),
		"contentmodel": "wikitext"
	}).done(function(data) {
		formatEditNotice(data);
	});
	// doStuffToNom();
}

function formatEditNotice(data) {
	// The real work horse of the program.
	hookHTML = $.parseHTML(data["parse"]["text"]['*']);
	console.log(hookHTML[0]);
	var listItems = hookHTML[0].getElementsByTagName("li");
	var radioDiv = document.createElement("div"); 
	var i;
	for (i = 0; i < listItems.length; i++) { 
		makeRadioButton(i,listItems,radioDiv,"hooks");
	}
	for ( i = 0; i < prepDiv.length; i++) {
		loadPrepSet(i);
	}
	var editNotice = document.getElementById('editnotice-area');
	editNotice.innerHTML = "";
	editNotice.appendChild(radioDiv);
	// Add buttons to do stuff
	console.log(masterList);
	var inputDiv = document.createElement("div");
	var prepSelect = document.createElement("select");
	prepSelect.setAttribute("id","dyk-prepSelect");
	for (i=0;i < masterList.length;i++) {
		var option = document.createElement("option");
		option.setAttribute("value",i);
		var slotText = ", ".concat(masterList[i].slots).concat(" open slots");
		option.innerText = masterList[i].name.concat(slotText);
		prepSelect.appendChild(option);
	}
	inputDiv.appendChild(prepSelect);
	var selectButton = document.createElement("input");
	selectButton.setAttribute("id","dyk-selectButton");
	selectButton.setAttribute("type","button");
	selectButton.setAttribute("value","Show this prep");
	selectButton.setAttribute("onclick","showPrep()");
	inputDiv.appendChild(selectButton);
	editNotice.appendChild(inputDiv);
}

function getPrepId(pD) {
	var prepTitle = $(pD).find('#firstHeading')[0].innerText;
	console.log(prepTitle);
	if (prepTitle.includes("Preparation area 1")) {
		return 1;
	} else if (prepTitle.includes("Preparation area 2")) {
		return 2;
	} else if (prepTitle.includes("Preparation area 3")) {
		return 3;
	} else if (prepTitle.includes("Preparation area 4")) {
		return 4;
	} else if (prepTitle.includes("Preparation area 5")) {
		return 5;
	} else if (prepTitle.includes("Preparation area 6")){
		return 6;
	} else {
		return -1;
	}
}

function loadPrepSet(ix) {
	var j = getPrepId(prepDiv[ix]);
	console.log("Loaded prep ".concat(j));
	//prepDiv = prepDiv[0]
	console.log(prepDiv[ix]);
	var hookObject = getPrepHooks(prepDiv[ix]);
	if (hookObject.slots > 0) {
		var hookSection = hookObject.hooks;
		hookSection.firstChild.setAttribute("style","list-style-type: none; list-style-image: none;");
		prepDiv[ix].appendChild(hookSection);
		prepDiv[ix].setAttribute("class","prep");
		prepDiv[ix].setAttribute("id","prep-".concat(j));
		hookObject.div = prepDiv[ix];
		hookObject.name = "Prep ".concat(j);
		masterList.push(hookObject);
	}
}

function addPortletLink(f, text) {
	var portletLink = mw.util.addPortletLink('p-cactions', '#', text);
    $(portletLink).click(function (e) {
        e.preventDefault();
        f();
    });
}

function showPrep() {
	var editNotice = document.getElementById('editnotice-area');
	var dropdown = document.getElementById("dyk-prepSelect");
	var i = dropdown.options[dropdown.selectedIndex].value;
	var previewAbsent = document.getElementById("dyk-prepPreview");
	// console.log(!previewAbsent);
	var preview;
	if (!previewAbsent) {
		preview = document.createElement("div");
		preview.setAttribute("id","dyk-prepPreview");
		preview.setAttribute("class","prep");
		preview.appendChild(masterList[i].hooks);
		editNotice.appendChild(preview);
	} else {
		preview = document.getElementById('dyk-prepPreview');
		preview.innerHTML = "";
		preview.appendChild(masterList[i].hooks);
	}
	var selectButton = document.createElement("input");
	selectButton.setAttribute("id","dyk-moveToPrepButton");
	selectButton.setAttribute("type","button");
	selectButton.setAttribute("value","Move to prep");
	selectButton.setAttribute("onclick","addToPrep()");
	preview.appendChild(selectButton);
}

function makeRadioButton(i,listItems,radioDiv,name) {
	var radioPara = document.createElement("p");
	var radioButton = document.createElement("input");
	radioButton.setAttribute("type","radio");
	radioButton.setAttribute("id",name.concat(i));
	radioButton.setAttribute("name",name);
	radioButton.setAttribute("value",i);
	radioPara.appendChild(radioButton);
	var radioLabel = document.createElement("label");
	radioLabel.setAttribute("for",i);
	radioLabel.innerHTML = listItems[i].innerHTML; 
	radioPara.appendChild(radioLabel);
	radioDiv.appendChild(radioPara);
	console.log(listItems[i].innerText);
}

function getPrepHooks(pD) {
	console.log(pD);
	var hookSection = $(pD).find('ul')[2]; // If order of stuff on prep changes, this may no longer work
	console.log(hookSection);
	var hooks = $(hookSection).find('li');
	console.log(hooks);
	var i;
	var hookNum = 0;
	for (i = 0; i < hooks.length; i++) {
		var hookText = hooks[i].innerText;
		console.log(hookText);
		if (hookText.includes("... that ...")) {
			hookNum = hookNum+1;
		}
	}
	hookSection = addRadioButtons(hookSection)
	var emptySlots = hookNum;
	var hookObject = {
		hooks: hookSection,
		slots: emptySlots
	};
	console.log(hookObject);
	return hookObject;
}

function closeNom() {
	var wikiText = document.getElementById('wpTextbox1').innerHTML;
	wikiText.replace("{{DYKsubpage","{{subst:DYKsubpage");
	// Ideally only if passed... but that will be done later
	wikiText.replace("<!--When closing discussion, enter yes, no, or withdrawn -->","yes")
	// Edit summary should say what Prep area it went to
	// API edit request here
}

function getPrepFromName(name) {
	if (name.includes("Prep 1")) {
		return 1;
	} else if (name.includes("Prep 2")) {
		return 2;
	} else if (name.includes("Prep 3")) {
		return 3;
	} else if (name.includes("Prep 4")) {
		return 4;
	} else if (name.includes("Prep 5")) {
		return 5;
	} else if (name.includes("Prep 6")) {
		return 6;
	} else {
		return -1;
	}
}

function addToPrep() {
	// Need a way for users to select the prep and position for a hook to
	//   be moved to, this info then needs to make its way to the closeNom()
	//   function so that the edit summary is accurate.
	var editNotice = document.getElementById('editnotice-area');
	var dropdown = document.getElementById("dyk-prepSelect");
	var prepName = dropdown.options[dropdown.selectedIndex].innerText;
	var prepNum = getPrepFromName(prepName);
	var chosenPrepUrl = "https://en.wikipedia.org/w/index.php?title=Template:Did_you_know/Preparation_area_".concat(prepNum);
	var prepEditUrl = chosenPrepUrl.concat("&action=edit");
	var prepEditDiv = document.createElement('div');
	var prepPosition = parseInt($('input[name="prep"]:checked').val())+1;
	$( prepEditDiv ).load( prepEditUrl, function() {
		var prepSlot = $("input[name='prep']:checked").val();
		var hookIndex = parseInt($("input[name='hooks']:checked").val());
		var wikiText = document.getElementById('wpTextbox1').innerHTML;
		var hooks = getHooks(wikiText);
		console.log(hooks);
		var hookWikiText = hooks[hookIndex];
		console.log(hookWikiText);
		var prepWikiText = $(prepEditDiv).find("#wpTextbox1").text()
		var prepTextPieces = prepWikiText.split(/\* \.\.\. that /)
		prepTextPieces[prepPosition] = hookWikiText.replace('... that ','').concat('\n')
		//console.log(hookIndex+2)
		console.log(prepTextPieces)
		var newWikiText = prepTextPieces.join('* ... that ')
		console.log(newWikiText)
	});
	return;
}

function addRadioButtons(hookSection) {
	var listItems = hookSection.children
	var i;
	var item;
	var prepRadioDiv = document.createElement("div")
	for (i = 0; i < listItems.length; i++) {
		makeRadioButton(i,listItems,prepRadioDiv,"prep")
		if (listItems[i].childElementCount > 0) {
			$(prepRadioDiv).find('#prep'.concat(i))[0].disabled = 'disabled';
		}
	}
	return prepRadioDiv;
}

// On page load
var i;
var prepURL = 'https://en.wikipedia.org/wiki/Template:Did_you_know/Preparation_area_';
var prepDiv = [];
var pageIsDykNom = mw.config.values.wgTitle.includes("Did you know nominations/");
for (i=0;i<6;i++) {
	// Only load preps if page is a DYK nom for performance reasons
	if (pageIsDykNom === true) {
		prepDiv[i] = document.createElement("div");
		$( prepDiv[i] ).load( prepURL.concat(i+1), function(){});
	}
}

var wgAction = mw.config.values.wgAction; 
var wgTitle = mw.config.values.wgTitle;  

if (document.title.includes("Editing Template:Did you know nominations/")) {
    addPortletLink(displayHooks, "Close DYK Nom");
}

var api = new mw.Api();

//var contentText = document.getElementById('mw-content-text');
//var firstHeading = document.getElementById('firstHeading');
//var redirMsg = contentText.getElementsByClassName('redirectMsg')[0];
//var uiWrapper = el('div');
//var edittoken = null;