User:Tom29739/TWLresponses.js

//

/*	TWLresponses - Makes it easier to respond to TWL resource requests. Created by User:Tom29739. Based on https://en.wikipedia.org/wiki/User:MusikAnimal/responseHelper.js by User:MusikAnimal. Licenced under CC BY-SA 3.0.

(function { var responses = {}, inline = false, indentation = ":", templateName = "", defaultPrompt = "", anew = false, spi = false, unresolved;

// show shortcuts if not editing if (!mw.config.get('wgEditMessage')) { mw.util.addPortletLink('p-navigation', mw.config.get( 'wgServer' ) + '/wiki/Wikipedia:OUP', "WP:OUP"); mw.util.addPortletLink('p-navigation', mw.config.get( 'wgServer' ) + '/wiki/Wikipedia:Newspapers.com', "WP:Newspapers.com"); return; }

if (/Wikipedia:OUP/.test(mw.config.get('wgPageName')) || /Wikipedia:Newspapers.com/.test(mw.config.get('wgPageName'))) { templateName = "User:Tom29739/TWLresponses"; inline = true; indentation = "#:"; defaultPrompt = "Additional comments"; responses = { "Approved" : { code : "a", summary : "Approved." },     "Not approved" : { code : "u", prompt: "Please provide a reason for not approving", summary : "Not approved." },     "On hold" : { code : "h", prompt: "Please provide a reason for putting on hold", summary : "On hold." },     "In progress" : { code : "p", summary : "In progress." }   };  }

var respondFn = function (e) { var response = e.data.response; var code = response.code, comment = "", value = "";

if (code.indexOf("|X") !== -1) { value = prompt((response.prompt ? response.prompt : defaultPrompt) + " (optional, hit OK to omit)"); if (value === null) return false; code = code.slice(0,(value.length ? -1 : -2)) + value; } else if (response.prompt) { value = prompt(response.prompt + " (optional, hit OK to omit)"); if (value === null) return false; if (value.length) comment = " " + value; }

var $textarea = $("#wpTextbox1"); var currentText = $textarea.val; var responseStr = indentation + "" + comment + " ~";

if (inline) { var caretPos = $textarea[0].selectionStart; $textarea.val(currentText.substring(0, caretPos) + responseStr + currentText.substring(caretPos)); } else if (spi) { $textarea.val(       currentText.replace(/(\n<\!---|$)/, responseStr + "\n$&")      ); } else { $textarea.val(currentText + responseStr); }

if (anew && !unresolved) { var textArray = $textarea.val.split("\n"); $textarea.val(       textArray[0].replace('(Result: )','(Result: ' + (response.summary[0].toUpperCase + response.summary.slice(1) + value).trim + ')') +        '\n' + $textarea.val.split("\n").splice(1).join("\n")      ); }

$("#wpSummary").val($("#wpSummary").val + (response.summary + " " + value).trim + " (using TWLresponses)"); };

for (var response in responses) { var id = responses[response].id || responses[response].code.replace(/\W/g, ''); mw.util.addPortletLink('p-navigation', 'javascript:', "(" + response + ")", "rh-" + id, responses[response].summary); $("#rh-"+id).click({     response : responses[response]    }, respondFn); } }); //