Pick a DB file and store its name in session

This commit is contained in:
Brandon Dyck 2024-07-29 14:44:06 -06:00
parent 52ca219659
commit b80242b74b
10 changed files with 121 additions and 6 deletions

View File

@ -1,3 +1,4 @@
Use pattern matching (https://github.com/silentbicycle/tamale) for parsed sexps
Write schema management package
Unceremoniously dump a rudimentary schema management UI on index
Set up page routes

View File

@ -8,9 +8,45 @@ db:exec("PRAGMA foreign_keys")
-- migrate.migrate(db)
fm = require "fullmoon"
fm.setTemplate({"/tmpl/", tmpl = "fmt"})
fm.setRoute("/hello/:name", function(r)
return fm.serveContent("hello", {name = r.params.name})
end)
fm.setRoute("/", fm.serveContent("opendb"))
fm.run()
fm.sessionOptions.secret = false
fm.setTemplate({ "/tmpl/", tmpl = "fmt" })
fm.setRoute("/static/*", fm.servePath)
fm.setRoute(fm.POST "/open", function(r)
local filename = r.params.filename
if filename == "" then filename = nil end
r.session.filename = filename
return false
end)
fm.setRoute("/open", function(r)
local files = {}
local filename = r.session.filename or ""
return fm.serveContent("opendb", { filename = filename, dir = files })
end)
fm.setRoute("/dirlist", function(r)
local function isSpecial(name)
return name == "." or name == ".." or string.sub(name, 1, 1) == "$"
end
local dirpath = r.params.dirpath
local files, directories = {}, {}
for name, kind in assert(unix.opendir(dirpath)) do
if not isSpecial(name) then
local file = { name = name, path = path.join(dirpath, name) }
if kind == unix.DT_REG then
table.insert(files, file)
elseif kind == unix.DT_DIR then
table.insert(directories, file)
end
end
end
local function compareName(f1, f2)
return f1.name < f2.name
end
table.sort(directories, compareName)
table.sort(files, compareName)
return fm.serveContent("fragment/dirlist", { files = files, directories = directories })
end)
fm.setRoute("/", fm.serveContent "home")
fm.run()

View File

@ -0,0 +1,26 @@
.filepicker {
details:has(input[type=radio]:checked)>summary,
label:has(input[type=radio]:checked) {
background-color: lightblue;
}
.directories {
margin-left: 0.7em;
}
.files>li {
list-style-type: none;
}
.directories+.files {
margin-top: 0;
}
.directories:has(+ .files) {
margin-bottom: 0;
}
max-height: 30em;
overflow-y: scroll;
}

View File

@ -0,0 +1,5 @@
body {
width: 60em;
margin-left: auto;
margin-right: auto;
}

1
assets/static/js/_hyperscript.min.js vendored Normal file

File diff suppressed because one or more lines are too long

1
assets/static/js/htmx.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,23 @@
{% if #directories ~= 0 then %}
<ul class="directories">
{% for _, dir in ipairs(directories) do %}
<details
hx-trigger="toggle once"
hx-get="/dirlist?dirpath={%& dir.path %}"
hx-target="find .details-children">
<summary>{%& dir.name %}</summary>
<div class="details-children"></div>
</details>
{% end %}
</ul>
{% end %}
{% if #files ~= 0 then %}
<ul class="files">
{% for _, file in ipairs(files) do %}
<li><label>
<input type="radio" name="filename-option" value="{%& file.path %}">
{%& file.name %}
</label></li>
{% end %}
</ul>
{% end %}

4
assets/tmpl/home.tmpl Normal file
View File

@ -0,0 +1,4 @@
{% function block.pagebody() %}
<a href="/open">Open…</a>
{% end %}
{% render('layout') %}

View File

@ -1,9 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<script src="/static/js/htmx.min.js"></script>
<script src="/static/js/_hyperscript.min.js"></script>
<title>Everything is Peachy</title>
<link rel="stylesheet" href="/static/css/site.css">
{% if block.styles then for _, style in ipairs(block.styles) do %}
<link rel="stylesheet" href="{%& style %}">
{% end end %}
</head>
<body>
<h1>Peachy</h1>
{% function block.pagebody() %}
<h1>It works!</h1>
<p>But something seems to be missing…</p>

View File

@ -1,4 +1,15 @@
{% block.styles = { "/static/css/filepicker.css" } %}
{% function block.pagebody() %}
<p>Please select a database file to load.</p>
<form action="/open" method=POST>
<input id="filename-input" name="filename" value="{%& filename %}">
<input type="submit">
</form>
<div
class="filepicker"
hx-get="/dirlist?dirpath=/"
hx-trigger="load"
_="on change(target)[target.type=='radio'] set #filename-input.value to target.value">
</div>
{% end %}
{% render('layout') %}