-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
244 lines (217 loc) · 6.14 KB
/
script.js
File metadata and controls
244 lines (217 loc) · 6.14 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
var index;
var menuPath = "";
var model;
//var annotationsShown = true; //replaced by aD
//var currentIndex;
var aD = document.getElementById("showAnnotations"); //annotations decider, is checkbox
function loadNewModel(path) {
modelCollection = document.getElementsByTagName("model-viewer");
model = modelCollection[0];
annos = model.getElementsByClassName("hotspot");//Delete previous annotations
for (var i = annos.length-1; i >= 0; i--) {
document.getElementById(annos[i].id).remove();
}
model.setAttribute("src", path);
window.history.pushState("object or string", "3D for ISEM", "/?f=" + path);
loadAnnotations(path);//Add annos for this model
};
function loadAnnotations(path) {
//Get path of annotations.txt of this model
pathTXT = getAnnoPath(path);
//console.log(pathTXT);
try {
fetch(pathTXT)
.then((response)=>{
return response.json();
}).then((annoJson)=>{
//console.log(annoJson);
for (n in annoJson) {
model.innerHTML = model.innerHTML + annoJson[n]
.replace("ä", "ä").replace("ü", "ü").replace("ö", "ö").replace("Ã", "Ä")
.replace("Ã", "Ä").replace("Ã", "Ü").replace("Ã", "ß"); //works so whatever
}
//Check if annos should be displayed or not, similar to annotationsShowHide()
var annos = model.getElementsByClassName("hotspot");
//console.log(annos);
for (var i=0; i<annos.length; i++) {
annos[i].style.visibility = aD.checked ? "visible" : "hidden";
}
return annoJson;
});
} catch (e) {
console.log(e);
}
}
function getAnnoPath(path) {
var pathTXT = "";
var ar = path.split("/");
ar[ar.length-1] = "_" + ar[ar.length-1].substr(0, ar[ar.length-1].length-4);
for (n in ar) {
pathTXT = pathTXT + (ar[n] + "/");
}
pathTXT = pathTXT + "annotations.txt";
return pathTXT;
}
//Show or hide annotations as chosen in options
function annotationsShowHide() {
var annos = model.getElementsByClassName("hotspot");
for (var i=0; i<annos.length; i++) {
annos[i].style.visibility = aD.checked ? "visible" : "hidden";
}
//annotationsShown = !annotationsShown;
}
function loadIndex() {
fetch("./fileindex.txt")
.then((response)=>response.json())
.then((responseJson)=>{
index = responseJson;
//console.log(index);
buildMenu(viewLevel());
});
};
function getCurrentDir() {
// Get the current path based on the shown URL
var query = new URLSearchParams(window.location.search);
var cpathList = query.get("f").split("/");
//console.log(cpathList);
var cpath = "";
for (var i = 0; i<cpathList.length-1; i++) {
cpath = cpath + cpathList[i] + "/";
//console.log("loop ex");
}
return cpath;
};
function addPanel(name, path, thumbnail, ci) {
/* Create a panel from name and Thumbnail */
/* Hyperlink based on name */
var panel = document.createElement("div");
var thumb = document.createElement("img");
var caption = document.createElement("span");
if (ci[name]["folderPath"] != undefined) {
panel.onclick = () => {
buildMenu(ci[name]);
};
} else {
panel.onclick = () => {
loadNewModel(ci[name].cad);
}
}
thumb.setAttribute("src", thumbnail);
caption.innerText = name;
//thumb_contain.appendChild(thumb);
//panel.appendChild(thumb_contain);
panel.appendChild(thumb);
panel.appendChild(caption);
panel.classList.add("clickable");
document.getElementsByTagName("sidebar")[0].appendChild(panel);
}
function viewLevel() {
/* Get all items in this folder */
//console.log(index);
var dir = getCurrentDir();
var currentIndex = index;
//console.log(currentIndex);
dirList = dir.split("/");
for (var i = 2; i<dirList.length; i++) {
if (dirList[i] != "") {
//console.log(dirList[i]);
currentIndex = currentIndex[dirList[i]];
}
}
return currentIndex;
}
function buildMenu(ci) {
//Title:
document.getElementById("path").innerText = "ISEM: " + ci.folderPath;
//clear all menu items from sidebar
sidebar = document.getElementsByTagName("sidebar")[0];
nav = sidebar.getElementsByTagName("nav")[0];
while (sidebar.lastElementChild != nav) {
sidebar.removeChild(sidebar.lastElementChild);
}
//console.log(ci);
for (var el in ci) {
switch (el) {
case "folderPath":
case "isDir":
break;
default:
path = ci[el].cad;
thumbnail = ci[el].thumbnail;
//console.log(ci[el].thumbnail);
if (thumbnail == undefined) {
thumbnail = "./folderIcon.png"
}
addPanel(el, path, thumbnail, ci);
}
}
}
function oneUp() {
var pathL = getCurrentDir().split("/");
var nci = index;
//console.log(nci, pathL);
for (var i = 2; i<pathL.length-2; i++) {
if (pathL[i] == "") {
continue;
}
//console.log(pathL[i]);
nci = nci[pathL[i]];
}
//console.log(nci);
buildMenu(nci);
}
//On load
loadIndex();
loadNewModel((new URLSearchParams(window.location.search)).get("f"));
//Below handels the animation stuff
//On model load -> timing for getting information from the model
//var curDuration = 0;
model.addEventListener("load", addAnimations);
function addAnimations() {
var an = model.availableAnimations;
var pp = document.getElementsByClassName("animationsList")[0];
if (an.length === 0) {
pp.innerHTML = "No animations available for this model";
} else {
pp.innerHTML = "";
for (n in an) {
var b = document.createElement("button");
b.innerText = an[n];
//console.log(an[n]);
b.onclick = function(it) {
//console.log(it);
model.animationName = it.srcElement.innerText;
model.play();
isPaused = false;
document.getElementById("playPauseButton").innerHTML = "⏸";
}
pp.appendChild(b);
}
}
}
//Handle play-pause
var isPaused = true;
function animationPlayPause(i) {//i always is a self reference
if (isPaused) {
//change to playing
model.play();
model.timeScale = document.getElementById("playbackSpeed").value;
i.innerHTML = "▶";//▶
} else {
model.pause()
i.innerHTML = "⏸";//⏸ without the blue
}
isPaused = !isPaused;
}
//Handle the playback range
function jumpAnimation(i) {
model.timeScale = 1;
model.pause();
isPaused = true;
model.currentTime = i.value/100*model.duration;
//console.log(i.value/100*model.duration, model.currentTime, model.duration);
}
//Handle speed
function changeSpeed(i) {
model.timeScale = i.value;
}