commit 6a2304ba960a5321d93a1f33deaea30a661d01a0
parent 5342bf77764266182a8269b77b75a1e0085a985f
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Fri, 9 Aug 2024 10:59:40 -0700
Update ui.button and ui.toggle
They now allow you to set any attribute aswell as add a handler for
any event (although as of yet, at most one handler)).
Diffstat:
3 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/files/floorplans/main.js b/files/floorplans/main.js
@@ -198,9 +198,9 @@ function create_floorplan(floorplan) {
{ button: ui.button("Save", "Save floorplan", "save"), func: commit_editable_floorplan_func(root, floorplan) },
)
)
- aside.append(ui.button("Delete", "Delete floorplan", "trash", delete_floorplan_func(root, floorplan)))
+ aside.append(ui.button("Delete", "Delete floorplan", "trash", { handlers: { click: delete_floorplan_func(root, floorplan) } }))
} else {
- aside.append(ui.button("Create", "Create floorplan", "create", editable_floorplan_create_func(root)))
+ aside.append(ui.button("Create", "Create floorplan", "create", { handlers: { click: editable_floorplan_create_func(root) } }))
}
root.append(aside)
@@ -237,7 +237,7 @@ var create_field = {
let heading = document.createElement("h2")
heading.setAttribute("class", floorplan_info_class("name"))
let link = document.createElement("a")
- link.href = "floorplans/" + localStorage.getItem("username") + "/" + text
+ link.href = "./floorplan/?name=" + etc.url_literal(text)
link.appendChild(document.createTextNode(text))
heading.append(link)
return heading
diff --git a/files/lib/etc.js b/files/lib/etc.js
@@ -63,7 +63,7 @@ export function error(message, on) {
let msg = document.createElement("p")
msg.appendChild(document.createTextNode(message))
err_elem.append(msg)
- err_elem.append(ui.button("Dismiss", "Dismiss error", "close", function() { err_elem.remove() }))
+ err_elem.append(ui.button("Dismiss", "Dismiss error", "close", { handlers: { click: { function() { err_elem.remove() } } } }))
on.before(err_elem)
}
diff --git a/files/lib/ui.js b/files/lib/ui.js
@@ -1,4 +1,4 @@
-export function input(name, memo, attributes) {
+export function input(name, memo, options) {
if (!name) {
throw new Error("No name provided")
}
@@ -7,28 +7,38 @@ export function input(name, memo, attributes) {
e.name = name
e.placeholder = name
e.setAttribute("title", memo)
- for (let i in attributes) {
- console.log(i,attributes[i])
- e.setAttribute(i, attributes[i])
+
+ if (!options) {
+ options = {}
}
+ if (options.attributes) {
+ for (let i in options.attributes) {
+ console.debug("Input", name, i, options.attributes[i])
+ e.setAttribute(i, options.attributes[i])
+ }
+ }
+ if (options.handlers) {
+ for (let i in options.handlers) {
+ e.addEventListener(i, options.handlers[i], false)
+ }
+ }
+
return e
}
-export function button(name, memo, icon, func, options) {
- let button_options = {
+export function button(name, memo, icon, options) {
+ let button = input(name, memo, options)
+ let attrs = {
alt: name,
type: "image",
class: "icon",
- src: "/icons/" + icon + "-outline.svg",
+ src: "/icons/" + icon + "-outline.svg"
}
- let button = input(name, memo, options)
- for (let i in button_options) {
- button.setAttribute(i, button_options[i])
+ for (let i in attrs) {
+ console.debug("Button", name, i, attrs[i])
+ button.setAttribute(i, attrs[i])
}
- if (func) {
- button.addEventListener("click", func, false)
- }
return button
}