-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.js
More file actions
219 lines (171 loc) · 5.25 KB
/
Copy pathbubble.js
File metadata and controls
219 lines (171 loc) · 5.25 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
/*
List of all bubbles for the page
Each bubble has the following structure:
Bubble:
- bubbleID
- page
- xPercent
- yPercent
*/
var bubbleInfoList = [];
/*
Indicates which bubble the user is currently selected.
Should adjust the side panel to match the information associated with the bubble.
*/
var currentBubbleID = null;
function bubbleLoad() {
}
/*
The state of the pdf renderer.
*/
var myState = {
pdf: null,
currentPage: 1,
zoom: 1
}
pdfjsLib.getDocument('ap21-frq-calculus-bc.pdf').then((pdf) => {
myState.pdf = pdf;
render();
});
function render() {
myState.pdf.getPage(myState.currentPage).then((page) => {
var canvas = document.getElementById("pdf_renderer");
var ctx = canvas.getContext('2d');
var viewport = page.getViewport(myState.zoom);
canvas.width = viewport.width;
canvas.height = viewport.height;
page.render({
canvasContext: ctx,
viewport: viewport
});
});
// Only show the bubbles of the current page
loadData(sessionID);
}
function nextPage(){
myState.currentPage++;
if(myState.currentPage > myState.pdf.numPages){
myState.currentPage = myState.pdf.numPages;
}
render();
}
function previousPage(){
myState.currentPage--;
if(myState.currentPage < 1){
myState.currentPage = 1;
}
render();
}
function createBubbleClick(e) {
createBubble(e.pageX, e.pageY);
}
function newBubble(x, y){
// First clear the side panel
clearSidePanel();
// Create a new bubble
var id = createBubble(x, y, "needs_id");
// Assign this bubble as the "current" bubble
currentBubbleID = "needs_id";
// Send this new bubble to the server
updateCurrentBubble(true);
}
function clearSidePanel(){
// Clear the text
document.getElementById('textArea').value = "";
// Clear the canvas
const context = document.getElementById('myCanvas').getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
}
function createBubble(xPercent, yPercent, id) {
// Create a new id for the bubble
// Make a bubble object which will be stored by the list
var newBubble = {
bubbleID: id,
page: myState.currentPage,
xPercent: xPercent,
yPercent: yPercent
};
// Save the bubble into the dictionary
// The key is it's ID
bubbleInfoList[id] = newBubble;
var rendererDiv = document.getElementById('pdf_renderer');
var rendererOffset = getOffset(rendererDiv);
var newX = (xPercent * rendererDiv.offsetWidth) + rendererOffset.left;
var newY = (yPercent * rendererDiv.offsetHeight) + rendererOffset.top;
var coordinates = toPageCoordinates(xPercent, yPercent);
// Make the div for the bubble
createBubbleDiv(coordinates.newX, coordinates.newY, id);
return newBubble.bubbleID;
}
function toPageCoordinates(xPercent, yPercent){
var rendererDiv = document.getElementById('pdf_renderer');
var rendererOffset = getOffset(rendererDiv);
var newX = (xPercent * rendererDiv.offsetWidth) + rendererOffset.left;
var newY = (yPercent * rendererDiv.offsetHeight) + rendererOffset.top;
return {
newX: newX,
newY: newY
};
}
function createBubbleDiv(x, y, id){
// Create a new div with class name "bubble"
var bubble = document.createElement('div');
bubble.className = 'bubble';
document.body.appendChild(bubble);
// Assign the position of the div
bubble.style.left = x + "px";
bubble.style.top = y + "px";
// Assign the appropriate event listeners to the div
bubble.addEventListener('mousedown', startDragging);
window.addEventListener('mouseup', stopDragging);
window.addEventListener('mousemove', dragBubble);
// set the id of the div
bubble.id = id;
return bubble;
}
var draggingBubbleId = null;
var selectedBubbleId = null;
function startDragging(e){
// Update the visuals of the bubble
if(selectedBubbleId != null){
var element = document.getElementById(selectedBubbleId);
if(element != null){
element.className = "bubble";
}
}
// Set the dragging and selected bubbles
draggingBubbleId = e.target.id;
selectedBubbleId = e.target.id;
// Set the current bubble to the div that was just clicked
currentBubbleID = e.target.id;
document.getElementById(draggingBubbleId).className = "bubble_dragged";
}
function stopDragging(e){
// If a bubble was actually being dragged
if(draggingBubbleId != null){
var element = document.getElementById(draggingBubbleId);
// Set the class name so it's only "selected," not "dragged"
element.className = "bubble_selected";
// Send current position to server
updateCurrentBubblePosition();
draggingBubbleId = null;
console.log("bubble moved");
}
}
function dragBubble(e) {
if(draggingBubbleId == null){
return;
}
var element = document.getElementById(draggingBubbleId);
element.style.left = (e.pageX - element.offsetWidth / 2) + "px";
element.style.top = (e.pageY - element.offsetHeight / 2) + "px";
}
function repositionBubbleList(){
for(var i = 0; i < bubbleInfoList.length; i++){
var bubbleInfo = bubbleInfoList[i];
var coordinates = toPageCoordinates(bubbleInfo.xPercent, bubbleInfo.yPercent);
var element = document.getElementById(bubbleInfo.bubbleID);
element.style.top = coordinates.newY + "px";
element.style.left = coordinates.newX + "px";
}
}