MediaWiki:FanonUpload.js

From Pikmin Fanon

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
//<nowiki>

//Forked from Pikipedia's PikipediaUpload.js script.

var description_textarea;
var type_select;
var game_text;
var subject_select;
var source_select;
var specific_source_question;
var specific_source_text;
var license_select;
var final_textarea;

var description_tr;
var type_tr;
var game_tr;
var subject_tr;
var source_tr;
var specific_source_span;
var license_tr;
var final_tr;

var final_toggler_a;

function main() {
  
  if(typeof(mw) === "undefined" || mw.config.get("wgCanonicalNamespace") != "Special" || mw.config.get("wgTitle") != "Upload") return;
  if(window.location.href.indexOf("wpForReUpload=1") != -1) return;

  //Get the table with the upload fields.
  var table;
  if (mw.config.get('wgMFMode'))
    table = document.getElementById("mw-htmlform-description");
  else
    table = document.getElementById("mw-htmlform-description").firstChild;
  
  if(document.getElementById("wpUploadDescription") && document.getElementById("wpUploadDescription").value.length > 0) {
    //Hold on, there's already text here! Chances are this is the warning page.
    //Let's just hide the license box and quit.
    hide_license(table);
    return;
  }

  create_description_textarea(table);
  create_type_list(table);
  create_game_text(table);
  create_subject_list(table);
  create_source_list(table);
  create_license_list(table);
  
  hide_license(table);
  setup_final(table);
  replace_uploadtext();
  create_script_notification();
  
  handle_type_choice();
  
}


function create_description_textarea(table) {
  description_textarea = document.createElement("textarea");
  description_textarea.onchange = create_final_result;
  description_tr = add_field("Description:", description_textarea, table);
}


function create_type_list(table) {
  type_select = document.createElement("select");
  type_select.name = "type";
  type_select.onchange = handle_type_choice;
  
  add_option("screenshot", "Screenshot", type_select);
  add_option("boxart", "Box art", type_select);
  add_option("concept", "Concept art", type_select);
  add_option("texture", "Texture", type_select);
  add_option("sprite", "Pixel art", type_select);
  add_option("3d", "3D render", type_select);
  add_option("audio", "Audio", type_select);
  add_option("video", "Video", type_select);
  add_option("map", "Map", type_select);
  add_option("icon", "Icon", type_select);
  add_option("card", "Trading card", type_select);
  add_option("location", "Location", type_select);
  add_option("joke", "Joke image", type_select);
  add_option("real", "Real world image", type_select);
  add_option("user", "Non-wiki user image", type_select);
  add_option("wiki", "Pikmin Fanon special image", type_select);
  
  type_tr = add_field("Type of file:", type_select, table);
}


function create_game_text(table) {
  game_text = document.createElement("input");
  game_text.name = "game";
  game_text.onchange = handle_game_choice;

  game_tr = add_field("Game:", game_text, table);
}


function create_subject_list(table) {
  var subject_span = document.createElement("span");
  
  subject_select = document.createElement("select");
  subject_select.name = "subject";
  subject_select.onchange = handle_subject_choice;
  
  add_option("none", "N/A", subject_select);
  
  var spec_optgroup = add_optgroup("A specific creature", subject_select);
  var collectible_optgroup = add_optgroup("A collectible", subject_select);
  var object_optgroup = add_optgroup("An in-game object", subject_select);
  var other_optgroup = add_optgroup("Other", subject_select);
  
  add_option("character",  "Character",           spec_optgroup);
  add_option("pikmin",     "Pikmin",              spec_optgroup);
  add_option("enemy",      "Enemy",               spec_optgroup);
  add_option("item",       "Generic item",        collectible_optgroup);
  add_option("vehicle_part",  "Vehicle part",     collectible_optgroup);
  add_option("treasure",   "Treasure",            collectible_optgroup);
  add_option("fruit",      "Fruit",               collectible_optgroup);
  add_option("upgrade",    "Upgrade",             collectible_optgroup);
  add_option("onion",      "Onion",               object_optgroup);
  add_option("vegetation", "Vegetation",          object_optgroup);
  add_option("obstacle",   "Obstacle",            object_optgroup);
  add_option("hazard",     "Hazard",              object_optgroup);
  add_option("vehicle",    "Vehicle",             object_optgroup);
  add_option("planet",     "Planet",              object_optgroup);
  add_option("console",    "Gaming console",      other_optgroup);
  
  subject_span.appendChild(subject_select);
  subject_tr = add_field("Main subject:", subject_span, table);
}


function create_source_list(table) {
  var source_span = document.createElement("span");
  
  source_select = document.createElement("select");
  source_select.name = "source";
  source_select.onchange = handle_source_choice;
  source_span.appendChild(source_select);
  
  add_option("none",      "N/A", source_select);
  add_option("self",      "I made this myself", source_select);
  add_option("edited",    "I edited this", source_select);
  add_option("someone",   "Someone else made it", source_select);
  add_option("pikipedia", "From Pikipedia", source_select);
  add_option("web",       "Off a website", source_select);
  
  specific_source_span = document.createElement("span");
  source_span.appendChild(specific_source_span);
  
  specific_source_text = document.createElement("input");
  specific_source_text.onchange = create_final_result;
  specific_source_question = document.createTextNode("");
  specific_source_span.appendChild(specific_source_question);
  specific_source_span.appendChild(specific_source_text);
  
  source_tr = add_field("Source:", source_span, table);
}


function create_license_list(table) {
  license_select = document.createElement("select");
  license_select.name = "license";
  license_select.onchange = create_final_result;
  
  add_option("unknown",         "I don't know...",                             license_select);
  add_option("fairuse",         "Fair use",                                    license_select);
  add_option("cc by-sa 1.0",    "CC Attribution-ShareAlike 1.0",               license_select);
  add_option("cc by-sa 2.1 jp", "CC Attribution-ShareAlike 2.1 Japan",         license_select);
  add_option("cc by 2.0",       "CC Attribution 2.0",                          license_select);
  add_option("cc by-sa 2.0",    "CC Attribution-ShareAlike 2.0",               license_select);
  add_option("cc by-nc-sa 2.0", "CC Attribution-NonCommercial-ShareAlike 2.0", license_select);
  add_option("cc by 2.5",       "CC Attribution 2.5",                          license_select);
  add_option("cc by-sa 2.5",    "CC Attribution-ShareAlike 2.5",               license_select);
  add_option("cc by 3.0",       "CC Attribution 3.0",                          license_select);
  add_option("cc by-sa 3.0",    "CC Attribution-ShareAlike 3.0",               license_select);
  add_option("cc by-sa 4.0",    "CC Attribution-ShareAlike 4.0",               license_select);
  add_option("pd",              "Public domain",                               license_select);
  
  license_tr = add_field("License:", license_select, table);
}


function handle_type_choice() {
  handle_game_choice();
  handle_source_choice();
  update_fields();
}


function handle_game_choice() {
  update_fields();
}


function handle_subject_choice() {
  update_fields();
}


function handle_source_choice() {
  var cur_source = get_option(source_select);
  
  if(cur_source == "someone") {
    specific_source_question.nodeValue = " Who? "
    specific_source_span.style.display = "inline";
  } else if(cur_source == "web") {
    specific_source_question.nodeValue = " What's the link? "
    specific_source_span.style.display = "inline";
  } else if(cur_source == "pikipedia") {
    specific_source_question.nodeValue = " What's the page name? "
    specific_source_span.style.display = "inline";
  } else {
    specific_source_span.style.display = "none";
  }
  
  update_fields();
}


function update_fields() {
  create_final_result();
}


function create_final_result() {
  var t = "";
  var cur_type = get_option(type_select);
  var cur_subject = get_option(subject_select);
  var cur_source = get_option(source_select);
  var cur_license = get_option(license_select);
  
  t += "{{file info\n";
  t += "|summary = " + description_textarea.value + "\n";
  t += "|type = " + cur_type + "\n";
  
  if(game_text.value != "") {
    t += "|game = " + game_text.value + "\n";
  }

  if(cur_source != "N/A") {
    t += "|source = " + (cur_source == "self" ? "Made by the uploader" : cur_source == "edited" ? "Edited by the uploader" : cur_source == "pikipedia" ? "{{c|" + specific_source_text.value + "|Pikipedia}}" : specific_source_text.value) + "\n";
  }
  
  license_str_map = {};
  license_str_map["unknown"] = "";
  license_str_map["fairuse"] = "{{fair use}}";
  license_str_map["cc by-sa 1.0"] = "CC Attribution-ShareAlike 1.0";
  license_str_map["cc by-sa 2.1 jp"] = "CC Attribution-ShareAlike 2.1 Japan";
  license_str_map["cc by 2.0"] = "CC Attribution 2.0";
  license_str_map["cc by-sa 2.0"] = "CC Attribution-ShareAlike 2.0";
  license_str_map["cc by-nc-sa 2.0"] = "CC Attribution-NonCommercial-ShareAlike 2.0";
  license_str_map["cc by 2.5"] = "CC Attribution 2.5";
  license_str_map["cc by-sa 2.5"] = "CC Attribution-ShareAlike 2.5";
  license_str_map["cc by 3.0"] = "CC Attribution 3.0";
  license_str_map["cc by-sa 3.0"] = "CC Attribution-ShareAlike 3.0";
  license_str_map["cc by-sa 4.0"] = "{{CC BY-SA 4.0}}";
  license_str_map["pd"] = "{{public domain}}";

  t += "|license = " + license_str_map[cur_license] + "\n";

  if(cur_type == "icon" && cur_subject != "none") {
    t += "|noiconcat = y\n";
  }
  
  t += "}}\n\n"

  subject_str_map = {};
  subject_str_map["pikmin"] = "Pikmin";
  subject_str_map["character"] = "Character";
  subject_str_map["enemy"] = "Enemy";
  subject_str_map["item"] = "Item";
  subject_str_map["vehicle_part"] = "Vehicle part";
  subject_str_map["treasure"] = "Treasure";
  subject_str_map["fruit"] = "Fruit";
  subject_str_map["upgrade"] = "Upgrade";
  subject_str_map["onion"] = "Onion";
  subject_str_map["vegetation"] = "Vegetation";
  subject_str_map["obstacle"] = "Obstacle";
  subject_str_map["hazard"] = "Hazard";
  subject_str_map["vehicle"] = "Vehicle";
  subject_str_map["planet"] = "Planet";
  subject_str_map["console"] = "Console";
  
  if(cur_subject != "none") {
    if(cur_type == "icon") {
      t += "[[Category:" + subject_str_map[cur_subject] + " icons]]\n";
    } else {
      t += "[[Category:" + subject_str_map[cur_subject] + " images]]\n";
    }
  }
  
  if(t.substr(t.length - 2) == "\n\n") {
      t = t.slice(0, t.length - 1);
  }
  
  final_textarea.value = t;
}


function hide_license(table) {
  document.getElementById("wpLicense").parentNode.parentNode.style.display = "none";
}


function setup_final(table) {
  var final_toggler_div = document.createElement("div");
  final_toggler_div.style.marginTop = "64px";
  
  final_toggler_a = document.createElement("a");
  final_toggler_a.href = "#";
  final_toggler_a.innerHTML = "Show"
  final_toggler_a.onclick = function(e) { e.preventDefault(); toggle_final(); };
  final_toggler_div.appendChild(document.createTextNode("["));
  final_toggler_div.appendChild(final_toggler_a);
  final_toggler_div.appendChild(document.createTextNode("] final result box (if you need fine tuning)"));
  
  add_field("", final_toggler_div, table);
  final_tr = document.getElementById("wpUploadDescription").parentNode.parentNode;
  final_textarea = final_tr.getElementsByTagName("textarea")[0];
  final_tr.style.display = "none";
  final_tr.parentNode.appendChild(final_tr); //Move to last row.
}


function replace_uploadtext() {
  var uploadtext = document.getElementById("uploadtext");
  clear_el(uploadtext);
  
  uploadtext.innerHTML =
    "<p>Use this form to upload new files. Please follow every step in the form. " +
    "Also, use a descriptive name, like <code>Pikmin 3 Burgeoning Spiderwort closeup.jpg</code> " +
    "instead of <code>xGFW34.JPG</code>, and make sure " +
    "your file does not <a href=\"/Special:ListFiles\">already exist</a>.</p>";
}


function create_script_notification() {
  var notification_div = document.createElement("div");
  notification_div.style.float = "right";
  notification_div.style.margin = "10px";
  notification_div.style.border = "1px solid";
  notification_div.style.padding = "2px";
  notification_div.style.fontSize = "75%";
  notification_div.style.fontStyle = "italic";
  notification_div.innerHTML = "This page is running the <a href=\"/wiki/User:Espyo\">FanonUpload</a> script.";
  
  var upload_text = document.getElementById("uploadtext");
  upload_text.parentNode.insertBefore(notification_div, upload_text);
}


function toggle_final() {
  if(final_tr.style.display == "none") {
    if (mw.config.get('wgMFMode'))
      var show = "block";
    else
      var show = "table-row";
    final_tr.style.display = show;
    final_toggler_a.innerHTML = "Hide";
  } else {
    final_tr.style.display = "none";
    final_toggler_a.innerHTML = "Show";
  }
}


function clear_el(el) {
  while(el.firstChild){
    el.removeChild(el.firstChild);
  }
}


function get_option(sel) {
  if (sel.options.length == 0) return null;
  return sel.options[sel.selectedIndex].value;
}


function add_option(value, innerHTML, parent, selected) {
  var opt = document.createElement("option");
  opt.value = value;
  opt.innerHTML = innerHTML;
  parent.appendChild(opt);
  if(typeof(selected) != "undefined") {
    opt.setAttribute("selected", "");
  }
  return opt;
}


function add_optgroup(label, parent) {
  var optgroup = document.createElement("optgroup");
  optgroup.label = label;
  parent.appendChild(optgroup);
  return optgroup;
}


function add_field(label, content, table) {
  if (mw.config.get('wgMFMode')) {
    var tr = document.createElement("div");
    tr.className = "mw-htmlform-field-HTMLTextField";
    var td1 = document.createElement("div");
    td1.className = "mw-label";
    td1.innerHTML = label;
    tr.appendChild(td1);
    var td2 = document.createElement("div");
    td2.className = "mw-input";
    td2.appendChild(content);
    tr.appendChild(td2);
  } else {
    var tr = document.createElement("tr");
    var td1 = document.createElement("td");
    td1.className = "mw-label";
    td1.innerHTML = label;
    tr.appendChild(td1);
    var td2 = document.createElement("td");
    td2.className = "mw-input";
    td2.appendChild(content);
    tr.appendChild(td2);
  }
  
  var ref = table.children[table.children.length - 3];
  table.insertBefore(tr, ref);
  return tr;
}


main();

//</nowiki>