2020-11-17 23:39:27 +01:00
|
|
|
<script>
|
2023-11-16 16:33:35 +01:00
|
|
|
import { fs_delete_all, fs_rename } from './../FilesystemAPI.js'
|
2023-05-17 15:34:56 +02:00
|
|
|
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'
|
2023-11-16 16:33:35 +01:00
|
|
|
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
|
2023-05-17 15:34:56 +02:00
|
|
|
export let edit_window
|
|
|
|
export let directory_view = ""
|
2023-11-16 19:05:30 +01:00
|
|
|
let large_icons = false
|
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
|
2023-04-19 18:26:50 +02:00
|
|
|
let show_hidden = false
|
2022-02-21 21:55:09 +01:00
|
|
|
|
2023-11-17 13:14:16 +01:00
|
|
|
$: selected_files = state.children.reduce((acc, file) => {
|
|
|
|
if (file.fm_selected) {
|
|
|
|
acc++
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, 0)
|
|
|
|
|
2022-02-21 21:55:09 +01:00
|
|
|
export const upload = files => {
|
|
|
|
return uploader.upload(files)
|
|
|
|
}
|
2020-11-17 23:39:27 +01:00
|
|
|
|
2023-11-16 19:05:30 +01:00
|
|
|
// Navigation functions
|
|
|
|
|
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)
|
2023-11-16 16:33:35 +01:00
|
|
|
} else if (mode === "moving") {
|
|
|
|
// If we are moving files we can only enter directories, and only if
|
2023-11-17 13:14:16 +01:00
|
|
|
// they're not selected. That last requirement prevents people from
|
|
|
|
// moving a directory into itself
|
2023-11-16 16:38:26 +01:00
|
|
|
if (state.children[index].type === "dir" && !state.children[index].fm_selected) {
|
2023-11-16 16:33:35 +01:00
|
|
|
fs_navigator.navigate(state.children[index].path, true)
|
|
|
|
}
|
2020-11-17 23:39:27 +01:00
|
|
|
} else if (mode === "selecting") {
|
2023-11-17 13:14:16 +01:00
|
|
|
select_node(index)
|
2020-11-17 23:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-16 19:05:30 +01:00
|
|
|
let node_context = e => {
|
|
|
|
// If this is a touch event we will select the item
|
|
|
|
if (navigator.maxTouchPoints && navigator.maxTouchPoints > 0) {
|
|
|
|
e.detail.event.preventDefault()
|
|
|
|
node_select({detail: e.detail.index})
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 16:59:11 +02:00
|
|
|
const node_share_click = e => {
|
|
|
|
let index = e.detail
|
|
|
|
|
|
|
|
creating_dir = false
|
|
|
|
fs_navigator.navigate(state.children[index].id, true)
|
|
|
|
}
|
2023-11-16 16:33:35 +01:00
|
|
|
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
|
|
|
|
2023-11-16 19:05:30 +01:00
|
|
|
// Deletion function
|
|
|
|
|
2023-11-16 16:33:35 +01:00
|
|
|
const delete_selected = async () => {
|
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)
|
|
|
|
|
2023-11-16 16:33:35 +01:00
|
|
|
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
|
|
|
|
2023-11-16 16:33:35 +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
|
|
|
}
|
2023-11-16 16:33:35 +01:00
|
|
|
}
|
2023-11-16 19:05:30 +01:00
|
|
|
|
|
|
|
// Mode switches
|
|
|
|
|
2023-11-16 16:33:35 +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
|
|
|
|
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) => {
|
2023-11-16 16:33:35 +01:00
|
|
|
if (child.fm_selected) {
|
|
|
|
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
|
|
|
}
|
2023-05-17 15:34:56 +02:00
|
|
|
|
|
|
|
const toggle_view = () => {
|
|
|
|
if (directory_view === "list") {
|
|
|
|
directory_view = "gallery"
|
|
|
|
} else {
|
|
|
|
directory_view = "list"
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem("directory_view", directory_view)
|
|
|
|
}
|
2023-11-16 19:05:30 +01:00
|
|
|
const toggle_large_icons = () => {
|
|
|
|
large_icons = !large_icons
|
|
|
|
localStorage.setItem("large_icons", large_icons)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Moving functions
|
2023-11-16 16:33:35 +01:00
|
|
|
|
|
|
|
let moving_items = []
|
|
|
|
|
2023-11-17 13:14:16 +01:00
|
|
|
// We need to detect if shift is pressed so we can select multiple items
|
|
|
|
let shift_pressed = false
|
|
|
|
let last_selected_node = -1
|
|
|
|
const detect_shift = (e) => {
|
|
|
|
if (e.key === "Shift") {
|
|
|
|
shift_pressed = e.type === "keydown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const select_node = index => {
|
|
|
|
if (shift_pressed) {
|
|
|
|
// If shift is pressed we do a range select. We select all files between
|
|
|
|
// the last selected file and the file that is being selected now
|
|
|
|
let id_low = Math.min(last_selected_node, index)
|
|
|
|
let id_high = Math.max(last_selected_node, index)
|
|
|
|
|
|
|
|
for (let i = id_low; i <= id_high; i++) {
|
|
|
|
if (i != last_selected_node) {
|
|
|
|
state.children[i].fm_selected = !state.children[i].fm_selected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state.children[index].fm_selected = !state.children[index].fm_selected
|
|
|
|
}
|
|
|
|
|
|
|
|
last_selected_node = index
|
|
|
|
}
|
|
|
|
|
2023-11-16 16:33:35 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-17 13:14:16 +01:00
|
|
|
|
|
|
|
let moving_files = 0
|
|
|
|
let moving_directories = 0
|
2023-11-16 16:33:35 +01:00
|
|
|
const move_start = () => {
|
2023-11-17 13:14:16 +01:00
|
|
|
moving_files = 0
|
|
|
|
moving_directories = 0
|
2023-11-16 16:33:35 +01:00
|
|
|
moving_items = state.children.reduce((acc, child) => {
|
|
|
|
if (child.fm_selected) {
|
2023-11-17 13:14:16 +01:00
|
|
|
if (child.type === "file") {
|
|
|
|
moving_files++
|
|
|
|
} else if (child.type === "dir") {
|
|
|
|
moving_directories++
|
|
|
|
}
|
2023-11-16 16:33:35 +01:00
|
|
|
acc.push(child)
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, [])
|
|
|
|
mode = "moving"
|
|
|
|
}
|
2023-11-17 13:14:16 +01:00
|
|
|
|
2023-11-16 16:33:35 +01:00
|
|
|
const move_here = async () => {
|
|
|
|
dispatch("loading", true)
|
|
|
|
|
|
|
|
let target_dir = state.base.path + "/"
|
|
|
|
|
|
|
|
try {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 15:34:56 +02:00
|
|
|
onMount(() => {
|
|
|
|
if(typeof Storage !== "undefined") {
|
|
|
|
directory_view = localStorage.getItem("directory_view")
|
2023-11-16 19:05:30 +01:00
|
|
|
large_icons = localStorage.getItem("large_icons") === "true"
|
2023-05-17 15:34:56 +02:00
|
|
|
}
|
|
|
|
if (directory_view === "" || directory_view === null) {
|
|
|
|
directory_view = "list"
|
|
|
|
}
|
|
|
|
})
|
2020-11-17 23:39:27 +01:00
|
|
|
</script>
|
|
|
|
|
2023-11-17 13:14:16 +01:00
|
|
|
<svelte:window on:keydown={detect_shift} on:keyup={detect_shift} />
|
|
|
|
|
2020-11-17 23:39:27 +01:00
|
|
|
<div class="container">
|
|
|
|
<div class="width_container">
|
2023-11-16 16:33:35 +01:00
|
|
|
{#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>
|
2023-11-16 16:33:35 +01:00
|
|
|
<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>
|
2023-11-16 16:33:35 +01:00
|
|
|
<button on:click={fs_navigator.reload()} title="Refresh directory listing">
|
|
|
|
<i class="icon">refresh</i>
|
2022-02-22 19:53:48 +01:00
|
|
|
</button>
|
2020-11-17 23:39:27 +01:00
|
|
|
|
2023-11-16 16:33:35 +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>
|
|
|
|
|
2023-11-16 19:05:30 +01:00
|
|
|
<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>
|
|
|
|
|
|
|
|
<button class="button_large_icons" on:click={() => toggle_large_icons()} title="Switch between large and small icons">
|
|
|
|
{#if large_icons}
|
|
|
|
<i class="icon">zoom_out</i>
|
|
|
|
{:else}
|
|
|
|
<i class="icon">zoom_in</i>
|
|
|
|
{/if}
|
|
|
|
</button>
|
|
|
|
|
2023-11-16 16:33:35 +01:00
|
|
|
<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>
|
2023-11-16 16:33:35 +01:00
|
|
|
<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>
|
2023-11-16 16:33:35 +01:00
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
{:else if mode === "selecting"}
|
|
|
|
<div class="toolbar toolbar_edit">
|
|
|
|
<Button click={viewing_mode} icon="close"/>
|
2023-11-17 13:14:16 +01:00
|
|
|
<div class="toolbar_spacer">
|
|
|
|
Selected {selected_files} files
|
|
|
|
</div>
|
2023-11-16 16:33:35 +01:00
|
|
|
<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">
|
2023-11-17 13:14:16 +01:00
|
|
|
Moving {moving_files} files and {moving_directories} directories
|
2020-11-17 23:39:27 +01:00
|
|
|
</div>
|
2023-11-16 16:33:35 +01:00
|
|
|
<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}
|
2023-11-16 16:33:35 +01:00
|
|
|
|
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"}
|
2023-05-30 16:59:11 +02:00
|
|
|
<ListView
|
|
|
|
state={state}
|
|
|
|
show_hidden={show_hidden}
|
2023-11-16 19:05:30 +01:00
|
|
|
large_icons={large_icons}
|
2023-05-30 16:59:11 +02:00
|
|
|
on:node_click={node_click}
|
2023-11-16 19:05:30 +01:00
|
|
|
on:node_context={node_context}
|
2023-05-30 16:59:11 +02:00
|
|
|
on:node_share_click={node_share_click}
|
|
|
|
on:node_settings={node_settings}
|
2023-11-16 16:33:35 +01:00
|
|
|
on:node_select={node_select}
|
2023-05-30 16:59:11 +02:00
|
|
|
/>
|
2022-02-22 19:53:48 +01:00
|
|
|
{:else if directory_view === "gallery"}
|
2023-05-30 16:59:11 +02:00
|
|
|
<GalleryView
|
|
|
|
state={state}
|
|
|
|
show_hidden={show_hidden}
|
2023-11-16 19:05:30 +01:00
|
|
|
large_icons={large_icons}
|
2023-05-30 16:59:11 +02:00
|
|
|
on:node_click={node_click}
|
2023-11-16 19:05:30 +01:00
|
|
|
on:node_context={node_context}
|
2023-05-30 16:59:11 +02:00
|
|
|
on:node_settings={node_settings}
|
2023-11-16 16:33:35 +01:00
|
|
|
on:node_select={node_select}
|
2023-05-30 16:59:11 +02:00
|
|
|
/>
|
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;
|
2023-11-15 15:50:54 +01:00
|
|
|
overflow: auto;
|
|
|
|
display: block;
|
2020-11-17 23:39:27 +01:00
|
|
|
}
|
|
|
|
.width_container {
|
2023-11-16 16:33:35 +01:00
|
|
|
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 {
|
2023-11-16 16:33:35 +01:00
|
|
|
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;
|
2023-11-16 16:33:35 +01:00
|
|
|
margin: auto;
|
2020-11-17 23:39:27 +01:00
|
|
|
padding: 0;
|
|
|
|
justify-content: center;
|
2023-11-16 16:33:35 +01:00
|
|
|
align-items: center;
|
2020-11-17 23:39:27 +01:00
|
|
|
}
|
|
|
|
.toolbar > * { flex: 0 0 auto; }
|
2023-11-16 16:33:35 +01:00
|
|
|
.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
|
|
|
}
|
2023-11-16 19:05:30 +01:00
|
|
|
|
|
|
|
/* Large icon mode only supported on wide screens. Hide the button on small screen */
|
|
|
|
@media (max-width: 500px) {
|
|
|
|
.button_large_icons {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 23:39:27 +01:00
|
|
|
</style>
|