Difference between revisions of "Template:Waterloo/JS/citetag"

(update to handle references referenced multiple times, not debugged)
m
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
// Implements the <cite> tagRequires a "ref" attribute that corresponds to one listed in references.json and that
+
// Automatically populate <cite> tags with ordinal citations.   
// references.json be included in the global scope
+
//
 +
// References should be given as <cite ref="refID"></cite>, where the ref attribute corresponds
 +
// to a JSON variable called "references" in the same scope with a matching "refID". The <cite>
 +
// tag will then be filled with an appropriate numeral and, if the page contains an <ol> element
 +
// with id="reflist", a <li> will be added with the value of the JSON matching "refID".
 +
//
 +
// See https://2015.igem.org/wiki/index.php?title=Template:Waterloo/JS/references for example JSON.
  
var refLinks = {};
+
var scrollLinks = {};
  
$(document).ready(function() {
+
$(document).ready(function() {
 
   var citation_idx = 1;
 
   var citation_idx = 1;
 
    
 
    
 
   // On first encounter, fill in all <cite> tags on the page with a certain reference
 
   // On first encounter, fill in all <cite> tags on the page with a certain reference
   $( "cite" ).each(  
+
   $( "cite" ).each( function() {
     if ( $(this).html.indexOf("href") != -1 ) { // tag already been given link in reference list
+
   
 +
     if ( $(this).html().indexOf("href") != -1 ) { // tag already been given link in reference list
 
     }
 
     }
 
     else {
 
     else {
      var refID = $(this).attr("ref");
+
        var refID = $(this).attr("ref");
 +
       
 +
        // Add link with ordinal citation inside <cite> tag of all with refID
 +
        $( "cite[ref~='" + refID + "']" ).each( function() {
 +
            $(this).html( "<a class=\"ref-link\" href=\"#" + refID + "\"><em>[" + citation_idx + "]</em></a>");
 +
        });
 +
       
 +
        // Add refID to list
 +
        scrollLinks[$(this).text()] = refID;
 
      
 
      
      // Add link with ordinal citation inside <cite> tag of all with refID
+
        // Add appropriate li to the references list
      $("cite#"+refID).each( function() {
+
        $("#reflist").append("<li id=\"" + refID + "\">" + references[refID] + "</li>");
            $(this).html( "<a class=\"ref-link\" href=\"#" + refID + "\"><em>[" + citation_idx + "]</em></a>");
+
      })
+
     
+
      // Add refID to list
+
      scrollLinks[$(this).text()] = refID;
+
  
      // Add appropriate li to the references list
+
        citation_idx = citation_idx + 1;
      $("#reflist").append("<li id=\"" + refID + "\">" + references[refID] + "</li>");
+
     
+
      citation_idx = citation_idx + 1;
+
 
     }
 
     }
   );
+
   }); // end citetag.each
 
+
 
   $(".ref-link").each(function(i, obj) {
 
   $(".ref-link").each(function(i, obj) {
 
       $(obj).click(function() {
 
       $(obj).click(function() {
Line 34: Line 40:
 
       });
 
       });
 
   });
 
   });
});
+
}); // end document.ready
  
 
// for smooth scrolling
 
// for smooth scrolling

Latest revision as of 03:36, 17 September 2015

// Automatically populate tags with ordinal citations. // // References should be given as <cite ref="refID">, where the ref attribute corresponds // to a JSON variable called "references" in the same scope with a matching "refID". The

// tag will then be filled with an appropriate numeral and, if the page contains an
    element // with id="reflist", a
  1. will be added with the value of the JSON matching "refID". // // See https://2015.igem.org/wiki/index.php?title=Template:Waterloo/JS/references for example JSON. var scrollLinks = {}; $(document).ready(function() { var citation_idx = 1; // On first encounter, fill in all <cite> tags on the page with a certain reference $( "cite" ).each( function() { if ( $(this).html().indexOf("href") != -1 ) { // tag already been given link in reference list } else { var refID = $(this).attr("ref"); // Add link with ordinal citation inside <cite> tag of all with refID $( "cite[ref~='" + refID + "']" ).each( function() { $(this).html( "<a class=\"ref-link\" href=\"#" + refID + "\">[" + citation_idx + "]</a>"); }); // Add refID to list scrollLinks[$(this).text()] = refID; // Add appropriate li to the references list $("#reflist").append("
  2. " + references[refID] + "
  3. ");
           citation_idx = citation_idx + 1;
       }
     }); // end citetag.each
     $(".ref-link").each(function(i, obj) {
         $(obj).click(function() {
             scrollToAnchor(scrollLinks[obj.text]);
         });
     });
    

    }); // end document.ready

    // for smooth scrolling function scrollToAnchor(aid){

       var aTag = $('#'+aid);
       if(aTag.length){
         $('html, body').animate({scrollTop:$(aTag).position().top}, 'slow');
       }
    
    }