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

(Created page with "// Implements the <cite> tag. Requires a "ref" attribute that corresponds to one listed in references.json and that // references.json be included in the global scope var re...")
 
(update to handle references referenced multiple times, not debugged)
Line 5: Line 5:
  
 
$(document).ready(function() {
 
$(document).ready(function() {
   $( "cite" ).each(
+
  var citation_idx = 1;
     function( index ) {
+
 
 +
  // On first encounter, fill in all <cite> tags on the page with a certain reference
 +
   $( "cite" ).each(  
 +
     if ( $(this).html.indexOf("href") != -1 ) { // tag already been given link in reference list
 +
    }
 +
    else {
 
       var refID = $(this).attr("ref");
 
       var refID = $(this).attr("ref");
 
+
   
       // Add link with ordinal citation inside <cite> tag
+
       // Add link with ordinal citation inside <cite> tag of all with refID
       $(this).html( "<a class=\"ref-link\" href=\"#" + refID + "\"><em>[" + (index + 1) + "]</em></a>");
+
       $("cite#"+refID).each( function() {
 
+
            $(this).html( "<a class=\"ref-link\" href=\"#" + refID + "\"><em>[" + citation_idx + "]</em></a>");
 +
      })
 +
     
 
       // Add refID to list
 
       // Add refID to list
 
       scrollLinks[$(this).text()] = refID;
 
       scrollLinks[$(this).text()] = refID;
Line 17: Line 24:
 
       // Add appropriate li to the references list
 
       // Add appropriate li to the references list
 
       $("#reflist").append("<li id=\"" + refID + "\">" + references[refID] + "</li>");
 
       $("#reflist").append("<li id=\"" + refID + "\">" + references[refID] + "</li>");
 +
     
 +
      citation_idx = citation_idx + 1;
 
     }
 
     }
 
   );
 
   );

Revision as of 22:11, 13 September 2015

// Implements the tag. Requires a "ref" attribute that corresponds to one listed in references.json and that // references.json be included in the global scope

var refLinks = {};

$(document).ready(function() {

 var citation_idx = 1;
 
 // On first encounter, fill in all <cite> tags on the page with a certain reference
 $( "cite" ).each(   
   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#"+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("
  • " + references[refID] + "
  • ");
         citation_idx = citation_idx + 1;
       }
     );
    
     $(".ref-link").each(function(i, obj) {
         $(obj).click(function() {
             scrollToAnchor(scrollLinks[obj.text]);
         });
     });
    

    });

    // for smooth scrolling function scrollToAnchor(aid){

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

    }