Expand image explorer date navigation
Browse files- app.py +85 -17
- explorer.css +63 -3
- explorer.js +140 -12
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import os
|
|
| 6 |
import re
|
| 7 |
import secrets
|
| 8 |
import shutil
|
|
|
|
| 9 |
import subprocess
|
| 10 |
import sys
|
| 11 |
import tempfile
|
|
@@ -93,6 +94,7 @@ MOUNTED_CUSTOM_NODES_DIR = BUCKET_MOUNT / "custom_nodes"
|
|
| 93 |
CUSTOM_NODES_DIR = Path("/tmp/custom_nodes")
|
| 94 |
OUTPUT_DIR = DATA_DIR / "output"
|
| 95 |
IMAGE_DIR = DATA_DIR / "img"
|
|
|
|
| 96 |
TIMEZONE = ZoneInfo("Asia/Singapore")
|
| 97 |
CPU_DEVICE = torch.device("cpu")
|
| 98 |
MIB = 1024 * 1024
|
|
@@ -746,6 +748,11 @@ class DownloadRequest(BaseModel):
|
|
| 746 |
items: list[str]
|
| 747 |
|
| 748 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 749 |
class MatrixRequest(BaseModel):
|
| 750 |
model_config = ConfigDict(extra="forbid")
|
| 751 |
|
|
@@ -2240,6 +2247,35 @@ def natural_key(path):
|
|
| 2240 |
]
|
| 2241 |
|
| 2242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2243 |
def stored_bytes(path):
|
| 2244 |
data = path.read_bytes()
|
| 2245 |
if not data.startswith(FILE_MAGIC):
|
|
@@ -3542,33 +3578,64 @@ def upscale_download(
|
|
| 3542 |
)
|
| 3543 |
|
| 3544 |
|
| 3545 |
-
|
| 3546 |
-
|
| 3547 |
-
|
| 3548 |
-
|
| 3549 |
-
|
| 3550 |
-
|
| 3551 |
-
|
| 3552 |
-
|
| 3553 |
-
|
| 3554 |
-
|
| 3555 |
-
|
| 3556 |
-
|
| 3557 |
-
return {"folders": folders}
|
| 3558 |
-
path = (root / folder).resolve()
|
| 3559 |
-
if path.parent != root.resolve() or not path.is_dir():
|
| 3560 |
raise HTTPException(404, "folder not found")
|
| 3561 |
with os.scandir(path) as entries:
|
| 3562 |
-
|
| 3563 |
{
|
| 3564 |
"name": Path(item.name).with_suffix(".png").name,
|
| 3565 |
"path": (Path(folder) / item.name).as_posix(),
|
|
|
|
| 3566 |
}
|
| 3567 |
for item in sorted(entries, key=natural_key)
|
| 3568 |
if Path(item.name).suffix.casefold() in IMAGE_SUFFIXES
|
| 3569 |
and item.is_file()
|
| 3570 |
]
|
| 3571 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3572 |
|
| 3573 |
|
| 3574 |
@api.get(
|
|
@@ -3631,6 +3698,7 @@ def image_download(body: DownloadRequest):
|
|
| 3631 |
def image_delete(body: DownloadRequest):
|
| 3632 |
paths = selected_paths(body.items)
|
| 3633 |
for path in paths.values():
|
|
|
|
| 3634 |
path.unlink()
|
| 3635 |
for value in body.items:
|
| 3636 |
path = (IMAGE_DIR / value).resolve()
|
|
|
|
| 6 |
import re
|
| 7 |
import secrets
|
| 8 |
import shutil
|
| 9 |
+
import sqlite3
|
| 10 |
import subprocess
|
| 11 |
import sys
|
| 12 |
import tempfile
|
|
|
|
| 94 |
CUSTOM_NODES_DIR = Path("/tmp/custom_nodes")
|
| 95 |
OUTPUT_DIR = DATA_DIR / "output"
|
| 96 |
IMAGE_DIR = DATA_DIR / "img"
|
| 97 |
+
STAR_DB = DATA_DIR / "explorer.db"
|
| 98 |
TIMEZONE = ZoneInfo("Asia/Singapore")
|
| 99 |
CPU_DEVICE = torch.device("cpu")
|
| 100 |
MIB = 1024 * 1024
|
|
|
|
| 748 |
items: list[str]
|
| 749 |
|
| 750 |
|
| 751 |
+
class StarRequest(BaseModel):
|
| 752 |
+
path: str
|
| 753 |
+
starred: bool
|
| 754 |
+
|
| 755 |
+
|
| 756 |
class MatrixRequest(BaseModel):
|
| 757 |
model_config = ConfigDict(extra="forbid")
|
| 758 |
|
|
|
|
| 2247 |
]
|
| 2248 |
|
| 2249 |
|
| 2250 |
+
def star_database():
|
| 2251 |
+
STAR_DB.parent.mkdir(parents=True, exist_ok=True)
|
| 2252 |
+
database = sqlite3.connect(STAR_DB)
|
| 2253 |
+
database.execute(
|
| 2254 |
+
"CREATE TABLE IF NOT EXISTS stars (path TEXT PRIMARY KEY)"
|
| 2255 |
+
)
|
| 2256 |
+
return database
|
| 2257 |
+
|
| 2258 |
+
|
| 2259 |
+
def starred_paths():
|
| 2260 |
+
database = star_database()
|
| 2261 |
+
try:
|
| 2262 |
+
return {row[0] for row in database.execute("SELECT path FROM stars")}
|
| 2263 |
+
finally:
|
| 2264 |
+
database.close()
|
| 2265 |
+
|
| 2266 |
+
|
| 2267 |
+
def set_star(path, starred):
|
| 2268 |
+
database = star_database()
|
| 2269 |
+
try:
|
| 2270 |
+
if starred:
|
| 2271 |
+
database.execute("INSERT OR IGNORE INTO stars VALUES (?)", (path,))
|
| 2272 |
+
else:
|
| 2273 |
+
database.execute("DELETE FROM stars WHERE path = ?", (path,))
|
| 2274 |
+
database.commit()
|
| 2275 |
+
finally:
|
| 2276 |
+
database.close()
|
| 2277 |
+
|
| 2278 |
+
|
| 2279 |
def stored_bytes(path):
|
| 2280 |
data = path.read_bytes()
|
| 2281 |
if not data.startswith(FILE_MAGIC):
|
|
|
|
| 3578 |
)
|
| 3579 |
|
| 3580 |
|
| 3581 |
+
def explorer_folder_has_images(path):
|
| 3582 |
+
with os.scandir(path) as entries:
|
| 3583 |
+
return any(
|
| 3584 |
+
item.is_file()
|
| 3585 |
+
and Path(item.name).suffix.casefold() in IMAGE_SUFFIXES
|
| 3586 |
+
for item in entries
|
| 3587 |
+
)
|
| 3588 |
+
|
| 3589 |
+
|
| 3590 |
+
def explorer_images(folder, starred):
|
| 3591 |
+
path = (IMAGE_DIR / folder).resolve()
|
| 3592 |
+
if path.parent != IMAGE_DIR.resolve() or not path.is_dir():
|
|
|
|
|
|
|
|
|
|
| 3593 |
raise HTTPException(404, "folder not found")
|
| 3594 |
with os.scandir(path) as entries:
|
| 3595 |
+
return [
|
| 3596 |
{
|
| 3597 |
"name": Path(item.name).with_suffix(".png").name,
|
| 3598 |
"path": (Path(folder) / item.name).as_posix(),
|
| 3599 |
+
"starred": (Path(folder) / item.name).as_posix() in starred,
|
| 3600 |
}
|
| 3601 |
for item in sorted(entries, key=natural_key)
|
| 3602 |
if Path(item.name).suffix.casefold() in IMAGE_SUFFIXES
|
| 3603 |
and item.is_file()
|
| 3604 |
]
|
| 3605 |
+
|
| 3606 |
+
|
| 3607 |
+
@api.get("/api/images", dependencies=[Depends(require_pass)])
|
| 3608 |
+
def image_list(folder: str = Query(None), infinite: bool = Query(False)):
|
| 3609 |
+
root = IMAGE_DIR
|
| 3610 |
+
if not root.is_dir():
|
| 3611 |
+
return {"groups": []} if infinite else (
|
| 3612 |
+
{"folders": []} if folder is None else {"images": []}
|
| 3613 |
+
)
|
| 3614 |
+
with os.scandir(root) as entries:
|
| 3615 |
+
folders = [
|
| 3616 |
+
item.name
|
| 3617 |
+
for item in sorted(entries, key=natural_key, reverse=True)
|
| 3618 |
+
if item.is_dir(follow_symlinks=False)
|
| 3619 |
+
and explorer_folder_has_images(item.path)
|
| 3620 |
+
]
|
| 3621 |
+
if folder is None and not infinite:
|
| 3622 |
+
return {"folders": [{"name": name} for name in folders]}
|
| 3623 |
+
starred = starred_paths()
|
| 3624 |
+
if infinite:
|
| 3625 |
+
return {
|
| 3626 |
+
"groups": [
|
| 3627 |
+
{"name": name, "images": explorer_images(name, starred)}
|
| 3628 |
+
for name in folders
|
| 3629 |
+
]
|
| 3630 |
+
}
|
| 3631 |
+
return {"images": explorer_images(folder, starred)}
|
| 3632 |
+
|
| 3633 |
+
|
| 3634 |
+
@api.post("/api/images/star", dependencies=[Depends(require_pass)])
|
| 3635 |
+
def image_star(body: StarRequest):
|
| 3636 |
+
path = stored_path(body.path).relative_to(IMAGE_DIR.resolve()).as_posix()
|
| 3637 |
+
set_star(path, body.starred)
|
| 3638 |
+
return {"starred": body.starred}
|
| 3639 |
|
| 3640 |
|
| 3641 |
@api.get(
|
|
|
|
| 3698 |
def image_delete(body: DownloadRequest):
|
| 3699 |
paths = selected_paths(body.items)
|
| 3700 |
for path in paths.values():
|
| 3701 |
+
set_star(path.relative_to(IMAGE_DIR.resolve()).as_posix(), False)
|
| 3702 |
path.unlink()
|
| 3703 |
for value in body.items:
|
| 3704 |
path = (IMAGE_DIR / value).resolve()
|
explorer.css
CHANGED
|
@@ -30,10 +30,41 @@ image-explorer {
|
|
| 30 |
width: var(--width);
|
| 31 |
}
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
h1 {
|
| 34 |
font-size: 1.5rem;
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
.tools {
|
|
@@ -68,7 +99,7 @@ button.primary {
|
|
| 68 |
}
|
| 69 |
|
| 70 |
button:disabled {
|
| 71 |
-
cursor:
|
| 72 |
opacity: .6;
|
| 73 |
}
|
| 74 |
|
|
@@ -122,6 +153,11 @@ button:disabled {
|
|
| 122 |
display: grid;
|
| 123 |
gap: .5rem;
|
| 124 |
padding: .5rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
|
| 127 |
.preview {
|
|
@@ -138,6 +174,18 @@ button:disabled {
|
|
| 138 |
width: 100%;
|
| 139 |
}
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
.image-info {
|
| 142 |
display: grid;
|
| 143 |
gap: .2rem;
|
|
@@ -221,3 +269,15 @@ button:disabled {
|
|
| 221 |
[hidden] {
|
| 222 |
display: none !important;
|
| 223 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
width: var(--width);
|
| 31 |
}
|
| 32 |
|
| 33 |
+
h1,
|
| 34 |
+
h2 {
|
| 35 |
+
font-weight: 500;
|
| 36 |
+
margin: 0;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
h1 {
|
| 40 |
font-size: 1.5rem;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
h2 {
|
| 44 |
+
color: var(--accent);
|
| 45 |
+
font-size: 1.1rem;
|
| 46 |
+
margin-bottom: var(--gap);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
.date-heading {
|
| 50 |
+
align-items: center;
|
| 51 |
+
display: grid;
|
| 52 |
+
gap: var(--gap);
|
| 53 |
+
grid-template-columns: auto 1fr auto;
|
| 54 |
+
margin-bottom: var(--gap);
|
| 55 |
+
text-align: center;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
.date-prev {
|
| 59 |
+
grid-column: 1;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
.date-heading h1 {
|
| 63 |
+
grid-column: 2;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
.date-next {
|
| 67 |
+
grid-column: 3;
|
| 68 |
}
|
| 69 |
|
| 70 |
.tools {
|
|
|
|
| 99 |
}
|
| 100 |
|
| 101 |
button:disabled {
|
| 102 |
+
cursor: not-allowed;
|
| 103 |
opacity: .6;
|
| 104 |
}
|
| 105 |
|
|
|
|
| 153 |
display: grid;
|
| 154 |
gap: .5rem;
|
| 155 |
padding: .5rem;
|
| 156 |
+
position: relative;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
.date-group + .date-group {
|
| 160 |
+
margin-top: 2rem;
|
| 161 |
}
|
| 162 |
|
| 163 |
.preview {
|
|
|
|
| 174 |
width: 100%;
|
| 175 |
}
|
| 176 |
|
| 177 |
+
.star {
|
| 178 |
+
color: var(--accent);
|
| 179 |
+
font-size: 1.2rem;
|
| 180 |
+
line-height: 1;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
.card-star {
|
| 184 |
+
position: absolute;
|
| 185 |
+
right: .75rem;
|
| 186 |
+
top: .75rem;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
.image-info {
|
| 190 |
display: grid;
|
| 191 |
gap: .2rem;
|
|
|
|
| 269 |
[hidden] {
|
| 270 |
display: none !important;
|
| 271 |
}
|
| 272 |
+
|
| 273 |
+
@media (max-width: 36rem) {
|
| 274 |
+
image-explorer {
|
| 275 |
+
margin: 1rem auto;
|
| 276 |
+
width: calc(100% - 2rem);
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
.lightbox {
|
| 280 |
+
height: 100dvh;
|
| 281 |
+
width: 100%;
|
| 282 |
+
}
|
| 283 |
+
}
|
explorer.js
CHANGED
|
@@ -9,12 +9,17 @@ const original = path => api("/api/images/original", { path })
|
|
| 9 |
const MIN_ZOOM = 1
|
| 10 |
const MAX_ZOOM = 8
|
| 11 |
const ZOOM_STEP = 1.2
|
|
|
|
| 12 |
|
| 13 |
class ImageExplorer extends HTMLElement {
|
| 14 |
connectedCallback() {
|
| 15 |
this.innerHTML = `
|
| 16 |
<main>
|
| 17 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
<div class="tools">
|
| 19 |
<button class="back" type="button" hidden>Folders</button>
|
| 20 |
<input type="search" placeholder="Filter folders" aria-label="Filter folders">
|
|
@@ -29,6 +34,7 @@ class ImageExplorer extends HTMLElement {
|
|
| 29 |
<dialog class="lightbox">
|
| 30 |
<div class="image-actions">
|
| 31 |
<button class="upscale primary" type="button">Upscale</button>
|
|
|
|
| 32 |
<span class="upscale-status" role="status" aria-live="polite"></span>
|
| 33 |
</div>
|
| 34 |
<button class="close" type="button" aria-label="Close">Close</button>
|
|
@@ -43,6 +49,8 @@ class ImageExplorer extends HTMLElement {
|
|
| 43 |
this.setupPreviews()
|
| 44 |
this.load()
|
| 45 |
$(".back", this).addEventListener("click", () => this.load())
|
|
|
|
|
|
|
| 46 |
$(".all", this).addEventListener("click", () => this.select(true))
|
| 47 |
$(".clear", this).addEventListener("click", () => this.select(false))
|
| 48 |
$(".download", this).addEventListener("click", () => this.download())
|
|
@@ -59,7 +67,10 @@ class ImageExplorer extends HTMLElement {
|
|
| 59 |
$(".content", this).replaceChildren()
|
| 60 |
let data = this.cache.get(folder)
|
| 61 |
if (!data) {
|
| 62 |
-
const
|
|
|
|
|
|
|
|
|
|
| 63 |
if (!response.ok) {
|
| 64 |
$(".status", this).textContent = "Unable to load images"
|
| 65 |
return
|
|
@@ -68,17 +79,40 @@ class ImageExplorer extends HTMLElement {
|
|
| 68 |
this.cache.set(folder, data)
|
| 69 |
}
|
| 70 |
this.folder = folder
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
$(".back", this).hidden = !folder
|
|
|
|
| 73 |
const search = $("input[type=search]", this)
|
| 74 |
search.value = ""
|
| 75 |
search.placeholder = `Filter ${folder ? "images" : "folders"}`
|
| 76 |
search.setAttribute("aria-label", search.placeholder)
|
| 77 |
-
if (folder) this.
|
|
|
|
| 78 |
else this.addFolders(data.folders)
|
| 79 |
this.update()
|
| 80 |
}
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
setupPreviews() {
|
| 83 |
this.previewObserver = new IntersectionObserver(entries => {
|
| 84 |
for (const entry of entries) {
|
|
@@ -94,6 +128,18 @@ class ImageExplorer extends HTMLElement {
|
|
| 94 |
addFolders(folders) {
|
| 95 |
const grid = document.createElement("div")
|
| 96 |
grid.className = "grid folders"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
for (const folder of folders) {
|
| 98 |
const card = document.createElement("div")
|
| 99 |
card.className = "folder entry"
|
|
@@ -120,6 +166,22 @@ class ImageExplorer extends HTMLElement {
|
|
| 120 |
$(".content", this).append(grid)
|
| 121 |
}
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
checkbox(path) {
|
| 124 |
const input = document.createElement("input")
|
| 125 |
input.type = "checkbox"
|
|
@@ -143,6 +205,7 @@ class ImageExplorer extends HTMLElement {
|
|
| 143 |
picture.dataset.src = preview(image.path)
|
| 144 |
this.previewObserver.observe(picture)
|
| 145 |
open.append(picture)
|
|
|
|
| 146 |
const info = document.createElement("label")
|
| 147 |
info.className = "image-info"
|
| 148 |
const input = this.checkbox(image.path)
|
|
@@ -150,10 +213,64 @@ class ImageExplorer extends HTMLElement {
|
|
| 150 |
name.className = "name"
|
| 151 |
name.textContent = image.name
|
| 152 |
info.append(input, name)
|
| 153 |
-
card.append(open, info)
|
| 154 |
return card
|
| 155 |
}
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
select(checked) {
|
| 158 |
for (const input of $$(".item", this)) input.checked = checked
|
| 159 |
this.update()
|
|
@@ -169,6 +286,9 @@ class ImageExplorer extends HTMLElement {
|
|
| 169 |
for (const entry of $$(".entry", this)) {
|
| 170 |
entry.hidden = Boolean(query && !entry.dataset.name.includes(query))
|
| 171 |
}
|
|
|
|
|
|
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
async download() {
|
|
@@ -243,6 +363,9 @@ class ImageExplorer extends HTMLElement {
|
|
| 243 |
const image = $("img", viewer)
|
| 244 |
$(".close", dialog).addEventListener("click", () => dialog.close())
|
| 245 |
$(".upscale", dialog).addEventListener("click", () => this.upscale())
|
|
|
|
|
|
|
|
|
|
| 246 |
$(".zoom-out", dialog).addEventListener("click", () => this.zoom(1 / ZOOM_STEP))
|
| 247 |
$(".reset", dialog).addEventListener("click", () => this.resetView())
|
| 248 |
$(".zoom-in", dialog).addEventListener("click", () => this.zoom(ZOOM_STEP))
|
|
@@ -271,20 +394,25 @@ class ImageExplorer extends HTMLElement {
|
|
| 271 |
}
|
| 272 |
|
| 273 |
openImage(image) {
|
| 274 |
-
const dialog = $(".lightbox", this)
|
| 275 |
this.imageIndex = this.images.indexOf(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
$("img", dialog).src = original(image.path)
|
| 277 |
$("img", dialog).alt = image.name
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
| 279 |
}
|
| 280 |
|
| 281 |
moveImage(step) {
|
| 282 |
const count = this.images.length
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
this.imageIndex = index
|
| 286 |
-
$("img", $(".lightbox", this)).src = original(image.path)
|
| 287 |
-
$("img", $(".lightbox", this)).alt = image.name
|
| 288 |
}
|
| 289 |
|
| 290 |
resetView() {
|
|
|
|
| 9 |
const MIN_ZOOM = 1
|
| 10 |
const MAX_ZOOM = 8
|
| 11 |
const ZOOM_STEP = 1.2
|
| 12 |
+
const INFINITE = "*"
|
| 13 |
|
| 14 |
class ImageExplorer extends HTMLElement {
|
| 15 |
connectedCallback() {
|
| 16 |
this.innerHTML = `
|
| 17 |
<main>
|
| 18 |
+
<div class="date-heading">
|
| 19 |
+
<button class="date-prev" type="button" aria-label="Previous date" hidden>←</button>
|
| 20 |
+
<h1>Image folders</h1>
|
| 21 |
+
<button class="date-next" type="button" aria-label="Next date" hidden>→</button>
|
| 22 |
+
</div>
|
| 23 |
<div class="tools">
|
| 24 |
<button class="back" type="button" hidden>Folders</button>
|
| 25 |
<input type="search" placeholder="Filter folders" aria-label="Filter folders">
|
|
|
|
| 34 |
<dialog class="lightbox">
|
| 35 |
<div class="image-actions">
|
| 36 |
<button class="upscale primary" type="button">Upscale</button>
|
| 37 |
+
<button class="star expanded-star" type="button" aria-label="Star image"></button>
|
| 38 |
<span class="upscale-status" role="status" aria-live="polite"></span>
|
| 39 |
</div>
|
| 40 |
<button class="close" type="button" aria-label="Close">Close</button>
|
|
|
|
| 49 |
this.setupPreviews()
|
| 50 |
this.load()
|
| 51 |
$(".back", this).addEventListener("click", () => this.load())
|
| 52 |
+
$(".date-prev", this).addEventListener("click", () => this.navigateDate(1))
|
| 53 |
+
$(".date-next", this).addEventListener("click", () => this.navigateDate(-1))
|
| 54 |
$(".all", this).addEventListener("click", () => this.select(true))
|
| 55 |
$(".clear", this).addEventListener("click", () => this.select(false))
|
| 56 |
$(".download", this).addEventListener("click", () => this.download())
|
|
|
|
| 67 |
$(".content", this).replaceChildren()
|
| 68 |
let data = this.cache.get(folder)
|
| 69 |
if (!data) {
|
| 70 |
+
const query = folder === INFINITE
|
| 71 |
+
? { infinite: true }
|
| 72 |
+
: folder ? { folder } : {}
|
| 73 |
+
const response = await fetch(api("/api/images", query))
|
| 74 |
if (!response.ok) {
|
| 75 |
$(".status", this).textContent = "Unable to load images"
|
| 76 |
return
|
|
|
|
| 79 |
this.cache.set(folder, data)
|
| 80 |
}
|
| 81 |
this.folder = folder
|
| 82 |
+
if (!folder) this.folders = data.folders.map(item => item.name)
|
| 83 |
+
$("h1", this).textContent = folder === INFINITE
|
| 84 |
+
? "Infinite"
|
| 85 |
+
: folder || "Image folders"
|
| 86 |
$(".back", this).hidden = !folder
|
| 87 |
+
this.updateDateNav()
|
| 88 |
const search = $("input[type=search]", this)
|
| 89 |
search.value = ""
|
| 90 |
search.placeholder = `Filter ${folder ? "images" : "folders"}`
|
| 91 |
search.setAttribute("aria-label", search.placeholder)
|
| 92 |
+
if (folder === INFINITE) this.addGroups(data.groups)
|
| 93 |
+
else if (folder) this.addImages(data.images)
|
| 94 |
else this.addFolders(data.folders)
|
| 95 |
this.update()
|
| 96 |
}
|
| 97 |
|
| 98 |
+
updateDateNav() {
|
| 99 |
+
const index = this.folders?.indexOf(this.folder) ?? -1
|
| 100 |
+
const previous = $(".date-prev", this)
|
| 101 |
+
const next = $(".date-next", this)
|
| 102 |
+
previous.hidden = index < 0
|
| 103 |
+
next.hidden = index < 0
|
| 104 |
+
if (index < 0) return
|
| 105 |
+
previous.disabled = index === this.folders.length - 1
|
| 106 |
+
next.disabled = index === 0
|
| 107 |
+
previous.title = this.folders[index + 1] || ""
|
| 108 |
+
next.title = this.folders[index - 1] || ""
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
navigateDate(step) {
|
| 112 |
+
const index = this.folders.indexOf(this.folder) + step
|
| 113 |
+
if (this.folders[index]) this.load(this.folders[index])
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
setupPreviews() {
|
| 117 |
this.previewObserver = new IntersectionObserver(entries => {
|
| 118 |
for (const entry of entries) {
|
|
|
|
| 128 |
addFolders(folders) {
|
| 129 |
const grid = document.createElement("div")
|
| 130 |
grid.className = "grid folders"
|
| 131 |
+
const infinite = document.createElement("div")
|
| 132 |
+
infinite.className = "folder entry"
|
| 133 |
+
infinite.dataset.name = "infinite"
|
| 134 |
+
const infiniteOpen = document.createElement("button")
|
| 135 |
+
infiniteOpen.className = "folder-open"
|
| 136 |
+
infiniteOpen.type = "button"
|
| 137 |
+
infiniteOpen.addEventListener("click", () => this.load(INFINITE))
|
| 138 |
+
const infiniteName = document.createElement("strong")
|
| 139 |
+
infiniteName.textContent = "∞ Infinite"
|
| 140 |
+
infiniteOpen.append(infiniteName)
|
| 141 |
+
infinite.append(infiniteOpen)
|
| 142 |
+
grid.append(infinite)
|
| 143 |
for (const folder of folders) {
|
| 144 |
const card = document.createElement("div")
|
| 145 |
card.className = "folder entry"
|
|
|
|
| 166 |
$(".content", this).append(grid)
|
| 167 |
}
|
| 168 |
|
| 169 |
+
addGroups(groups) {
|
| 170 |
+
this.images = groups.flatMap(group => group.images)
|
| 171 |
+
const content = $(".content", this)
|
| 172 |
+
for (const group of groups) {
|
| 173 |
+
const section = document.createElement("section")
|
| 174 |
+
section.className = "date-group"
|
| 175 |
+
const heading = document.createElement("h2")
|
| 176 |
+
heading.textContent = group.name
|
| 177 |
+
const grid = document.createElement("div")
|
| 178 |
+
grid.className = "grid"
|
| 179 |
+
for (const image of group.images) grid.append(this.card(image))
|
| 180 |
+
section.append(heading, grid)
|
| 181 |
+
content.append(section)
|
| 182 |
+
}
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
checkbox(path) {
|
| 186 |
const input = document.createElement("input")
|
| 187 |
input.type = "checkbox"
|
|
|
|
| 205 |
picture.dataset.src = preview(image.path)
|
| 206 |
this.previewObserver.observe(picture)
|
| 207 |
open.append(picture)
|
| 208 |
+
const star = this.starButton(image, "card-star")
|
| 209 |
const info = document.createElement("label")
|
| 210 |
info.className = "image-info"
|
| 211 |
const input = this.checkbox(image.path)
|
|
|
|
| 213 |
name.className = "name"
|
| 214 |
name.textContent = image.name
|
| 215 |
info.append(input, name)
|
| 216 |
+
card.append(open, star, info)
|
| 217 |
return card
|
| 218 |
}
|
| 219 |
|
| 220 |
+
starButton(image, className) {
|
| 221 |
+
const button = document.createElement("button")
|
| 222 |
+
button.className = `star ${className}`
|
| 223 |
+
button.type = "button"
|
| 224 |
+
button.dataset.path = image.path
|
| 225 |
+
button.addEventListener("click", () => this.toggleStar(image))
|
| 226 |
+
this.renderStar(button, image.starred)
|
| 227 |
+
return button
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
renderStar(button, starred) {
|
| 231 |
+
button.textContent = starred ? "★" : "☆"
|
| 232 |
+
button.setAttribute("aria-pressed", starred)
|
| 233 |
+
button.setAttribute("aria-label", `${starred ? "Unstar" : "Star"} image`)
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
setStar(path, starred) {
|
| 237 |
+
for (const data of this.cache.values()) {
|
| 238 |
+
const images = data.images || data.groups?.flatMap(group => group.images) || []
|
| 239 |
+
for (const image of images) {
|
| 240 |
+
if (image.path === path) image.starred = starred
|
| 241 |
+
}
|
| 242 |
+
}
|
| 243 |
+
for (const button of $$(".star", this)) {
|
| 244 |
+
if (button.dataset.path === path) this.renderStar(button, starred)
|
| 245 |
+
}
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
async toggleStar(image) {
|
| 249 |
+
this.pendingStars ||= new Set()
|
| 250 |
+
if (this.pendingStars.has(image.path)) return
|
| 251 |
+
this.pendingStars.add(image.path)
|
| 252 |
+
const buttons = $$(".star", this).filter(button => (
|
| 253 |
+
button.dataset.path === image.path
|
| 254 |
+
))
|
| 255 |
+
for (const button of buttons) button.disabled = true
|
| 256 |
+
const starred = !image.starred
|
| 257 |
+
this.setStar(image.path, starred)
|
| 258 |
+
try {
|
| 259 |
+
const response = await fetch(api("/api/images/star"), {
|
| 260 |
+
method: "POST",
|
| 261 |
+
headers: { "Content-Type": "application/json" },
|
| 262 |
+
body: JSON.stringify({ path: image.path, starred }),
|
| 263 |
+
})
|
| 264 |
+
if (!response.ok) throw new Error("Unable to update star")
|
| 265 |
+
} catch (error) {
|
| 266 |
+
this.setStar(image.path, !starred)
|
| 267 |
+
$(".status", this).textContent = error.message
|
| 268 |
+
} finally {
|
| 269 |
+
this.pendingStars.delete(image.path)
|
| 270 |
+
for (const button of buttons) button.disabled = false
|
| 271 |
+
}
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
select(checked) {
|
| 275 |
for (const input of $$(".item", this)) input.checked = checked
|
| 276 |
this.update()
|
|
|
|
| 286 |
for (const entry of $$(".entry", this)) {
|
| 287 |
entry.hidden = Boolean(query && !entry.dataset.name.includes(query))
|
| 288 |
}
|
| 289 |
+
for (const group of $$(".date-group", this)) {
|
| 290 |
+
group.hidden = !$(".entry:not([hidden])", group)
|
| 291 |
+
}
|
| 292 |
}
|
| 293 |
|
| 294 |
async download() {
|
|
|
|
| 363 |
const image = $("img", viewer)
|
| 364 |
$(".close", dialog).addEventListener("click", () => dialog.close())
|
| 365 |
$(".upscale", dialog).addEventListener("click", () => this.upscale())
|
| 366 |
+
$(".expanded-star", dialog).addEventListener("click", () => {
|
| 367 |
+
this.toggleStar(this.images[this.imageIndex])
|
| 368 |
+
})
|
| 369 |
$(".zoom-out", dialog).addEventListener("click", () => this.zoom(1 / ZOOM_STEP))
|
| 370 |
$(".reset", dialog).addEventListener("click", () => this.resetView())
|
| 371 |
$(".zoom-in", dialog).addEventListener("click", () => this.zoom(ZOOM_STEP))
|
|
|
|
| 394 |
}
|
| 395 |
|
| 396 |
openImage(image) {
|
|
|
|
| 397 |
this.imageIndex = this.images.indexOf(image)
|
| 398 |
+
this.showImage(image)
|
| 399 |
+
$(".lightbox", this).showModal()
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
showImage(image) {
|
| 403 |
+
const dialog = $(".lightbox", this)
|
| 404 |
$("img", dialog).src = original(image.path)
|
| 405 |
$("img", dialog).alt = image.name
|
| 406 |
+
$(".upscale-status", dialog).textContent = ""
|
| 407 |
+
const star = $(".expanded-star", dialog)
|
| 408 |
+
star.dataset.path = image.path
|
| 409 |
+
this.renderStar(star, image.starred)
|
| 410 |
}
|
| 411 |
|
| 412 |
moveImage(step) {
|
| 413 |
const count = this.images.length
|
| 414 |
+
this.imageIndex = (this.imageIndex + step + count) % count
|
| 415 |
+
this.showImage(this.images[this.imageIndex])
|
|
|
|
|
|
|
|
|
|
| 416 |
}
|
| 417 |
|
| 418 |
resetView() {
|