-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.js
173 lines (147 loc) · 4.85 KB
/
image.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
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
"use strict";
const cmInteractAreaTemplate = document.querySelector(
".cm-interact-area-template"
);
const cmInteractAreas = [];
// const cmContainer = document.getElementsByClassName("cm-container");
const cmContainer = document.querySelector(".cm-container");
cmContainer.addEventListener(
"click",
function (e) {
console.log("cmContainer click", e);
createBtnObject(e);
}
// { once: true }
);
cmContainer.addEventListener("dragover", function (e) {
e.preventDefault();
});
function createBtnObject(e) {
console.log("createBtnObject");
// close all info boxes
hideInfoBoxes();
// create a copy of the button template
let newAreaNode = cmInteractAreaTemplate.cloneNode(true);
newAreaNode.classList.remove("cm-interact-area-template");
newAreaNode.classList.add("cm-interact-area");
//// constructor interaction areas
function NewArea(node, e, parElem) {
this.node = node;
// create a headline
this.setHeadline = function (headline) {
this.headline = headline;
this.node.querySelector(".cm-infobox-headline").innerHTML = this.headline;
};
this.setHeadline("Headline");
// create a text
this.setText = function (text) {
this.text = text;
this.node.querySelector(".cm-infobox-text").innerHTML = this.text;
};
this.setText(
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor..."
);
// set Perc Pos
this.setPercPos = function (e, parElem) {
//calc rel position
const bounds = parElem.getBoundingClientRect();
const offsetX = bounds.left;
const offsetY = bounds.top;
const x = e.clientX - offsetX;
const y = e.clientY - offsetY;
console.log(`
e.clientX ${e.clientX}
e.clientY ${e.clientY}`);
// map rel pos to % and set
this.x = 100 / (parElem.clientWidth / x);
this.y = 100 / (parElem.clientHeight / y);
this.node.style.left = this.x + "%";
this.node.style.top = this.y + "%";
return { x: this.x, y: this.y };
};
this.setPercPos(e, parElem);
//listener to open infobox
const cmCircleIcon = this.node.querySelector(".cm-circle-icon");
cmCircleIcon.addEventListener(
"click",
function (e) {
e.stopPropagation(); // prevent to bubble to the container
// only show this infobox
hideInfoBoxes();
newArea.node.querySelector(".cm-info-box").classList.remove("hidden");
},
true // useCapture to fire this one before being dispatched to any EventTarget beneath it
);
let xTmp;
//listener to drag
cmCircleIcon.addEventListener("drag", function (ev) {
xTmp = newArea.setPercPos(ev, parElem).x;
console.log(`xTmp ${xTmp}`);
});
// cmCircleIcon.addEventListener("dragstart", function (ev) {
// xTmp = newArea.setPercPos(ev, parElem).x;
// console.log(`UP xTmp ${xTmp}`);
// });
// //listener to dragend
// cmCircleIcon.addEventListener("dragend", function (ev) {
// newArea.setPercPos(ev, parElem);
// });
}
// create a new interaction area
let newArea = new NewArea(newAreaNode, e, cmContainer);
// add to the list of areas and into the DOM
cmInteractAreas.push(newArea);
cmContainer.appendChild(newArea.node);
// add event listener to drag
let deltaX;
let startX;
// cmCircleIcon.addEventListener(
// "dragstart",
// function (e) {
// e.preventDefault();
// startX = e.clientX;
// console.log("dragstart X: ", startX);
// },
// true
// );
// cmCircleIcon.addEventListener(
// "drag",
// function (e) {
// // e.stopPropagation();
// e.preventDefault();
// console.log(`xPercPos ${getPercPos(e, cmContainer)["xPerc"]}`);
// //x = newArea.node.style.left = getPercPos(e, cmContainer)["xPerc"] + "%";
// //newArea.node.style.top = getPercPos(e, cmContainer)["yPerc"] + "%";
// percPos = getPercPos(e, cmContainer);
// x = percPos.xPerc;
// y = percPos.yPerc;
// },
// true
// );
// cmCircleIcon.addEventListener(
// "dragend",
// function (e) {
// e.preventDefault();
// console.log(`xPercPos dragend ${getPercPos(e, cmContainer)["xPerc"]}`);
// newArea.node.style.left = getPercPos(e, cmContainer)["xPerc"] + "%";
// },
// true
// );
}
function hideInfoBoxes() {
for (const area of cmInteractAreas) {
area.node.querySelector(".cm-info-box").classList.add("hidden");
}
}
// get Click position relativ to parent div
// function getPercPos(e, elem) {
// let bounds = elem.getBoundingClientRect();
// let offsetX = bounds.left;
// let offsetY = bounds.top;
// let x = e.clientX - offsetX;
// const y = e.clientY - offsetY;
// // map to % position
// let xPerc = 100 / (cmContainer.clientWidth / x);
// let yPerc = 100 / (cmContainer.clientHeight / y);
// return { xPerc, yPerc };
// }