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

View File

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

View File

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