User:P,TO 19104/COI Request Tool.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.
// Copied from User:Terasail/COI Request Tool.js - please use that script.
// Stable version of script
//
//<nowiki>
var COIResponse = [
["ntmp", "speechBubbleAdd", "Comment", "Comment (No template)"],
["d", "checkAll", "Done", "Done"],
["pd", "check", "Partly done", "Partly done:"],
["a", "clock", "Already Done", "Already Done"],
["q", "helpNotice", "Question", "Question:"],
["g", "edit", "Go ahead", "Go ahead: I have reviewed these proposed changes and suggest that you go ahead and make the proposed changes to the page."],
["note", "ellipsis", "Note", "Note:"],
["n", "notice", "Not done", "Not done:"],
["nfn", "notice", "Not done for now", "Not done for now:"],
["c", "userGroup", "Not done – Needs consensus", "Not done for now: Please establish a consensus with editors engaged in the subject area before using the {{Request edit}} template for this proposed change."],
["udp","undo", "Partly undone", "Undone: This request has been partially undone."], 
["ud", "undo", "Undone", "Undone: This request has been undone."]];

function executeCOI(currentBox, selectedReply, inputText, answered) {
	if (confirm("Confirm in order to reply to this edit request.")) {
		currentBox.getElementsByTagName('tr')[2].remove();
		var infoBox = new OO.ui.MessageWidget( {
			icon: 'pageSettings',
			type: 'notice',
			label: 'Processing request — Edit request starting, getting section data to edit.'
		});
		var firstRow = currentBox.children[1].children[0];
		firstRow.innerHTML = "";
		progressBar = new OO.ui.ProgressBarWidget( {
			progress: false
		});
		$(firstRow).append(progressBar.$element);
		$(firstRow).append(infoBox.$element);
		progressBar.$element[0].style = "margin:auto";
		infoBox.$element[0].style = "margin:10px auto 0px; max-width:50em; text-align:left";
		var header = "";
		var curElement = currentBox.parentNode; //Table tag
		do {
			curElement = curElement.previousElementSibling;
			if (curElement.getElementsByClassName("mw-headline").length == 1) {
				header = curElement.getElementsByClassName("mw-headline")[0].id;
			}
		}
		while (header == "");
		var api = new mw.Api();
		api.get( {
			action: "parse",
			page: mw.config.values.wgPageName,
			prop: "sections",
		}).done(function(data) {
			infoBox.setLabel("Processing request — Making changes to the edit request");
			var sections = data.parse.sections;
			var sectionNum = -1;
			var offset = 0;
			for (j = 0; j < sections.length; j++) {
				if (sections[j].anchor == header) {
					sectionNum = j + 1;
					sectionNum -= offset;
				}
				if (sections[j].fromtitle != mw.config.values.wgPageName) {
					offset++;
				}
			}
			infoBox.setLabel("Processing request — Getting information from section: " + sectionNum + ".");
			api.get( {
				action: "parse",
				page: mw.config.values.wgPageName,
				prop: "wikitext",
				section: sectionNum
			}).done( function(data) {
				infoBox.setLabel("Processing request — Making changes to the edit request.");
				var wikitext = data.parse.wikitext["*"];
				var tempStr = "";
				if (answered || selectedReply == "CLOSEREQ") {
					tempStr = "|A";
				}
				var tempType = "{{request edit";
				if (wikitext.search(tempType) == -1) {
					tempType = "{{Request edit";
				} 
				if (wikitext.search(tempType) == -1) {
					infoBox.setLabel('Failed to find {{Request edit}} template, please report to "User talk:Terasail/COI Request Tool"');
					infoBox.setType("error");
				} else {
					wikitext = wikitext.replace(tempType, "{{Request edit" + tempStr);
					var editSummary = "/* " + header.replaceAll("_", " ") + " */ ";
					if (selectedReply == "REMOVE") {
						wikitext = "";
						editSummary = "Removed edit request";
						selectedReply = -1;
					} else if (selectedReply == "CLOSEREQ") {
						editSummary += "Closed edit request";
						selectedReply = -1;
					} else {
						if (selectedReply == 0) { //Do not add response template
							wikitext += "\n:" + inputText.value;
						} else {
							wikitext += "\n:{{subst:ECOI|" + COIResponse[selectedReply][0] + "}}";
							if (inputText != "") {
								wikitext += " " + inputText.value;
							}
						}
						wikitext += " ~~~~";
						editSummary += COIResponse[selectedReply][2];
					}
					editSummary += " ([[User:Terasail/COI_Request_Tool|COI Request Tool]])";
					if (selectedReply != -1 && infoBox.type != "error") {
						infoBox.setType("success");
						infoBox.setLabel("Processing '" + COIResponse[selectedReply][2] + "' request — Saving changes to the talk page.");
					}
					console.log(sectionNum);
					console.log(editSummary);
					console.log(wikitext);
					api.postWithEditToken({
						action: 'edit',
						title: mw.config.values.wgPageName,
						text: wikitext,
						section: sectionNum,
						summary: editSummary
					}).done(function(result) {
						window.location = "https" + "://en.wikipedia.org/w/index.php?title=" + mw.config.values.wgPageName + "&type=revision&diff=cur&oldid=prev";
					});
				}
			});
		});
	}
}

function loadButtons(currentBox) {
	$(currentBox).append('<tr><td colspan=2 style="padding-bottom:10px"><div style="display: flex; justify-content: center;"></div></td></tr><tr style="display:none"><td colspan=2 style="padding-bottom:10px; text-align:center"></td></tr>');
	var firstRow = currentBox.children[1].children[0];
	//Create dropdown menu
	var dropMenu = new OO.ui.DropdownWidget( {
		label: "Select reply option -  Add additional text below",
		menu: {items: []}
	});
	COIResponse.forEach(function(item) {
		var newOption = new OO.ui.MenuOptionWidget({
			label: item[3],
			icon: item[1]
		});
		dropMenu.menu.addItems(newOption);
	});
	dropMenu.$element[0].style = "text-align:left; margin:0px";
	$(currentBox.children[2].children[0]).append(dropMenu.$element);
	//Create input box
	var inputText = new OO.ui.MultilineTextInputWidget({autosize: true, label: "'~~" + "~~" + "' is automatically added"});
	inputText.$element[0].style = "margin:auto";
	$(currentBox.children[2].children[0]).append(inputText.$element);
	//Toggle answer button
	var answerB = new OO.ui.ToggleButtonWidget( { 
		icon: "close",
		label: "Close",
		value: false
	});
	answerB.on("click", function() {
		if (answerB.value == true) {
			answerB.setIcon("check");
		} else {
			answerB.setIcon("close");
		}
	});
	//Done button
	var doneB = new OO.ui.ButtonWidget( { 
		icon: "checkAll",
		flags: ["primary", "progressive"],
		label: "Done"
	});
	doneB.on("click", function() {
		if (doneB.getLabel() == "Submit") {
			dropMenu = dropMenu.getLabel();
			for (k = 0; k < COIResponse.length; k++) {
				if (COIResponse[k][3] == dropMenu) {
					executeCOI(currentBox, k, inputText, answerB.value);
				}
			}
		} else {
			executeCOI(currentBox, 1, inputText, answerB.value);
		}
	});
	$(firstRow.children[0]).append(doneB.$element);
	//Go ahead button
	var gaB = new OO.ui.ButtonWidget( { 
		icon: "edit",
		label: "Go ahead"
	});
	$(firstRow.children[0]).append(gaB.$element);
	gaB.on("click", function() {
		executeCOI(currentBox, 5, inputText, answerB.value);
	});
	//Consensus button
	var conB = new OO.ui.ButtonWidget( { 
		icon: "userGroup",
		label: "WP:Consensus"
	});
	$(firstRow.children[0]).append(conB.$element);
	conB.on("click", function() {
		executeCOI(currentBox, 9, inputText, answerB.value);
	});
	//Responses button
	var responseB = new OO.ui.ButtonWidget( { 
		icon: "add",
		label: "Other"
	});
	$(firstRow.children[0]).append(responseB.$element);
	responseB.on("click", function() {
		if (currentBox.children[2].style.display == "") {
			responseB.setIcon("add");
			currentBox.children[2].style = "display:none";
			doneB.setLabel("Done");
		} else {
			responseB.setIcon("subtract");
			currentBox.children[2].style = "";
			doneB.setLabel("Submit");
		}
	});
	$(firstRow.children[0]).append(answerB.$element);
	//Close request
	var closeB = new OO.ui.ButtonWidget( { 
		icon: "unFlag",
		invisibleLabel: true
	});
	closeB.$element[0].style = "margin:10px 10px 0px";
	$(currentBox.children[0].children[0]).append(closeB.$element);
	closeB.on("click", function() {
		executeCOI(currentBox, "CLOSEREQ", "", answerB.value)
	});
	//Remove request
	var removeB = new OO.ui.ButtonWidget( { 
		icon: "trash",
		flags: ["primary", "destructive"],
		invisibleLabel: true
	});
	removeB.$element[0].style = "margin:10px";
	$(currentBox.children[0].children[0]).append(removeB.$element);
	removeB.on("click", function() {
		executeCOI(currentBox, "REMOVE", "", answerB.value)
	});
}

function addReplyButton(currentBox, boxType) {
	var replyB = new OO.ui.ButtonWidget( { 
		icon: "edit",
		flags: "progressive",
		invisibleLabel: true
	});
	replyB.$element[0].style = "margin:5px 0px";
	$(currentBox.children[0].children[0]).append(replyB.$element);
	replyB.on("click", function() {
		currentBox.parentElement.setAttribute("class", "plainlinks tmbox tmbox-notice editrequest");
		var img = currentBox.children[0].children[0].children[0]
		img.width = 53;
		img.height = 53;
		img.src = "//upload.wikimedia.org/wikipedia/commons/f/f4/Info_talk.png"
		img.scrset = "";
		currentBox.children[0].children[1].innerHTML = '<div style="text-align:center; font-size:125%">Please read the instructions for the parameters used by this template for accepting and declining them, and review the request below and make the edit if it is well sourced, neutral, and follows other Wikipedia guidelines and policies.</div>'
		replyB.$element.remove();
		loadButtons(currentBox);
	});
}

var COIRequestBoxes = document.getElementsByClassName("plainlinks tmbox tmbox-notice editrequest");
if (COIRequestBoxes) {
	mw.loader.using(["oojs-ui-core", "oojs-ui-widgets"]).done(function() {
		mw.loader.load(["oojs-ui.styles.icons-alerts", "oojs-ui.styles.icons-interactions", "oojs-ui.styles.icons-moderation", "oojs-ui.styles.icons-user", "oojs-ui.styles.icons-editing-core"]);
		for (i = 0; i < COIRequestBoxes.length; i++) {
			var currentBox = COIRequestBoxes[i].children[0];
			var edReqCheck = currentBox.children[0].children[1];
			if (edReqCheck.innerText.search("conflict") != -1) { //Check that it is a conflict edit request
				if (edReqCheck.innerText.search("The requested edits backlog is") != -1) { //For unanswered boxes
					loadButtons(currentBox);
				} else { //For answered boxes
					addReplyButton(currentBox);
				}
			}
		}
	});
}
//</nowiki>