Files
fnx_web/svelte/src/filesystem/filemanager/FileManager.svelte

323 lines
8.2 KiB
Svelte
Raw Normal View History

2020-11-17 23:39:27 +01:00
<script>
import { fs_delete_all, fs_rename } from './../FilesystemAPI.js'
import { createEventDispatcher, onMount } from 'svelte'
2020-11-17 23:39:27 +01:00
import CreateDirectory from './CreateDirectory.svelte'
2022-02-22 19:53:48 +01:00
import ListView from './ListView.svelte'
import GalleryView from './GalleryView.svelte'
import Button from '../../layout/Button.svelte';
2020-11-17 23:39:27 +01:00
let dispatch = createEventDispatcher()
2023-05-17 22:46:27 +02:00
export let fs_navigator
2020-11-17 23:39:27 +01:00
export let state
export let edit_window
export let directory_view = ""
let uploader
2020-11-17 23:39:27 +01:00
let mode = "viewing"
let creating_dir = false
2023-04-19 18:26:50 +02:00
let show_hidden = false
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") {
2023-05-17 22:46:27 +02:00
fs_navigator.navigate(state.children[index].path, true)
} else if (mode === "moving") {
// If we are moving files we can only enter directories, and only if
// they're not selected
if (state.children[index].type === "dir" && !state.children[index].fm_selected) {
fs_navigator.navigate(state.children[index].path, true)
}
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 node_share_click = e => {
let index = e.detail
creating_dir = false
fs_navigator.navigate(state.children[index].id, true)
}
const node_select = e => {
let index = e.detail
mode = "selecting"
state.children[index].fm_selected = !state.children[index].fm_selected
}
2023-05-11 19:07:29 +02:00
const node_settings = e => {
2023-05-25 14:47:36 +02:00
edit_window.edit(state.children[e.detail], false)
2023-05-11 19:07:29 +02:00
}
2020-11-17 23:39:27 +01:00
const navigate_up = () => {
creating_dir = false
// Go to the path of the last parent
2023-04-11 23:44:50 +02:00
if (state.path.length > 1) {
2023-05-17 22:46:27 +02:00
fs_navigator.navigate(state.path[state.path.length-2].path, true)
2020-11-17 23:39:27 +01:00
}
}
2023-05-29 22:15:46 +02:00
const navigate_back = () => {
creating_dir = false
history.back()
}
2020-11-17 23:39:27 +01:00
const delete_selected = async () => {
2021-12-09 22:33:02 +01:00
let count = state.children.reduce((acc, cur) => {
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)
try {
// Save all promises with deletion requests in an array
let promises = []
state.children.forEach(child => {
if (!child.fm_selected) { return }
promises.push(fs_delete_all(child.path))
})
2020-11-17 23:39:27 +01:00
// Wait for all the promises to finish
await Promise.all(promises)
} catch (err) {
console.log(err)
alert("Delete failed: " + err.message + " ("+err.value+")")
} finally {
viewing_mode()
2023-05-25 14:47:36 +02:00
fs_navigator.reload()
2020-11-17 23:39:27 +01:00
}
}
const selecting_mode = () => {
mode = "selecting"
}
const viewing_mode = () => {
// Remove any items which we were moving
moving_items = []
2020-11-17 23:39:27 +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) => {
if (child.fm_selected) {
state.children[i].fm_selected = false
}
2020-11-17 23:39:27 +01:00
})
mode = "viewing"
2020-11-17 23:39:27 +01:00
}
const toggle_view = () => {
if (directory_view === "list") {
directory_view = "gallery"
} else {
directory_view = "list"
}
localStorage.setItem("directory_view", directory_view)
}
let moving_items = []
// When the directory is reloaded we want to keep our selection, so this
// function watches the children array for changes and updates the selection
// when it changes
$: update(state.children)
const update = (children) => {
// Highlight the files which were previously selected
for (let i = 0; i < children.length; i++) {
for (let j = 0; j < moving_items.length; j++) {
if (moving_items[j].path === children[i].path) {
children[i].fm_selected = true
}
}
}
}
const move_start = () => {
moving_items = state.children.reduce((acc, child) => {
if (child.fm_selected) {
acc.push(child)
}
return acc
}, [])
mode = "moving"
}
const move_here = async () => {
dispatch("loading", true)
let target_dir = state.base.path + "/"
try {
// Save all promises with deletion requests in an array
let promises = []
moving_items.forEach(item => {
console.log("moving", item.path, "to", target_dir + item.name)
promises.push(fs_rename(item.path, target_dir + item.name))
})
// Wait for all the promises to finish
await Promise.all(promises)
} catch (err) {
console.log(err)
alert("Move failed: " + err.message + " ("+err.value+")")
} finally {
viewing_mode()
fs_navigator.reload()
}
}
onMount(() => {
if(typeof Storage !== "undefined") {
directory_view = localStorage.getItem("directory_view")
}
if (directory_view === "" || directory_view === null) {
directory_view = "list"
}
})
2020-11-17 23:39:27 +01:00
</script>
<div class="container">
<div class="width_container">
{#if mode === "viewing"}
<div class="toolbar">
<button on:click={navigate_back} title="Back">
<i class="icon">arrow_back</i>
2022-02-22 19:53:48 +01:00
</button>
<button on:click={navigate_up} disabled={state.path.length <= 1} title="Up">
<i class="icon">north</i>
2022-11-07 17:10:17 +01:00
</button>
<button on:click={fs_navigator.reload()} title="Refresh directory listing">
<i class="icon">refresh</i>
2022-02-22 19:53:48 +01:00
</button>
<button on:click={() => toggle_view()} title="Switch between gallery view and list view">
{#if directory_view === "list"}
<i class="icon">collections</i>
{:else if directory_view === "gallery"}
<i class="icon">list</i>
{/if}
</button>
2020-11-17 23:39:27 +01:00
<button on:click={() => {show_hidden = !show_hidden}} title="Toggle hidden files">
{#if show_hidden}
<i class="icon">visibility_off</i>
{:else}
<i class="icon">visibility</i>
{/if}
2020-11-17 23:39:27 +01:00
</button>
<div class="toolbar_spacer"></div>
{#if state.permissions.update}
<button on:click={() => dispatch("upload_picker")} title="Upload files to this directory">
<i class="icon">cloud_upload</i>
2020-11-17 23:39:27 +01:00
</button>
<Button click={() => {creating_dir = !creating_dir}} highlight={creating_dir} icon="create_new_folder" title="Make folder"/>
<button
on:click={selecting_mode}
class:button_highlight={mode === "selecting"}
title="Select and delete files"
>
<i class="icon">edit</i>
2020-11-17 23:39:27 +01:00
</button>
{/if}
</div>
{:else if mode === "selecting"}
<div class="toolbar toolbar_edit">
<Button click={viewing_mode} icon="close"/>
<div class="toolbar_spacer"></div>
<Button click={move_start} icon="drive_file_move" label="Move"/>
<button on:click={delete_selected} class="button_red">
<i class="icon">delete</i>
Delete
</button>
</div>
{:else if mode === "moving"}
<div class="toolbar toolbar_edit">
<Button click={viewing_mode} icon="close"/>
<Button click={navigate_up} disabled={state.path.length <= 1} icon="north"/>
<div class="toolbar_spacer">
Moving {moving_items.length} items
2020-11-17 23:39:27 +01:00
</div>
<Button click={() => {creating_dir = !creating_dir}} highlight={creating_dir} icon="create_new_folder" title="Make folder"/>
<Button click={move_here} highlight icon="done" label="Move here"/>
2020-11-17 23:39:27 +01:00
</div>
{/if}
2022-02-22 19:53:48 +01:00
{#if creating_dir}
2023-05-25 14:47:36 +02:00
<CreateDirectory
state={state}
on:done={() => {fs_navigator.reload(); creating_dir = false;}}
on:loading
/>
2022-02-22 19:53:48 +01:00
{/if}
2020-11-17 23:39:27 +01:00
</div>
2022-02-22 19:53:48 +01:00
{#if directory_view === "list"}
<ListView
state={state}
show_hidden={show_hidden}
on:node_click={node_click}
on:node_share_click={node_share_click}
on:node_settings={node_settings}
on:node_select={node_select}
/>
2022-02-22 19:53:48 +01:00
{:else if directory_view === "gallery"}
<GalleryView
state={state}
show_hidden={show_hidden}
on:node_click={node_click}
on:node_settings={node_settings}
on:node_select={node_select}
/>
2022-02-22 19:53:48 +01:00
{/if}
2020-11-17 23:39:27 +01:00
</div>
<style>
.container {
height: 100%;
width: 100%;
padding: 0;
overflow: auto;
display: block;
2020-11-17 23:39:27 +01:00
}
.width_container {
position: sticky;
top: 0;
2022-02-22 19:53:48 +01:00
display: block;
2022-11-07 17:10:17 +01:00
width: 100%;
2022-02-22 19:53:48 +01:00
margin: auto;
2020-11-17 23:39:27 +01:00
padding: 0;
2022-11-07 17:10:17 +01:00
background: var(--shaded_background);
2020-11-17 23:39:27 +01:00
}
.toolbar {
display: flex;
2020-11-17 23:39:27 +01:00
flex-direction: row;
width: 100%;
2022-11-07 17:10:17 +01:00
max-width: 1000px;
margin: auto;
2020-11-17 23:39:27 +01:00
padding: 0;
justify-content: center;
align-items: center;
2020-11-17 23:39:27 +01:00
}
.toolbar > * { flex: 0 0 auto; }
.toolbar_spacer {
flex: 1 1 auto;
text-align: center;
}
.toolbar_edit {
background-color: rgba(0, 255, 0, 0.05);
2020-11-17 23:39:27 +01:00
}
</style>