Files
fnx_web/svelte/src/file_viewer/EditWindow.svelte

203 lines
4.3 KiB
Svelte
Raw Normal View History

2021-11-01 17:15:23 +01:00
<script>
2021-11-23 23:45:42 +01:00
import { createEventDispatcher } from "svelte";
import Spinner from "util/Spinner.svelte"
2021-11-23 23:45:42 +01:00
let dispatch = createEventDispatcher()
2021-11-01 17:15:23 +01:00
export let file = {
id: "",
name: "",
get_href: "",
2021-11-23 23:45:42 +01:00
can_edit: false,
}
export let list = {
id: "",
title: "",
files: [],
can_edit: false,
info_href: "",
2021-11-01 17:15:23 +01:00
}
2021-11-23 23:45:42 +01:00
let loading = false
2021-11-01 17:15:23 +01:00
let result_success = false
let result_text = ""
2021-11-23 23:45:42 +01:00
let file_name = ""
2021-11-01 17:15:23 +01:00
$: update_file(file.id)
let update_file = () => {
file_name = file.name
}
2021-11-23 23:45:42 +01:00
let list_name = ""
$: update_list(list.id)
let update_list = () => {
list_name = list.title
}
2021-11-01 17:15:23 +01:00
let rename_file = async e => {
e.preventDefault()
2021-11-23 23:45:42 +01:00
loading = true
2021-11-01 17:15:23 +01:00
const form = new FormData()
form.append("action", "rename")
form.append("name", file_name)
try {
const resp = await fetch(file.get_href, { method: "POST", body: form });
if (resp.status >= 400) {
throw (await resp.json()).message
}
result_success = true
result_text = "File name has been changed. Reload the page to see the changes"
} catch (err) {
result_success = false
result_text = "Could not change file name: " + err
2021-11-23 23:45:42 +01:00
} finally {
loading = false
dispatch("reload")
2021-11-01 17:15:23 +01:00
}
}
let delete_file = async e => {
if (!confirm("Are you sure you want to delete '" + file.name + "'?")) {
return
}
2021-11-23 23:45:42 +01:00
loading = true
2021-11-01 17:15:23 +01:00
try {
const resp = await fetch(file.get_href, { method: "DELETE" });
if (resp.status >= 400) {
throw (await resp.json()).message
}
result_success = true
result_text = "This file has been deleted, you can close the page"
} catch (err) {
result_success = false
result_text = "Could not delete file: " + err
2021-11-23 23:45:42 +01:00
} finally {
loading = false
}
}
let rename_list = async e => {
e.preventDefault()
loading = true
let listjson = {
title: list_name,
files: [],
}
list.files.forEach(f => {
listjson.files.push({
id: f.id,
})
})
try {
const resp = await fetch(
list.info_href,
{ method: "PUT", body: JSON.stringify(listjson) },
);
if (resp.status >= 400) {
throw (await resp.json()).message
}
result_success = true
result_text = "Album name has been changed. Reload the page to see the changes"
} catch (err) {
result_success = false
result_text = "Could not change album name: " + err
} finally {
loading = false
dispatch("reload")
}
}
let delete_list = async e => {
if (!confirm("Are you sure you want to delete '" + list.title + "'?")) {
return
}
loading = true
try {
const resp = await fetch(list.info_href, { method: "DELETE" });
if (resp.status >= 400) {
throw (await resp.json()).message
}
result_success = true
result_text = "This album has been deleted, you can close the page"
} catch (err) {
result_success = false
result_text = "Could not delete album: " + err
} finally {
loading = false
2021-11-01 17:15:23 +01:00
}
}
</script>
2022-01-04 15:55:20 +01:00
<div class="indent">
2021-11-23 23:45:42 +01:00
{#if loading}
<div class="spinner_container">
<Spinner></Spinner>
</div>
{/if}
2021-11-01 17:15:23 +01:00
{#if result_text !== ""}
<div class:highlight_green={result_success} class:highligt_red={!result_success}>
{result_text}
</div>
{/if}
2021-11-23 23:45:42 +01:00
{#if list.can_edit}
<h3>Edit album</h3>
2022-10-11 14:21:06 +02:00
Name:<br/>
2022-01-04 15:55:20 +01:00
<form on:submit={rename_list} style="display: flex;">
2021-11-23 23:45:42 +01:00
<input bind:value={list_name} type="text" style="flex: 1 1 auto"/>
2022-10-11 14:21:06 +02:00
<button type="submit" style="flex: 0 0 auto" class="button_highlight">
2021-11-23 23:45:42 +01:00
<i class="icon">save</i> Save
</button>
</form>
<h4>Delete</h4>
<p>
When you delete an album the files in the album will not be deleted,
only the album itself.
</p>
2022-01-04 15:55:20 +01:00
<button on:click={delete_list} class="button_red">
<i class="icon small">delete</i> Delete album
</button>
2021-11-23 23:45:42 +01:00
{/if}
{#if file.can_edit}
<h3>Edit file</h3>
2022-10-11 14:21:06 +02:00
Name:<br/>
2022-01-04 15:55:20 +01:00
<form on:submit={rename_file} style="display: flex;">
2021-11-23 23:45:42 +01:00
<input bind:value={file_name} type="text" style="flex: 1 1 auto"/>
2022-10-11 14:21:06 +02:00
<button type="submit" style="flex: 0 0 auto" class="button_highlight">
2021-11-23 23:45:42 +01:00
<i class="icon">save</i> Save
</button>
</form>
<h4>Delete</h4>
<p>
When you delete a file it cannot be recovered.
Nobody will be able to download it and the link will
stop working. The file will also disappear from any
lists it's contained in.
</p>
2022-01-04 15:55:20 +01:00
<button on:click={delete_file} class="button_red">
<i class="icon small">delete</i> Delete file
</button>
2021-11-23 23:45:42 +01:00
{/if}
2021-11-01 17:15:23 +01:00
</div>
<style>
2021-11-23 23:45:42 +01:00
.spinner_container {
position: absolute;
top: 10px;
left: 10px;
height: 100px;
width: 100px;
}
2021-11-01 17:15:23 +01:00
</style>