-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_blocks.js
58 lines (50 loc) · 1.82 KB
/
process_blocks.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
function process_blocks(blocksJSON) {
function setElementPosition(elementId, x, y) {
var elmnt = document.getElementById(elementId);
elmnt.style.left = x + 'px';
elmnt.style.top = y + 'px';
}
function makeElementDraggable(elementId) {
var elmnt = document.getElementById(elementId);
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
var minx = elmnt.parentNode.style.left;
var miny = elmnt.parentNode.style.top;
if (document.getElementById(elementId + 'header')) {
// if present, the header is where you move the DIV from:
document.getElementById(elementId + 'header').onmousedown = dragMouseDown;
} else {
// otherwise, move the DIV from anywhere inside the DIV:
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = Math.max(elmnt.offsetTop - pos2, miny) + 'px';
elmnt.style.left = Math.max(elmnt.offsetLeft - pos1, minx) + 'px';
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
const blocks_dict = JSON.parse(blocksJSON);
for (let block_id in blocks_dict) {
setElementPosition(block_id, blocks_dict[block_id]['x'], blocks_dict[block_id]['y']);
makeElementDraggable(block_id);
}
}