MediaWiki:UploadForm.js

From WikiLectures

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.
/* <pre> */

/* ============ UPLOAD FILE FORM ======================================
   author:    [[User:Slepi]]
   ====================================================================
*/

// LABELS AND HINTS FOR SOME FORM FIELDS
// HERE YOU CAN EASILY EDIT LABELS OF THE FORM

// Field "Description"
var popis = "Description:";
var napoveda_popis = "";

// Field "Source"
// Warning: at the end of the text there is a checkbox "My own work"
// no description, the text should introduce it
var zdroj = "Source:";
var napoveda_zdroj = "Please enter the source of the file. If the file is your own work, please check My own work.";
var popisek_zatr_zdroj = "Own work:";

// Field "Author"
var autor = "Author:";
var napoveda_autor = "";

// Field "Date"
// Warning: at the end of the text there is a checkbox "Today"
// no description, the text should introduce it
var datum = "Date:";
var napoveda_datum = "We recommend write the date in YYYY-MM-DD or YYYY-MM or YYYY. Or you can check the box Today.";
var popisek_zatr_datum = "Today:";

// Field "License" is generated by the MediaWiki code.

// Field "Category"
var kategorie = "Categories:";
var napoveda_kategorie = "Enter categories, which are suitable for your file, and split them by a comma (,).";
var popis_odkazu_kategorii = "Link to categories list (open in new window):";
var odkaz_seznam_kategorii = "Category:Categories";

/* == SCRIPT SOURCE CODE - please do not edit, if you are not sure = */

// FUNCTION RETURNING THE USER
// if he did not filled importent fields
function nezadanyParametr(parametr) {
  window.alert("You did not enter a "+parametr+" of the file! Please, fill in the missing information.");
  return false;
}
 
// VARIABLES USED THROUGH WHOLE CODE
 
// the sending form
var formular = "";
 
// Submit button
var tlacitkoOdeslat = "";
 
// license chosen in "Own work"
var zvolenaLicence = 0;
 
// FUNCTION FILLING FILEDS WITH "MY OWN WORK"
function vlastniPraceFce() {
  if (formular.elements["vlastniprace"].checked == true) {
    formular.elements["mySource"].value = "{{own work}}";
    formular.elements["mySource"].disabled = true;
    formular.elements["myAuthor"].value = "~~" + "~";
    formular.elements["myAuthor"].disabled = true;
    //  license "CC-BY-SA"
    zvolenaLicence = formular.elements["wpLicense"].selectedIndex;
    formular.elements["wpLicense"].selectedIndex = 6;
    licenseSelectorCheck();
  } else {
    formular.elements["mySource"].disabled = false;
    formular.elements["myAuthor"].disabled = false;
    formular.elements["wpLicense"].selectedIndex = zvolenaLicence;
    licenseSelectorCheck();
  }
}
 
// FUNCTION FILLING CURRENT DATE
function dnesniDatumFce() {
  if (formular.elements["dnesnidatum"].checked == true) {
    dnesni_datum = new Date();
 
    vyplneni_rok = dnesni_datum.getFullYear();
    vyplneni_mesic = (dnesni_datum.getMonth() + 1);
    if (vyplneni_mesic < 10) vyplneni_mesic = "0" + vyplneni_mesic;
    vyplneni_den = dnesni_datum.getDate();
    if (vyplneni_den < 10) vyplneni_den = "0" + vyplneni_den;
    vyplneni_data = vyplneni_rok + "-" + vyplneni_mesic + "-" + vyplneni_den;
 
    formular.elements["myDate"].value = vyplneni_data;
    formular.elements["myDate"].disabled = true;
 
  } else {
    formular.elements["myDate"].disabled = false;
  }
}
 
// FUNCTION WHICH SUBMITS THE FORM
// during uploading
function odeslatFormular () {
 
  // variables read from form, testing of input
  mujPopis = formular.elements['myDesc'].value;
  if (!mujPopis) {nezadanyParametr("description"); return false;}
  mujZdroj = formular.elements['mySource'].value;
  if (!mujZdroj) {nezadanyParametr("source"); return false;}
  mujDatum = formular.elements['myDate'].value;
  mujAutor = formular.elements['myAuthor'].value;
  if (!mujAutor) {nezadanyParametr("author"); return false;}
  mujKategorie = formular.elements['myCategory'].value;
  if (!mujKategorie) mujKategorie="Uncategorized files";
 
  // fill the license and delete it from MediaWiki Field
  MediaLicence = formular.elements['wpLicense'].options[formular.elements['wpLicense'].selectedIndex].value;
  if (MediaLicence) {
    mujLicence = "{{";
    mujLicence += MediaLicence;
    mujLicence += "}}";
 } else {
    mujLicence = "{{no license}}";
    testLicence = window.confirm("You did not enter any license of the file.\n\nIf you want to save file under another license, please, enter the license after file upload.\n\nIf you are not sure abou the licenses, please, contact our editorial board (info@wikilectures.eu).\n\nDo you really want to save this file without licese?");
    if(!testLicence) return false;
  }
  formular.elements['wpLicense'].options[formular.elements['wpLicense'].selectedIndex].value = "";
 
  // Category check
  mujKategorieSplit=mujKategorie.split(",");
  mujKategorie="";
  for(i=0;i<mujKategorieSplit.length;i++) {
    mujKategorie += "[[Category:" + trim(mujKategorieSplit[i]) + "]]\n";
  }
 
  // added category according to its mime type
  if (mujKategorie.indexOf("Images") == -1 && mujKategorie.indexOf("Flash animations") == -1 && mujKategorie.indexOf("Sounds") == -1 && mujKategorie.indexOf("text files") == -1 && mujKategorie.indexOf("Compiled archives") == -1) {
    nazevSouboru = formular.elements["wpDestFile"].value.split(".");
    switch(nazevSouboru[nazevSouboru.length-1].toLowerCase()) {
      case "png":
      case "gif":
      case "jpg":
      case "jpeg":
      case "djvu":
      case "svg":
        mujKategorie += "[[Category:Images]]\n";
        break;
      case "swf":
        mujKategorie += "[[Category:Flash animations]]\n";
        break;
      case "mp3":
      case "ogg":
        mujKategorie += "[[Category:Sounds]]\n";
        break;
      case "pdf":
        mujKategorie += "[[Category:Text files]]\n";
        break;
      case "zip":
        mujKategorie += "[[Category:Compiled archives]]\n";
        break;
    }
  }
 
  // template File with input text
  formular_popis = "{{File\n|description = ";
  formular_popis += mujPopis;
  formular_popis += "\n|source = ";
  formular_popis += mujZdroj;
  formular_popis += "\n|date = ";
  formular_popis += mujDatum;
  formular_popis += "\n|author = ";
  formular_popis += mujAutor;
  formular_popis += "\n|license = ";
  formular_popis += mujLicence;
  formular_popis += "\n}}\n";
  formular_popis += mujKategorie;
 
  // insert the template back into form
  document.getElementById("wpUploadDescription").value = formular_popis;
 
  // submit click simulation
  puvodniSubmity = document.getElementsByName("wpUpload");
  var tlacitkoOdeslat = puvodniSubmity[0];
  tlacitkoOdeslat.click();
 
  return false;
}
 
// FUNCTION PREPARING A ROW IN THE FORM
// with checkbox, with onclick etc.
function vytvoritRadek (jmeno, id, napoveda, pop_checkbox, nabidka_checkboxu, funkce_checkboxu) {
 
  novyradek = '<tr><td class="mw-label"><label for="' + id + '">' + jmeno + '</label></td><td class="mw-input"><input type="text" id="' + id + '" name="' + id + '" size="60" class="mw-input" onblur="this.value = trim(this.value);" />';
 
  if (nabidka_checkboxu && funkce_checkboxu) {
    novyradek += '<br /><label for="' + nabidka_checkboxu + '">' + pop_checkbox + '</label><input type="checkbox" name="' + nabidka_checkboxu + '" id="' + nabidka_checkboxu + '" onclick="' + funkce_checkboxu + '();" />';
  }
 
  if (napoveda || napoveda != " ") {
    novyradek += '<div class="htmlform-tip">' + napoveda + '</div>';
  }
 
  novyradek+="</td></tr>";
 
  $(".mw-htmlform-field-Licenses").before(novyradek);
}
 
// FUNCTION CHANGING THE FORM
// of Upload file
function zmenitFormular() {
 
  // if no upload, exit
  if(mw.config.get("wgPageName") != "Special:Upload") return false;
 
  // exit when being on the warning page
  if(document.getElementById("uploadwarning")) return;
 
  // exit when not logged in
  if(!mw.config.get("wgUserName")) return;
 
  // exit if only uploading new version
  wpReUpload = mw.util.getParamValue('wpForReUpload'); // get url parameter
  if(wpReUpload!=null) return;
 
  // exit if error message is displayed
  pocetChyb = getElementsByClass("error");
  if(pocetChyb.length > 0) return;
 
  // link to form
  if(document.forms["mw-upload-form"]) formular = document.forms["mw-upload-form"];
 
  // link to tab
  if(document.getElementById("mw-htmlform-description")) tabulka = document.getElementById("mw-htmlform-description");
 
  // exit if deleted file
  // do not show edit tools and license field
  if (mw.config.get("wgUploadSourceIds").length == 0) {
    tabulka.deleteRow(4);
    tabulka.deleteRow(3);
    return false;
  }
 
  // hide submit button
  $('input[name="wpUpload"]').css({"visibility":"hidden", "position":"absolute"});
 
 // delete description row
  $(".mw-htmlform-field-HTMLTextAreaField").css({"visibility":"hidden", "position":"absolute"})
 
  // hide edittools
  $(".mw-editTools").hide();
 
  // new line: description
  vytvoritRadek(popis, "myDesc", napoveda_popis);
 
  // new line: source
  vytvoritRadek(zdroj, "mySource", napoveda_zdroj, popisek_zatr_zdroj, "vlastniprace", "vlastniPraceFce");
 
 // new line: author
  vytvoritRadek(autor, "myAuthor", napoveda_autor);
 
  // new line: date
  vytvoritRadek(datum, "myDate", napoveda_datum, popisek_zatr_datum, "dnesnidatum", "dnesniDatumFce");
 
  // new line: category
  vytvoritRadek(kategorie, "myCategory", napoveda_kategorie);
 
  // new submit button
  $('table#mw-htmlform-options tr:last').after('<tr><td></td><td><button onclick="return odeslatFormular();">Upload file</button></td></tr>');
}
 
// start
$(function() {
  zmenitFormular();
});

/* </pre> */