Cleanup and drag & drop fix

This commit is contained in:
Eduard Urbach 2019-04-21 17:39:24 +09:00
parent bffa28d338
commit d07232bff8
3 changed files with 18 additions and 14 deletions

View File

@ -9,7 +9,7 @@ export async function join(arn: AnimeNotifier, element: HTMLElement) {
let apiEndpoint = arn.findAPIEndpoint(element)
try {
await arn.post(`${apiEndpoint}/join`, null)
await arn.post(`${apiEndpoint}/join`)
arn.reloadContent()
arn.statusMessage.showInfo("Joined group!", 1000)
} catch(err) {
@ -26,7 +26,7 @@ export async function leave(arn: AnimeNotifier, element: HTMLElement) {
let apiEndpoint = arn.findAPIEndpoint(element)
try {
await arn.post(`${apiEndpoint}/leave`, null)
await arn.post(`${apiEndpoint}/leave`)
arn.reloadContent()
arn.statusMessage.showInfo("Left group!", 1000)
} catch(err) {

View File

@ -6,7 +6,7 @@ export async function like(arn: AnimeNotifier, element: HTMLElement) {
let apiEndpoint = arn.findAPIEndpoint(element)
try {
await arn.post(apiEndpoint + "/like", null)
await arn.post(apiEndpoint + "/like")
arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)
@ -19,7 +19,7 @@ export async function unlike(arn: AnimeNotifier, element: HTMLElement) {
let apiEndpoint = arn.findAPIEndpoint(element)
try {
await arn.post(apiEndpoint + "/unlike", null)
await arn.post(apiEndpoint + "/unlike")
arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)

View File

@ -527,7 +527,7 @@ export default class AnimeNotifier {
let apiEndpoint = this.findAPIEndpoint(element)
this.post(apiEndpoint + "/use/" + element.dataset.index, "")
this.post(apiEndpoint + "/use/" + element.dataset.index)
.then(() => this.reloadContent())
.then(() => this.statusMessage.showInfo(`You used ${itemName}.`))
.catch(err => this.statusMessage.showError(err))
@ -546,13 +546,17 @@ export default class AnimeNotifier {
}, false)
element.addEventListener("drop", e => {
let toElement = e.toElement as HTMLElement
toElement.classList.remove("drag-enter")
element.classList.remove("drag-enter")
e.stopPropagation()
e.preventDefault()
let inventory = e.toElement.parentElement
let inventory = element.parentElement
if(!inventory || !e.dataTransfer) {
return
}
let fromIndex = e.dataTransfer.getData("text")
if(!fromIndex) {
@ -560,24 +564,24 @@ export default class AnimeNotifier {
}
let fromElement = inventory.childNodes[fromIndex] as HTMLElement
let toIndex = element.dataset.index
let toIndex = toElement.dataset.index
if(fromElement === toElement || fromIndex === toIndex) {
if(!toIndex || fromElement === element || fromIndex === toIndex) {
console.error("Invalid drag & drop from", fromIndex, "to", toIndex)
return
}
// Swap in database
let apiEndpoint = this.findAPIEndpoint(inventory)
this.post(apiEndpoint + "/swap/" + fromIndex + "/" + toIndex, "")
this.post(apiEndpoint + "/swap/" + fromIndex + "/" + toIndex)
.catch(err => this.statusMessage.showError(err))
// Swap in UI
swapElements(fromElement, toElement)
swapElements(fromElement, element)
fromElement.dataset.index = toIndex
toElement.dataset.index = fromIndex
element.dataset.index = fromIndex
}, false)
// Prevent re-attaching the same listeners