commit 7c594a6798cf848220a830268a21d7d345a7fd43
parent 5d7a3c1d24b7b8478afbbf779a8bd44b2ba260c5
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Mon, 9 Sep 2024 06:52:58 -0700
Allow updating furniture values
Furniture and furniture maps can now be updated, and every parameter
is optional so long as the required ones are already set.
Also add angle input to the furniture menu.
Diffstat:
3 files changed, 76 insertions(+), 52 deletions(-)
diff --git a/files/floorplans/floorplan/backend.js b/files/floorplans/floorplan/backend.js
@@ -515,38 +515,40 @@ export class FloorplanBackend {
}
}
- addFurniture(type, options) {
- options = options ?? {}
- if (options.width == null || options.depth == null) {
- throw new Error("These 'options' are not yet optional")
- }
+ addFurniture(params, id) {
+ params = params ?? {}
- if (typeof type !== "string") {
- throw new Error(type + ": Expected string type")
- }
+ let f = id ? this.reqObj(id) : {}
- let f = {
- type: type,
- }
- if (options.width != undefined) {
- f.width = Math.round(options.width)
+ if (params.width != undefined) {
+ f.width = Math.round(params.width)
if (f.width <= 0) {
- throw new Error(options.width + ": rounded width must be greater than zero")
+ throw new Error(params.width + ": rounded width must be greater than zero")
}
}
- if (options.depth != undefined) {
- f.depth = Math.round(options.depth)
+ if (params.depth != undefined) {
+ f.depth = Math.round(params.depth)
if (f.depth <= 0) {
- throw new Error(options.depth + ": rounded depth must be greater than zero")
+ throw new Error(params.depth + ": rounded depth must be greater than zero")
+ }
+ }
+ if (params.name != undefined) {
+ if (typeof params.name !== "string") {
+ throw new Error(params.name + ": Expected string name")
}
+ f.name = params.name
}
- if (options.name != undefined) {
- if (typeof options.name !== "string") {
- throw new Error(options.name + ": Expected string name")
+ if (params.type != undefined) {
+ if (typeof params.type !== "string") {
+ throw new Error("Invalid type")
}
- f.name = options.name
+ f.type = params.type
}
- return this.addData("furniture", f)
+
+ if (f.width == null || f.depth == null || f.type == null) {
+ throw new Error("Missing required parameters")
+ }
+ return this.addData(id ?? "furniture", f)
}
removeFurniture(id, options) {
@@ -556,26 +558,41 @@ export class FloorplanBackend {
this.removeData(id, options)
}
- mapFurniture(def, x, y, options) {
- options = options ?? {}
+ mapFurniture(params, id) {
+ let fm = id ? this.reqObj(id) : {}
- let fm = {
- furniture_id: def,
- x: Math.round(x),
- y: Math.round(y)
+ if (params.furniture_id != undefined) {
+ if (this.obj(params.furniture_id) == undefined) {
+ throw new Error("invalid furniture id for furniture map")
+ }
+ fm.furniture_id = params.furniture_id
+ } else if (fm.furniture_id == undefined) {
+ throw new Error("Missing furniture id")
+ }
+ if (params.x != undefined) {
+ if (typeof params.x !== "number") {
+ throw new Error("Invalid x coordinate")
+ }
+ fm.x = params.x
+ }
+ if (params.y != undefined) {
+ if (typeof params.x !== "number") {
+ throw new Error("Invalid y coordinate")
+ }
+ fm.y = params.y
}
- if (options.angle != undefined) {
- if (typeof options.angle !== "number" || options.angle < 0 ||
- options.angle >= 360) {
- throw new Error(options.angle + ": Invalid angle")
+ if (params.angle != undefined) {
+ if (typeof params.angle !== "number" || params.angle < 0 ||
+ params.angle >= 360) {
+ throw new Error(params.angle + ": Invalid angle")
}
- fm.angle = options.angle
+ fm.angle = params.angle
}
- if (options.layout != undefined) {
- if (typeof options.layout !== "string") {
- throw new Error(options.layout + ": Invalid layout")
+ if (params.layout != undefined) {
+ if (typeof params.layout !== "string") {
+ throw new Error(params.layout + ": Invalid layout")
}
- fm.layout = options.layout
+ fm.layout = params.layout
} else {
// for now, this should be handled by the server later
fm.layout = "1"
@@ -587,14 +604,14 @@ export class FloorplanBackend {
this.removeData(id, options)
}
- addMappedFurniture(type, x, y, options) {
- let id = this.addFurniture(type, options)
- return this.mapFurniture(id, x, y, options)
+ addMappedFurniture(params, id) {
+ params.furniture_id = this.addFurniture(params, id ? this.reqObj(id).furniture_id : null)
+ return this.mapFurniture(params)
}
reqObj(id) {
- let obj = this.data(id)
- if (!obj) {
+ let obj = this.obj(id)
+ if (obj == null) {
throw new Error(id + " doesn't exist")
}
return obj
diff --git a/files/floorplans/floorplan/editor.js b/files/floorplans/floorplan/editor.js
@@ -538,17 +538,17 @@ export class FloorplanEditor {
return id
}
- addFurniture(type, options) {
- this.backend.addFurniture(type, options)
+ addFurniture(params, id) {
+ this.backend.addFurniture(params, id)
}
- mapFurniture(def, x, y, options) {
- this.backend.mapFurniture(def, x, y, options)
+ mapFurniture(params, id) {
+ this.backend.mapFurniture(params, id)
this.updateDisplay()
}
- addMappedFurniture(type, x, y, options) {
- this.backend.addMappedFurniture(type, x, y, options)
+ addMappedFurniture(params, id) {
+ this.backend.addMappedFurniture(params, id)
this.updateDisplay()
}
diff --git a/files/floorplans/floorplan/main.js b/files/floorplans/floorplan/main.js
@@ -649,6 +649,7 @@ function furnitureMenu(editor, p) {
menuItem("variety", "Variety", { enum: editor.furniture_types[defaultType].varieties }),
menuItem("width", "Width", { attributes: { required: true } }),
menuItem("depth", "Depth", { attributes: { required: true } }),
+ menuItem("angle", "Angle", { attributes: { min: 0, max: 359, type: "number", value: 0, required: true } }),
menuItem("add", null, { attributes: { value: "Add", type: "Submit" } })
]
let keys = {}
@@ -695,15 +696,21 @@ function furnitureMenu(editor, p) {
if (name.length === 0) {
name = null
}
- editor.addMappedFurniture(items[keys.type].input.value, p.x, p.y, {
- width: items[keys.width].input.value,
- depth: items[keys.depth].input.value,
+ let params = {
+ type: items[keys.type].input.value,
+ x: p.x,
+ y: p.y,
+ width: Number(items[keys.width].input.value),
+ depth: Number(items[keys.depth].input.value),
+ angle: Number(items[keys.angle].input.value),
name
- })
+ }
+ editor.addMappedFurniture(params)
editor.finishAction()
}
catch(err) {
etc.error(err, menu)
+ throw err
}
menu.remove()
})