Make pile list behavior a bit less dumb

- drag and drop doesn't jump all over the place
 - can't drag cards from list into piles
This commit is contained in:
Adam Goldsmith 2017-09-28 15:23:39 -04:00
parent 16e553b4ff
commit a69bbd4f8d

View File

@ -1,6 +1,8 @@
let deckName, deckJSON, cardCount, deckWidth, deckHeight, let deckName, deckJSON, cardCount, deckWidth, deckHeight,
piles = {'deck': [], discard: []}; piles = {'deck': [], discard: []};
interact.dynamicDrop(true);
window.addEventListener('load', () => { window.addEventListener('load', () => {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => { xhr.addEventListener("load", () => {
@ -73,12 +75,28 @@ interact('.card.in-list')
}) })
.dropzone({ .dropzone({
accept: '.card', accept: '.card',
ondragenter: event => { ondropmove: event => {
// TODO: drop after if cursor > 50% in card
let target = event.target; let target = event.target;
// TODO: there must be a better way to do this
let relCursorPos = (event.dragEvent.pageX -
target.getBoundingClientRect().left) /
parseInt(window.getComputedStyle(target).width);
let oldOffsetLeft = event.relatedTarget.offsetLeft;
let oldOffsetTop = event.relatedTarget.offsetTop;
// move the object in the DOM // move the object in the DOM
target.parentElement.insertBefore(event.relatedTarget, target); if (relCursorPos < 0.5 || relCursorPos === Infinity) {
target.parentElement.insertBefore(event.relatedTarget, target);
}
else {
target.parentElement.insertBefore(event.relatedTarget, target.nextSibling);
}
// update position
dragMoveListener({target: event.relatedTarget,
dx: oldOffsetLeft - event.relatedTarget.offsetLeft,
dy: oldOffsetTop - event.relatedTarget.offsetTop});
// rebuild source pile // rebuild source pile
piles[target.getAttribute('data-pile')] = piles[target.getAttribute('data-pile')] =
@ -92,7 +110,6 @@ interact('.card.in-list')
let listDiv = target.parentElement; let listDiv = target.parentElement;
// re-parent // re-parent
target.parentElement.removeChild(target);
document.querySelector("#card-container").appendChild(target); document.querySelector("#card-container").appendChild(target);
// rebuild source pile // rebuild source pile
@ -109,7 +126,7 @@ interact('.card.in-list')
interact('.card-pile') interact('.card-pile')
.dropzone({ .dropzone({
accept: '.card', accept: '.card:not(.in-pile)',
ondrop: event => { ondrop: event => {
// TODO: fix duped zeros // TODO: fix duped zeros
let pileName = event.target.getAttribute('data-pile'); let pileName = event.target.getAttribute('data-pile');