-
Notifications
You must be signed in to change notification settings - Fork 1
/
svs.js
196 lines (177 loc) · 8.43 KB
/
svs.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
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
svs = {}
svs.saveFile = function (x, fileName) { // x is the content of the file
var bb = new Blob([x]);
var url = URL.createObjectURL(bb);
var a = document.createElement('a');
a.href = url;
if (fileName) {
if (typeof (fileName) == "string") { // otherwise this is just a boolean toggle or something of the sort
a.download = fileName;
}
a.click() // then download it automatically
}
return a
}
svs.readCSV = async (url = 'HALO All Sherlock IDs 07282020_HP0493-001-007.csv') => {
let txt = await (await fetch(url)).text()
return txt
}
svs.readJSON = async (url = 'HALO All Sherlock IDs 07282020_HP0493-001-007.json') => {
xx = await (await fetch(url)).json()
return xx
}
svs.gcsBasePath = "https://storage.googleapis.com/imagebox_test"
// svs.serverBasePath = "https://imageboxv2-oxxe7c4jbq-uc.a.run.app/iiif"
// svs.serverBasePath = "http://localhost:8080/iiif"
svs.loadHashParams = async () => {
hashParams = {}
if (window.location.hash.includes("=")) {
window.location.hash.slice(1).split('&').forEach(param => {
let [key, value] = param.split('=')
value = value.replace(/['"]+/g, "") // for when the hash parameter contains quotes.
value = decodeURIComponent(value)
hashParams[key] = value
})
}
if (hashParams.imageTag && svs.validImageNames.includes(hashParams.imageTag)) {
const correspondingObject = svs.imageNameToIdMapping.find(x => x.ImageTag === hashParams.imageTag)
if (!hashParams.imageNslcId) {
window.location.hash += `&imageNslcId=${correspondingObject["NSLC ID"]}`
return
} else if (hashParams.imageNslcId && hashParams.imageNslcId !== correspondingObject["NSLC ID"]) {
window.location.hash = window.location.hash.replace(`imageNslcId=${hashParams.imageNslcId}`, `imageNslcId=${correspondingObject["NSLC ID"]}`)
return
} else {
svs.loadImage(`${svs.gcsBasePath}/${hashParams.imageTag}`)
document.getElementById("imageId").innerText = correspondingObject["NSLC ID"]
}
} else if (hashParams.imageNslcId && svs.validImageIDs.includes(hashParams.imageNslcId)) {
const correspondingObject = svs.imageNameToIdMapping.find(x => x["NSLC ID"] === hashParams.imageNslcId)
if (!hashParams.imageTag) {
window.location.hash += `&imageTag=${correspondingObject.ImageTag}`
return
} else if (hashParams.imageTag && hashParams.imageTag !== correspondingObject.ImageTag) {
window.location.hash = window.location.hash.replace(`imageTag=${hashParams.imageTag}`, `imageTag=${correspondingObject.ImageTag}`)
return
} else {
svs.loadImage(`${svs.gcsBasePath}/${correspondingObject.imageTag}`)
document.getElementById("imageId").innerText = correspondingObject["NSLC ID"]
}
}
}
svs.populateSelects = () => {
const imageSelectNameElement = document.getElementById("imageSelectName")
const imageSelectIdElement = document.getElementById("imageSelectId")
imageSelectNameElement.innerHTML = ""
imageSelectIdElement.innerHTML = ""
svs.validImageNames.forEach((image, idx) => {
const correspondingObject = svs.imageNameToIdMapping.find(x => x.ImageTag === image)
const nameOptionElement = document.createElement("option")
const idOptionElement = document.createElement("option")
nameOptionElement.value = encodeURIComponent(image)
nameOptionElement.innerText = image
idOptionElement.value = correspondingObject["NSLC ID"]
idOptionElement.innerText = correspondingObject["NSLC ID"]
nameOptionElement.setAttribute("nslcId", correspondingObject["NSLC ID"])
nameOptionElement.setAttribute("imageTag", image)
idOptionElement.setAttribute("nslcId", correspondingObject["NSLC ID"])
idOptionElement.setAttribute("imageTag", image)
imageSelectNameElement.appendChild(nameOptionElement)
imageSelectIdElement.appendChild(idOptionElement)
if (idx === 0 && !hashParams.imageTag) {
nameOptionElement.setAttribute("selected", "selected")
idOptionElement.setAttribute("selected", "selected")
svs.selectImageByName(image)
} else if (hashParams.imageTag && hashParams.imageTag === image) {
nameOptionElement.setAttribute("selected", "selected")
idOptionElement.setAttribute("selected", "selected")
}
// if (image.endsWith(".ndpi")) {
// nameOptionElement.setAttribute("disabled", "true")
// idOptionElement.setAttribute("disabled", "true")
// }
})
}
svs.selectImageByName = () => {
document.getElementById("loadingText").style.display = "block"
const image = document.getElementById("imageSelectName").value
const correspondingObject = svs.imageNameToIdMapping.find(x => x.ImageTag === decodeURIComponent(image))
document.getElementById("imageSelectId").value = correspondingObject["NSLC ID"]
window.location.hash = `imageTag=${image}&imageNslcId=${correspondingObject["NSLC ID"]}`
document.getElementById("openseadragon1").setAttribute("imageTag", correspondingObject.ImageTag)
document.getElementById("openseadragon1").setAttribute("imageNslcId", correspondingObject["NSLC ID"])
}
svs.selectImageById = () => {
document.getElementById("loadingText").style.display = "block"
const image = document.getElementById("imageSelectId").value
const correspondingObject = svs.imageNameToIdMapping.find(x => x["NSLC ID"] === image)
document.getElementById("imageSelectName").value = encodeURIComponent(correspondingObject.ImageTag)
window.location.hash = `imageTag=${correspondingObject.ImageTag}&imageNslcId=${image}`
document.getElementById("openseadragon1").setAttribute("imageTag", correspondingObject.ImageTag)
document.getElementById("openseadragon1").setAttribute("imageNslcId", correspondingObject["NSLC ID"])
}
svs.loadImage = async (urlInGCP) => {
// if (urlInGCP.substr(urlInGCP.length - 4, 4) === "ndpi") {
// alert("NDPI Images not yet supported!")
// return
// }
urlInGCP = urlInGCP.replace(/\s/g, "_")
// const format = urlInGCP.endsWith(".ndpi") ? "ndpi" : "svs"
// const p = `${svs.serverBasePath}/?iiif=${urlInGCP}`;
// const infoURL = `${p}/info.json`
// let imageInfo
// try {
// imageInfo = await (await fetch(infoURL)).json()
// } catch (e) {
// alert("An error occurred retrieving the image information. Please try again later.")
// // window.history.back()
// // setTimeout(() => {
// // document.getElementById("imageSelectName").value = hashParams.imageTag
// // document.getElementById("imageSelectId").value = hashParams.imageNslcId
// // }, 1000)
// document.getElementById("loadingText").style.display = "none"
// return
// }
// console.log("image Info : ", imageInfo)
// const infoTable = document.getElementById("infoTable")
// infoTable.innerHTML = ""
// infoTable.style.width = '20%'
// infoTable.style.border = "1px solid black"
// infoTable.style.textAlign = "center"
// document.getElementById("imageInfo").appendChild(infoTable)
// Object.entries(imageInfo).forEach(([key, val]) => {
// if (!key.trim().startsWith("@")) {
// key = key.slice(0, 1).toUpperCase() + key.slice(1)
// infoTable.innerHTML += `<tr><td>\n${key}</td><td>${val}</td></tr>`
// }
// })
// infoTable.querySelectorAll("tr").forEach(el => {
// el.style.border = "1px solid black"
// el.querySelectorAll("td").forEach(el2 => el2.style.border = "1px solid black")
// })
document.getElementById("openseadragon1").innerHTML = ""
const tileSources = await OpenSeadragon.GeoTIFFTileSource.getAllTileSources(urlInGCP, { logLatency: false, cache: true, slideOnly: true })
console.log(tileSources)
const viewer1 = OpenSeadragon({
id: "openseadragon1",
visibilityRatio: 1,
minZoomImageRatio: 1,
prefixUrl: "https://episphere.github.io/svs/openseadragon/images/",
imageLoaderLimit: 5,
timeout: 1000 * 1000,
crossOriginPolicy: "Anonymous",
tileSources: await OpenSeadragon.GeoTIFFTileSource.getAllTileSources(urlInGCP, { logLatency: false, cache: true, slideOnly: true })
});
setTimeout(() => document.getElementById("loadingText").style.display = "none", 5000)
}
if (typeof (define) != 'undefined') {
define(svs)
}
window.onload = () => fetch(`${window.location.origin}${window.location.pathname}mapping.json`).then(async resp => {
svs.imageNameToIdMapping = await resp.json()
svs.validImageNames = svs.imageNameToIdMapping.map(x => x.ImageTag).sort((a, b) => b.endsWith(".svs") ? 1 : -1)
svs.validImageIDs = svs.imageNameToIdMapping.map(x => x["NSLC ID"])
svs.loadHashParams()
svs.populateSelects()
})
window.onhashchange = svs.loadHashParams