From 7e01b0aad067d7b4d62a23e0d04956dfa633ed13 Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Thu, 23 Apr 2020 11:48:03 -0500 Subject: [PATCH] fix some interactions between click-to-select and drag --- js/pacosako_ui.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/js/pacosako_ui.js b/js/pacosako_ui.js index ae87164..a1fdd1f 100644 --- a/js/pacosako_ui.js +++ b/js/pacosako_ui.js @@ -164,6 +164,9 @@ $(function (){ square.on('click.destination', squareClickDestination); } } + if (event === 'drag') { + cbSquare(from).droppable('enable'); + } } function pieceEndMove(piece, to) { @@ -193,7 +196,12 @@ $(function (){ } function squareDropDestination(ev, ui) { - pieceEndMove(ui.draggable, cbSquareLocation(this)); + const droppedAt = cbSquareLocation(this); + if ($(ui.draggable).data('location') == droppedAt) { + squareClickSelect.call(cbSquare(droppedAt)); + } else { + pieceEndMove(ui.draggable, droppedAt); + } } function squareClickUnselect(ev, ui) { @@ -201,6 +209,7 @@ $(function (){ } function squareClickSelect(ev, ui) { + renderBoard(); const clicked = $(this).children('.cb-piece.ui-draggable').not('.ui-draggable-disabled'); clicked.addClass('cb-selected'); $('#cb_board .cb-square').off('click.select'); @@ -209,13 +218,22 @@ $(function (){ } function pieceStartDrag(ev, ui) { + const dragged = $(this); $('#cb_board .cb-selected').removeClass('cb-selected'); - $('#cb_board .cb-square').off('click.destination'); - pieceStartMove($(this), 'drag'); + $('#cb_board .cb-legal').removeClass('cb-legal'); + dragged.data('saved-style', dragged.attr('style')); + $('#cb_board').data('dragging_from', dragged.data('location')); + pieceStartMove(dragged, 'drag'); } function pieceStopDrag(ev, ui) { - renderBoard(); + const dragged = $(this); + dragged.attr('style', dragged.data('saved-style')); + dragged.removeData('saved-style'); + dragged.css('z-index', ''); + if ($('#cb_board').data('dragging_from') === dragged.data('location')) { + renderBoard(); + } } function placePiece(where, side, type, suffix) { @@ -235,7 +253,7 @@ $(function (){ piece.draggable({ disabled: true, containment: '#cb_inner', - revert: 'invalid', + revert: true, zIndex: 100, start: pieceStartDrag, stop: pieceStopDrag, @@ -244,6 +262,7 @@ $(function (){ } function renderBoard(animate) { + $('#cb_board').removeData('dragging_from'); $('#cb_board .cb-piece').stop(true); $('#cb_board .cb-square').off('click.select'); $('#cb_board .cb-square').off('click.unselect');