Extension:HarvardReferences/Scripts

From MediaWiki.org
Jump to: navigation, search

This script is optional for Extension:HarvardReferences, but it can highlight references and change it's displaying in the article. To install it, make personal or common script, e.g.

  • [[HarvardReferences.js]] - text of the script as seen below.
  • to MediaWiki:Common.js add call of script: importScript('HarvardReferences.js');

Then purge browser's cache (press Ctrl-F5).

Text of script[edit]

//Scripts for supporting extension http://www.mediawiki.org/wiki/Extension:HarvardReferences

var HrvHighlight = new function(){

var state = "";

function setBackground(prm_name){
	var anchorTags = document.getElementsByTagName("li");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className=="harvBackground"){
			ob1.className = "";
		}
	}

	var anchorTags = document.getElementsByTagName("sup");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.id.indexOf("harv_note-"+prm_name)==0){
			ob1.parentNode.className = "harvBackground";
		}else if(ob1.id.indexOf("harv_note-")==0){
			ob1.parentNode.className = "";
		}
	}
}


function setBackground1(prm_name, prm_n){
	var anchorTags = document.getElementsByTagName("sup");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.id.indexOf("harv_note-")==0){
			ob1.parentNode.className = "";
		}
	}
	var anchorTags = document.getElementsByTagName("li");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className=="harvBackground"){
			ob1.className = "";
		}
		
		if(ob1.id.indexOf("cite_note-"+prm_name+"-"+prm_n)==0){
			ob1.className = "harvBackground";
		}else if(ob1.id.indexOf("cite_note-")==0){
			ob1.className = "";
		}
	}
	
}

function setBackground2(prm_name){
	
	var n="";

	var arr=prm_name.split("-");
	var arr1=arr[1].split("_");
	
	if(arr[2]){
		n="cite_note-"+arr1[0]+"-"+arr1[1];
	}else{
		n="cite_note-"+arr[1];
	}

	var anchorTags = document.getElementsByTagName("li");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className=="harvBackground"){
			ob1.className = "";
		}
		if(ob1.id.indexOf("cite_note-")==0){
			if(ob1.id==n){
				ob1.className = "harvBackground";
			}else{
				ob1.className = "";
			}
		}
	}
	
}

//when user clicks to harv ref in article text
function whenUserClicksToHarvRef(prm_name, prm_n){
	var anchorTags = document.getElementsByTagName("a");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className == "harvReferenceLink" || ob1.className == "harvVisitedLink"){
			ob1.className = "";
		}
	}
	
	var anchorTags = document.getElementsByTagName("sup");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.id=="harv_ref-"+prm_name+"-"+prm_n){
			ob1.className = "harvVisitedLink";
		}else if(ob1.id=="harv_note-"+prm_name+"-"+prm_n){	
			ob1.className = "harvReferenceLink";
		}else if(ob1.id=="harv_note-"+prm_name){	
			ob1.className = "harvReferenceLink";
		}else{	
			ob1.className = "";
		}
	}
	setBackground(prm_name);
}

//when user clicks to big backlink from bibliography
function whenUserClicksToHarvBigBacklink(prm_name){
	var anchorTags = document.getElementsByTagName("a");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className == "harvReferenceLink" || ob1.className == "harvVisitedLink"){
			ob1.className = "";
		}
	}

	var anchorTags = document.getElementsByTagName("sup");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className == "harvReferenceLink" || ob1.className == "harvVisitedLink"){
			ob1.className = "";
		}
		
		if(ob1.id.indexOf("harv_ref-"+prm_name+"-")==0){
			ob1.className = "harvVisitedLink";
		}else if(ob1.id=="harv_note-"+prm_name){
			ob1.className = "harvReferenceLink";
		}
	}
	
	setBackground(prm_name);
}

//when user clicks to small (number) backlink from bibliography
function whenUserClicksToHarvSmallBacklink(prm_name, prm_n){
	var anchorTags = document.getElementsByTagName("a");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className == "harvReferenceLink" || ob1.className == "harvVisitedLink"){
			ob1.className = "";
		}
	}

	var anchorTags = document.getElementsByTagName("sup");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className == "harvReferenceLink" || ob1.className == "harvVisitedLink"){
			ob1.className = "";
		}
		
		if(ob1.id=="harv_ref-"+prm_name+"-"+prm_n){
			ob1.className = "harvVisitedLink";
		}else if(ob1.id=="harv_note-"+prm_name){
			ob1.className = "harvReferenceLink";
		}else if(ob1.id=="harv_note-"+prm_name+"-"+prm_n){
			ob1.className = "harvReferenceLink";
		}
	}
	setBackground(prm_name);
}

function showRefsMode(n){
	var anchorTags = document.getElementsByTagName("sup");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.id.indexOf("harv_ref-")==0){
			if(n==""){
				ob1.style.display = "";
				ob2=ob1.childNodes[0]; 
				ob3=ob2.childNodes[0]; 
				if(ob3.nodeValue.indexOf("*")>=0){
					t=ob2.title;
					ob3.nodeValue=t;
				};
			}else if(n=="hide"){
				ob1.style.display = "none";
			}else if (n=="short"){
				ob1.style.display = "";
				ob2=ob1.childNodes[0]; 
				ob3=ob2.childNodes[0]; 
				var t=ob3.nodeValue;
				if(ob1.childNodes.length>1){
					var p=ob1.childNodes[1].nodeValue;
					t=t+ob1.childNodes[1].nodeValue;
					ob1.childNodes[1].nodeValue="";
				}
				ob2.title=t;
				ob3.nodeValue="* ";
			}else{	
				ob1.style.display = "";
			}
		}
	}
}

this.changeFormat = function(){
	
	var s=state;
	if(s==""){
		s="short";
	}else if(s=="short"){	
		s="hide";
	}else if(s=="hide"){	
		s="";
	}
	state=s;
	showRefsMode(s);
}

//when user clicks to ref in article text whenUserClicksToRef
function whenUserClicksToRef(prm_name, prm_n){
	var anchorTags = document.getElementsByTagName("sup");
	//alert('"#cite_ref'+prm_name+'-'+prm_n+'"');
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		//alert(ob1.id+"==="+ob1.id.indexOf('cite_ref-'));
		if(ob1.id.indexOf("cite_ref-")==0){	
			if(ob1.id=="cite_ref"+prm_name+"-"+prm_n){
				ob1.className = "harvVisitedLink";
			}else{	
				ob1.className = "harvNone";
			}
		}else if(ob1.id.indexOf("harv_ref-")==0){	
			ob1.className = "";
		}else if(ob1.id.indexOf("harv_note-")==0){	
			ob1.className = "";
		}
	}
	//setBackground(prm_name);
	
	var anchorTags = document.getElementsByTagName("a");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		var h=ob1.href;
		var p=h.lastIndexOf("#");
		if(p==-1) continue;
		var h=h.substring(p, h.length);
		if(h.indexOf("#cite_ref-")==0){	
			if(h == "#cite_ref"+prm_name+"-"+prm_n){	
				ob1.className = "harvReferenceLink";
			}else{	
				ob1.className = "";
			}
		}
	}
	setBackground1(prm_name, prm_n);
}

function handler1(id){
	var arr=id.split("-");
	whenUserClicksToHarvRef(arr[1], arr[2]);
}

function handler2(id){
	var arr=id.split("-");
	if(arr[2]){
		whenUserClicksToHarvSmallBacklink(arr[1], arr[2]);
	}else{
		whenUserClicksToHarvBigBacklink(arr[1]);
	}
}

function handler3(id){
	var arr=id.split("-");
	if(arr[2]){
		whenUserClicksToRef("-"+arr[1], arr[2]);
	}else{
		whenUserClicksToRef("", arr[1]);
	}
}

function handler4(id, h){
	var p=h.lastIndexOf("#");
	if(p==-1) return;
	var h=h.substring(p+1, h.length);
	whenUserClicksToBackRef(h);
}

//when user clicks to ref in article text whenUserClicksToRef
function whenUserClicksToBackRef(prm_href){
	
	var anchorTags = document.getElementsByTagName("sup");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		if(ob1.className == "harvReferenceLink"){
			ob1.className = "";
		}
		if(ob1.className == "harvVisitedLink"){
			ob1.className = "";
		}
		if(ob1.id.indexOf("cite_ref-")==0){	
			if(ob1.id==prm_href){
				ob1.className = "harvVisitedLink";
			}else{	
				ob1.className = "";
			}
		}
	}
	
	var anchorTags = document.getElementsByTagName("a");
	for (var i = 0; i < anchorTags.length ; i++){
		var ob1=anchorTags[i];
		var h=ob1.href;
		var p=h.lastIndexOf("#");
		if(p==-1) continue;
		var h=h.substring(p, h.length);
		if(h.indexOf("#cite_ref-")==0){	
			if(h == "#"+prm_href){	
				ob1.className = "harvReferenceLink";
			}else{	
				ob1.className = "";
			}
		}
	}
	setBackground2(prm_href);
}

//Main function on load page
this.onLoad = function(){
	if (wgAction=="view"){ //only for view pages
		var ok=0;
		//install events
		var anchorTags = document.getElementsByTagName("a");
		for (var i = 0; i < anchorTags.length ; i++){
			var ob1=anchorTags[i];
			
			if(ob1.href.indexOf("#harv_note-")>=0){
				ob1.onclick= function(){handler1(this.parentNode.id)};
			}else if(ob1.href.indexOf("#harv_ref-")>=0){
				ob1.onclick= function(){handler2(this.parentNode.id)};
				ok=1;
			}else if(ob1.href.indexOf("#cite_note-")>=0){
				ob1.onclick= function(){handler3(this.parentNode.id)};
				ok=1;
			}else if(ob1.href.indexOf("#cite_ref-")>=0){
				ob1.onclick= function(){handler4(this.parentNode.id, this.href)};
				ok=1;
			}
		}
		
		//install the portlet
		if(ok==1){
			var hist; var url;
			if (!(hist = document.getElementById('ca-history') )) return;
			if (!(url = hist.getElementsByTagName('a')[0] )) return;
			if (!(url = url.href )) return;
			addPortletLink('p-cactions', 'javascript:HrvHighlight.changeFormat();',
						   'links', 'ca-harvrefs', 'Change format of harvard references', '');
			
			var s=state;
			if(s!="") showRefsMode(s);
		}
	}
}

}//obj

//Script is starting here
if (doneOnloadHook) HrvHighlight.onLoad()
else addOnloadHook(HrvHighlight.onLoad);

Addition to Commons.css[edit]

Put at the end of file MediaWiki:Common.css this text:

/*For HarvardReferences extension*/
.harvVisitedLink { 
	background-color: #AAAAAA;
}
.harvReferenceLink { 
	background-color: #FFFF00;
}
.harvNone { 
	background-color: inherit;
}
.harvBackground { 
	background-color: #DDEEFF;
}