Template:McMaster/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) {
       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 successfuly recievment 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();
               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 () {
       var obj = this;
       $.get(this.bibfile, function(data) {obj.dataReceivedCallback(data)});
   }

};