2020-11-17 23:39:27 +01:00
|
|
|
<script>
|
|
|
|
import { fs_delete_node } from './../FilesystemAPI.svelte'
|
|
|
|
import { createEventDispatcher } from 'svelte'
|
|
|
|
import CreateDirectory from './CreateDirectory.svelte'
|
|
|
|
import FileUploader from './FileUploader.svelte'
|
2022-02-22 19:53:48 +01:00
|
|
|
import ListView from './ListView.svelte'
|
|
|
|
import GalleryView from './GalleryView.svelte'
|
2020-11-17 23:39:27 +01:00
|
|
|
let dispatch = createEventDispatcher()
|
|
|
|
|
|
|
|
export let state
|
2022-02-22 19:53:48 +01:00
|
|
|
export let directory_view = "gallery"
|
2022-02-21 21:55:09 +01:00
|
|
|
let uploader
|
2020-11-17 23:39:27 +01:00
|
|
|
let mode = "viewing"
|
|
|
|
let creating_dir = false
|
2022-02-21 21:55:09 +01:00
|
|
|
|
|
|
|
export const upload = files => {
|
|
|
|
return uploader.upload(files)
|
|
|
|
}
|
2020-11-17 23:39:27 +01:00
|
|
|
|
2022-02-22 19:53:48 +01:00
|
|
|
const node_click = e => {
|
|
|
|
let index = e.detail
|
|
|
|
|
2020-11-17 23:39:27 +01:00
|
|
|
creating_dir = false
|
|
|
|
|
|
|
|
// We prefix our custom state properties with fm_ to not interfere with
|
|
|
|
// other modules
|
|
|
|
if (mode === "viewing") {
|
2021-12-09 22:33:02 +01:00
|
|
|
dispatch("navigate", state.children[index].path)
|
2020-11-17 23:39:27 +01:00
|
|
|
} else if (mode === "selecting") {
|
2021-12-09 22:33:02 +01:00
|
|
|
state.children[index].fm_selected = !state.children[index].fm_selected
|
2020-11-17 23:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const navigate_up = () => {
|
|
|
|
creating_dir = false
|
|
|
|
|
|
|
|
// Go to the path of the last parent
|
|
|
|
if (state.parents.length !== 0) {
|
|
|
|
dispatch("navigate", state.parents[state.parents.length-1].path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const reload = () => { dispatch("navigate", state.base.path) }
|
|
|
|
|
2020-12-01 23:01:21 +01:00
|
|
|
const delete_selected = () => {
|
|
|
|
if (mode !== "selecting") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-09 22:33:02 +01:00
|
|
|
let count = state.children.reduce((acc, cur) => {
|
2020-12-01 23:01:21 +01:00
|
|
|
if (cur.fm_selected) {
|
|
|
|
acc++
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, 0)
|
|
|
|
|
|
|
|
let confirmSingle = `Are you sure you want to delete this file? This action is irreversible.`
|
|
|
|
let confirmMulti = `Are you sure you want to delete these ${count} files? This action is irreversible.`
|
|
|
|
if (count === 0 ||
|
|
|
|
(count === 1 && !confirm(confirmSingle)) ||
|
|
|
|
(count > 1 && !confirm(confirmMulti))) {
|
2020-11-17 23:39:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch("loading", true)
|
|
|
|
|
|
|
|
// Save all promises with deletion requests in an array
|
|
|
|
let promises = []
|
2021-12-09 22:33:02 +01:00
|
|
|
state.children.forEach(child => {
|
2020-12-01 23:01:21 +01:00
|
|
|
if (!child.fm_selected) { return }
|
2020-11-17 23:39:27 +01:00
|
|
|
promises.push(fs_delete_node(state.bucket.id, child.path))
|
|
|
|
})
|
|
|
|
|
|
|
|
// Wait for all the promises to finish
|
|
|
|
Promise.all(promises).catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
}).finally(() => {
|
|
|
|
mode = "viewing"
|
|
|
|
reload()
|
|
|
|
})
|
|
|
|
}
|
2020-12-01 23:01:21 +01:00
|
|
|
const toggle_select = () => {
|
|
|
|
if (mode !== "selecting") {
|
|
|
|
mode = "selecting"
|
2020-11-17 23:39:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:01:21 +01:00
|
|
|
// Unmark all the selected files and return to viewing mode
|
2021-12-09 22:33:02 +01:00
|
|
|
state.children.forEach((child, i) => {
|
2020-12-01 23:01:21 +01:00
|
|
|
if (child.fm_selected) {
|
2021-12-09 22:33:02 +01:00
|
|
|
state.children[i].fm_selected = false
|
2020-11-17 23:39:27 +01:00
|
|
|
}
|
|
|
|
})
|
2020-12-01 23:01:21 +01:00
|
|
|
mode = "viewing"
|
2020-11-17 23:39:27 +01:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="container">
|
|
|
|
<div class="width_container">
|
|
|
|
<div class="toolbar">
|
2020-12-01 23:01:21 +01:00
|
|
|
<button on:click={navigate_up} disabled={state.parents.length === 0}><i class="icon">arrow_back</i></button>
|
|
|
|
<button on:click={reload}><i class="icon">refresh</i></button>
|
2022-02-22 19:53:48 +01:00
|
|
|
{#if directory_view === "list"}
|
|
|
|
<button on:click={() => {directory_view = "gallery"}} alt="Switch to gallery view">
|
|
|
|
<i class="icon">collections</i>
|
|
|
|
</button>
|
|
|
|
{:else if directory_view === "gallery"}
|
|
|
|
<button on:click={() => {directory_view = "list"}} alt="Switch to list view">
|
|
|
|
<i class="icon">list</i>
|
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
|
2020-11-17 23:39:27 +01:00
|
|
|
<div class="toolbar_spacer"></div>
|
|
|
|
{#if state.bucket.permissions.update}
|
|
|
|
<button on:click={uploader.picker}><i class="icon">cloud_upload</i></button>
|
2022-02-22 19:53:48 +01:00
|
|
|
<button on:click={() => {creating_dir = !creating_dir}} class:button_highlight={creating_dir}>
|
|
|
|
<i class="icon">create_new_folder</i>
|
|
|
|
</button>
|
2020-11-17 23:39:27 +01:00
|
|
|
|
|
|
|
<button
|
2020-12-01 23:01:21 +01:00
|
|
|
on:click={toggle_select}
|
|
|
|
class:button_highlight={mode === "selecting"}>
|
|
|
|
<i class="icon">edit</i>
|
2020-11-17 23:39:27 +01:00
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
<br/>
|
|
|
|
|
2020-12-01 23:01:21 +01:00
|
|
|
{#if mode === "selecting"}
|
|
|
|
<div class="toolbar toolbar_edit highlight_green">
|
2020-11-17 23:39:27 +01:00
|
|
|
<div style="flex: 1 1 auto; justify-self: center;">
|
2020-12-01 23:01:21 +01:00
|
|
|
Select files or directories by clicking on them. Then you
|
|
|
|
can choose which action to perform
|
2020-11-17 23:39:27 +01:00
|
|
|
</div>
|
|
|
|
<div style="display: flex; flex-direction: row; justify-content: center;">
|
2020-12-01 23:01:21 +01:00
|
|
|
<button on:click={toggle_select}>
|
2020-11-17 23:39:27 +01:00
|
|
|
<i class="icon">undo</i>
|
|
|
|
Cancel
|
|
|
|
</button>
|
2020-12-01 23:01:21 +01:00
|
|
|
<button on:click={delete_selected} class="button_red">
|
2020-11-17 23:39:27 +01:00
|
|
|
<i class="icon">delete</i>
|
|
|
|
Delete selected
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<br/>
|
|
|
|
{/if}
|
2022-02-22 19:53:48 +01:00
|
|
|
{#if creating_dir}
|
|
|
|
<CreateDirectory state={state} on:done={() => {reload(); creating_dir = false;}} on:loading></CreateDirectory>
|
|
|
|
{/if}
|
2020-11-17 23:39:27 +01:00
|
|
|
|
2022-02-22 19:53:48 +01:00
|
|
|
<FileUploader
|
|
|
|
bind:this={uploader}
|
|
|
|
bucket_id={state.bucket.id}
|
|
|
|
target_dir={state.base.path}
|
|
|
|
on:reload={reload}
|
|
|
|
write_password={state.write_password}
|
|
|
|
></FileUploader>
|
2020-11-17 23:39:27 +01:00
|
|
|
|
|
|
|
</div>
|
2022-02-22 19:53:48 +01:00
|
|
|
|
|
|
|
{#if directory_view === "list"}
|
|
|
|
<ListView state={state} on:node_click={node_click}></ListView>
|
|
|
|
{:else if directory_view === "gallery"}
|
|
|
|
<GalleryView state={state} on:node_click={node_click}></GalleryView>
|
|
|
|
{/if}
|
2020-11-17 23:39:27 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.container {
|
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
padding: 0;
|
|
|
|
overflow-y: auto;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
.width_container {
|
|
|
|
position: relative;
|
2022-02-22 19:53:48 +01:00
|
|
|
display: block;
|
2022-02-21 21:55:09 +01:00
|
|
|
max-width: 95%;
|
2020-11-17 23:39:27 +01:00
|
|
|
width: 1000px;
|
2022-02-22 19:53:48 +01:00
|
|
|
margin: auto;
|
2020-11-17 23:39:27 +01:00
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
.toolbar {
|
|
|
|
position: relative;
|
|
|
|
display: inline-flex;
|
|
|
|
flex-direction: row;
|
|
|
|
width: 100%;
|
|
|
|
margin: 16px 0 0 0;
|
|
|
|
padding: 0;
|
|
|
|
justify-content: center;
|
|
|
|
}
|
|
|
|
.toolbar > * { flex: 0 0 auto; }
|
|
|
|
.toolbar_spacer { flex: 1 1 auto; }
|
|
|
|
|
|
|
|
@media(max-width: 800px) {
|
2020-12-01 23:01:21 +01:00
|
|
|
.toolbar_edit {
|
2020-11-17 23:39:27 +01:00
|
|
|
flex-direction: column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|