@@ -13,7 +13,8 @@ var PASTA_CONFIG = {
13
13
"pagesTopElementId" : "paginationTop" , // Element to display result page links above results
14
14
"pagesBotElementId" : "paginationBot" , // Element to display result page links below results
15
15
"showPages" : 5 , // MUST BE ODD NUMBER! Max number of page links to show
16
- "sortDiv" : "sortDiv" // Element with interactive sort options
16
+ "sortDiv" : "sortDiv" , // Element with interactive sort options
17
+ "useCiteService" : true // true if we should use EDI Cite service to build citations instead of building from PASTA results
17
18
} ;
18
19
19
20
var QUERY_URL = "" ; // Query URL without row limit or start parameter
@@ -29,19 +30,83 @@ function getParameterByName(name, url) {
29
30
return decodeURIComponent ( results [ 2 ] . replace ( / \+ / g, " " ) ) . trim ( ) ;
30
31
}
31
32
32
- // Parse Pasta search results into HTML
33
- function parsePastaResults ( xmlDoc ) {
34
- var docs = xmlDoc . getElementsByTagName ( "document" ) ;
33
+ // Parse citation dictionary into HTML
34
+ function buildHtml ( citations ) {
35
35
var html = [ ] ;
36
- var sortDiv = document . getElementById ( PASTA_CONFIG [ "sortDiv" ] ) ;
37
- if ( sortDiv ) {
38
- if ( docs . length )
39
- sortDiv . style . display = "block" ;
40
- else
41
- sortDiv . style . display = "none" ;
36
+ var citationCount = Object . keys ( citations ) . length ;
37
+
38
+ for ( var i = 0 ; i < citationCount ; i ++ ) {
39
+ var citation = citations [ i ] ;
40
+ var authors = citation [ "authors" ] ;
41
+ var date = ( citation [ "pub_year" ] ) ? " Published " + citation [ "pub_year" ] + "" : "" ;
42
+ // default ESIP formatting has trailing period after DOI
43
+ var link = ( citation [ "doi" ] ) ? citation [ "doi" ] . slice ( 0 , - 1 ) : "https://portal.edirepository.org/nis/mapbrowse?packageid=" + citation [ "pid" ] ;
44
+ var title = '<a rel="external noopener" href="' + link + '" target="_blank">' + citation [ "title" ] + '</a>' ;
45
+ var row = '<p><span class="dataset-title">' + title +
46
+ '</span><br><span class="dataset-author">' + authors + date +
47
+ '</span></p>' ;
48
+ html . push ( row ) ;
49
+ }
50
+ if ( citationCount ) {
51
+ return html . join ( "\n" ) ;
52
+ } else {
53
+ return "<p>Your search returned no results.</p>" ;
54
+ }
55
+ }
56
+
57
+ // Download citations to a dictionary keyed by package ID
58
+ function getCitations ( packageIds ) {
59
+ var header = {
60
+ "Accept" : "application/json"
61
+ } ;
62
+ var callsRemaining = packageIds . length ;
63
+ var baseUri = "https://cite.edirepository.org/cite/" ;
64
+ var citations = { } ;
65
+
66
+ packageIds . forEach ( function ( pid , index ) {
67
+ var uri = baseUri + pid ;
68
+ makeCorsRequest (
69
+ uri ,
70
+ header ,
71
+ ( function ( index ) { // enable the callback to know which package this is
72
+ return function ( headers , response ) {
73
+ var citation = JSON . parse ( response ) ;
74
+ citation [ "pid" ] = packageIds [ index ] ;
75
+ citations [ index ] = citation ;
76
+
77
+ -- callsRemaining ;
78
+ if ( callsRemaining <= 0 ) {
79
+ var html = buildHtml ( citations ) ;
80
+ document . getElementById ( "searchResults" ) . innerHTML = html ;
81
+ showLoading ( false ) ;
82
+ }
83
+ } ;
84
+ } ) ( index ) , // immediately call the closure with the current index value
85
+ errorCallback
86
+ ) ;
87
+ } ) ;
88
+ }
89
+
90
+ // Build dataset citations using Cite service, with package IDs from PASTA
91
+ function buildCitationsFromCite ( pastaDocs ) {
92
+ var packageIds = [ ] ;
93
+ for ( var i = 0 ; i < pastaDocs . length ; i ++ ) {
94
+ var doc = pastaDocs [ i ] ;
95
+ packageIds . push ( doc . getElementsByTagName ( "packageid" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) ;
96
+ }
97
+ if ( packageIds . length ) {
98
+ getCitations ( packageIds ) ;
99
+ } else {
100
+ document . getElementById ( "searchResults" ) . innerHTML = "<p>Your search returned no results.</p>" ;
101
+ showLoading ( false ) ;
42
102
}
43
- for ( var i = 0 ; i < docs . length ; i ++ ) {
44
- var doc = docs [ i ] ;
103
+ }
104
+
105
+ // Build dataset citations from PASTA XML
106
+ function buildCitationsFromPasta ( pastaDocs ) {
107
+ var html = [ ] ;
108
+ for ( var i = 0 ; i < pastaDocs . length ; i ++ ) {
109
+ var doc = pastaDocs [ i ] ;
45
110
var authorNodes = doc . getElementsByTagName ( "author" ) ;
46
111
var authors = [ ] ;
47
112
for ( var authorIndex = 0 ; authorIndex < authorNodes . length ; authorIndex ++ ) {
@@ -73,11 +138,14 @@ function parsePastaResults(xmlDoc) {
73
138
'</span></p>' ;
74
139
html . push ( row ) ;
75
140
}
76
- if ( docs . length ) {
77
- return html . join ( "\n" ) ;
141
+ var resultHtml ;
142
+ if ( html . length ) {
143
+ resultHtml = html . join ( "\n" ) ;
78
144
} else {
79
- return "<p>Your search returned no results.</p>" ;
145
+ resultHtml = "<p>Your search returned no results.</p>" ;
80
146
}
147
+ document . getElementById ( "searchResults" ) . innerHTML = resultHtml ;
148
+ showLoading ( false ) ;
81
149
}
82
150
83
151
function showLoading ( isLoading ) {
@@ -168,7 +236,7 @@ function downloadCsv(count) {
168
236
169
237
for ( var i = 0 ; i < calls ; i ++ ) {
170
238
var url = baseUri + start ;
171
- makeCorsRequest ( url , addChunk , errorCallback ) ;
239
+ makeCorsRequest ( url , null , addChunk , errorCallback ) ;
172
240
start += limit ;
173
241
}
174
242
@@ -184,12 +252,22 @@ function successCallback(headers, response) {
184
252
return html ;
185
253
}
186
254
187
- showLoading ( false ) ;
188
-
189
255
// Write results to page
190
256
var parser = new DOMParser ( ) ;
191
257
var xmlDoc = parser . parseFromString ( response , "text/xml" ) ;
192
- setHtml ( PASTA_CONFIG [ "resultsElementId" ] , parsePastaResults ( xmlDoc ) ) ;
258
+ var docs = xmlDoc . getElementsByTagName ( "document" ) ;
259
+ var sortDiv = document . getElementById ( PASTA_CONFIG [ "sortDiv" ] ) ;
260
+ if ( sortDiv ) {
261
+ if ( docs . length )
262
+ sortDiv . style . display = "block" ;
263
+ else
264
+ sortDiv . style . display = "none" ;
265
+ }
266
+ if ( PASTA_CONFIG [ "useCiteService" ] ) {
267
+ buildCitationsFromCite ( docs ) ;
268
+ } else {
269
+ buildCitationsFromPasta ( docs ) ;
270
+ }
193
271
var count = parseInt ( xmlDoc . getElementsByTagName ( "resultset" ) [ 0 ] . getAttribute ( "numFound" ) ) ;
194
272
setHtml ( PASTA_CONFIG [ "csvElementId" ] , makeCsvLink ( count ) ) ;
195
273
@@ -228,7 +306,7 @@ function searchPasta(limit, pageStart) {
228
306
var url = QUERY_URL + params ;
229
307
showUrl ( url ) ;
230
308
showLoading ( true ) ;
231
- makeCorsRequest ( url , successCallback , errorCallback ) ;
309
+ makeCorsRequest ( url , null , successCallback , errorCallback ) ;
232
310
}
233
311
234
312
function initCollapsible ( expanded ) {
0 commit comments