fix some interactions between click-to-select and drag

This commit is contained in:
Jesse D. McDonald 2020-04-23 11:48:03 -05:00
parent fc73c872ed
commit 7e01b0aad0
1 changed files with 24 additions and 5 deletions

View File

@ -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,14 +218,23 @@ $(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) {
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) {
const code = pieceCode(side, type);
@ -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');