commit 2a5144bc839132c38a6fc189612ae7c3a6d8cc77
parent e315e63e798cbd8de4231ac350b165088a985a90
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Mon, 14 Oct 2024 11:16:15 -0700
Use pointmaps endpoint to validate pointmap types
The pointmaps endpoint was recently added to the API and it returns
a list of valid pointmap types. Now we just use that endpoint to
verify valid types instead of hardcoding them in here.
This also brought about some changes to the backend and editor
initialization process: Both set the initialization field to a
promise that resolves only when they're fully initialized. This
prevents race conditions that would otherwise occur. Furthermore
furniture parameters are now fetched in the backend instead of
(strangely) from the main.js file.
Diffstat:
3 files changed, 34 insertions(+), 14 deletions(-)
diff --git a/files/floorplans/floorplan/backend.js b/files/floorplans/floorplan/backend.js
@@ -309,6 +309,7 @@ class BackendHistory {
export class FloorplanBackend {
constructor(floorplan, options) {
+ let backend = this
if (!options) {
options = {}
}
@@ -322,6 +323,18 @@ export class FloorplanBackend {
this.callbacks = options.callbacks
}
+ this.params = {}
+ this.initialized = api.fetch("GET", "pointmaps")
+ .then(function(resp) {
+ backend.params.pointmaps = resp
+ })
+ this.initialized = Promise.all([this.initialized,
+ api.fetch("GET", "furniture")
+ .then(function(furniture) {
+ backend.params.furniture = furniture
+ })
+ ])
+
// Cache for server (both from and to)
this.cache = {
// { pointId: { x: Number, y: Number } }
@@ -494,7 +507,13 @@ export class FloorplanBackend {
type: {
required: true,
validate: function(type) {
- return type === "wall" || type === "door" || type === "window" || type === "railing"
+ let types = backend.params.pointmaps.types
+ for (let i = 0; i < types.length; ++i) {
+ if (type === types[i]) {
+ return true
+ }
+ }
+ return false
}
},
a: {
diff --git a/files/floorplans/floorplan/editor.js b/files/floorplans/floorplan/editor.js
@@ -374,6 +374,8 @@ export class FloorplanEditor {
this.draw.on("select", function(event) {
editor.selection = event.detail.selection
})
+
+ this.initialized = this.backend.initialized
}
useUnits(system) {
@@ -823,10 +825,10 @@ export class FloorplanEditor {
}
varietyFrom(params) {
- if (this.furniture_types[params.type] == null) {
+ if (this.backend.params.furniture[params.type] == null) {
throw new Error(params.type + ": Invalid furniture type")
}
- let vars = this.furniture_types[params.type].varieties
+ let vars = this.backend.furniture[params.type].varieties
for (let v in vars) {
if (params.width == vars[v].width && params.depth == vars[v].depth) {
return v
diff --git a/files/floorplans/floorplan/main.js b/files/floorplans/floorplan/main.js
@@ -49,15 +49,14 @@ let State = {
lastClick: null
}
+const debug = (new URLSearchParams(new URL(document.URL).search)).get("debug") != undefined
+
// turn off bubbling
const escapeEvent = new Event("escape")
etc.handle_wrap(init)
function init() {
- // Just to get stuff out of the way for now
- let debug = (new URLSearchParams(new URL(document.URL).search)).get("debug") != undefined
-
let floorplan_id = (new URLSearchParams(new URL(document.URL).search)).get("id")
if (!floorplan_id) {
document.location.href = "/floorplans"
@@ -116,15 +115,15 @@ function init() {
}
}
})
+
+ editor.initialized
+ .then(function() { run(editor) })
+ .catch(etc.error)
+}
+
+function run(editor) {
editor.useUnits("imperial")
editor.draw.hide()
- api.fetch("GET", "furniture")
- .then(function(furniture) {
- editor.furniture_types = furniture
- })
- .catch(function(err) {
- etc.error("That's unexpected. Unable to get furniture definitions")
- })
for (let mode in modes) {
editor.addMode(mode, modes[mode])
@@ -317,7 +316,7 @@ function selectHandler(event, editor, state) {
}
}
c.appendChild(
- selector({ wall: true, door: true }, changeTypes, { current, text: "Type:" })
+ selector(editor.backend.params.pointmaps.types, changeTypes, { current, text: "Type:" })
)
}
}