-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_table.js
More file actions
469 lines (391 loc) · 17.5 KB
/
create_table.js
File metadata and controls
469 lines (391 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"use strict";
function buildHierarchy() {
// get and manipulate values from form inputs
const bibIdInput = document.getElementById("bibid");
let bibIdInputValue = bibIdInput.value;
// we allow inputs without the prefix and with whitespaces
// the following does some cleanup and prefixing for that
const noPrefixRegex = new RegExp('^(?!' + bibIdPrefix + ')(\d+)')
const bibId = bibIdInputValue.replace(/\s/g, '').replace(noPrefixRegex, bibIdPrefix + '$2').toUpperCase();
bibIdInput.value = bibId;
const instId = document.getElementById("alma-inst-id").value;
// make vars global where necessary
let headingForTable;
let numberOfRecords;
let records;
let titleForHeadBibId;
const params = new URLSearchParams({
"bib_id": bibId
});
const requestUrl = "./fetchsru.php?" + params;
const namespace = "http://www.loc.gov/MARC21/slim";
const tableId = "bib-hierarchy-"+bibId;
const csvLinkId = "download-csv";
const main = document.getElementById("main");
// change the current location in browser's location bar
const bibIdUri = encodeURI(bibId);
const instIdUri = encodeURI(instId);
history.replaceState(null, null, "?bibid="+bibIdUri+"&alma_inst_id="+instIdUri);
// disable input after submit
const inputForm = document.getElementById("input-form");
inputForm.setAttribute("disabled", "disabled");
// remove data from previous function call
const existingSection = document.getElementsByTagName("section");
try {
existingSection[0].remove();
} catch {}
// add loader icon and text
const loaderString = "Bitte warten. Ladezeit kann Sekunden bis Minuten betragen.";
const loaderText = createElementByTagAndText("p", loaderString);
loaderText.setAttribute("id", "loader-text");
const loaderIcon = document.createElement("div");
loaderIcon.setAttribute("id", "loader");
const loaderImage = document.createElement("img");
loaderImage.setAttribute("src", "spinner.svg");
loaderImage.setAttribute("alt", "Ladeanimation");
loaderIcon.appendChild(loaderImage);
main.appendChild(loaderText);
main.appendChild(loaderIcon);
const xlsLinkId = "download-xls";
// call fetchsru.php and build table
const xhr = new XMLHttpRequest;
xhr.open('GET', requestUrl);
xhr.responseType = 'document';
xhr.overrideMimeType('text/xml');
xhr.onload = function() {
let modifiedSectionForCurrentBibId;
const sectionForCurrentBibId = document.createElement("section");
sectionForCurrentBibId.setAttribute("id", bibId);
const headingForTableText = "Ausgehend von " + bibId;
headingForTable = createElementByTagAndText("h2", headingForTableText);
headingForTable.setAttribute("id", "bib-hierarchy-table-heading");
sectionForCurrentBibId.append(headingForTable);
if (xhr.readyState === xhr.DONE && xhr.status === 200) {
const xmlObject = xhr.responseXML;
numberOfRecords = checkNumberOfRecords(xmlObject);
if ( numberOfRecords && numberOfRecords > 0 ) {
modifiedSectionForCurrentBibId = createTable(xmlObject, sectionForCurrentBibId);
} else {
const errorP = createElementByTagAndText("p", "Für " + bibId + " wurden keine Datensätze gefunden.");
sectionForCurrentBibId.appendChild(errorP);
loaderIcon.remove();
loaderText.remove();
inputForm.removeAttribute("disabled");
}
} else {
console.log("Error encountered on call of fetchsru.php");
const errorP = createElementByTagAndText("p", "SRU lieferte Fehler für " + bibId);
loaderIcon.remove();
loaderText.remove();
sectionForCurrentBibId.appendChild(errorP);
}
try {
main.appendChild(modifiedSectionForCurrentBibId);
} catch (error) {
//console.log(error);
console.log("Could not create table.");
} finally {
main.appendChild(sectionForCurrentBibId);
}
if (document.querySelector("tbody")) {
makeTableSortable();
}
try {
document.querySelector("thead").querySelector("th").click();
} catch (error) {
//console.log(error);
}
try {
downloadTableAsCsv(tableId, csvLinkId);
} catch(error) {
console.log(error);
}
};
xhr.send();
function checkNumberOfRecords(recordsXml) {
let numRecords;
try {
numRecords = recordsXml.childNodes[0].childNodes.length;
} catch {
numRecords = undefined;
} finally {
return numRecords;
}
}
function createElementByTagAndText(elementTag, elementText) {
const element = document.createElement(elementTag);
const textElement = document.createTextNode(elementText);
element.appendChild(textElement);
return element;
}
function createTableHeading() {
const table = document.createElement("table");
table.setAttribute("id", tableId);
const tableHead = document.createElement("thead");
const tableHeadRow = document.createElement("tr");
const tableHeadings = [
"773$$q bzw. 830$$v",
"Linktyp",
"Titel des Teils",
"Erscheinungsjahr",
"Ausgabebezeichnung",
networkIdName,
"Lokalbestand " + instId
]
tableHeadings.forEach(item => {
const thElement = createElementByTagAndText("th", item);
tableHeadRow.appendChild(thElement);
});
tableHead.appendChild(tableHeadRow);
table.appendChild(tableHead);
return table;
}
function createTableContents(table, recordsXml) {
const numDependentRecords = numberOfRecords-1;
if (numDependentRecords > 0) {
const sectionAddition = " mit " + numDependentRecords + " abhängigen Datensätzen";
headingForTable.textContent += sectionAddition;
} else {
headingForTable.textContent = "Keine abhängigen Datensätze für " + bibId;
}
const tableBody = document.createElement("tbody");
for (let i = 0; i < numberOfRecords; i ++) {
//console.log(i);
const currentRecord = recordsXml.children[0].children[i];
const currentTr = document.createElement('tr');
function nsResolver(prefix) {
return namespace;
}
const partOrder = extractPartOrder(recordsXml, currentRecord, nsResolver);
const linkType = extractLinkType(recordsXml, currentRecord, nsResolver);
const partTitle = extractPartTitle(recordsXml, currentRecord, nsResolver);
const partYear = extractPartYear(recordsXml, currentRecord, nsResolver);
const partEdition = extractPartEdition(recordsXml, currentRecord, nsResolver);
const partId = extractPartId(recordsXml, currentRecord, nsResolver);
const partHoldings = extractPartHoldings(recordsXml, currentRecord, nsResolver);
const isDublette = extractDublette(recordsXml, currentRecord, nsResolver);
const isbdTitle = buildTitleFromSubfields(partTitle, linkType, isDublette);
if (partId.substring(2) == bibId.substring(2)) {
titleForHeadBibId = createElementByTagAndText("p", '"' + isbdTitle + '"');
titleForHeadBibId.setAttribute("id", "title-" + bibId);
titleForHeadBibId.setAttribute("class", "title");
continue;
}
if (typeof partOrder === "undefined") {
console.log("Skipping " + partId + ", because there is no valid $$w in either 773 or 830.");
continue;
}
let hasInstHoldings;
if (partHoldings) {
hasInstHoldings = "Ja";
currentTr.setAttribute("class", "has-holdings");
} else if ( linkType == "MTM" ) {
hasInstHoldings = "";
} else {
hasInstHoldings = "Nein";
currentTr.setAttribute("class", "has-no-holdings");
}
appendTableDataToRow(partOrder, currentTr, false);
appendTableDataToRow(linkType, currentTr, false);
appendTableDataToRow(isbdTitle, currentTr, isDublette);
appendTableDataToRow(partYear, currentTr, false);
appendTableDataToRow(partEdition, currentTr, false);
appendTableDataToRow(partId, currentTr, false);
appendTableDataToRow(hasInstHoldings, currentTr, false);
tableBody.appendChild(currentTr);
table.appendChild(tableBody);
}
function appendTableDataToRow(tdText, tableRow, cssClass) {
const td = createElementByTagAndText('td', tdText);
if (cssClass) {
td.setAttribute("class", cssClass);
}
tableRow.appendChild(td);
}
function extractPartOrder(recordsXml, currentRecord, nsResolver) {
// extract order of parts from 773 q or 830 v
let partOrder;
let currentCategory;
const xpath773Or830 = 'default:datafield[@tag="773" or @tag="830"]';
const xpath773Or830Result = recordsXml.evaluate(xpath773Or830, currentRecord, nsResolver);
if (xpath773Or830Result.resultType !== 4) {
console.log("No 773 or 830 found.");
return false;
}
while (currentCategory = xpath773Or830Result.iterateNext()) {
let xpathSfWResult;
let sfWText;
const currentCategoryNumber = currentCategory.getAttribute("tag");
const xpathSfW = 'default:subfield[@code="w"]/text()';
try {
xpathSfWResult = recordsXml.evaluate(xpathSfW, currentCategory, nsResolver);
sfWText = xpathSfWResult.iterateNext().wholeText;
} catch (error) {
continue;
}
if (typeof sfWText !== "undefined" && sfWText.indexOf(bibId) !== -1) {
let xpathSfOrder;
if (currentCategoryNumber == 773) {
xpathSfOrder = 'default:subfield[@code="q"]/text()';
} else if (currentCategoryNumber == 830) {
xpathSfOrder = 'default:subfield[@code="v"]/text()';
}
const partOrderResult = recordsXml.evaluate(xpathSfOrder, currentCategory, nsResolver);
try {
partOrder = partOrderResult.iterateNext().wholeText;
} catch (error) {
return "";
}
} else {
continue;
}
}
return partOrder;
}
function extractLinkType(recordsXml, currentRecord, nsResolver) {
// extract link type from leader position 19
const xpath = "default:leader/text()";
const xpathResult = recordsXml.evaluate(xpath, currentRecord, nsResolver);
const leader = xpathResult.iterateNext().wholeText;
const linkType = leader.substring(19, 20);
if (linkType == "c") {
return "TAT";
} else if (linkType == "b") {
return "TUT";
} else if (linkType == "a") {
return "MTM";
} else {
return "???";
}
}
function extractPartTitle(recordsXml, currentRecord, nsResolver) {
// extract the title of the current record
const xpathText = 'default:datafield[@tag="245"]/default:subfield/text()';
const xpathResultText = recordsXml.evaluate(xpathText, currentRecord, nsResolver);
const xpathSf = 'default:datafield[@tag="245"]/default:subfield';
const xpathResultSf = recordsXml.evaluate(xpathSf, currentRecord, nsResolver);
const subfields = [];
let subfield, text;
try {
while (subfield = xpathResultSf.iterateNext().getAttribute('code'), text = xpathResultText.iterateNext().wholeText) {
const sfObject = {[subfield]: text};
subfields.push(sfObject);
}
} catch (error) {
//console.log(error);
}
return subfields;
}
function buildTitleFromSubfields(subfields, linkType, isDublette) {
let title = "";
for (let i = 0; i < subfields.length; i ++) {
const subfield = subfields[i];
const subfieldKey = Object.keys(subfield)[0];
const subfieldValue = Object.values(subfield)[0];
if (subfieldKey === "6") continue;
if ( linkType === "TAT" && ["a", "b", "c"].indexOf(subfieldKey) >= 0 ) {
continue;
} else {
if (title !== "") {
if (subfieldKey === "b") {
title += ' : ';
} else if (subfieldKey === "c") {
title += ' / ';
} else if (subfieldKey === "n") {
title += '. ';
} else if (subfieldKey === "p") {
title += ', ';
}
}
title += subfieldValue;
}
}
if (isDublette) {
title = "[DUBLETTE] " + title;
}
return title;
}
function extractPartYear(recordsXml, currentRecord, nsResolver) {
// extract the publication year of the current record
const xpath = 'default:controlfield[@tag="008"]/text()';
const xpathResult = recordsXml.evaluate(xpath, currentRecord, nsResolver);
const marc008 = xpathResult.iterateNext().wholeText;
const year = marc008.substring(7,11);
return year;
}
function extractPartEdition(recordsXml, currentRecord, nsResolver) {
// extract edition from MARC 250
const xpath = 'default:datafield[@tag="250"]/default:subfield/text()';
const xpathResult = recordsXml.evaluate(xpath, currentRecord, nsResolver);
let marc250 = "";
try {
marc250 = xpathResult.iterateNext().wholeText;
} catch {
} finally {
return marc250;
}
}
function extractPartId(recordsXml, currentRecord, nsResolver) {
// extract AC-Number of the current record
const xpath = 'default:controlfield[@tag="009"]/text()';
const xpathResult = recordsXml.evaluate(xpath, currentRecord, nsResolver);
const marc009 = xpathResult.iterateNext().wholeText;
return marc009;
}
function extractPartHoldings(recordsXml, currentRecord, nsResolver) {
// extract holdings info from marc datafield
let marcPartInstId;
const xpath = 'default:datafield[@tag="' + marcPartTag + '"]/default:subfield[@code="' + marcPartSf + '"]/text()';
const xpathResult = recordsXml.evaluate(xpath, currentRecord, nsResolver);
let hasInstHoldings = false;
try {
while ( marcPartInstId = xpathResult.iterateNext().wholeText ) {
if (marcPartInstId == instId) {
hasInstHoldings = true;
}
}
} catch {
}
return hasInstHoldings;
}
function extractDublette(recordsXml, currentRecord, nsResolver) {
// extract info on whether record is a "Dublette"
let datafield970;
let dublettenCheck = new RegExp('.*dublette.*', 'i');
const xpath = 'default:datafield[@tag="970"][@ind1="0"][@ind2=" "]/default:subfield/text()';
const xpathResult = recordsXml.evaluate(xpath, currentRecord, nsResolver);
try {
while ( datafield970 = xpathResult.iterateNext().wholeText ) {
if ( dublettenCheck.test(datafield970) == true ) {
console.log("Dublette found.");
return "dublette";
}
}
} catch {
}
return false;
}
}
function createTable(recordsXml, sectionElement) {
console.log("Creating table…");
if (recordsXml) {
const table = createTableHeading();
createTableContents(table, recordsXml);
if (numberOfRecords > 1) {
const csvDownloadLink = createElementByTagAndText("a", "CSV Download");
csvDownloadLink.setAttribute("id", csvLinkId);
sectionElement.append(csvDownloadLink);
}
sectionElement.append(titleForHeadBibId);
if (numberOfRecords > 1) {
sectionElement.append(table);
}
loaderIcon.remove();
loaderText.remove();
inputForm.removeAttribute("disabled");
return sectionElement;
} else {
console.log("Input for fetchsru.php did not lead to XML output.");
}
}
}