-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripta.js
More file actions
120 lines (103 loc) · 3.56 KB
/
Copy pathscripta.js
File metadata and controls
120 lines (103 loc) · 3.56 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
// Annotation script
// Just for adding annos, display of safed annos is in script.js
//var annotations = {};
//var annoCounter = 0;//only used for unique ids
var model = document.getElementsByTagName("model-viewer")[0];
//document.body.addEventListener("mouseup", (event) => {document.getElementById("anoContent").focus()});
function getCoord() {
var offsets = model.getBoundingClientRect();
var y = Math.floor(offsets.bottom*0.5+4);
var x = Math.floor(offsets.right*0.5);
return model.positionAndNormalFromPoint(x,y);
}
function postIt(type, content) { //type "thumbnail-img", "annotation-post" or "annotation-img"
var s = window.location.search;
var currentPath = s.substr(3, s.length-6);
//console.log(content);
switch (type) {
case "annotation-post":
annoCol = model.getElementsByClassName("hotspot");
annos = {};
//for (n in annoCol) {
for (var i = 0; i<annoCol.length; i++) {
annos[annoCol[i].id] = annoCol[i].outerHTML;
//console.log(annoCol[i]);
}
//console.log(annos)
fetch(window.location, {
method: "POST",
body: JSON.stringify(annos),
headers: {
"Content-type": "application/json; charset=UTF-8",
"authentication": document.getElementById("postAuth").value
}
}).then((res)=>{
//console.log(res);
document.getElementById("last-status").innerText = (res.status === 204) ? " Saved" : " Not authenticated";
});
}
}
function postAnnotations() {
postIt("annotation-post", "");
}
//Delete, fired when rightclicking on annotation-point
function del() {
this.remove();
}
document.getElementsByTagName("body")[0].addEventListener("keydown", (event) => {
//Create
if (event.code === "Enter") {
//Notify user that there are unsafed changes
document.getElementById("last-status").innerText = " Unsafed changes";
//Get the selected content-type
var contentType = document.querySelector('input[name="content"]:checked').value;
var content = document.getElementById(contentType);
var result;
switch (contentType) {
case "content-text":
result = content.value;
content.value = "";
content.focus();
break;
case "content-link":
var label = document.getElementById("content-link-label");
result = "<a target='_blank' href='" + content.value + "'>" + label.value + "</a>";
label.value = "";
content.value = "";
break;
case "content-img":
//Send uploaded image to the server and store it, then create an <img> tag
console.log("image is not yet implemented");
var imgFile = document.getElementById("content-img").files[0]
postIt("annotation-img", imgFile)
break;
}
var color = document.getElementById("color").value;
var content = document.createElement("div");
content.innerHTML = result;
content.setAttribute("class", "annotation");
content.style.backgroundColor = color;
var coord = getCoord();
var anno = document.createElement("button");
//annoCounter = model.getElementsByClassName("hotspot").length;
//check if id is already occupied
var i = 0;
while (document.getElementById("hotspot-" + i) != null) {
i++;
}
anno.setAttribute("class", "hotspot");
anno.setAttribute("slot", "hotspot-" + i);
anno.setAttribute("data-position", coord.position.toString());
anno.setAttribute("data-normal", coord.normal.toString());
anno.setAttribute("id", "hotspot-" + i);
anno.style.backgroundColor = color;
anno.oncontextmenu = del;
anno.appendChild(content);
model.appendChild(anno);
//annoCounter++;
}
});
//Warning when trying to close with unsaved changes
window.addEventListener('beforeunload', (e)=>{
console.log(e);
});