2021-10-26 22:15:01 +02:00
|
|
|
<script>
|
2021-11-23 23:45:42 +01:00
|
|
|
import { createEventDispatcher } from "svelte";
|
2021-10-26 22:15:01 +02:00
|
|
|
|
|
|
|
let dispatch = createEventDispatcher()
|
|
|
|
|
|
|
|
export let files = []
|
2021-12-13 16:33:23 +01:00
|
|
|
export let shuffle = false
|
2021-10-26 22:15:01 +02:00
|
|
|
let file_list_div
|
|
|
|
let selected_file_index = 0
|
|
|
|
|
|
|
|
export const next = () => {
|
|
|
|
if (shuffle) {
|
|
|
|
rand_item()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-23 23:45:42 +01:00
|
|
|
select_item_event(selected_file_index+1)
|
|
|
|
}
|
|
|
|
export const prev = () => {
|
|
|
|
select_item_event(selected_file_index-1)
|
2021-10-26 22:15:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let history = []
|
|
|
|
export const rand_item = () => {
|
|
|
|
// Avoid viewing the same file multiple times
|
|
|
|
let rand
|
|
|
|
do {
|
2021-12-13 16:33:23 +01:00
|
|
|
rand = Math.floor(Math.random() * files.length)
|
2021-10-26 22:15:01 +02:00
|
|
|
console.log("rand is " + rand)
|
|
|
|
} while(history.indexOf(rand) > -1)
|
|
|
|
|
2021-12-13 16:33:23 +01:00
|
|
|
select_item_event(rand)
|
2021-10-26 22:15:01 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 16:33:23 +01:00
|
|
|
// select_item_event signals to the FileViewer that the file needs to be
|
|
|
|
// changed. The FileViewer then calls set_item if the change has been approved.
|
|
|
|
// ListNavigator cannot call set_item itself because it will cause a loop.
|
|
|
|
const select_item_event = idx => {
|
|
|
|
dispatch("set_file", idx)
|
|
|
|
}
|
2021-10-26 22:15:01 +02:00
|
|
|
|
|
|
|
export const set_item = idx => {
|
|
|
|
// Remove the class from the previous selected file
|
|
|
|
selected_file_index = idx
|
2021-11-23 23:45:42 +01:00
|
|
|
files.forEach((f, i) => {
|
|
|
|
f.selected = selected_file_index === i
|
|
|
|
})
|
|
|
|
files = files
|
2021-10-26 22:15:01 +02:00
|
|
|
|
|
|
|
// Add item to history
|
|
|
|
if(history.length >= (files.length - 6)){
|
|
|
|
history.shift()
|
|
|
|
}
|
|
|
|
history.push(idx)
|
|
|
|
|
|
|
|
// Smoothly scroll the navigator to the correct element
|
|
|
|
let selected_file = file_list_div.children[idx]
|
|
|
|
let cst = window.getComputedStyle(selected_file)
|
|
|
|
let itemWidth = selected_file.offsetWidth + parseInt(cst.marginLeft) + parseInt(cst.marginRight)
|
|
|
|
|
|
|
|
let start = file_list_div.scrollLeft
|
|
|
|
let end = ((idx * itemWidth) + (itemWidth / 2)) - (file_list_div.clientWidth / 2)
|
2021-11-28 15:19:00 +01:00
|
|
|
let steps = 30 // One second
|
2021-10-26 22:15:01 +02:00
|
|
|
let stepSize = (end - start)/steps
|
|
|
|
|
|
|
|
let animateScroll = (pos, step) => {
|
|
|
|
file_list_div.scrollLeft = pos
|
|
|
|
|
|
|
|
if (step < steps) {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
animateScroll(pos+stepSize, step+1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
animateScroll(start, 0)
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-11-28 15:19:00 +01:00
|
|
|
<div class="nav_container">
|
|
|
|
<button class="nav_button" style="margin-right: 0;" on:click={prev}>
|
|
|
|
<i class="icon">chevron_left</i>
|
|
|
|
</button>
|
|
|
|
<div bind:this={file_list_div} class="list_navigator">
|
|
|
|
{#each files as file, index}
|
|
|
|
<div class="list_item file_button"
|
|
|
|
class:file_button_selected={file.selected}
|
|
|
|
on:click={() => { select_item_event(index) }}>
|
|
|
|
<img src={file.icon_href+"?width=48&height=48"} alt={file.name} class="list_item_thumbnail" loading="lazy"/>
|
|
|
|
{file.name}
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
<button class="nav_button" style="margin-left: 0;" on:click={next}>
|
|
|
|
<i class="icon">chevron_right</i>
|
|
|
|
</button>
|
2021-10-26 22:15:01 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style>
|
2021-11-28 15:19:00 +01:00
|
|
|
.nav_container{
|
2021-10-26 22:15:01 +02:00
|
|
|
flex-grow: 0;
|
|
|
|
flex-shrink: 0;
|
2021-11-28 15:19:00 +01:00
|
|
|
display: flex;
|
2021-10-26 22:15:01 +02:00
|
|
|
position: relative;
|
|
|
|
width: 100%;
|
|
|
|
border-top: 1px solid var(--layer_2_color_border);
|
|
|
|
text-align: center;
|
|
|
|
line-height: 1em;
|
2021-11-28 15:19:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
.nav_button{
|
|
|
|
flex-grow: 0;
|
|
|
|
flex-shrink: 0;
|
|
|
|
height: 2.6em;
|
|
|
|
margin-top: 8px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.list_item {
|
|
|
|
height: 2.6em !important;
|
|
|
|
width: 220px !important;
|
|
|
|
}
|
|
|
|
.list_navigator {
|
|
|
|
flex-grow: 1;
|
|
|
|
flex-shrink: 1;
|
2021-10-26 22:15:01 +02:00
|
|
|
overflow-x: auto;
|
|
|
|
overflow-y: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
}
|
|
|
|
</style>
|