summaryrefslogtreecommitdiff
path: root/public/board.js
diff options
context:
space:
mode:
authorame <[email protected]>2024-09-01 14:30:11 -0500
committerame <[email protected]>2024-09-01 14:30:11 -0500
commitac624cd97ac014d209a732474b5fbde29f889e09 (patch)
treedb4e30d5970bdda2196e975be0458c76f813e84c /public/board.js
parentb97b195aec4576f7d8f496004a2f314fc09998c6 (diff)
drag and drop
Diffstat (limited to 'public/board.js')
-rw-r--r--public/board.js19
1 files changed, 15 insertions, 4 deletions
diff --git a/public/board.js b/public/board.js
index 785efff..de3953a 100644
--- a/public/board.js
+++ b/public/board.js
@@ -6,8 +6,17 @@ sync_board()
let highlighted_squares = []
+//allow ondrop to fire
+document.addEventListener("dragover", (e) => e.preventDefault(), false)
+
+function piece_events(e){
+ e.onclick = e.ondrag = handle_highlight(e.id)
+ e.draggable = true
+ e.ondrop = null
+}
+
for (let i = 0; i < squares.length; i++) {
- squares[i].onclick = handle_highlight(squares[i].id)
+ piece_events(squares[i])
}
function handle_highlight(id) {
@@ -17,13 +26,14 @@ function handle_highlight(id) {
for (let i = 0; i < highlighted_squares.length; i++) {
highlighted_squares[i].classList.remove('highlight')
- highlighted_squares[i].onclick = handle_highlight(highlighted_squares[i].id)
+ piece_events(highlighted_squares[i])
}
for (let i = 0; i < moves.length; i++) {
let hlsquare = document.getElementById(`${moves[i][0]}${moves[i][1]}`)
hlsquare.classList.add('highlight')
+ hlsquare.ondrop = handle_move(id, hlsquare.id)
hlsquare.onclick = handle_move(id, hlsquare.id)
highlighted_squares.push(hlsquare)
@@ -105,7 +115,7 @@ function handle_move(from_id, to_id) {
let passanted_pawn = document.getElementById(`3${en_passant_move}`)
while (passanted_pawn.firstChild) {
- passanted_pawn.removeChild(passanted_pawn.firstChild)
+ onclickpassanted_pawn.removeChild(passanted_pawn.firstChild)
}
} else {
let passanted_pawn = document.getElementById(`4${en_passant_move}`)
@@ -154,7 +164,8 @@ function handle_move(from_id, to_id) {
to_element.append(piece)
for (let i = 0; i < highlighted_squares.length; i++) {
- highlighted_squares[i].onclick = handle_highlight(highlighted_squares[i].id)
+ //highlighted_squares[i].onclick = handle_highlight(highlighted_squares[i].id)
+ piece_events(highlighted_squares[i])
highlighted_squares[i].classList.remove('highlight')
}