feat(ui): base.html, macros (badges, lightbox), shared.js, register_shared_ui

This commit is contained in:
2026-06-27 17:11:49 +02:00
parent 4db684fbdc
commit c36d93e3c7
5 changed files with 151 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
import os
from flask import Blueprint, Flask
from jinja2 import ChoiceLoader, FileSystemLoader
TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), "templates")
STATIC_DIR = os.path.join(os.path.dirname(__file__), "static")
def register_shared_ui(app: Flask) -> None:
app.jinja_loader = ChoiceLoader([app.jinja_loader, FileSystemLoader(TEMPLATE_DIR)])
bp = Blueprint("shared_ui", __name__, static_folder=STATIC_DIR,
static_url_path="/shared-static")
app.register_blueprint(bp)
__all__ = ["TEMPLATE_DIR", "STATIC_DIR", "register_shared_ui"]
+57
View File
@@ -0,0 +1,57 @@
// Generic grid + lightbox behavior shared across photoflow apps.
// Apps spread this into their own Alpine component: { ...photoGrid(), ...appLogic }
function photoGrid() {
return {
focused: null,
lightboxOpen: false,
cards() {
return [...document.querySelectorAll('.photo-card')]
.filter(c => c.style.display !== 'none');
},
select(el) {
if (this.focused) this.focused.classList.remove('ring-4', 'ring-white', 'z-10');
this.focused = el;
if (el) {
el.classList.add('ring-4', 'ring-white', 'z-10');
el.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
if (this.lightboxOpen) this.updateLightbox();
}
},
selectFirst() {
const cards = this.cards();
if (cards.length) this.select(cards[0]);
},
navigate(dir) {
const cards = this.cards();
if (!cards.length) return;
const idx = this.focused ? cards.indexOf(this.focused) : -1;
const next = cards[Math.max(0, Math.min(cards.length - 1, idx + dir))];
if (next) this.select(next);
},
openLightbox(el) {
this.select(el);
this.lightboxOpen = true;
document.getElementById('lb').style.display = '';
this.updateLightbox();
},
closeLightbox() {
this.lightboxOpen = false;
const lb = document.getElementById('lb');
if (lb) lb.style.display = 'none';
},
updateLightbox() {
const el = this.focused;
if (!el) return;
const img = el.querySelector('img');
const lbImg = document.getElementById('lb-img');
if (img && lbImg) lbImg.src = img.src;
},
};
}
+24
View File
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html data-theme="forest" lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}photoflow{% endblock %}</title>
<link href="https://cdn.jsdelivr.net/npm/daisyui@4/dist/full.min.css" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/htmx.org@1.9.12"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js"></script>
</head>
<body class="min-h-screen bg-base-200">
<div class="navbar bg-base-100 shadow-sm sticky top-0 z-40">
<div class="navbar-start px-4 font-bold text-lg">
<a href="/">{% block navbar_title %}photoflow{% endblock %}</a>
</div>
</div>
<div class="p-4">
{% block content %}{% endblock %}
</div>
<script src="/shared-static/shared.js"></script>
{% block extra_scripts %}{% endblock %}
</body>
</html>
+30
View File
@@ -0,0 +1,30 @@
{% macro status_badge(status) %}
{% set cls = {'pending':'badge-ghost','approved':'badge-success','non_trip':'badge-neutral',
'skipped':'badge-warning','merged':'badge-info','split':'badge-info'} %}
<span class="badge badge-sm {{ cls.get(status, 'badge-ghost') }}">{{ status }}</span>
{% endmacro %}
{% macro confidence_badge(confidence) %}
{% if confidence < 0.4 %}
<span class="badge badge-sm badge-warning" title="needs your eye">low</span>
{% elif confidence < 0.75 %}
<span class="badge badge-sm badge-info">med</span>
{% else %}
<span class="badge badge-sm badge-success">high</span>
{% endif %}
{% endmacro %}
{% macro lightbox() %}
<div id="lb" class="fixed inset-0 z-50 bg-black/95 flex items-center justify-center" style="display:none">
<button class="absolute top-4 right-4 btn btn-circle btn-sm btn-ghost text-white"
@click="closeLightbox()">&#10005;</button>
<button class="absolute left-3 top-1/2 -translate-y-1/2 btn btn-circle btn-ghost text-white text-4xl"
@click="navigate(-1)">&#8249;</button>
<button class="absolute right-3 top-1/2 -translate-y-1/2 btn btn-circle btn-ghost text-white text-4xl"
@click="navigate(1)">&#8250;</button>
<div class="flex flex-col items-center gap-3 px-16 max-w-full">
<img id="lb-img" src="" class="max-h-[80vh] max-w-[88vw] object-contain rounded-lg" alt="">
<div class="text-white/40 text-xs">&larr; &rarr; navigate · Esc close</div>
</div>
</div>
{% endmacro %}
+24
View File
@@ -0,0 +1,24 @@
import os
from flask import Flask, render_template
from photoflow.ui import TEMPLATE_DIR, STATIC_DIR, register_shared_ui
def test_dirs_exist():
assert os.path.isfile(os.path.join(TEMPLATE_DIR, "base.html"))
assert os.path.isfile(os.path.join(STATIC_DIR, "shared.js"))
def test_register_serves_shared_static_and_template():
app = Flask(__name__)
register_shared_ui(app)
@app.route("/page")
def page():
return render_template("base.html")
client = app.test_client()
r = client.get("/page")
assert r.status_code == 200
assert b"/shared-static/shared.js" in r.data
js = client.get("/shared-static/shared.js")
assert js.status_code == 200 and b"photoGrid" in js.data