//<nowiki>
/*jshint esversion: 6*/
var editRequestBoxes = document.getElementsByClassName("plainlinks tmbox tmbox-notice editrequest");
var pageWatchers = "There are currently ";
var api = new mw.Api();
var pageName = mw.config.values.wgPageName;
var encodePageName = encodeURIComponent(pageName);
function postEdit(wikitext, editSummary, pageName, secIndx) {
api.postWithEditToken({
action: 'edit',
title: pageName,
text: wikitext,
section: secIndx,
summary: editSummary
}).done(function(result) {
window.___location = "/w/index.php?title=" + encodePageName + "&type=revision&diff=cur&oldid=prev";
});
}
function execute(responseTable, dataERT, replyOption, inputText, answered, boxType, targets) {//currentBox
OO.ui.confirm("Confirm in order to reply to this edit request.").done(function(confirmed) {
if (confirmed) {
//Create label box & remove action buttons
let firstRow = responseTable.children[1].children[0];
firstRow.innerHTML = "";
responseTable.children[5].children[0].remove();
let infoBox = new OO.ui.MessageWidget( {
icon: 'pageSettings',
type: 'notice',
label: 'Processing request — Edit request starting, getting section data to edit.'
});
infoBox.$element[0].style = "margin:5px 0; max-width:50em";
$(firstRow).append(infoBox.$element);
//Create loading bar
let progressBar = new OO.ui.ProgressBarWidget( {
progress: false
});
$(firstRow).append(progressBar.$element);
//Set preview for output
if (replyOption[0] != "") {//Don't preview a non-response
showOutput(inputText, replyOption, responseTable.children[4].children[0], dataERT, boxType);
}
//Find header
let header = "";
let curElement = responseTable.parentNode;
do {
curElement = curElement.previousElementSibling;
if (curElement.getElementsByClassName("mw-headline").length == 1) {
header = curElement.getElementsByClassName("mw-headline")[0].id;
}
}
while (header == "");
api.get( {
action: "parse",
page: pageName,
prop: "sections|wikitext|revid"
}).done(function(data) {
infoBox.setLabel("Processing request — Making changes to the edit request");
let editTemplate = "{{Edit " + boxType + "-protected";
if (answered) { answered = "yes"; } else { answered = "no"; }
for (let c3 = 0; c3 < targets.length; c3++) {
editTemplate += "|" + targets[c3];
}
editTemplate += "|answered=" + answered;
let sections = data.parse.sections;
let newRev = data.parse.revid;
let secIndx = sections[0];
for (let j = 0; j < sections.length; j++) {
if (sections[j].anchor == header) {
secIndx = parseInt(sections[j].index);
}
}
api.get( {
action: "parse",
page: pageName,
section: secIndx,
prop: "wikitext"
}).done(function(data) {
let wikitext = data.parse.wikitext["*"];
wikitext = wikitext.replace(/{{ *([SETFI]PER|Edit([ -]?[A-Z]+[ -]?|[- ])Protected)\s*[^}}]*/i, editTemplate);
let editSummary = "/* " + header.replaceAll("_", " ") + " */ " + replyOption[2] + " ([[User:Terasail/Edit_Request_Tool|Edit Request Tool]])";
if (replyOption[1] != "Close") {
let lastMsg = wikitext.lastIndexOf("\n", wikitext.lastIndexOf("(UTC)")) + 1;
wikitext = wikitext.trim() + "\n:";
while(lastMsg == wikitext.indexOf(":", lastMsg)) {
lastMsg++;
wikitext += ":";
}
if (replyOption[0] != "") { wikitext += "{{subst:" + dataERT.protections[boxType][1] + replyOption[0] + "}} "; }
if (inputText != "") { wikitext += inputText.replaceAll(/\s*~~~~\s*/g, "") + " "; }
wikitext += "~~~~";
if (replyOption[1] == "Remove") {
wikitext = "";
editSummary = editSummary.replace(/[^]+\*\/ /, "");
}
}
infoBox.setType("success");
infoBox.setLabel("Processing request — Saving changes to the talk page.");
if (newRev == mw.config.values.wgRevisionId) {
postEdit(wikitext, editSummary, pageName, secIndx);
} else {
OO.ui.confirm("There has been a new revision to the page, do you wish to continue?").done(function(revCon) {
if (revCon) { postEdit(wikitext, editSummary, pageName, secIndx); }
});
}
});
});
}
});
}
function addButtons(currentBox, dataERT, replyButton) {
let boxType = currentBox.parentElement.dataset.origlevel;
boxType = boxType.replace("full", "fully");
let tableElem = currentBox.parentElement;
$('<table style="border:1px solid #A2A9B1; border-radius:2px; padding:12px 16px; margin:auto; max-width:55em; clear:both;"><tr><td style="color:#808080"><div style="font-style:italic; margin-left:1em;">' + pageWatchers + '</div><div>Quick options:</div></td></tr><tr style="display: flex; justify-content: center;"><td></td></tr><tr><td style="color:#808080">Custom response:</td></tr><tr style="text-align:center;"><td></td></tr><tr style="background:#F6F6F6;"><td style="display:none;"><div style="color:#808080">Preview:</div><div></div></td></tr><tr style="margin-top:5px; display: flex; justify-content: right;"><td></td></tr></table>').insertAfter(tableElem);
let responseTable = tableElem.nextElementSibling.children[0];
let firstRow = responseTable.children[1].children[0];
let secondRow = responseTable.children[3].children[0];
let thirdRow = responseTable.children[4].children[0];
let fourthRow = responseTable.children[5].children[0];
let protections = Object.entries(dataERT.protections);
let responses = Object.entries(dataERT.response);
let quickResponses = Object.entries(dataERT.quickResponse);
let nonResponses = dataERT.nonResponse;
//Create type change dropdown
let tcOptions = [];
for (let i = 0; i < protections.length; i++) {
tcOptions.push({data: protections[i][0], label: protections[i][1][0]});
}
let typeChange = new OO.ui.DropdownInputWidget( {
value: boxType,
options: tcOptions
});
typeChange.on("change", function() {
submitB.setDisabled(false);
});
typeChange.$element[0].style = "text-align:left; margin:auto";
$(secondRow).append(typeChange.$element);
//Create target page list
let boxLinks = currentBox.getElementsByClassName("mbox-text")[0].getElementsByClassName("external text");
let pageLinks = [];
if (boxLinks.length > 0) {//Open request
for(let c1 = 0; c1 < boxLinks.length; c1++) {
if (boxLinks[c1].parentElement.tagName == "LI" || boxLinks[c1].parentElement.tagName == "B") {
pageLinks[pageLinks.length] = boxLinks[c1].innerHTML;
}
}
} else {//Closed request
boxLinks = currentBox.getElementsByClassName("mbox-text")[0].getElementsByTagName("A");
for(let c2 = 1; c2 < boxLinks.length; c2++) {
pageLinks[pageLinks.length] = boxLinks[c2].title;
}
if (pageLinks.length == 0) {
pageLinks = pageName.replace(/(_talk|Talk:)/, "").replaceAll("_", " ");
}
}
let targetPages = new OO.ui.TagMultiselectWidget( {
placeholder: 'Target Pages',
allowArbitrary: true,
selected: pageLinks
});
targetPages.on("change", function() {
submitB.setDisabled(false);
});
targetPages.$element[0].style = "text-align:left; margin:5px auto";
$(secondRow).append(targetPages.$element);
//Create dropdown menu
let dropMenu = new OO.ui.DropdownWidget( {
label: "Select reply option - Add additional text below",
menu: {items: []}
});
for (let count = 0; count < responses.length; count++) {
let newOption = new OO.ui.MenuOptionWidget({
label: responses[count][0],
icon: responses[count][1][1]
});
dropMenu.menu.addItems([newOption]);
}
responses = dataERT.response;
dropMenu.$element[0].style = "text-align:left; margin:0px";
$(secondRow).append(dropMenu.$element);
dropMenu.on("labelChange", function() {
submitB.setDisabled(false);
showOutput(inputText, responses[dropMenu.getLabel()], thirdRow, dataERT, typeChange.value);
});
//Create input box
let inputText = new OO.ui.MultilineTextInputWidget({autosize: true, rows:4, label: "Additional text"});
inputText.$element[0].style = "margin:5px auto";
$(secondRow).append(inputText.$element);
inputText.on("change", function(newText) {
showOutput(inputText, responses[dropMenu.getLabel()], thirdRow, dataERT, typeChange.value);
});
//Create top horizontal layout
let hzLayoutT = new OO.ui.HorizontalLayout();
//Create firstrow fieldset
let fieldsetT = new OO.ui.FieldsetLayout();
fieldsetT.addItems([new OO.ui.FieldLayout(new OO.ui.Widget({content:[hzLayoutT]}))]);
$(firstRow).append(fieldsetT.$element);
//Remove button
let remove = new OO.ui.ButtonWidget( {
icon: "trash",
flags: ["primary", "destructive"],
invisibleLabel: true,
title: "Delete entire section!"
});
remove.on("click", function() {
execute(responseTable, dataERT, nonResponses.Remove, "", null, typeChange.defaultValue, targetPages.getValue());
});
hzLayoutT.addItems([remove]);
//Open & Close button
if (currentBox.parentElement.attributes[2].localName != "data-origlevel") {
let closeB = new OO.ui.ButtonWidget( {
icon: "unFlag",
invisibleLabel: true,
title: "Mark as answered"
});
closeB.on("click", function() {
execute(responseTable, dataERT, nonResponses.Close, "", true, typeChange.defaultValue, targetPages.getValue());
});
hzLayoutT.addItems([closeB]);
} else {
let openB = new OO.ui.ButtonWidget( {
icon: "flag",
invisibleLabel: true,
title: "Mark as unanswered"
});
openB.on("click", function() {
execute(responseTable, dataERT, nonResponses.Open, "", false, typeChange.defaultValue, targetPages.getValue());
});
hzLayoutT.addItems([openB]);
}
//Create quick response buttons
for (let i = 0; i < quickResponses.length; i++) {
let newButton = new OO.ui.ButtonWidget({
label: quickResponses[i][1][0],
flags: quickResponses[i][1][1],
title: quickResponses[i][1][2]
});
newButton.on("click", function() {
execute(responseTable, dataERT, responses[quickResponses[i][0]], "", toggleAns.selected, typeChange.defaultValue, targetPages.getValue());
});
hzLayoutT.addItems([newButton]);
}
//Toggle answer button
let toggleAns = new OO.ui.CheckboxInputWidget({selected: true});
hzLayoutT.addItems([toggleAns, new OO.ui.LabelWidget({label: "Answered"})]);
//Cancel response button
let cancelB = new OO.ui.ButtonWidget( {
icon: "cancel",
flags: ["destructive"],
label: "Cancel",
framed: false,
title: "Cancel the response & close menu"
});
cancelB.on("click", function() {
replyButton.setDisabled(false);
responseTable.parentElement.remove();
});
$(fourthRow).append(cancelB.$element);
//Submit response button
let submitB = new OO.ui.ButtonWidget( {
icon: "checkAll",
flags: ["primary", "progressive"],
label: "Submit",
title: "Submit the response",
disabled: true
});
submitB.on("click", function() {
let newResponse = responses[dropMenu.getLabel()];
let newText = inputText.value;
let isAns = toggleAns.selected;
let newType = typeChange.value;
let newTargets = targetPages.getValue();
let targets = targetPages.items;
let targetChange = false;
if (typeof(newResponse) != "undefined") {
execute(responseTable, dataERT, newResponse, newText, isAns, newType, newTargets);
} else if (typeChange.value != typeChange.defaultValue) {
execute(responseTable, dataERT, nonResponses.ChangeLevel, "", isAns, newType, newTargets);
}
if (targets.length == pageLinks.length) {
for(let item = 0; item < targets.length; item++) {
if (targets[item].data != pageLinks[item]) { targetChange = true; }
}
} else {
targetChange = true;
}
if (targetChange) {
execute(responseTable, dataERT, nonResponses.ChangeTarget, "", isAns, newType, newTargets);
}
});
$(fourthRow).append(submitB.$element);
}
function showOutput(inputText, replyOption, tableRow, dataERT, template) {
var restTransform = "https://en.wikipedia.org/api/rest_v1/transform/wikitext/to/html/" + encodePageName;
let preview = "";
let newText = inputText.value;
if (typeof(inputText) == 'string') {
newText = inputText;
}
template = dataERT.protections[template][1];
if (typeof(replyOption) != "undefined") {
preview += "{{" + template + replyOption[0] + "}} ";
}
if (newText != "" && typeof(newText) != "undefined") { preview += newText + " "; }
if (preview != "") {
let nickname = " " + mw.user.options.values.nickname;
if (nickname == " ") {//Create default signature if no nickname
nickname = mw.user.getName();
nickname = " [[User:" + nickname + "|" + nickname + "]] ([[User talk:" + nickname + "|talk]])";
}
let timeNow = new Date();
timeNow = timeNow.toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric', hour:'2-digit', minute: '2-digit'}).split(" at ");
preview += nickname + " " + timeNow[1] + ", " + timeNow[0] + " (UTC)";
$.post(restTransform, 'wikitext=' + preview + '&body_only=true',
function(html) {
if (inputText.value != "" || typeof(replyOption) != "undefined") {//Stops preview appearing with empty input box
tableRow.style = "padding:8px 1em; 2px";
tableRow.children[1].innerHTML = html;
}
}
);
} else {
tableRow.style = "display:none;";
}
}
if (editRequestBoxes.length != 0) {
let pageID = mw.config.values.wgArticleId;
api.get({
action: "query",
prop: "info",
pageids: pageID,
inprop: "watchers|visitingwatchers",
format: "json"
}).done(function(data) {
data = data.query.pages[pageID];
let watchers = data.watchers;
let visiting = data.visitingwatchers;
if (typeof(watchers) == "undefined") {
pageWatchers += "less than 30";
} else {
pageWatchers += watchers;
}
pageWatchers += " users watching this page (";
if (typeof(visiting) == "undefined") {
pageWatchers += "0";
} else {
pageWatchers += visiting;
}
pageWatchers += " have viewed recent edits).";
});
var jsonERTURL = "https://en.wikipedia.org/w/index.php?title=User:Terasail/Edit_Request_Tool.json&action=raw&ctype=text/json";
$.getJSON(jsonERTURL, function(dataERT) {
mw.loader.using(["oojs-ui-core", "oojs-ui-widgets", "oojs-ui-windows"]).done(function() {
mw.loader.load(["oojs-ui.styles.icons-layout", "oojs-ui.styles.icons-alerts", "oojs-ui.styles.icons-interactions", "oojs-ui.styles.icons-moderation", "oojs-ui.styles.icons-user", "oojs-ui.styles.icons-content", "oojs-ui.styles.icons-editing-core", "oojs-ui.styles.icons-editing-advanced"]);
for (let i = 0; i < editRequestBoxes.length; i++) {
let currentBox = editRequestBoxes[i].children[0]; //The tbody tag for the box
if (typeof(currentBox.parentElement.dataset.origlevel) != "undefined") {
let isSmall = false;
if (editRequestBoxes[i].id == "") {
isSmall = true;
}
let replyButton = new OO.ui.ButtonWidget( {
icon: "recentChanges",
flags: ["progressive"],
label: "Respond",
invisibleLabel: isSmall,
title: "Respond to the edit request."
});
replyButton.on("click", function() {
addButtons(currentBox, dataERT, replyButton);
replyButton.setDisabled(true);
});
replyButton.$element[0].style = "margin:2px 0";
if (isSmall) {
$(currentBox.children[0].children[0]).append(replyButton.$element);
} else {
$(currentBox).append('<tr><td colspan=2><div style="display: flex; justify-content: center;"></div></td></tr>');
$(currentBox.children[1].children[0].children[0]).append(replyButton.$element);
}
}
}
});
});
}
//</nowiki>[[Category:Wikipedia scripts]]