]> Softwares of Agnibho - sysrev.git/blob - script.js
Confirm before reloading from URL
[sysrev.git] / script.js
1 var bibgraph={};
2 var idx=0;
3 var doi="https://doi.org/";
4 var urlParams=new URLSearchParams(window.location.search);
5 if(urlParams.has("doi")){
6 doi="https://"+urlParams.get("doi")+"/";
7 }
8 if(localStorage.getItem("bibliographies")!=null){
9 bibgraph=JSON.parse(localStorage.getItem("bibliographies"));
10 }
11 function write(){
12 localStorage.setItem("bibliographies", JSON.stringify(bibgraph));
13 }
14 function abstractFormat(text){
15 console.log(text);
16 text="\n"+text;
17 text=text.replace(/\nobjectives?/i, "<strong>Objectives</strong>");
18 text=text.replace(/\nbackground/i, "<strong>Background</strong>");
19 text=text.replace(/\nmethods?/i, "\n<strong>Methods</strong>");
20 text=text.replace(/\ndesigns?/i, "\n<strong>Design</strong>");
21 text=text.replace(/\nmaterials? and methods?/i, "\n<strong>Materials and methods</strong>");
22 text=text.replace(/\nmethodology/i, "\n<strong>Methodology</strong>");
23 text=text.replace(/\ninterventions?/i, "\n<strong>Interventions</strong>");
24 text=text.replace(/\nstatistical analysis/i, "\n<strong>Statistical Analysis</strong>");
25 text=text.replace(/\nprincipal findings?/i, "\n<strong>Principal Findings</strong>");
26 text=text.replace(/\nfindings?/i, "\n<strong>Findings</strong>");
27 text=text.replace(/\noutcomes?/i, "\n<strong>Outcomes</strong>");
28 text=text.replace(/\nresults?/i, "\n<strong>Results</strong>");
29 text=text.replace(/\nconclusions?/i, "\n<strong>Conclusions</strong>");
30 text=text.replace(/\nsignificance/i, "\n<strong>Significance</strong>");
31 console.log(text);
32 return text;
33 }
34 function showAllBibs(){
35 $("#all-bibs").html("");
36 Object.values(bibgraph).forEach(function(bib){
37 idx=bib.config.id;
38 $("#all-bibs").html($("#all-bibs").html()+"<li class='list-group-item'><a href='#' data-id='"+bib.config.id+"' class='select-bib' >"+bib.config.label+"</a> ["+bib.config.id+"] <button data-id='"+bib.config.id+"' class='btn btn-sm btn-outline-secondary delete-bib'>Delete</button></li>");
39 });
40 $("#all-bibs").on("click", ".select-bib", function(e){
41 e.preventDefault();
42 $("#loader").addClass("d-none");
43 $("#step-1-div").removeClass("d-none");
44 idx=$(this).data("id");
45 $("#step-1").html("");
46 bibgraph[idx].items.forEach(function(item,i){
47 bibgraph[idx].items[i].selected=[false,false,false];
48 $("#step-1").html($("#step-1").html()+"<a href='#' class='list-group-item to-select' data-step='0' data-cite='"+item.citationKey+"'><input type='checkbox' class='step-1-select'> "+item.title+" ["+item.creators[0].lastName+" et al]</a>");
49 });
50 });
51 $("#all-bibs").on("click", ".delete-bib", function(e){
52 e.preventDefault();
53 delete bibgraph[$(this).data("id")];
54 write();
55 showAllBibs();
56 });
57 }
58 $(document).ready(function(){
59 showAllBibs();
60 $(".step-selector").on("click", ".to-select", function(e){
61 e.preventDefault();
62 $(this).children("input").prop("checked", !$(this).children("input").prop("checked"));
63 bibgraph[idx].items.find(i=>i.citationKey==$(this).data("cite")).selected[$(this).data("step")]=$(this).children("input").prop("checked");
64 write();
65 });
66 if(urlParams.has("src")){
67 if(urlParams.has("file")){
68 file="&file="+urlParams.get("file");
69 }
70 else{
71 file="";
72 }
73 $.getJSON(urlParams.get("src")+"?callback=?"+file, function(data){
74 if(typeof bibgraph[data.config.id]=="undefined"){
75 bibgraph[data.config.id]=data;
76 }
77 else{
78 if(confirm("Data with same ID already exists. Overwrite?")){
79 bibgraph[data.config.id]=data;
80 }
81 }
82 write();
83 showAllBibs();
84 });
85 }
86 $("#load-file").click(function(e){
87 e.preventDefault();
88 file=document.getElementById("file-selector").files[0];
89 fr=new FileReader();
90 fr.onload=fileLoaded;
91 fr.readAsText(file);
92 function fileLoaded(){
93 try{
94 result=JSON.parse(fr.result);
95 bibgraph[result.config.id]=result;
96 }
97 catch(e){
98 alert("Failed to parse data. \n"+e);
99 }
100 write();
101 showAllBibs();
102 }
103 });
104 $("#step-1to2").click(function(e){
105 e.preventDefault();
106 $("#step-1-div").addClass("d-none");
107 $("#step-2-div").removeClass("d-none");
108 $("#step-2").html("");
109 bibgraph[idx].items.forEach(function(item, idx){
110 flag="";
111 if(item.selected[1]==true) flag="checked";
112 if(item.selected[0]){
113 $("#step-2").html($("#step-2").html()+"<div class='list-group-item'><a href='#' class='to-select' data-step='1' data-cite='"+item.citationKey+"'><input type='checkbox' "+flag+" class='step-2-select'> "+item.title+" ["+item.creators[0].lastName+" et al]</a><div class='abstract'>"+abstractFormat(item.abstractNote)+"</div></div>");
114 }
115 });
116 });
117 $("#step-2to3").click(function(e){
118 e.preventDefault();
119 $("#step-2-div").addClass("d-none");
120 $("#step-3-div").removeClass("d-none");
121 $("#step-3").html("");
122 bibgraph[idx].items.forEach(function(item, idx){
123 flag="";
124 if(item.selected[2]==true) flag="checked";
125 if(item.selected[1]){
126 if(item.url){
127 url="<br>Link: <a href='"+item.url+"' target='_blank'>"+item.url+"</a>";
128 }
129 else{
130 url="";
131 }
132 $("#step-3").html($("#step-3").html()+"<div class='list-group-item'><a href='#' class='to-select' data-step='2' data-cite='"+item.citationKey+"'><input type='checkbox' "+flag+" class='step-3-select'> "+item.title+" ["+item.creators[0].lastName+" et al]</a><div class='alert alert-info text-right'>Paper available at DOI: <a href='"+doi+item.DOI+"' target='_blank'>"+item.DOI+"</a>"+url+"</div></div>");
133 }
134 });
135 });
136 $("#finish").click(function(e){
137 e.preventDefault();
138 $("#step-3-div").addClass("d-none");
139 $("#download-div").removeClass("d-none");
140 finalBib=$.extend(true, {}, bibgraph[idx]);
141 for(i=(finalBib.items.length-1); i>=0; i--){
142 if(!(finalBib.items[i].selected[0] && finalBib.items[i].selected[1] && finalBib.items[i].selected[2])){
143 finalBib.items.splice(i,1);
144 }
145 }
146 $("#finished").html("");
147 finalBib.items.forEach(function(item){
148 if(item.selected[2]){
149 $("#finished").html($("#finished").html()+"<li class='list-group-item'>"+item.title+" ["+item.creators[0].lastName+" et al]</li>");
150 delete item.selected;
151 }
152 });
153 data="data:text/json;charset=utf-8,"+encodeURIComponent(JSON.stringify(finalBib, null, 2));
154 $("#download").prop("href", data);
155 $("#download").prop("download", finalBib.config.label+".json");
156 });
157 $("#step-back").click(function(e){
158 e.preventDefault();
159 $("#step-1-div").addClass("d-none");
160 $("#loader").removeClass("d-none");
161 });
162 $("#step-2to1").click(function(e){
163 e.preventDefault();
164 $("#step-2-div").addClass("d-none");
165 $("#step-1-div").removeClass("d-none");
166 });
167 $("#step-3to2").click(function(e){
168 e.preventDefault();
169 $("#step-3-div").addClass("d-none");
170 $("#step-2-div").removeClass("d-none");
171 });
172 $("#step-backto3").click(function(e){
173 e.preventDefault();
174 $("#download-div").addClass("d-none");
175 $("#step-3-div").removeClass("d-none");
176 });
177 });