Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
202 lines (164 loc) · 5.42 KB
/
Copy pathscript.js
File metadata and controls
202 lines (164 loc) · 5.42 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
function setSessionID(){
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
var id = urlParams.get('sessionid');
if(id != null)
{
return urlParams.get('sessionid');
}
return "";
}
var sessionID = setSessionID();
window.addEventListener('load', function() {
drawLoad();
bubbleLoad();
loadData(sessionID);
});
function sendData(data) {
let xhttp = new XMLHttpRequest();
xhttp.open("POST", "https://FRQ-Coop-Server.spirou7.repl.co/send-data", true);
xhttp.setRequestHeader('Content-type', 'application/json');
// Create the JSON
var jsonData = JSON.stringify(data);
xhttp.send(jsonData);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
try{
var data = JSON.parse(xhttp.responseText);
// Check if the server has an ID for the "needs_id" bubble
if (data.newId != null) {
console.log("WERE UPDATING IT HOLD ON");
document.getElementById("needs_id").id = data.newId;
// Update the array
var bubble = bubbleInfoList["needs_id"];
delete bubbleInfoList['needs_id'];
bubbleInfoList[data.newId] = bubble;
bubble.bubbleID = data.newId;
}
else {
// Check if the server wants client to refresh
updateWithSessionData(data);
}
}
catch{
}
}
}
}
function loadData(sessionID) {
document.getElementById('sessionID').innerHTML = "Session ID: " + sessionID;
let xhttp = new XMLHttpRequest();
xhttp.open("POST", "https://FRQ-Coop-Server.spirou7.repl.co/get-data", true);
xhttp.setRequestHeader('Content-type', 'application/json');
var jsonData = JSON.stringify({
sessionID: sessionID
});
xhttp.send(jsonData);
// When the server responds with data
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Save the data into a variable
try {
var data = JSON.parse(xhttp.responseText);
console.log(Object.keys(bubbleInfoList));
updateWithSessionData(data);
}
catch {
}
}
}
}
function updateWithSessionData(sessionData) {
// Clear the bubbles
var keys = Object.keys(bubbleInfoList);
for (var i = 0; i < keys.length; i++) {
document.getElementById(keys[i]).remove();
}
bubbleInfoList = [];
var panelBubble = null;
// Loop through every bubble and add it to the screen
for (var i = 0; i < sessionData.bubbles.length; i++) {
// If there is a bubble on the current page
if (sessionData.bubbles[i].page == myState.currentPage) {
var xPosition = parseFloat(sessionData.bubbles[i].xPosition);
var yPosition = parseFloat(sessionData.bubbles[i].yPosition);
createBubble(xPosition, yPosition, sessionData.bubbles[i].bubbleID);
if (currentBubbleID != null) {
if (sessionData.bubbles[i].bubbleID == currentBubbleID) {
document.getElementById(currentBubbleID).className = "bubble_selected";
panelBubble = sessionData.bubbles[i];
}
}
}
}
if (panelBubble != null) {
//Set the text area to be the text of the bubble
document.getElementById("textArea").value = panelBubble.text;
var img = new Image;
img.addEventListener("load", function() {
var myCanvas = document.getElementById('myCanvas');
var ctx = myCanvas.getContext('2d');
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.drawImage(img, 0, 0, myCanvas.width, myCanvas.height);
});
img.src = panelBubble.image;
}
}
function updateCurrentBubblePosition() {
var currentBubbleDiv = document.getElementById(currentBubbleID);
var bubbleOffset = getOffset(currentBubbleDiv);
var rendererDiv = document.getElementById('pdf_renderer');
var rendererOffset = getOffset(rendererDiv);
var newX = (bubbleOffset.left * 1.0 - rendererOffset.left) / rendererDiv.offsetWidth;
var newY = (bubbleOffset.top * 1.0 - rendererOffset.top) / rendererDiv.offsetHeight;
var data = {
updatePosition: true,
sessionID: sessionID,
bubbleID: currentBubbleID,
xPosition: newX,
yPosition: newY,
};
sendData(data);
}
function updateCurrentBubble(requestNewId) {
var currentBubbleDiv = document.getElementById(currentBubbleID);
var bubbleOffset = getOffset(currentBubbleDiv);
var text = document.getElementById("textArea").value;
var imageData = document.getElementById("myCanvas").toDataURL();
var rendererDiv = document.getElementById('pdf_renderer');
var rendererOffset = getOffset(rendererDiv);
var newX = (bubbleOffset.left * 1.0 - rendererOffset.left) / rendererDiv.offsetWidth;
var newY = (bubbleOffset.top * 1.0 - rendererOffset.top) / rendererDiv.offsetHeight;
if (requestNewId) {
currentBubbleID = "needs_id";
}
console.log("current Bubble ID: " + currentBubbleID);
sendData({
bubbleID: currentBubbleID,
xPosition: newX,
yPosition: newY,
sessionID: sessionID,
page: myState.currentPage,
text: text,
image: imageData
});
console.log("bubble saved");
}
function deleteCurrentBubble() {
sendData({
sessionID: sessionID,
deleteBubbleID: currentBubbleID,
});
document.getElementById(currentBubbleID).remove();
delete bubbleInfoList[currentBubbleID];
}
function loadEdits() {
loadData(sessionID);
}
function getOffset(el) {
const rect = el.getBoundingClientRect();
return {
left: rect.left + window.scrollX,
top: rect.top + window.scrollY
};
}