-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
138 lines (118 loc) · 3.76 KB
/
main.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
class Slot {
constructor(element) {
this.element = element;
this.value = element.dataset.value;
}
fill(choice) {
this.element.textContent = '';
this.choice = choice;
this.choice.choose()
}
isOverlapping(choice) {
const choiceBB = choice.boundingBox;
const slotBB = this.boundingBox;
const [left, right] = choiceBB.left < slotBB.left
? [choiceBB, slotBB]
: [slotBB, choiceBB];
const [top, bottom] = choiceBB.top < slotBB.top
? [choiceBB, slotBB]
: [slotBB, choiceBB];
return left.right >= right.left && top.bottom >= bottom.top;
}
hasChoice() {
return !!this.choice;
}
hasCorrectChoice() {
return this.choice.value == this.value;
}
get boundingBox() {
return this.element.getBoundingClientRect();
}
}
class Choice {
constructor(svg, value, x, y, onDragStart, onDragEnd) {
this.svg = svg;
this.value = value;
this.x = x;
this.y = y;
this.color = 'black';
this.isRendered = false;
this.isChosen = false;
this.element = document.createElementNS('http://www.w3.org/2000/svg','text');
this.element.addEventListener('mousedown', downEvent => {
if (this.isChosen) {
return;
}
if (onDragStart) {
onDragStart(downEvent, this);
}
if (downEvent.defaultPrevented) {
return;
}
const onMouseMove = moveEvent => {
this.x = moveEvent.clientX - 30;
this.y = moveEvent.clientY;
this.render();
};
this.svg.addEventListener('mousemove', onMouseMove);
const onMouseUp = upEvent => {
if (onDragEnd) {
onDragEnd(upEvent, this);
}
this.svg.removeEventListener('mousemove', onMouseMove);
this.svg.removeEventListener('mouseup', onMouseUp);
};
this.svg.addEventListener('mouseup', onMouseUp);
});
}
render() {
this.element.setAttribute('x', this.x);
this.element.setAttribute('y', this.y);
this.element.style.fill = this.color;
this.element.textContent = this.value;
if (!this.isRendered) {
this.svg.appendChild(this.element);
this.isRendered = true;
}
}
choose() {
this.isChosen = true;
this.color = 'blue';
this.render();
}
get boundingBox() {
return this.element.getBoundingClientRect();
}
}
function start(svg) {
let filledSlotCount = 0;
const slots = Array.from(svg.querySelectorAll('.slot'))
.map(s => new Slot(s));
const choiceX = 500;
const choiceY = 25;
const choices = slots.map((s, i) =>
new Choice(svg, s.value,
choiceX, (choiceY * (i+1)),
null, (evt, choice) => {
const selectedSlot = slots
.filter(s => !s.hasChoice())
.find(s => s.isOverlapping(choice))
if (selectedSlot) {
selectedSlot.fill(choice);
filledSlotCount++;
}
if (filledSlotCount === slots.length) {
complete(slots);
}
}));
for (let choice of choices) {
choice.render();
}
}
function complete(slots) {
if (slots.every(s => s.hasCorrectChoice())) {
alert("You did it!");
} else {
alert("Some answers were wrong...:(")
}
}