commit 6f1edbe2dc55750fd2b4e5b88fd0857f465f77f5
parent f697d3fab8a5cb1b0d29362a740ac2f708491ea3
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Sun, 13 Oct 2024 19:44:30 -0700
Add floorplan cloning button
Diffstat:
2 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/files/floorplans/main.js b/files/floorplans/main.js
@@ -211,7 +211,7 @@ function create_floorplan_item(floorplan) {
function create_floorplan(floorplan) {
let root = document.createElement("div")
- root.setAttribute("class", "floorplan")
+ root.classList.add("class", "floorplan")
let aside = document.createElement("aside")
@@ -222,8 +222,10 @@ function create_floorplan(floorplan) {
{ button: ui.button("Save", "Save floorplan", "save"), func: commit_editable_floorplan_func(root, floorplan) },
)
)
+ aside.append(ui.button("Copy", "Copy floorplan", "copy", { handlers: { click: function() { copy_floorplan(floorplan) } } }))
aside.append(ui.button("Delete", "Delete floorplan", "trash", { handlers: { click: ask_delete_floorplan_func(root, floorplan) } }))
} else {
+ root.id = "adder"
aside.append(ui.button("Create", "Create floorplan", "create", { handlers: { click: editable_floorplan_create_func(root) } }))
}
@@ -294,3 +296,46 @@ function show_floorplans(floorplans) {
list.append(create_floorplan_item(floorplans[i]))
}
}
+
+function insertFloorplan(floorplan) {
+ let e = create_floorplan_item(floorplan)
+
+ let adder = document.getElementById("adder")
+ if (adder) {
+ adder.parentElement.after(e)
+ } else {
+ let list = document.getElementById("floorplans")
+ list.prepend(create_floorplan(floorplan))
+ }
+}
+
+
+function copy_floorplan(floorplan, name, depth) {
+ if (!name) {
+ name = floorplan.name + " (Copy)"
+ }
+ api.fetch("GET", `floorplans/${floorplan.user}/${floorplan.id}/data`)
+ .then(function(data) {
+ let f = structuredClone(floorplan)
+ f.name = name
+ return api.fetch("POST", "floorplans/:user", f)
+ .then(function(floorplan) {
+ insertFloorplan(floorplan)
+ return api.fetch("PUT", `floorplans/${floorplan.user}/${floorplan.id}/data`, data)
+ .catch(function(err) {
+ api.fetch("DELETE", `floorplans/:user/${floorplan.id}`)
+ throw err
+ })
+ })
+ .catch(function(err) {
+ depth = depth ?? 0
+ if (depth < 10 && err.message.indexOf('violates unique constraint "id"')) {
+ return copy_floorplan(floorplan, name + " (Copy)", depth + 1)
+ } else {
+ etc.error(err)
+ throw err
+ }
+ })
+ })
+
+}
diff --git a/files/icons/copy-outline.svg b/files/icons/copy-outline.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 512 512"><rect x="128" y="128" width="336" height="336" rx="57" ry="57" style="fill:none;stroke:#000;stroke-linejoin:round;stroke-width:32px"/><path d="M383.5,128l.5-24a56.16,56.16,0,0,0-56-56H112a64.19,64.19,0,0,0-64,64V328a56.16,56.16,0,0,0,56,56h24" style="fill:none;stroke:#000;stroke-linecap:round;stroke-linejoin:round;stroke-width:32px"/></svg>
+\ No newline at end of file