Skip to content

Commit

Permalink
Fix for mozillascience#135 and adds a test widget (also as an example)
Browse files Browse the repository at this point in the history
  • Loading branch information
josmas committed Oct 27, 2016
1 parent 665e11d commit e47c01f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
22 changes: 22 additions & 0 deletions public/widgets/demos/doi-paper-badger-widget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<div class="my-container"></div>

<script type="text/javascript">
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "../paper-badger-widget.js";
document.write(decodeURIComponent("%3Cscript src='../paper-badger-widget.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script>
var conf={"article-doi": "10.1093/nar/gkt1063", "container-class": "my-container"};
showBadges(conf);
</script>
</body>
</html>
34 changes: 20 additions & 14 deletions public/widgets/paper-badger-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var clickEvent={
runAnalytics : [],
assignCallback : function(func)
{
this.runAnalytics=func;
this.runAnalytics=func;
}
};

Expand All @@ -14,7 +14,7 @@ function callAjax(url, callbackFunction) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callbackFunction(xmlhttp.responseText);
}
}
};

xmlhttp.open("GET", url, true);
xmlhttp.send();
Expand Down Expand Up @@ -59,7 +59,7 @@ function insertCSS()
function showLine(event)
{
var bindBadge=this.getAttribute("data-bind-badge");

if(event.type=="click") {clickEvent.runAnalytics({"orcid" : bindBadge, "taxonomyClass" : this.getAttribute("data-bind-taxonomy")});}

//var boundElem=$(".badge span[data-bind-badge="+bindBadge+"]");
Expand Down Expand Up @@ -94,7 +94,13 @@ function getEndpoint(confIn, count)
var countPoint=(count) ? "/count" : "";

var endPoint=[];
if(doi) {endPoint.push("papers/"+doi);}
if(doi) {
var splitDoi = doi.split('/');
if (splitDoi.length === 3) {
doi = splitDoi[0] + '/' + encodeURIComponent(splitDoi[1] + '/' + splitDoi[2]);
}
endPoint.push("papers/"+doi);
}
if(orcid) {endPoint.push("users/"+orcid);}
return "https://badges.mozillascience.org/"+endPoint.join("/")+"/badges"+countPoint;
}
Expand All @@ -103,28 +109,28 @@ function showBadgeFurniture(confIn)
{
var furnitureClass=(confIn["furniture-class"]) ? confIn["furniture-class"] : "paper-badges-hidden";
var endPoint=getEndpoint(confIn, 1);

callAjax(endPoint, function(dataItem){
var badgesCount=(dataItem) ? dataItem : 0;

if(badgesCount && dataItem/dataItem)
{
visitArray(document.querySelectorAll("."+furnitureClass), function(elem){
var list=elem.classList;

if(list!==undefined && list.contains(furnitureClass))
{
list.remove(furnitureClass);
list.remove(furnitureClass);
}
else
{
var className=elem.className;
var indStart=className.indexOf(furnitureClass);
var indEnd=className.indexOf(" ", indStart);

indEnd=(indEnd>=0) ? indEnd : className.length;
elem.className=className.substring(0, indStart)+" "+className.substring(indEnd);
}
}
});
}
});
Expand All @@ -139,7 +145,7 @@ function showBadges(confIn, callback){

if(callback)
{
clickEvent.assignCallback(callback);
clickEvent.assignCallback(callback);
}

callAjax(endPoint, function( entryData ) {
Expand Down Expand Up @@ -226,12 +232,12 @@ function showBadges(confIn, callback){
containerClassQuery.appendChild(newNode).innerHTML=returnString;
}
}

visitArray(document.getElementsByClassName("badge-span"), function(elem){
if(elem){
elem.addEventListener("mouseover", showLine);
elem.addEventListener("click", showLine);
elem.addEventListener("click", nextAction);
elem.addEventListener("click", showLine);
elem.addEventListener("click", nextAction);
}
});
});
Expand Down

0 comments on commit e47c01f

Please sign in to comment.