-
Notifications
You must be signed in to change notification settings - Fork 0
/
swapper.js
73 lines (46 loc) · 1.93 KB
/
swapper.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
const bars = document.querySelectorAll('.fa-bars');
bars.forEach(bar => {
bar.addEventListener('dragend', e => swapDragEnd(e));
bar.addEventListener('dragstart', e => {
bar.classList.add('dragging');
});
bar.addEventListener('drag', e => swapDrag(e));
});
function swapDragEnd(e) {
if (getOperatorSign(e) != null) {
return;
}
const draggedRow = document.querySelector('.dragging').parentElement.parentElement;
document.querySelectorAll('.dragging').forEach(dragging => dragging.classList.remove('dragging'));
const y = Array.from(draggedRow.parentElement.children).indexOf(draggedRow) - 1;
const row = getHoveredRow(e);
if (row) {
const y2 = Array.from(row.parentElement.children).indexOf(row) - 1;
if (y == y2) {
return;
}
party.confetti(row);
var clone = matrix.map(function(arr) {
return arr.slice();
});
[clone[y], clone[y2]] = [clone[y2], clone[y]];
print(`${integerToRomanNumber(y+1)}' = ${integerToRomanNumber(y2+1)}; ${integerToRomanNumber(y2+1)}' = ${integerToRomanNumber(y+1)}`);
document.getElementById('last-operation').innerHTML = `Last Operation: ${integerToRomanNumber(y+1)}' = ${integerToRomanNumber(y2+1)}; ${integerToRomanNumber(y2+1)}' = ${integerToRomanNumber(y+1)}`;
animateMatrix(matrix, clone);
}
}
function swapDrag(e) {
const row = getHoveredRow(e);
if (row) {
const hoveredRowElements = getMatrixElements(row);
document.querySelectorAll('.matrix-part').forEach(td => {
if (hoveredRowElements.includes(td)) {
td.classList.add('selected');
} else {
td.classList.remove('selected');
}
});
}else {
document.querySelectorAll('.matrix-part').forEach(td => td.classList.remove('selected'));
}
}