Team:Heidelberg/js/zitator

function Zitator(textSelector, bibfile) {

   if (textSelector == undefined || bibfile == undefined) {
       console.log("Error: Not all mandatory parameters (textSelector, bibfile) given!");
       return;
   } else {
       this.textSelector = textSelector;
       this.bibfile = bibfile;
       //document.registerElement('x-ref');
   }

}

Zitator.prototype = {

   constructor: Zitator,
   // Puts the unordered references into a hashmap so they can accessed in expected O(1) time.
   byCitationKey: function(unordered) {
       byCitationKey = {};
       for (i in unordered) {
           ref = unordered[i];
           byCitationKey[ref.citationKey] = ref;
       }
       return byCitationKey;
   },
   // Formats the bibliography entry for an article
   formatArticle: function (tags) {
       return tags.author + " (" + tags.year + ") " + tags.title + ". " + tags.journal + " " + tags.volume + ", " + tags.pages.replace("--", "-");
   },
   // Formats the bibliography entry for a book
   formatBook: function(tags) {
       return tags.author + " (" + tags.year + ") " + tags.title +  ". " + tags.publisher + "";
   },
   // 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) {
       html = "";
       for (idx in refs) {
           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 successfuly recievment of data
   dataReceivedCallback: function(data) {
           this.bib = this.byCitationKey(bibtexParse.toJSON(data));
           var references = [];
           var haveNumber = {};
           var number = 1;
           $(this.textSelector + " x-ref").each(function (index) {
               var refName = $(this).textContent;
               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;
               }
           });
           $("#references").html(this.formatReferences(references));
   },
   // Fetches the bib file and builds the bibliography
   zitiere: function () {
       obj = this;
       $.get(this.bibfile, function(data) {obj.dataReceivedCallback(data)});
   }

};