Team:Slovenia/libraries/zitator-js

// modified by iGEM Team Slovenia 2016

/* Heidelberg iGEM 2015 wiki software tool

  https://2015.igem.org/Team:Heidelberg/js/zitator?action=raw&ctype=text/javascript
*/

function Zitator(textSelector, bibfile) {

   if (textSelector == undefined || bibfile == undefined) {
       console.log("Error: Not all mandatory parameters (textSelector, bibfile) given!");
   } else {
       this.textSelector = textSelector;
       this.bibfile = bibfile;
   }

}

Zitator.prototype = {

   constructor: Zitator,
   // Puts the unordered references into a hashmap so they can accessed in expected O(1) time.
   byCitationKey: function (unordered) {
       var byCitationKey = {};
       for (var i = 0; i < unordered.length; ++i) {
           var ref = unordered[i];
           byCitationKey[ref.citationKey] = ref;
       }
       return byCitationKey;
   },
   // Formats the bibliography entry for an article
   formatArticle: function (tags) {
       var retVal = "";
       if (tags.author != undefined) {
           retVal += tags.author;
       }
       if (tags.year != undefined) {
           retVal += " (" + tags.year + ")";
       }
       if (tags.title != undefined) {
           retVal += " " + tags.title + ".";
       }
       if (tags.journal != undefined) {
           retVal += "" + tags.journal + "";
       }
       if (tags.volume != undefined) {
           retVal += " " + tags.volume + "";
       }
       if (tags.pages != undefined) {
           retVal += ", " + tags.pages.replace("--", "-");
       }
       return retVal;
   },
   // Formats the bibliography entry for a book
   formatBook: function (tags) {
       var retVal = "";
       if (tags.author != undefined)
           retVal += tags.author;
       if (tags.year != undefined)
           retVal += " (" + tags.year + ")";
       if (tags.title != undefined)
           retVal += " " + tags.title + ".";
       if (tags.publisher != undefined)
           retVal += "" + tags.publisher + "";
       return retVal;
   },
   // Formats the bibliography entry for a website
   formatWebsite: function (tags) {
       return tags.author + ", " + tags.howpublished;
   },
   // Takes a list of references and outputs a bibliography containing these references
   formatReferences: function (refs) {
       var html = "";
       for (var idx = 0; idx < refs.length; ++idx) {
           var ref = this.bib[refs[idx]];
           if (ref == undefined) {
               console.error("Missing reference: %s", refs[idx]);
               continue;
           }
html += "

<a name='" + ref.citationKey + "'>[" + (Number(idx) + 1) + "] "; if (ref.entryType.toLowerCase() == "book") { html += this.formatBook(ref.entryTags); } else if (ref.entryType.toLowerCase() == "electronic") { html += this.formatWebsite(ref.entryTags); } else { html += this.formatArticle(ref.entryTags); } html += "</a>

";
       }
       return html;
   },
   // Callback for successfully recievement of data
   dataReceivedCallback: function (data) {
       this.bib = this.byCitationKey(bibtexParse.toJSON(data));
       var references = [];
       var haveNumber = {};
       var number = 1;
       $(this.textSelector).find("x-ref").each(function (index) {
           // var refName = $(this).text();
           var refNames = $(this).text().split(/, */);
           var output = [];
           for (var i = 0; i < refNames.length; i++) {
               refName = refNames[i];
               if (refName in haveNumber) {
                   console.log(refName, "already has a number");
                   //$(this).html("<a href='#" + refName + "'>[" + haveNumber[refName] + "]</a>");
               } else {
                   //$(this).html("<a href='#" + refName + "'>[" + number + "]</a>");
                   references.push(refName);
                   haveNumber[refName] = number;
                   number += 1;
               }
               if (refNames.length == 1) {
                   output += "<a href='#" + refName + "'>[" + haveNumber[refName] + "]</a>";
               } else if (i == 0) {
                   output += "<a href='#" + refName + "'>[" + haveNumber[refName] + ",</a>";
               } else if (i != refNames.length - 1) {
                   output += "<a href='#" + refName + "'>" + haveNumber[refName] + ",</a>";
               } else {
                   output += "<a href='#" + refName + "'>" + haveNumber[refName] + "]</a>";
               }
           }
           $(this).html(output);
       });
       $("#references").html(this.formatReferences(references));
   },
   // Fetches the bib file and builds the bibliography
   zitiere: function () {
       var obj = this;
       $.get(this.bibfile, function (data) {
           obj.dataReceivedCallback(data)
       });
   }

};