MediaWiki:Gadget-cite.js

/* */

/* ============== Citation ========================================== author:        User:Slepi date:          04/2011, last update 03/2012 documentation: MediaWiki:Gadget-citation.js/documentation description:   this script helps to create bibliographical data ==================================================================

/* ======================== Texts =========================================== */

baseTextCitace = ' Reset • Close Citation ';

/* ======================== auxiliary functions ============================= */

// FUNCTION TO GET COORDINATES OF THE WINDOW // http://www.howtocreate.co.uk/tutorials/javascript/browserwindow function getScrollXY { // coordinates var scrOfX = 0, scrOfY = 0;

// Netscape if( typeof( window.pageYOffset ) == 'number' ) { scrOfY = window.pageYOffset; scrOfX = window.pageXOffset;

// DOM } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft;

// IE6 } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft; }

// output return [ scrOfX, scrOfY ]; }

// FUNCTION PROCESSING CITATIONS AND INSTERTING THEM INTO TEXT // parameter "formular": form link // parameter "typ": type of the used source function zpracovatCitace(formular, typ) { // process ISSN if(formular.elements["issn"]) { if(formular.elements["issn"].value == "") formular.elements["issn"].value = "-"; }

// process ISBN if(formular.elements["isbn"]) { if(formular.elements["isbn"].value == "") formular.elements["isbn"].value = "-"; }

// output beginning var output = "\n";

// new citation instead of the strange characters... novyOutput = document.getElementById("wpTextbox1").value; novyOutput = novyOutput.replace("--//--//--", output); document.getElementById("wpTextbox1").value = novyOutput;

// close gadget closeCitationBox;

return false; }

// FUNCTION INSERTING CHARACTERS MESS // parameter "textovePole": ID of the changed textarea function vlozitTextAreaSpletZnaku(textovePole) {

pridanyText = "--//--//--";

// find ID if(!document.getElementById(textovePole)) return; textovePole = document.getElementById(textovePole);

// Microsoft Internet Explorer if (document.selection) { textovePole.focus;

// insert oblastVlozeni = document.selection.createRange; oblastVlozeni.text = pridanyText; }

// other browsers else if (textovePole.selectionStart || textovePole.selectionStart == 0) {

// insert and update text startText = textovePole.selectionStart; endText = textovePole.selectionEnd; aktualniText = textovePole.value; textovePole.value = aktualniText.substring(0, startText); textovePole.value += pridanyText; textovePole.value += aktualniText.substring(endText, aktualniText.length); }

// old browsers: add to end else { textovePole.value += pridanyText; } }

// FUNCTION PROCESSING THE COMPULSORY PARAMETERS OF THE TEMPLATE // parameter "poleParametru": field with all the compulsory parameters function classPovinneCitace(poleParametru) { for (i in poleParametru) { document.getElementById(poleParametru[i]).className += " compulsoryParameterCite"; } }

/* =================== Main functions to view form ================ */

// FUNCTION CREATING CITE BUTTON TO THE TOOLBAR function createCitationButton {

// end when not editing if(wgAction == "view" || !document.getElementById("wpTextbox1")) return;

// older browsers not supported if (!window.XMLHttpRequest) return;

// add CSS style importStylesheet("MediaWiki:Gadget-cite.css");

// create div for overlay overlayDiv = document.createElement("div"); overlayDiv.className = "overlay_div_cit"; overlayDiv.id = "overlay_div_cit";

// position and scrolling fixation kam = getScrollXY; overlayDiv.style.top = kam[0]; overlayDiv.style.position = "fixed";

// add div to the page document.body.appendChild(overlayDiv);

// block with text contentDiv = document.createElement("div"); contentDiv.className = "content_div_cit"; contentDiv.id = "content_div_cit";

// position of the block contentDiv.style.top = 20 + kam[0]; contentDiv.style.position = "fixed";

// add block to the page document.body.appendChild(contentDiv); }

// FUNCTION CLOSING CITATION AFTER CLICKING TO "CLOSE" function closeCitationBox { document.getElementById('overlay_div_cit').style.display = 'none'; document.getElementById('content_div_cit').style.display = 'none'; document.getElementById('wpTextbox1').value = document.getElementById('wpTextbox1').value.replace('--//--//--', ''); document.forms['editform'].elements['wpTextbox1'].focus;

return false; }

// FUNCTION CREATING A FORM ROW // parameter "popis": description (label) of the field) // parameter "parametr": parameter of the template Cite // parameter "typ": type of entry (text, checkbox, etc.) function createCitationFormRow(popis, parametr, typ) {

// default entry type: text if(!typ) typ = "text";

// create row output = ' ' + popis + ':   ';

// output return output; }

// FUNCTION RETURNING ROW FOR EXACT PARAMETER OF THE TEMPLATE CITE // parameter "parametr": template Cite parameter function createCitationInput(parametr){ // array of rows output = {

// authors "corporation" : createCitationFormRow("Name of corporation", "corporation"), "surname1" : createCitationFormRow("Surname of the first author", "surname1"), "name1" : createCitationFormRow("Name of the first author", "name1"), "surname2" : createCitationFormRow("Surname of the second author", "surname2"), "name2" : createCitationFormRow("Name of the second author", "name2"), "surname3" : createCitationFormRow("Surname of the third author", "surname3"), "name3" : createCitationFormRow("Name of the third author", "name3"), "others" : createCitationFormRow("Others", "others", "checkbox"), "author_of_collection" : createCitationFormRow("Author of the collection", "author_of_collection"), "responsibility" : createCitationFormRow("Author(s)", "responsibility"), "institution" : createCitationFormRow("Institution", "institution"), "institute" : createCitationFormRow("Institute", "institute"), "surname" : createCitationFormRow("Surname of the pedagogue", "surname"),// surname for lecture "name" : createCitationFormRow("Name of the pedagogue", "name"),// name for lecture

// titles "title" : createCitationFormRow("Title", "title"), "article" : createCitationFormRow("Title of the article", "article"), "source_name" : createCitationFormRow("Title", "source_name"), "subtitle" : createCitationFormRow("Subtitle", "subtitle"), "journal" : createCitationFormRow("Journal", "journal"), "serial_publication" : createCitationFormRow("Title of the serial publication", "serial_publication"), "label" : createCitationFormRow("Label of the document", "label"), "collection" : createCitationFormRow("Title of the collection", "collection"), "document" : createCitationFormRow("Title of the document", "document"), "topic" : createCitationFormRow("Lecture topic", "topic"),

// descriptions and publishers "url" : createCitationFormRow("Available from WWW", "url"), "edition" : createCitationFormRow("Edition", "edition"), "location" : createCitationFormRow("Location (city)", "location"), "country" : createCitationFormRow("Country", "country"), "publisher" : createCitationFormRow("Publisher", "publisher"), "year" : createCitationFormRow("Year", "year"), "date_of_revision" : createCitationFormRow("Last revision date", "date_of_revision"), "cited" : createCitationFormRow("Date of citation", "cited"), "the_year" : createCitationFormRow("Volume (year)", "the_year"), "number" : createCitationFormRow("Issue number", "number"), "range" : createCitationFormRow("Number of pages", "range"), "series" : createCitationFormRow("Series", "series"), "volume" : createCitationFormRow("Volume", "volume"), "chapter" : createCitationFormRow("Chapter number", "chapter"), "name_of_chapter" : createCitationFormRow("Title of the chapter", "name_of_chapter"), "pages" : createCitationFormRow("Cited pages", "pages"), "notes" : createCitationFormRow("Notes", "notes"), "isbn" : createCitationFormRow("ISBN", "isbn"), "issn" : createCitationFormRow("ISSN", "issn"), "pmid" : createCitationFormRow("PMID", "pmid"), "doi" : createCitationFormRow("DOI", "doi"), "subject" : createCitationFormRow("Lecture subject", "subject"), "specialization": createCitationFormRow("Subject specialization", "specialization"), "faculty" : createCitationFormRow("Faculty", "faculty"), "university" : createCitationFormRow("University or school", "university"), "date" : createCitationFormRow("Date of the lecture", "date") // date for lecture };

// output return output[parametr]; }

// FORM FOR CITATION function createCitationForm {

// character mess for further template if(document.getElementById('wpTextbox1').value.indexOf('--//--//--') == -1) vlozitTextAreaSpletZnaku("wpTextbox1"); document.getElementById('wpTextbox1').blur;

overlayDiv = document.getElementById("overlay_div_cit"); contentDiv = document.getElementById("content_div_cit");

// show div overlay_div_cit overlayDiv.style.display = "block";

// show div overlay_div_cit contentDiv.style.display = "block"; contentDiv.style.fontSize = "small";

// text contentDiv.innerHTML = baseTextCitace; contentDiv.innerHTML += " This tool helps you create proper citation of used literature. After filling in the form the template Cite will be generated and inserted into the article. Please, choose which source would you like to cite: ";

// basic menu contentDiv.innerHTML += " Books and collections " + "" + "" + "" + "book" + "" + "" + "chapter of a book (the same author)" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('collection');\">" + "chapter of a book (other author), collection</a></li>" + "</ul>" + " Articles, publications and periodicals " + "<ul>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('article');\">" + "article</a></li>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('academic_publication');\">" + "academic publication</a></li>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('serial_publication');\">" + "serial publication</a></li>" + "</ul>" + " Electronic sources " + "<ul>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('web');\">" + "webpage or website</a></li>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('cd');\">" + "CD, DVD and others</a></li>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('db');\">" + "database</a></li>" + "</ul>" + " Official publications " + "<ul>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('company_publication');\">" + "company publication</a></li>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('legislative_publication');\">" + "legislative material, laws, etc.</a></li>" + "" + "<a href=\"#\" onclick=\"return createCitationTypeForm('standard');\">" + "standard, patent</a></li>" + "</ul>" + " Others " + "<ul>" + "" + "<a href=\"#\"# onclick=\"return createCitationTypeForm('lecture');\">" + "lecture at the university</a></li>" + "</ul>";

// field for automatic generating from PMID contentDiv.innerHTML += " Quick citations " + " PMID " + '<form onsubmit="return false;" id="formPMID">' + ' Here you can generate a citation using only PMID of the article in the PubMed database! ' +   ' PMID: <input type="text" id="pmid" name="pmid" /> ' + '<input type="button" onclick="zpracovatPMID(document.forms[\'formPMID\'].elements[\'pmid\'].value)" value="Get citation from PMID" /> <span id="pmid_wait"><img src="' + wgServer + '/skins/common/images/ajax-loader.gif" width="20" height="20" /> Please wait&hellip; ' + ' ';   document.getElementById("pmid_wait").style.visibility= "hidden"; document.getElementById("wp_wait").style.visibility= "hidden"; return false; }

// FUNCTION FOR PROCESSING PMID var xmlPMID;

function zpracovatPMID(formularX) { document.getElementById("pmid_wait").style.visibility= "visible"; pmid = formularX; exptest = /^\d+$/;

if (exptest.test(pmid) == true) { var urlPMID = "http://www.wikilectures.eu/extensions/citace/pmid_en.php?pmid=" + pmid; getXMLHttpRequest("xmlPMID", urlPMID, "zpracovatPMIDRequest"); return false; } else { document.getElementById("pmid_wait").style.visibility= "hidden"; window.alert("This is not a valid PMID. Please, check it or try it again."); document.forms["formPMID"].elements["pmid"].value = ""; return false; }

}

// FUNCTION CREATING CITATION FORM PMID function zpracovatPMIDRequest { if (xmlPMID.readyState==4) {// 4 = "loaded" if (xmlPMID.status==200) {// 200 = OK     var output = xmlPMID.responseText; // srver response

if (output.indexOf("{{") == -1) { document.getElementById("pmid_wait").style.visibility= "hidden"; window.alert("This is not a valid PMID. Please, check it or try it again."); document.forms["formPMID"].elements["pmid"].value = ""; return false; }

// insert into article novyOutput = document.getElementById("wpTextbox1").value; novyOutput = novyOutput.replace("--//--//--", output); document.getElementById("wpTextbox1").value = novyOutput;

// close gadget document.getElementById("pmid_wait").style.visibility= "hidden"; closeCitationBox; return false;

} else { return false; } } }

// FUNCTION CREATING CERTAIN FORM FOR EVERY TYPE OF SOURCE // parameter "typ": type of used source function createCitationTypeForm(typ) {

// citation box contentDiv = document.getElementById("content_div_cit");

// beginning of citation output = ' Type of citation: ' + typ.replace("_", " ") + ' ';

// help output += ' Please, fill in fields of this form. Always provide authors of the source. All compulsory fields have red border. After clicking on Create citation the citation template will be generated and insrted into article on the place of the cursor. If you chose wrong type of source, you can restart this form clicking on Restart at the top. Link Close will close this window and return you to article edit page. ';

// beginning of the form // chapter = book, serial_publication = article output += '<form onsubmit="return zpracovatCitace(this, \'' + (typ == "chapter" ? "book" : (typ == "serial_publication" ? "article" : typ)) + '\');">';

// authors switch(typ) { case "book" : case "chapter" : case "collection" : case "article" : case "serial_publication" : case "web" : case "cd" : case "company_publication" : case "academic_publication" : output += ' Authors '; output += ' '; output += createCitationInput("corporation"); output += ' '; output += createCitationInput("surname1"); output += createCitationInput("name1"); output += ' '; output += createCitationInput("surname2"); output += createCitationInput("name2"); output += createCitationInput("surname3"); output += createCitationInput("name3"); output += ' '; output += createCitationInput("others"); output += ' <a href="#" onclick="return false;">&rarr; more authors and other options</a> '; output += ' <a href="#" onclick="return false;">&larr; one author, hide options</a> '; output += ' '; break; default : break; }

// other parameters switch(typ) {

// book types case "collection" : output += ' Title of the publication or chapter '; output += createCitationInput("title"); output += createCitationInput("subtitle"); output += ' '; output += ' Collection or book information '; output += createCitationInput("collection"); output += createCitationInput("author_of_collection"); output += ' Recommended: "SURNAME, Name". Inicials please write without punctuation. ';     output += ' '; output += ' Availability '; output += createCitationInput("url"); output += ' '; output += ' Edition '; output += createCitationInput("edition"); output += ' '; output += ' Publisher details and description '; output += createCitationInput("location"); output += createCitationInput("publisher"); output += createCitationInput("year"); output += createCitationInput("range"); output += createCitationInput("series"); output += createCitationInput("volume"); output += createCitationInput("pages"); output += createCitationInput("notes"); output += createCitationInput("isbn"); output += ' '; break; case "chapter" : case "company_publication" : output += ' Information about chapter '; output += createCitationInput("chapter"); output += createCitationInput("name_of_chapter"); output += ' '; case "book" : output += ' Title '; output += createCitationInput("title"); output += createCitationInput("subtitle"); output += ' '; output += ' Availability '; output += createCitationInput("url"); output += ' '; output += ' Edition '; output += createCitationInput("edition"); output += ' '; output += ' Publisher details and description '; output += createCitationInput("location"); output += createCitationInput("publisher"); output += createCitationInput("year"); output += createCitationInput("range"); output += createCitationInput("series"); output += createCitationInput("volume"); output += createCitationInput("pages"); output += createCitationInput("notes"); output += createCitationInput("isbn"); output += ' '; break;

// article types case "serial_publication" : output += ' Publication '; output += createCitationInput("serial_publication"); output += ' '; case "article" : output += ' Article/publication '; output += createCitationInput("article"); output += ' '; if (typ == "article") { output += ' Journal information '; output += createCitationInput("journal"); } else if (typ == "serial_publication") { output += ' Details '; }     output += createCitationInput("url"); output += createCitationInput("year"); output += createCitationInput("the_year"); output += createCitationInput("volume"); output += createCitationInput("number"); output += createCitationInput("pages"); output += createCitationInput("notes"); output += createCitationInput("issn"); output += ' '; break;

// electronic sources case "db" : output += ' Authors and responsibility '; output += createCitationInput("responsibility"); output += ' '; case "web" : output += ' Availability '; output += createCitationInput("url"); output += ' '; case "cd" : output += ' Source details '; output += createCitationInput("source_name"); output += createCitationInput("subtitle"); output += createCitationInput("publisher"); output += createCitationInput("year"); output += createCitationInput("date_of_revision"); output += createCitationInput("cited"); output += ' '; break;

// legislative document case "legislative_publication" : output += ' Legislative publication details '; output += createCitationInput("country"); output += createCitationInput("institution"); output += createCitationInput("document"); output += createCitationInput("collection"); output += createCitationInput("year"); output += createCitationInput("notes"); output += createCitationInput("pages"); output += createCitationInput("url"); output += ' '; break;

// standard case "standard" : output += ' Standard '; output += createCitationInput("label"); output += createCitationInput("source_name"); output += createCitationInput("location"); output += createCitationInput("institution"); output += createCitationInput("year"); output += createCitationInput("notes"); output += createCitationInput("pages"); output += createCitationInput("url"); output += ' '; break;

// academic literature case "academic_publication" : output += ' Academic publication details '; output += createCitationInput("source_name"); output += createCitationInput("subtitle"); output += createCitationInput("location"); output += createCitationInput("institute"); output += createCitationInput("year"); output += createCitationInput("url"); output += createCitationInput("pages"); output += createCitationInput("notes"); output += ' '; break;

case "lecture": output += ' University lectures '; output += createCitationInput("surname"); output += createCitationInput("name"); output += createCitationInput("topic"); output += createCitationInput("subject"); output += createCitationInput("specialization"); output += createCitationInput("faculty"); output += createCitationInput("university"); output += createCitationInput("location"); output += createCitationInput("date"); output += createCitationInput("url"); output += createCitationInput("notes"); output += ' '; break; }

if (typ == "article") { output += " Other identificators "; output += createCitationInput("pmid"); output += ' PMID number in PubMed database. ';   output += createCitationInput("doi"); output += ' DOI identificator. ';   output += " "; }

// button for submitting the form output += '<input type="submit" value="Create citation" /> '; output += '<input type="reset" value="Reset form" /> '; output += ' ';

// output contentDiv.innerHTML = baseTextCitace + output;

// compulsory parameters polePovinnychParametru = new Array; switch (typ) { case "book": case "chapter": polePovinnychParametru = ["title", "edition", "year", "isbn"]; break; case "collection": polePovinnychParametru = ["title", "edition", "author_of_collection", "year"]; break; case "article": polePovinnychParametru = ["article", "journal", "pages", "year", "the_year", "volume", "issn"]; break; case "serial_publication": polePovinnychParametru = ["article", "journal", "serial_publication", "pages", "year", "volume", "the_year", "issn"]; break; case "web": polePovinnychParametru = ["source_name", "cited", "url"]; break; case "cd": polePovinnychParametru = ["cited", "source_name"]; break; case "db": polePovinnychParametru = ["source_name", "responsibility", "cited", "url"]; break; case "company_publication": polePovinnychParametru = ["title", "publisher", "year"]; break; case "legislative_publication": polePovinnychParametru = ["country", "document", "year"]; break; case "standard": polePovinnychParametru = ["label", "source_name", "year"]; break; case "academic_publication": polePovinnychParametru = ["source_name", "location", "year"]; break; case "lecture": polePovinnychParametru = ["surname", "topic", "subject", "specialization", "faculty", "university", "location", "date"]; break; }

// classes for compulsory parameters classPovinneCitace(polePovinnychParametru);

// fill in the date if(document.getElementById("cited")) {

datumCitace = document.getElementById("cited");

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;

datumCitace.value = vyplneni_data; datumCitace.onclick = function { document.getElementById("cited").value = ""; document.getElementById("cited").onclick = null; } }

// authors // jQuery animation $j(document).ready(function{   $j("#citOdkaz1AutoriForm").click( function{ $j("#citKorporaceForm").slideToggle; $j("#citDalsiAutoriForm").slideToggle; $j("#citOdkaz1AutoriForm").slideToggle; $j("#citOdkaz2AutoriForm").slideToggle; }   );    $j("#citOdkaz2AutoriForm").click( function{ $j("#citKorporaceForm").slideToggle; $j("#citDalsiAutoriForm").slideToggle; $j("#citOdkaz1AutoriForm").slideToggle; $j("#citOdkaz2AutoriForm").slideToggle; }   );  });

// end return false; }

// start this! $(createCitationButton);

if ( typeof $j != 'undefined' && typeof $j.fn.wikiEditor != 'undefined' ) {$j( function {   $j( '#wpTextbox1' ).wikiEditor( 'addToToolbar', { 'section': 'advanced', 'group': 'insert', 'tools': {'citation': { label: 'Create citation', type: 'button', icon: 'http://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Text_document_with_page_number_icon.svg/22px-Text_document_with_page_number_icon.svg.png?uselang=cs', action: {type: 'callback', execute: function(context){createCitationForm;} }       }      }    });  }); }

/* */