2018-04-02 05:34:16 +00:00
|
|
|
import AnimeNotifier from "../AnimeNotifier"
|
2018-11-23 07:05:48 +00:00
|
|
|
import { applyTheme } from "./Theme"
|
2017-10-17 09:27:15 +00:00
|
|
|
|
|
|
|
// Save new data from an input field
|
2018-04-13 12:35:16 +00:00
|
|
|
export async function save(arn: AnimeNotifier, input: HTMLElement) {
|
2019-04-22 06:59:08 +00:00
|
|
|
if(!input.dataset.field) {
|
|
|
|
console.error("Input element missing data-field:", input)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:27:15 +00:00
|
|
|
let obj = {}
|
|
|
|
let isContentEditable = input.isContentEditable
|
2018-06-28 06:30:24 +00:00
|
|
|
let value = isContentEditable ? input.textContent : (input as HTMLInputElement).value
|
2017-11-16 21:54:42 +00:00
|
|
|
|
2019-04-22 06:59:08 +00:00
|
|
|
if(value === undefined || value === null) {
|
2017-10-17 09:27:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:42:39 +00:00
|
|
|
// Trim value
|
|
|
|
value = value.trim()
|
|
|
|
|
2017-10-17 09:27:15 +00:00
|
|
|
if((input as HTMLInputElement).type === "number" || input.dataset.type === "number") {
|
|
|
|
if(input.getAttribute("step") === "1" || input.dataset.step === "1") {
|
|
|
|
obj[input.dataset.field] = parseInt(value)
|
|
|
|
} else {
|
|
|
|
obj[input.dataset.field] = parseFloat(value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
obj[input.dataset.field] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isContentEditable) {
|
|
|
|
input.contentEditable = "false"
|
|
|
|
} else {
|
|
|
|
(input as HTMLInputElement).disabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
let apiEndpoint = arn.findAPIEndpoint(input)
|
|
|
|
|
2018-04-13 12:35:16 +00:00
|
|
|
try {
|
|
|
|
await arn.post(apiEndpoint, obj)
|
2017-10-17 09:27:15 +00:00
|
|
|
|
2018-03-03 18:03:29 +00:00
|
|
|
if(apiEndpoint.startsWith("/api/user/") && input.dataset.field === "Nick") {
|
2018-04-13 12:35:16 +00:00
|
|
|
// Update nickname based links on the page
|
2018-03-03 18:03:29 +00:00
|
|
|
return arn.reloadPage()
|
2018-04-13 12:35:16 +00:00
|
|
|
} else if(apiEndpoint.startsWith("/api/settings/") && input.dataset.field === "Theme") {
|
2018-11-23 07:05:48 +00:00
|
|
|
// Apply theme instantly
|
|
|
|
applyTheme((input as HTMLInputElement).value)
|
2018-11-23 10:11:46 +00:00
|
|
|
|
|
|
|
// Reload to update theme settings
|
|
|
|
return arn.reloadContent()
|
2018-03-03 18:03:29 +00:00
|
|
|
} else {
|
|
|
|
return arn.reloadContent()
|
|
|
|
}
|
2018-04-13 12:35:16 +00:00
|
|
|
} catch(err) {
|
|
|
|
arn.reloadContent()
|
|
|
|
arn.statusMessage.showError(err)
|
|
|
|
} finally {
|
|
|
|
if(isContentEditable) {
|
|
|
|
input.contentEditable = "true"
|
|
|
|
} else {
|
|
|
|
(input as HTMLInputElement).disabled = false
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 09:27:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 20:08:30 +00:00
|
|
|
// Enable (bool field)
|
|
|
|
export async function enable(arn: AnimeNotifier, button: HTMLButtonElement) {
|
2019-04-22 06:59:08 +00:00
|
|
|
if(!button.dataset.field) {
|
|
|
|
console.error("Button missing data-field:", button)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:08:30 +00:00
|
|
|
let obj = {}
|
|
|
|
let apiEndpoint = arn.findAPIEndpoint(button)
|
|
|
|
|
|
|
|
obj[button.dataset.field] = true
|
|
|
|
button.disabled = true
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Update boolean value
|
|
|
|
await arn.post(apiEndpoint, obj)
|
|
|
|
|
|
|
|
// Reload content
|
|
|
|
arn.reloadContent()
|
|
|
|
|
|
|
|
arn.statusMessage.showInfo("Enabled: " + button.title)
|
|
|
|
} catch(err) {
|
|
|
|
arn.statusMessage.showError(err)
|
|
|
|
} finally {
|
|
|
|
button.disabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable (bool field)
|
|
|
|
export async function disable(arn: AnimeNotifier, button: HTMLButtonElement) {
|
2019-04-22 06:59:08 +00:00
|
|
|
if(!button.dataset.field) {
|
|
|
|
console.error("Button missing data-field:", button)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:08:30 +00:00
|
|
|
let obj = {}
|
|
|
|
let apiEndpoint = arn.findAPIEndpoint(button)
|
|
|
|
|
|
|
|
obj[button.dataset.field] = false
|
|
|
|
button.disabled = true
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Update boolean value
|
|
|
|
await arn.post(apiEndpoint, obj)
|
|
|
|
|
|
|
|
// Reload content
|
|
|
|
arn.reloadContent()
|
|
|
|
|
|
|
|
arn.statusMessage.showInfo("Disabled: " + button.title)
|
|
|
|
} catch(err) {
|
|
|
|
arn.statusMessage.showError(err)
|
|
|
|
} finally {
|
|
|
|
button.disabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:27:15 +00:00
|
|
|
// Append new element to array
|
2019-04-22 06:59:08 +00:00
|
|
|
export async function arrayAppend(arn: AnimeNotifier, element: HTMLElement) {
|
2017-10-17 09:27:15 +00:00
|
|
|
let field = element.dataset.field
|
|
|
|
let object = element.dataset.object || ""
|
|
|
|
let apiEndpoint = arn.findAPIEndpoint(element)
|
|
|
|
|
2019-04-22 06:59:08 +00:00
|
|
|
try {
|
|
|
|
await arn.post(apiEndpoint + "/field/" + field + "/append", object)
|
|
|
|
await arn.reloadContent()
|
|
|
|
} catch(err) {
|
|
|
|
arn.statusMessage.showError(err)
|
|
|
|
}
|
2017-10-17 09:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove element from array
|
2019-04-22 06:59:08 +00:00
|
|
|
export async function arrayRemove(arn: AnimeNotifier, element: HTMLElement) {
|
2017-10-17 09:27:15 +00:00
|
|
|
if(!confirm("Are you sure you want to remove this element?")) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let field = element.dataset.field
|
|
|
|
let index = element.dataset.index
|
|
|
|
let apiEndpoint = arn.findAPIEndpoint(element)
|
|
|
|
|
2019-04-22 06:59:08 +00:00
|
|
|
try {
|
|
|
|
await arn.post(apiEndpoint + "/field/" + field + "/remove/" + index)
|
|
|
|
await arn.reloadContent()
|
|
|
|
} catch(err) {
|
|
|
|
arn.statusMessage.showError(err)
|
|
|
|
}
|
2017-10-17 09:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increase episode
|
|
|
|
export function increaseEpisode(arn: AnimeNotifier, element: HTMLElement) {
|
|
|
|
if(arn.isLoading) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-22 06:59:08 +00:00
|
|
|
let prev = element.previousSibling
|
|
|
|
|
|
|
|
if(prev === null || !(prev instanceof HTMLElement) || prev.textContent === null) {
|
|
|
|
console.error("Previous sibling is invalid:", element)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-28 06:30:24 +00:00
|
|
|
let episodes = parseInt(prev.textContent)
|
|
|
|
prev.textContent = String(episodes + 1)
|
2019-04-22 06:59:08 +00:00
|
|
|
return save(arn, prev)
|
2017-11-16 21:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add number
|
|
|
|
export function addNumber(arn: AnimeNotifier, element: HTMLElement) {
|
|
|
|
if(arn.isLoading) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-22 06:59:08 +00:00
|
|
|
if(!element.dataset.id || !element.dataset.add) {
|
|
|
|
console.error("Element is missing the data-id or data-add attribute:", element)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-02 05:44:11 +00:00
|
|
|
let input = document.getElementById(element.dataset.id) as HTMLInputElement
|
2017-11-16 21:54:42 +00:00
|
|
|
let add = parseInt(element.dataset.add)
|
|
|
|
let num = parseInt(input.value)
|
2017-11-17 13:25:24 +00:00
|
|
|
let newValue = num + add
|
|
|
|
let min = parseInt(input.min)
|
|
|
|
let max = parseInt(input.max)
|
|
|
|
|
|
|
|
if(newValue > max) {
|
|
|
|
arn.statusMessage.showError("Maximum: " + max)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newValue < min) {
|
|
|
|
arn.statusMessage.showError("Minimum: " + min)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
input.value = newValue.toString()
|
2019-04-22 06:59:08 +00:00
|
|
|
return save(arn, input)
|
2017-10-17 09:27:15 +00:00
|
|
|
}
|