2017-06-20 10:41:26 +00:00
|
|
|
import { Application } from "./Application"
|
|
|
|
import { AnimeNotifier } from "./AnimeNotifier"
|
2017-06-21 17:34:59 +00:00
|
|
|
import { Diff } from "./Diff"
|
2017-06-20 10:41:26 +00:00
|
|
|
|
2017-06-21 12:00:52 +00:00
|
|
|
// Save new data from an input field
|
|
|
|
export function save(arn: AnimeNotifier, input: HTMLInputElement | HTMLTextAreaElement) {
|
|
|
|
arn.loading(true)
|
|
|
|
|
|
|
|
let obj = {}
|
|
|
|
let value = input.value
|
|
|
|
|
|
|
|
if(input.type === "number") {
|
|
|
|
obj[input.id] = parseInt(value)
|
|
|
|
} else {
|
|
|
|
obj[input.id] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log(input.type, input.dataset.api, obj, JSON.stringify(obj))
|
|
|
|
|
|
|
|
let apiObject: HTMLElement
|
|
|
|
let parent = input as HTMLElement
|
|
|
|
|
|
|
|
while(parent = parent.parentElement) {
|
|
|
|
if(parent.dataset.api !== undefined) {
|
|
|
|
apiObject = parent
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!apiObject) {
|
|
|
|
throw "API object not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
input.disabled = true
|
|
|
|
|
|
|
|
fetch(apiObject.dataset.api, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(obj),
|
|
|
|
credentials: "same-origin"
|
|
|
|
})
|
|
|
|
.then(response => response.text())
|
|
|
|
.then(body => {
|
|
|
|
if(body !== "ok") {
|
|
|
|
throw body
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.error)
|
|
|
|
.then(() => {
|
|
|
|
arn.loading(false)
|
|
|
|
input.disabled = false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-20 20:54:45 +00:00
|
|
|
// Search
|
|
|
|
export function search(arn: AnimeNotifier, search: HTMLInputElement, e: KeyboardEvent) {
|
|
|
|
if(e.ctrlKey || e.altKey) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let term = search.value
|
|
|
|
|
|
|
|
if(!window.location.pathname.startsWith("/search/")) {
|
|
|
|
history.pushState("search", null, "/search/" + term)
|
|
|
|
} else {
|
|
|
|
history.replaceState("search", null, "/search/" + term)
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!term) {
|
|
|
|
arn.app.content.innerHTML = "No search term."
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-20 21:27:14 +00:00
|
|
|
var results = arn.app.find("results")
|
|
|
|
|
|
|
|
if(!results) {
|
|
|
|
results = document.createElement("div")
|
|
|
|
results.id = "results"
|
|
|
|
arn.app.content.innerHTML = ""
|
|
|
|
arn.app.content.appendChild(results)
|
|
|
|
}
|
2017-06-20 20:54:45 +00:00
|
|
|
|
|
|
|
arn.app.get("/_/search/" + encodeURI(term))
|
|
|
|
.then(html => {
|
|
|
|
if(!search.value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:34:59 +00:00
|
|
|
Diff.innerHTML(results, html)
|
2017-06-20 20:54:45 +00:00
|
|
|
arn.app.emit("DOMContentLoaded")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:41:26 +00:00
|
|
|
// Add anime to collection
|
|
|
|
export function addAnimeToCollection(arn: AnimeNotifier, button: HTMLElement) {
|
|
|
|
button.innerText = "Adding..."
|
|
|
|
arn.loading(true)
|
|
|
|
|
|
|
|
let {animeId, userId, userNick} = button.dataset
|
|
|
|
|
|
|
|
fetch("/api/animelist/" + userId + "/add", {
|
|
|
|
method: "POST",
|
2017-06-20 11:02:20 +00:00
|
|
|
body: animeId,
|
2017-06-20 12:19:35 +00:00
|
|
|
credentials: "same-origin"
|
2017-06-20 10:41:26 +00:00
|
|
|
})
|
|
|
|
.then(response => response.text())
|
|
|
|
.then(body => {
|
|
|
|
if(body !== "ok") {
|
|
|
|
throw body
|
|
|
|
}
|
|
|
|
|
2017-06-21 16:44:20 +00:00
|
|
|
return arn.reloadContent()
|
2017-06-20 10:41:26 +00:00
|
|
|
})
|
|
|
|
.catch(console.error)
|
|
|
|
.then(() => arn.loading(false))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove anime from collection
|
|
|
|
export function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElement) {
|
|
|
|
button.innerText = "Removing..."
|
|
|
|
arn.loading(true)
|
|
|
|
|
|
|
|
let {animeId, userId, userNick} = button.dataset
|
|
|
|
|
|
|
|
fetch("/api/animelist/" + userId + "/remove", {
|
|
|
|
method: "POST",
|
2017-06-20 11:02:20 +00:00
|
|
|
body: animeId,
|
2017-06-20 12:19:35 +00:00
|
|
|
credentials: "same-origin"
|
2017-06-20 10:41:26 +00:00
|
|
|
})
|
|
|
|
.then(response => response.text())
|
|
|
|
.then(body => {
|
|
|
|
if(body !== "ok") {
|
|
|
|
throw body
|
|
|
|
}
|
|
|
|
|
2017-06-20 13:46:49 +00:00
|
|
|
return arn.app.load("/+" + userNick + "/animelist")
|
2017-06-20 10:41:26 +00:00
|
|
|
})
|
|
|
|
.catch(console.error)
|
|
|
|
.then(() => arn.loading(false))
|
|
|
|
}
|