Jump to content

User:Tom29739/TWLresponses.js

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Tom29739 (talk | contribs) at 10:05, 6 March 2017 (Try and fix stuff again.). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
// <pre><nowiki>

/*
	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 + "{{" + (templateName ? templateName + "|" : "") + code + "}}" + 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 [[User:Tom29739/TWLresponses|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);
  }
}());
// </nowiki></pre>