154 lines
4.0 KiB
TypeScript
Raw Normal View History

2018-04-02 05:34:16 +00:00
import AnimeNotifier from "../AnimeNotifier"
2017-10-17 09:27:15 +00:00
// Edit post
export function editPost(arn: AnimeNotifier, element: HTMLElement) {
let postId = element.dataset.id
2018-04-02 05:44:11 +00:00
let render = document.getElementById("render-" + postId)
let toolbar = document.getElementById("toolbar-" + postId)
let title = document.getElementById("title-" + postId)
let source = document.getElementById("source-" + postId)
let edit = document.getElementById("edit-toolbar-" + postId)
2017-10-17 09:27:15 +00:00
render.classList.toggle("hidden")
toolbar.classList.toggle("hidden")
source.classList.toggle("hidden")
edit.classList.toggle("hidden")
if(title) {
title.classList.toggle("hidden")
}
}
// Save post
export function savePost(arn: AnimeNotifier, element: HTMLElement) {
let postId = element.dataset.id
2018-04-02 05:44:11 +00:00
let source = document.getElementById("source-" + postId) as HTMLTextAreaElement
let title = document.getElementById("title-" + postId) as HTMLInputElement
2017-10-17 09:27:15 +00:00
let text = source.value
let updates: any = {
Text: text,
}
// Add title for threads only
if(title) {
updates.Title = title.value
}
let apiEndpoint = arn.findAPIEndpoint(element)
arn.post(apiEndpoint, updates)
.then(() => arn.reloadContent())
.catch(err => arn.statusMessage.showError(err))
}
2017-11-25 14:26:56 +00:00
// Delete post
export function deletePost(arn: AnimeNotifier, element: HTMLElement) {
2017-11-25 14:26:56 +00:00
if(!confirm(`Are you sure you want to delete this Post?`)) {
return
}
let endpoint = arn.findAPIEndpoint(element)
2018-04-25 16:59:23 +00:00
arn.post(endpoint + "/delete")
2017-11-25 14:26:56 +00:00
.then(() => arn.reloadContent())
.catch(err => arn.statusMessage.showError(err))
}
2018-10-29 02:30:23 +00:00
// Create post
export function createPost(arn: AnimeNotifier, element: HTMLElement) {
2018-11-05 11:57:37 +00:00
let textarea = document.getElementById("new-post-text") as HTMLTextAreaElement
2018-10-29 02:30:23 +00:00
let {parentId, parentType} = element.dataset
2017-10-17 09:27:15 +00:00
let post = {
text: textarea.value,
2018-10-29 02:30:23 +00:00
parentId,
parentType,
2017-10-17 09:27:15 +00:00
tags: []
}
arn.post("/api/new/post", post)
.then(() => arn.reloadContent())
.then(() => textarea.value = "")
.catch(err => arn.statusMessage.showError(err))
}
// Create thread
export function createThread(arn: AnimeNotifier) {
2018-04-02 05:44:11 +00:00
let title = document.getElementById("title") as HTMLInputElement
let text = document.getElementById("text") as HTMLTextAreaElement
let category = document.getElementById("tag") as HTMLInputElement
2017-10-17 09:27:15 +00:00
let thread = {
title: title.value,
text: text.value,
tags: [category.value]
}
arn.post("/api/new/thread", thread)
.then(() => arn.app.load("/forum/" + thread.tags[0]))
.catch(err => arn.statusMessage.showError(err))
2018-04-25 16:59:23 +00:00
}
2018-11-05 10:05:36 +00:00
// Reply to a post
2018-11-05 11:57:37 +00:00
export async function reply(arn: AnimeNotifier, element: HTMLElement) {
let apiEndpoint = arn.findAPIEndpoint(element)
let repliesId = `replies-${element.dataset.postId}`
let replies = document.getElementById(repliesId)
// Delete old reply area
let oldReplyArea = document.getElementById("new-post")
if(oldReplyArea) {
oldReplyArea.remove()
}
// Delete old reply button
let oldPostActions = document.getElementById("new-post-actions")
if(oldPostActions) {
oldPostActions.remove()
}
// Fetch new reply UI
try {
let response = await fetch(`${apiEndpoint}/reply/ui`)
let html = await response.text()
replies.innerHTML = html + replies.innerHTML
arn.onNewContent(replies)
arn.assignActions()
} catch(err) {
arn.statusMessage.showError(err)
}
}
2018-11-05 10:05:36 +00:00
2018-11-05 11:57:37 +00:00
// Cancel replying to a post
export function cancelReply(arn: AnimeNotifier, element: HTMLElement) {
arn.reloadContent()
2018-11-05 10:05:36 +00:00
}
2018-04-25 16:59:23 +00:00
// Lock thread
export function lockThread(arn: AnimeNotifier, element: HTMLButtonElement) {
setThreadLock(arn, element, true)
}
// Unlock thread
export function unlockThread(arn: AnimeNotifier, element: HTMLButtonElement) {
setThreadLock(arn, element, false)
}
// Set thread locked state
function setThreadLock(arn: AnimeNotifier, element: HTMLButtonElement, state: boolean) {
let verb = state ? "lock" : "unlock"
if(!confirm(`Are you sure you want to ${verb} this Thread?`)) {
return
}
let endpoint = arn.findAPIEndpoint(element)
arn.post(`${endpoint}/${verb}`)
.then(() => arn.reloadContent())
.catch(err => arn.statusMessage.showError(err))
2017-10-17 09:27:15 +00:00
}