|
1 | | -import { base } from '$app/paths'; |
2 | 1 | import JSZip from 'jszip'; |
| 2 | +import { base } from '$app/paths'; |
3 | 3 |
|
4 | 4 | /** |
5 | 5 | * Creates and downloads a CSV file from inscription data |
6 | 6 | * |
7 | 7 | * @param {string} summary - The summary of the search |
8 | | - * @param {Array<{ file: string; title: string; notBefore: number; notAfter: number; places: Array<{ offset: string; _: string; type: string; }>; status: { _: string; }; type: { _: string; }; objectType: { _: string; }; language: { _: string; }; settlement: { _: string; }; }>} inscriptions - Array of inscription objects |
| 8 | + * @param {Array<{ file: string; tmNumber: string; title: string; notBefore: number; notAfter: number; places: Array<{ offset: string; _: string; type: string; }>; status: { _: string; }; type: { _: string; }; objectType: { _: string; }; language: { _: string; }; settlement: { _: string; }; }>} inscriptions - Array of inscription objects |
9 | 9 | */ |
10 | 10 | export async function downloadInscriptionsCSV(summary, inscriptions) { |
11 | | - const headers = |
12 | | - 'ID,Title,"Not before","No earlier than","No later than",Place,Status,Type,Object type,Language,Settlement\n'; |
| 11 | + const headers = [ |
| 12 | + 'ID', |
| 13 | + 'TM Number', |
| 14 | + 'Date notBefore', |
| 15 | + 'Date notAfter', |
| 16 | + 'Origin (ancient)', |
| 17 | + 'Origin (modern)', |
| 18 | + 'Origin latitude', |
| 19 | + 'Origin longitude', |
| 20 | + 'Provenance latitude', |
| 21 | + 'Provenance longitude', |
| 22 | + 'Material', |
| 23 | + 'Object type', |
| 24 | + 'Type', |
| 25 | + 'Execution type 1', |
| 26 | + 'Execution type 2', |
| 27 | + 'Language', |
| 28 | + 'Repository name', |
| 29 | + 'Inventory number' |
| 30 | + ].join(','); |
13 | 31 |
|
14 | 32 | const rows = inscriptions |
15 | 33 | .map((inscription) => { |
16 | 34 | return [ |
17 | 35 | inscription.file, |
18 | | - `"${inscription.title}"`, |
19 | | - inscription.notBefore, |
20 | | - inscription.notAfter, |
21 | | - `"${getInscriptionPlace(inscription)}"`, |
22 | | - inscription.status._, |
23 | | - `"${getInscriptionType(inscription)}"`, |
| 36 | + `"${inscription?.tmNumber || ''}"`, |
| 37 | + inscription?.notBefore || '', |
| 38 | + inscription?.notAfter || '', |
| 39 | + `"${getInscriptionPlace(inscription, 'ancient')}"`, |
| 40 | + `"${getInscriptionPlace(inscription, 'modern')}"`, |
| 41 | + inscription.geo?.[0]?.[0] || '', |
| 42 | + inscription.geo?.[0]?.[1] || '', |
| 43 | + inscription.provenanceGeo?.[0] || '', |
| 44 | + inscription.provenanceGeo?.[1] || '', |
| 45 | + `${inscription.material?.at(-1).replaceAll(':::', '.') || ''}`, |
24 | 46 | `"${getInscriptionObjectType(inscription)}"`, |
| 47 | + `"${getInscriptionType(inscription)}"`, |
| 48 | + `"${inscription.technique?.at(-1)?.replaceAll(':::', '.') || ''}"`, |
| 49 | + `"${inscription.pigment?.at(-1)?.replaceAll(':::', '.') || ''}"`, |
25 | 50 | `"${getInscriptionLanguage(inscription)}"`, |
26 | | - `"${getInscriptionSettlement(inscription)}"` |
| 51 | + `"${inscription.repository.at(-1)?.split(':::').at(-1) || ''}"`, |
| 52 | + `"${inscription.idno?._ || ''}"` |
27 | 53 | ].join(','); |
28 | 54 | }) |
29 | 55 | .join('\n'); |
30 | 56 |
|
31 | 57 | const a = document.createElement('a'); |
32 | | - a.href = `data:text/csv;charset=utf-8,${encodeURIComponent(`"${summary}"\n${headers}${rows}`)}`; |
| 58 | + a.href = `data:text/csv;charset=utf-8,${encodeURIComponent(`"${summary}"\n${headers}\n${rows}`)}`; |
33 | 59 | a.download = 'inscriptions.csv'; |
34 | 60 | a.click(); |
35 | 61 | a.remove(); |
36 | 62 | } |
37 | 63 |
|
38 | 64 | /** |
39 | 65 | * @param {any} inscription |
| 66 | + * @param {string} type |
40 | 67 | */ |
41 | | -function getInscriptionPlace(inscription) { |
42 | | - return inscription.places |
43 | | - .map((/** @type {{ offset: string; _: string; type: string; }} */ place) => { |
44 | | - let placeString = ''; |
45 | | - if (place.offset) { |
46 | | - placeString += `${place.offset} `; |
47 | | - } |
48 | | - placeString += `${place._}`; |
49 | | - placeString += `(${place.type})`; |
50 | | - return placeString; |
51 | | - }) |
52 | | - .join(', '); |
| 68 | +function getInscriptionPlace(inscription, type) { |
| 69 | + return inscription?.places?.find((place) => place.type === type)?._ || ''; |
53 | 70 | } |
54 | 71 |
|
55 | 72 | /** |
56 | 73 | * @param {{ type: { _: string; }; }} inscription |
57 | 74 | */ |
58 | 75 | function getInscriptionType(inscription) { |
59 | | - return inscription.type?._ || 'N/A'; |
| 76 | + return inscription.type?._?.trim() || 'N/A'; |
60 | 77 | } |
61 | 78 |
|
62 | 79 | /** |
|
0 commit comments