277 lines
7.0 KiB
TypeScript
Raw Normal View History

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-07-06 14:54:10 +00:00
import { findAll } from "./Utils"
2017-06-20 10:41:26 +00:00
2017-06-21 12:00:52 +00:00
// Save new data from an input field
2017-07-12 22:34:33 +00:00
export function save(arn: AnimeNotifier, input: HTMLElement) {
2017-06-21 12:00:52 +00:00
arn.loading(true)
2017-06-30 21:52:42 +00:00
let isContentEditable = input.isContentEditable
2017-06-21 12:00:52 +00:00
let obj = {}
2017-07-12 22:34:33 +00:00
let value = isContentEditable ? input.innerText : (input as HTMLInputElement).value
2017-06-21 12:00:52 +00:00
2017-07-12 22:34:33 +00:00
if((input as HTMLInputElement).type === "number" || input.dataset.type === "number") {
2017-06-30 21:52:42 +00:00
if(input.getAttribute("step") === "1" || input.dataset.step === "1") {
obj[input.dataset.field] = parseInt(value)
2017-06-26 09:43:47 +00:00
} else {
2017-06-30 21:52:42 +00:00
obj[input.dataset.field] = parseFloat(value)
2017-06-26 09:43:47 +00:00
}
2017-06-21 12:00:52 +00:00
} else {
2017-06-30 21:52:42 +00:00
obj[input.dataset.field] = value
2017-06-21 12:00:52 +00:00
}
2017-06-30 21:52:42 +00:00
if(isContentEditable) {
input.contentEditable = "false"
} else {
2017-07-12 22:34:33 +00:00
(input as HTMLInputElement).disabled = true
2017-06-30 21:52:42 +00:00
}
2017-06-21 12:00:52 +00:00
2017-07-06 14:54:10 +00:00
let apiEndpoint = arn.findAPIEndpoint(input)
fetch(apiEndpoint, {
2017-06-21 12:00:52 +00:00
method: "POST",
body: JSON.stringify(obj),
credentials: "same-origin"
})
.then(response => response.text())
.then(body => {
if(body !== "ok") {
throw body
}
})
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-06-21 12:00:52 +00:00
.then(() => {
arn.loading(false)
2017-06-30 21:52:42 +00:00
if(isContentEditable) {
input.contentEditable = "true"
} else {
2017-07-12 22:34:33 +00:00
(input as HTMLInputElement).disabled = false
2017-06-30 21:52:42 +00:00
}
2017-06-24 00:10:04 +00:00
return arn.reloadContent()
2017-06-21 12:00:52 +00:00
})
}
2017-07-12 18:37:34 +00:00
// Close status message
export function closeStatusMessage(arn: AnimeNotifier) {
arn.statusMessage.close()
}
2017-07-12 22:34:33 +00:00
// Increase episode
export function increaseEpisode(arn: AnimeNotifier, element: HTMLElement) {
let prev = element.previousSibling as HTMLElement
let episodes = parseInt(prev.innerText)
prev.innerText = String(episodes + 1)
save(arn, prev)
}
2017-06-27 02:15:52 +00:00
// Load
export function load(arn: AnimeNotifier, element: HTMLElement) {
let url = element.dataset.url || (element as HTMLAnchorElement).getAttribute("href")
arn.app.load(url)
}
2017-07-03 17:33:52 +00:00
// Soon
export function soon() {
alert("Coming Soon™")
}
2017-06-26 01:57:29 +00:00
// Diff
export function diff(arn: AnimeNotifier, element: HTMLElement) {
let url = element.dataset.url || (element as HTMLAnchorElement).getAttribute("href")
2017-07-03 15:21:00 +00:00
2017-07-06 14:54:10 +00:00
arn.diff(url).then(() => arn.scrollTo(element))
}
2017-07-05 19:12:33 +00:00
2017-07-06 14:54:10 +00:00
// Edit post
export function editPost(arn: AnimeNotifier, element: HTMLElement) {
let postId = element.dataset.id
let render = arn.app.find("render-" + postId)
let toolbar = arn.app.find("toolbar-" + postId)
2017-07-06 17:33:58 +00:00
let title = arn.app.find("title-" + postId)
2017-07-06 14:54:10 +00:00
let source = arn.app.find("source-" + postId)
let edit = arn.app.find("edit-toolbar-" + postId)
2017-07-06 17:33:58 +00:00
render.classList.toggle("hidden")
toolbar.classList.toggle("hidden")
source.classList.toggle("hidden")
edit.classList.toggle("hidden")
if(title) {
title.classList.toggle("hidden")
2017-07-06 14:54:10 +00:00
}
}
// Save post
export function savePost(arn: AnimeNotifier, element: HTMLElement) {
let postId = element.dataset.id
let source = arn.app.find("source-" + postId) as HTMLTextAreaElement
2017-07-06 17:33:58 +00:00
let title = arn.app.find("title-" + postId) as HTMLInputElement
2017-07-06 14:54:10 +00:00
let text = source.value
2017-07-06 17:33:58 +00:00
let updates: any = {
2017-07-06 14:54:10 +00:00
Text: text,
}
2017-07-06 17:33:58 +00:00
// Add title for threads only
if(title) {
updates.Title = title.value
}
2017-07-06 14:54:10 +00:00
let apiEndpoint = arn.findAPIEndpoint(element)
arn.post(apiEndpoint, updates)
.then(() => arn.reloadContent())
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-06-26 17:03:48 +00:00
}
2017-06-26 01:57:29 +00:00
2017-07-08 21:27:24 +00:00
// like
export function like(arn: AnimeNotifier, element: HTMLElement) {
let apiEndpoint = arn.findAPIEndpoint(element)
arn.post(apiEndpoint + "/like", null)
.then(() => arn.reloadContent())
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-07-08 21:27:24 +00:00
}
// unlike
export function unlike(arn: AnimeNotifier, element: HTMLElement) {
let apiEndpoint = arn.findAPIEndpoint(element)
arn.post(apiEndpoint + "/unlike", null)
.then(() => arn.reloadContent())
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-07-08 21:27:24 +00:00
}
2017-06-26 17:03:48 +00:00
// Forum reply
export function forumReply(arn: AnimeNotifier) {
let textarea = arn.app.find("new-reply") as HTMLTextAreaElement
2017-06-26 21:04:15 +00:00
let thread = arn.app.find("thread")
2017-06-26 01:57:29 +00:00
2017-06-26 21:04:15 +00:00
let post = {
text: textarea.value,
threadId: thread.dataset.id,
tags: []
}
2017-07-06 14:54:10 +00:00
arn.post("/api/new/post", post)
2017-06-26 21:04:15 +00:00
.then(() => arn.reloadContent())
2017-06-27 02:15:52 +00:00
.then(() => textarea.value = "")
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-06-27 02:15:52 +00:00
}
// Create thread
export function createThread(arn: AnimeNotifier) {
let title = arn.app.find("title") as HTMLInputElement
let text = arn.app.find("text") as HTMLTextAreaElement
let category = arn.app.find("tag") as HTMLInputElement
let thread = {
title: title.value,
text: text.value,
tags: [category.value]
}
2017-07-06 14:54:10 +00:00
arn.post("/api/new/thread", thread)
2017-06-27 02:15:52 +00:00
.then(() => arn.app.load("/forum/" + thread.tags[0]))
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-06-26 01:57:29 +00:00
}
2017-06-27 14:23:57 +00:00
// Create soundtrack
export function createSoundTrack(arn: AnimeNotifier, button: HTMLButtonElement) {
let soundcloud = arn.app.find("soundcloud-link") as HTMLInputElement
2017-06-27 21:33:21 +00:00
let youtube = arn.app.find("youtube-link") as HTMLInputElement
2017-06-27 14:23:57 +00:00
let anime = arn.app.find("anime-link") as HTMLInputElement
let osu = arn.app.find("osu-link") as HTMLInputElement
let soundtrack = {
soundcloud: soundcloud.value,
2017-06-27 21:33:21 +00:00
youtube: youtube.value,
2017-06-27 14:23:57 +00:00
tags: [anime.value, osu.value],
}
2017-07-06 14:54:10 +00:00
arn.post("/api/new/soundtrack", soundtrack)
2017-06-27 14:23:57 +00:00
.then(() => arn.app.load("/music"))
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-06-27 14:23:57 +00:00
}
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
2017-07-13 23:00:51 +00:00
arn.app.currentPath = "/search/" + term
2017-07-02 15:51:17 +00:00
if(window.location.pathname.startsWith("/search/")) {
2017-07-13 23:00:51 +00:00
history.replaceState("search", null, arn.app.currentPath)
2017-07-02 15:51:17 +00:00
} else {
2017-07-13 23:00:51 +00:00
history.pushState("search", null, arn.app.currentPath)
2017-06-20 20:54:45 +00:00
}
2017-06-22 12:32:38 +00:00
if(!term || term.length < 2) {
arn.app.content.innerHTML = "Please enter at least 2 characters to start searching."
2017-06-20 20:54:45 +00:00
return
}
2017-07-13 23:00:51 +00:00
arn.reloadContent()
2017-06-20 20:54:45 +00:00
}
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
})
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-06-20 10:41:26 +00:00
.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-07-12 22:34:33 +00:00
return arn.app.load("/+" + userNick + "/animelist/" + (arn.app.find("Status") as HTMLSelectElement).value)
2017-06-20 10:41:26 +00:00
})
2017-07-12 18:37:34 +00:00
.catch(err => arn.statusMessage.showError(err))
2017-06-20 10:41:26 +00:00
.then(() => arn.loading(false))
2017-06-25 16:06:03 +00:00
}
// Chrome extension installation
export function installExtension(arn: AnimeNotifier, button: HTMLElement) {
let browser: any = window["chrome"]
browser.webstore.install()
2017-06-20 10:41:26 +00:00
}