inspire-citesummary-js
Version:
Cite Summary component for INSPIRE HEP
22 lines • 10.6 kB
JavaScript
/*
* This file is part of INSPIRE HEP.
* Copyright (C) 2016 CERN.
*
* INSPIRE HEP is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* INSPIRE HEP is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Invenio; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* In applying this license, CERN does not
* waive the privileges and immunities granted to it by virtue of its status
* as an Intergovernmental Organization or submit itself to any jurisdiction.
*/!function(e){e.module("citeSummary",["citeSummary.services","citeSummary.directives"])}(angular),function(e){function t(){function e(e,t){return t.template}var t=["$scope","CiteSummaryService",function(e,t){e.vm={},e.vm.loading=!0,e.vm.searchTerm=null,e.vm.showRelated=!1,t.load(e.apiEndpoint).then(function(t){e.vm.data=t,setTimeout(function(){citeSummaryVis.renderSummary(e.vm.data),citeSummaryVis.registerResizeListener(e.vm.data)},0)})}];return{templateUrl:e,restrict:"AE",scope:{apiEndpoint:"@apiEndpoint"},controller:t}}e.module("citeSummary.directives",[]).directive("citeSummary",t)}(angular),function(e){e.module("citeSummary.services",[]).factory("CiteSummaryService",["$http",function(e){return{load:function(t){return e.get(t).then(function(e){return e.data}).catch(function(e){return console.error("cannot fetch content from "+t,e),{}})}}}])}(angular);var citeSummaryVis=function(){function e(){try{setTimeout(function(){MathJax.Hub.Queue(["Typeset",MathJax.Hub])},1)}catch(e){console.error("We're unable to run the MathJax formatting. Ensure you have added the library to your page.")}}var t=d3.scale.ordinal().domain(["Not Self","Self"]).range(["#3498db","#e74c3c"]),n=d3.scale.ordinal().domain(["Other","Collaboration Paper"]).range(["#95a5a6","#ecf0f1"]),i="%d %b %Y",r=0,a="#7f8c8d",o="#3498db",c={"hep-th":"Theory-HEP","hep-ex":"Experiment-HEP","nucl-th":"Theory-Nucl","hep-ph":"Phenomenology-HEP","nucl-ex":"Experiment-Nucl","hep-lat":"Lattice","gr-qc":"General Relativity","quant-ph":"Quantum Physics","Nuclear Reactions":"General Physics","double-beta decay":"General Physics",astro:"Astrophysics",math:"Math and Math Physics",cs:"Computing",stat:"Data Analysis and Statistics",cond:"Other",nlin:"Other",Relativistic:"Experiment-HEP",Electroweak:"General Physics",Accelerators:"Accelerators","Field Theory":"Lattice",Cosmology:"Astrophysics","Elementary Particles":"General Physics"},s={book:"Book",bookchapter:"Book Chapter",conferencepaper:"Conference Paper",note:"Note",proceedings:"Proceedings",published:"Published",report:"Report",review:"Review",thesis:"Thesis","":"None"},u={top:20,right:20,bottom:20,left:40},d=function(e){return e.value.paperCount},l=function(e){return e.value},p=d3.time.format(i),m=d3.format("s"),f=function(e){return new Date(e.substring(0,4),e.substring(5,7),e.substring(8))},h=function(e,t){return"string"==typeof e?(e=d3.time.format(i).parse(e),t=d3.time.format(i).parse(t),t-e):t.citation_date-e.citation_date},_=function(){return $(window).width()},v=function(e,t){return 900>=e?.83*e:e*t},g=function(t,n,i,r,a,o){var c=dc.rowChart(t).dimension(n).height(360).width(a).group(i).elasticX(!0).colors(r).valueAccessor(o);c.xAxis().ticks(5),c.xAxis().tickFormat(m),c.ordering(function(e){return-e.value}),c.on("filtered",e)},y=function(t,n,i,r,a){var o=dc.pieChart(t).width(a).dimension(n).radius(60).cx(80).cy(60).innerRadius(40).group(i).colors(r).legend(dc.legend2().x(170).y(0).itemHeight(12).gap(5).horizontal(1).legendWidth(90).itemWidth(95).showPercent(!0));o.ordering(function(e){return-e.value}),o.on("filtered",e)},b=function(e,t){return t.paper_id in e.observedPapers?e.observedPapers[t.paper_id]++:(e.observedPapers[t.paper_id]=1,e.paperCount++),e},x=function(e,t){return e.observedPapers[t.paper_id]--,0===e.observedPapers[t.paper_id]&&(delete e.observedPapers[t.paper_id],e.paperCount--),e},C=function(){return{paperCount:0,observedPapers:{}}},P=function(e){var t=A(e);return t.forEach(function(e,t){e.index=t,e.paper_date=f(e.paper_date),e.citation_date=f(e.citation_date),["paper","citation"].forEach(function(t){var n=e[t+"_subject"];e[t+"_subject"]in c?e[t+"_subject"]=c[n]:Object.keys(c).forEach(function(i){return-1!=n.indexOf(i)?(e[t+"_subject"]=c[i],void 0):void 0});var i=e[t+"_document_type"];e[t+"_document_type"]in s&&(e[t+"_document_type"]=s[i])})}),t},A=function(e){var t=[];return e.forEach(function(e){var n={};Object.keys(e).forEach(function(t){"citations"!==t&&(n["paper_"+t]=e[t])}),n.paper_citation_count=e.citations.length,e.citations.length>0?e.citations.forEach(function(e){var i=$.extend({},n);Object.keys(e).forEach(function(t){"citations"!==t&&(i["citation_"+t]=e[t])}),t.push(i)}):r++}),t};return{registerResizeListener:function(e){function t(){new Date-n<r?setTimeout(t,r):(i=!1,d3.select("#contents").classed("hidden",!0),d3.select("#spinner").classed("hidden",!1),citeSummaryVis.renderSummary(e))}var n,i=!1,r=200;$(window).resize(function(){n=new Date,i===!1&&(i=!0,setTimeout(t,r))})},renderSummary:function(i){var c=!1;i.length>1&&(c=!0);var s=P(i),f=crossfilter(s),A=f.dimension(function(e){return e.citation_date}),w=f.dimension(function(e){return d3.time.year(e.citation_date)}),E=f.dimension(function(e){return e.citation_document_type}),S=f.dimension(function(e){return e.citation_selfcite?"Self":"Not Self"}),k=f.dimension(function(e){return e.citation_collaboration?"Collaboration Paper":"Other"}),D=f.dimension(function(e){return e.citation_subject}),L=f.dimension(function(e){return e.paper_date}),j=f.dimension(function(e){return d3.time.year(e.paper_date)}),T=f.dimension(function(e){return e.paper_document_type}),H=f.dimension(function(e){return e.paper_citation_count}),O=f.dimension(function(e){return e.paper_subject}),G=j.group().reduce(b,x,C),R=H.group().reduce(b,x,C),z=O.group().reduce(b,x,C),N=T.group().reduce(b,x,C),B=w.group(),V=E.group(),Y=S.group(),F=D.group(),M=k.group(),$={all:function(){var e=0,t=[];return B.all().forEach(function(n){e+=n.value,t.push({key:n.key,value:e,single_value:n.value})}),t}},U={all:function(){var e=0,t=[];return G.all().forEach(function(n){e+=n.value.paperCount,t.push({key:n.key,value:e,single_value:n.value.paperCount})}),t}},J=_(),W=f.groupAll();dc.dataCount("#citations_summary").dimension(f).group(W);var q=f.groupAll(),I={};if(q.reduce(function(e,t){return t.paper_id in I?I[t.paper_id]++:(I[t.paper_id]=1,e++),e},function(e,t){return I[t.paper_id]--,0===I[t.paper_id]&&(delete I[t.paper_id],e--),e},function(){return 0}),dc.dataCount("#papers_summary").dimension(f).group(q).valueAccessor(function(e){return e.value.paperCount}),c){var Q=new Date(L.bottom(1)[0].paper_date),X=new Date(L.top(1)[0].paper_date);Q.setDate(Q.getDate()-30),X.setDate(X.getDate()+30);var K=dc.compositeChart(document.getElementById("papers"));K.width(v(J,c?.47:.85)).height(180).margins(u).dimension(j).x(d3.time.scale().domain([Q,X])).xUnits(d3.time.years).xAxisLabel("Publication Year").yAxisLabel("# Papers").elasticY(!0).renderHorizontalGridLines(!0).renderVerticalGridLines(!0).compose([dc.lineChart(K).dimension(j).group(U,"Cumulative Papers").valueAccessor(function(e){return e.value}).colors(a),dc.barChart(K).dimension(j).group(U,"Papers").x(d3.time.scale().domain([Q,X])).xUnits(d3.time.years).valueAccessor(function(e){return e.single_value}).colors(a)]),K.yAxis().tickFormat(m),K.on("filtered",e),K.legend(dc.legend().x(80).y(20).itemHeight(13).gap(5)).brushOn(!0);var Z=v(J,.24);g("#papers_document_type",T,N,a,Z,d),g("#papers_subject_area",O,z,a,Z,d);var et=[H.bottom(1)[0].paper_citation_count,H.top(1)[0].paper_citation_count],tt=dc.barChart("#papers_citation_count").width(v(J,.47)).height(150).dimension(H).elasticY(!0).group(R,"Papers").valueAccessor(function(e){return e.value.paperCount}).x(d3.scale.linear().domain([et[0]-1,et[1]+1])).xAxisLabel("# Citations").yAxisLabel("# Papers").colors(a);tt.xAxis().tickFormat(m),tt.on("filtered",e)}var nt=dc.compositeChart(document.getElementById("citations")),it=new Date(A.bottom(1)[0].citation_date),rt=new Date(A.top(1)[0].citation_date);it.setDate(it.getDate()-30),rt.setDate(rt.getDate()+30),nt.width(v(J,c?.42:.85)).height(180).margins(u).x(d3.time.scale().domain([it,rt])).xUnits(d3.time.years).xAxisLabel("Citation Year").yAxisLabel("# Citations").elasticY(!0).renderHorizontalGridLines(!0).dimension(w).renderVerticalGridLines(!0).compose([dc.lineChart(nt).dimension(w).group($,"Cumulative Citations").valueAccessor(function(e){return e.value}).colors(o),dc.barChart(nt).dimension(w).group(B,"Citations").colors(o)]),nt.yAxis().tickFormat(m),nt.on("filtered",e),nt.legend(dc.legend().x(80).y(20).itemHeight(13).gap(5)).brushOn(!0),g("#citations_subjects",E,V,o,Z,l),g("#citations_subject_area",D,F,o,Z,l),y("#citations_self_cites",S,Y,t,Z),y("#citations_collaboration",k,M,n,Z);var at=dc.dataTable("#citations_data_table");at.dimension(A).group(function(e){return'<div class="dc-title-text"><a href="/record/'+e.paper_id+'">'+e.paper_title+'</a> <div class="dc-published-date label label-default">Published '+p(e.paper_date)+'</div> <p class="text-muted">'+e.paper_citation_count+" citations including:</p> </div>"}).columns([function(){return""},function(e){return'<div class="label label-info cite-info">Cited on '+p(e.citation_date)+"</div>"},function(e){return'<div class="label '+(e.citation_selfcite?"label-danger":"label-default")+'">'+(e.citation_selfcite?" Self Citation":"")+'</div> <a href="/record/'+e.citation_id+'" target="_blank" class="dc-citation-link">'+e.citation_title+"</a>"},function(e){return e.citation_document_type},function(e){return e.citation_subject}]).sortBy(function(e){return e.citation_date}).order(h),at.on("filtered",e),dc.renderAll(),d3.select("#papers_summary .total-papers").text(i.length),r>0&&(d3.select("#papers_summary .zero-papers").classed("hidden",!1),d3.select("#papers_summary .zero-papers .count").text(r)),d3.select("#spinner").classed("hidden",!0),d3.select("#contents").classed("hidden",!1),setTimeout(function(){e()},0),d3.select("#download_csv").on("click",function(){var e=new Blob([d3.csv.format(L.top(1/0))],{type:"text/csv;charset=utf-8"});saveAs(e,"data.csv")})}}}();