1
1
import { pushState } from '$app/navigation' ;
2
2
import isFnurgel from '$lib/utils/isFnurgel' ;
3
- import type { BibIdObj , HoldersByType , HoldingsByInstanceId } from '$lib/types/holdings' ;
4
- import { LensType , type FramedData } from '$lib/types/xl' ;
3
+ import type {
4
+ BibIdObj ,
5
+ HoldersByType ,
6
+ HoldingsByInstanceId ,
7
+ ItemLinksByBibId ,
8
+ ItemLinksForHolder
9
+ } from '$lib/types/holdings' ;
10
+ import { LensType , type FramedData , JsonLd , BibDb } from '$lib/types/xl' ;
5
11
import type { LocaleCode } from '$lib/i18n/locales' ;
6
12
import type { LibraryItem , UserSettings } from '$lib/types/userSettings' ;
7
13
import { relativizeUrl } from '$lib/utils/http' ;
8
14
import { DisplayUtil , toString } from '$lib/utils/xl.js' ;
15
+ import getAtPath from '$lib/utils/getAtPath' ;
16
+ import { holdersCache } from '$lib/utils/holdersCache.svelte' ;
9
17
10
18
export function getHoldingsLink ( url : URL , value : string ) {
11
19
const newSearchParams = new URLSearchParams ( [ ...Array . from ( url . searchParams . entries ( ) ) ] ) ;
@@ -72,13 +80,24 @@ export function getHoldingsByInstanceId(
72
80
} , { } ) ;
73
81
}
74
82
75
- export function getBibIdsByInstanceId ( mainEntity , record ) : Record < string , BibIdObj > {
76
- return mainEntity [ '@reverse' ] ?. instanceOf ?. reduce ( ( acc , instanceOfItem ) => {
77
- const id = relativizeUrl ( instanceOfItem [ '@id' ] ) ?. replace ( '#it' , '' ) ;
83
+ export function getBibIdsByInstanceId (
84
+ mainEntity ,
85
+ displayUtil : DisplayUtil ,
86
+ record ,
87
+ locale : LocaleCode
88
+ ) : Record < string , BibIdObj > {
89
+ return mainEntity [ '@reverse' ] ?. instanceOf ?. reduce ( ( acc , instance ) => {
90
+ const id = relativizeUrl ( instance [ '@id' ] ) ?. replace ( '#it' , '' ) ;
78
91
79
- const bibId = instanceOfItem . meta ?. controlNumber || record ?. controlNumber ;
80
- const type = instanceOfItem [ '@type' ] ;
81
- const holders = instanceOfItem [ '@reverse' ] ?. itemOf ?. map ( ( i ) => i ?. heldBy ?. sigel ) ;
92
+ const bibId = instance . meta ?. controlNumber || record ?. controlNumber ;
93
+ const type = instance [ '@type' ] ;
94
+ const holders = instance [ '@reverse' ] ?. itemOf ?. map ( ( i ) => i ?. heldBy ?. sigel ) ;
95
+ const publication = instance . publication ;
96
+ let str = '' ;
97
+ if ( publication ) {
98
+ str =
99
+ toString ( displayUtil . lensAndFormat ( instance . publication [ 0 ] , LensType . Token , locale ) ) || '' ;
100
+ }
82
101
83
102
// add Legacy Libris III system number for ONR param
84
103
let onr = null ;
@@ -90,7 +109,7 @@ export function getBibIdsByInstanceId(mainEntity, record): Record<string, BibIdO
90
109
91
110
const isbn : string [ ] = [ ] ;
92
111
const issn : string [ ] = [ ] ;
93
- instanceOfItem . identifiedBy ?. forEach ( ( el : { '@type' : string ; value : string } ) => {
112
+ instance . identifiedBy ?. forEach ( ( el : { '@type' : string ; value : string } ) => {
94
113
if ( el [ '@type' ] === 'ISBN' ) {
95
114
isbn . push ( el . value ) ;
96
115
}
@@ -111,7 +130,8 @@ export function getBibIdsByInstanceId(mainEntity, record): Record<string, BibIdO
111
130
holders,
112
131
onr,
113
132
isbn,
114
- issn
133
+ issn,
134
+ str
115
135
}
116
136
} ;
117
137
} , { } ) ;
@@ -185,3 +205,145 @@ export function getMyLibsFromHoldings(
185
205
}
186
206
return Object . values ( result ) ;
187
207
}
208
+
209
+ export async function fetchHoldersIfAbsent ( holdersByType : HoldersByType ) {
210
+ const cachedHolders = holdersCache . holders ;
211
+ const allHolders = Object . values ( holdersByType ) . flat ( ) ;
212
+ for ( const h of allHolders ) {
213
+ const id = h . obj ?. [ '@id' ] ;
214
+
215
+ if ( h . sigel && cachedHolders && ! cachedHolders [ h . sigel ] ) {
216
+ const response = await fetch ( `${ id } ?framed=true` , {
217
+ headers : { Accept : 'application/ld+json' }
218
+ } ) ;
219
+ if ( response . ok ) {
220
+ const resJson = await response . json ( ) ;
221
+ const libraryMainEntity = resJson [ 'mainEntity' ] as FramedData ;
222
+ if ( libraryMainEntity ) {
223
+ cachedHolders [ h . sigel ] = libraryMainEntity ;
224
+ }
225
+ } else {
226
+ console . error ( `Could not fetch holder data for ${ id } ` ) ;
227
+ }
228
+ }
229
+ }
230
+ }
231
+
232
+ export function getItemLinksByBibId (
233
+ bibIdsByInstanceId : Record < string , BibIdObj > ,
234
+ locale : LocaleCode ,
235
+ displayUtil : DisplayUtil
236
+ ) : ItemLinksByBibId {
237
+ const linksByInstanceId : ItemLinksByBibId = { } ;
238
+ for ( const bibIdObj of Object . values ( bibIdsByInstanceId || [ ] ) ) {
239
+ const linksForHolder : ItemLinksForHolder = { } ;
240
+ bibIdObj . holders ?. forEach ( ( sigel ) => {
241
+ if ( holdersCache . holders ) {
242
+ const fullHolderData = holdersCache . holders [ sigel ] ;
243
+
244
+ const ilsPaths = [
245
+ [ BibDb . ils , BibDb . bibIdSearchUri ] ,
246
+ [ BibDb . ils , BibDb . isbnSearchUri ] ,
247
+ [ BibDb . ils , BibDb . issnSearchUri ]
248
+ ] ;
249
+
250
+ const lopacPaths = [ [ BibDb . lopac , BibDb . bibIdSearchUriByLang ] ] ;
251
+
252
+ let linksToItem = getLinksToItemFor ( bibIdObj , fullHolderData , ilsPaths , locale ) ;
253
+ const lopacLinksItem = getLinksToItemFor ( bibIdObj , fullHolderData , lopacPaths , locale ) ;
254
+
255
+ const linkTemplateEod = getAtPath ( fullHolderData , [ BibDb . eodUri ] , [ ] ) ;
256
+ if ( linkTemplateEod && linkTemplateEod . length !== 0 ) {
257
+ linksToItem = [ linkTemplateEod . replace ( / % B I B _ * I D % / , bibIdObj . bibId ) , ...linksToItem ] ;
258
+ }
259
+
260
+ //TODO: rename
261
+ const allLinks : { [ linkType : string ] : string [ ] } = { } ;
262
+
263
+ const itemStatusUri = getAtPath ( fullHolderData , [ BibDb . ils , BibDb . itemStatusUri ] , [ ] ) ;
264
+
265
+ if ( itemStatusUri && itemStatusUri . length !== 0 ) {
266
+ allLinks [ BibDb . ItemStatus ] = [ itemStatusUri ] ;
267
+ }
268
+
269
+ const linksToCatalog : string [ ] = [ ] ;
270
+ const linkToCatalog = getAtPath ( fullHolderData , [ BibDb . ils , 'url' ] , undefined ) ;
271
+ if ( linkToCatalog && linkToCatalog . length !== 0 ) {
272
+ linksToCatalog . push ( linkToCatalog ) ;
273
+ allLinks [ BibDb . LinksToCatalog ] = linksToCatalog ;
274
+ }
275
+
276
+ const linksToSite : string [ ] = [ ] ;
277
+ const linkToSite = getAtPath ( fullHolderData , [ 'url' , JsonLd . ID ] , undefined ) ;
278
+ if ( linkToSite && linkToSite . length !== 0 ) {
279
+ linksToSite . push ( linkToSite ) ;
280
+ allLinks [ BibDb . LinksToSite ] = linksToSite ;
281
+ }
282
+
283
+ const openingHoursList : string [ ] = [ ] ;
284
+ const openingHours = getAtPath ( fullHolderData , [ BibDb . openingHours ] , undefined ) ;
285
+ if ( openingHours && openingHours !== '' ) {
286
+ openingHoursList . push ( openingHours ) ;
287
+ allLinks [ BibDb . OpeningHours ] = openingHoursList ;
288
+ }
289
+
290
+ const addresses : string [ ] = [ ] ;
291
+ const address = getAtPath ( fullHolderData , [ BibDb . address , '*' ] , undefined ) ;
292
+ const postalAddress = address . find ( ( a ) => a [ JsonLd . TYPE ] === BibDb . postalAddress ) ;
293
+ const visitingAddress = address . find ( ( a ) => a [ JsonLd . TYPE ] === BibDb . visitingAddress ) ;
294
+
295
+ if ( address && address . length !== 0 ) {
296
+ addresses . push (
297
+ toString ( displayUtil . lensAndFormat ( visitingAddress , LensType . Card , locale ) ) || ''
298
+ ) ;
299
+ addresses . push (
300
+ toString ( displayUtil . lensAndFormat ( postalAddress , LensType . Card , locale ) ) || ''
301
+ ) ;
302
+ allLinks [ BibDb . Address ] = addresses ;
303
+ }
304
+
305
+ if ( linksToItem . length !== 0 ) {
306
+ allLinks [ BibDb . LinksToItem ] = linksToItem ;
307
+ }
308
+
309
+ if ( lopacLinksItem . length !== 0 ) {
310
+ allLinks [ BibDb . LoanReserveLink ] = lopacLinksItem ;
311
+ }
312
+
313
+ if ( Object . keys ( allLinks ) . length !== 0 ) {
314
+ linksForHolder [ sigel ] = allLinks ;
315
+ }
316
+ }
317
+ } ) ;
318
+ linksByInstanceId [ bibIdObj . bibId ] = linksForHolder ;
319
+ }
320
+ return linksByInstanceId ;
321
+ }
322
+
323
+ function getLinksToItemFor (
324
+ bibIdObj : BibIdObj ,
325
+ fullHolderData : FramedData ,
326
+ paths : string [ ] [ ] ,
327
+ locale : LocaleCode
328
+ ) : string [ ] {
329
+ let linksToItem : string [ ] = [ ] ;
330
+ for ( const path of paths ) {
331
+ const linkTemplate = getAtPath ( fullHolderData , path , [ ] ) ;
332
+ if ( linkTemplate && linkTemplate . length !== 0 ) {
333
+ if ( path . includes ( BibDb . bibIdSearchUriByLang ) && bibIdObj . bibId !== '' ) {
334
+ linksToItem = [ linkTemplate [ locale ] . replace ( / % B I B _ * I D % / g, bibIdObj . bibId ) , ...linksToItem ] ;
335
+ }
336
+ if ( path . includes ( BibDb . bibIdSearchUri ) && bibIdObj . bibId !== '' ) {
337
+ // forms in the wild %BIB_ID%, %BIBID%, more???
338
+ linksToItem = [ linkTemplate . replace ( / % B I B _ * I D % / g, bibIdObj . bibId ) , ...linksToItem ] ;
339
+ }
340
+ if ( path . includes ( BibDb . isbnSearchUri ) && bibIdObj . isbn . length !== 0 ) {
341
+ linksToItem = [ linkTemplate . replace ( / % I S B N % / g, bibIdObj . isbn ) , ...linksToItem ] ;
342
+ }
343
+ if ( path . includes ( BibDb . issnSearchUri ) && bibIdObj . issn . length !== 0 ) {
344
+ linksToItem = [ linkTemplate . replace ( / % I S S N % / g, bibIdObj . issn ) , ...linksToItem ] ;
345
+ }
346
+ }
347
+ }
348
+ return linksToItem ;
349
+ }
0 commit comments