-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsla-cite-global.js
145 lines (140 loc) · 4.59 KB
/
tsla-cite-global.js
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
var global = global || window;
pathArray = window.location.pathname.split("/");
collAlias = pathArray[3];
itemID = pathArray[5];
const mappings = {
Title: "itemTitle",
order: 0,
Creator: "itemCreator",
order: 1,
Date: "itemDate",
order: 2,
"Collection Name": "itemCollection",
order: 3,
"Storage Location": "itemStorage",
order: 4,
"ID#": "itemID",
order: 5,
"Owning Institution": "itemOwning",
order: 6,
Repository: "itemRepository",
order: 7,
};
// helper function to determine parent record ID of current item
function getParent(item, collection) {
return (
fetch(
`/digital/bl/dmwebservices/index.php?q=GetParent/${collection}/${item}/json`
)
.then((response) => response.json())
// make GetParent API call and return as JSON
.then((json) => {
let parent = false;
// parse JSON for 'parent' value; -1 indicates parent ID is the same as item ID
if (json.parent === -1) {
parent = item;
} else {
parent = json.parent;
}
return parent;
})
.then((parent) => {
// once parent is known, check if IIIF Pres manifest exists
return fetch(`/iiif/info/${collection}/${parent}/manifest.json`)
.then((response) => {
if (response.status != 200) {
console.log("No IIIF manifest exists for this record.");
parent = false;
// if no manifest exists, return is 'false' so that IIIF button is not inserted
return parent;
} else {
// check if manifest is for single-item PDF
return fetch(
`/digital/api/collections/${collection}/items/${parent}/false`
)
.then((response) => response.json())
.then((json) => {
if (json.filename.split(".").pop() === "pdf") {
// if item format is pdf return is false so that IIIF button is not inserted
console.log("pdf?", json.filename.split(".").pop());
parent = false;
return parent;
} else {
return parent;
}
})
.catch((error) =>
console.log("Item API request failed.", error)
);
}
})
.catch((error) => {
console.log("Manifest request failed.", error);
parent = false;
return parent;
});
})
.catch(function (error) {
console.log("GetParent request failed.", error);
parent = false;
return parent;
})
);
}
//API call
async function buildMetadataObject(collAlias, itemID) {
let response = await fetch(
"https://teva.contentdm.oclc.org/iiif/info/" +
global.collAlias +
"/" +
global.itemID +
"/manifest.json"
);
let data = await response.json();
return data.metadata.reduce(
(acc, { label, value }) => ({ ...acc, [mappings[label]]: value }),
{}
);
}
//create citation, ignoring undefined or null values
function insertCitation(data) {
var tTitle = data.itemTitle;
var url =
"https://teva.contentdm.oclc.org/digital/collection/" +
global.collAlias +
"/id/" +
global.itemID;
var dateToday = new Date().toISOString().slice(0, 10);
const fieldBlackList = ["itemTitle"];
const itemCite = `<hr /><strong>Citation:</strong> \n "${
data.itemTitle
}," ${Object.values(mappings)
.reduce((acc, cur) => {
if (fieldBlackList.includes(cur)) return acc;
const value = data[cur];
return value ? [...acc, value] : acc;
}, [])
.join(
", "
)}, ${url}, accessed ${dateToday}. <br>Note: This citation is auto-generated and may be incomplete. Check your preferred style guide or the <a href="https://teva.contentdm.oclc.org/customizations/global/pages/about.html">About TeVA page</a> for more information.`;
const citationContainer = document.createElement("div");
citationContainer.id = "citation";
citationContainer.innerHTML = itemCite;
if (tTitle) {
document
.querySelector(".ItemView-itemViewContainer")
.appendChild(citationContainer);
}
}
//print citation to page
(async () => {
document.addEventListener("cdm-item-page:ready", async function (e) {
const citationData = await buildMetadataObject();
insertCitation(citationData);
});
document.addEventListener("cdm-item-page:update", async function (e) {
document.getElementById("citation").remove();
const citationData = await buildMetadataObject();
insertCitation(citationData);
});
})();