From 304b56d9c6cd48fb8fc8438e145da23410527ab1 Mon Sep 17 00:00:00 2001 From: Candid Dauth Date: Tue, 14 Nov 2023 21:55:54 +0100 Subject: [PATCH] Fix handling of marker/line default values --- frontend/src/lib/components/ui/route-mode.vue | 3 ++- server/src/database/line.ts | 19 +++++++---------- server/src/database/marker.ts | 21 +++++++------------ server/src/database/migrations.ts | 4 ++-- types/src/line.ts | 6 +++--- types/src/marker.ts | 8 +++---- types/src/type.ts | 2 +- 7 files changed, 28 insertions(+), 35 deletions(-) diff --git a/frontend/src/lib/components/ui/route-mode.vue b/frontend/src/lib/components/ui/route-mode.vue index 50b484f9..5af217c3 100644 --- a/frontend/src/lib/components/ui/route-mode.vue +++ b/frontend/src/lib/components/ui/route-mode.vue @@ -173,7 +173,8 @@ class="btn-check" :id="`${id}-mode-${mode}`" :name="`${id}-mode`" - :checked="decodedMode.mode == mode" + v-model="decodedMode.mode" + :value="mode" :tabindex="tabindex != null ? tabindex + idx : undefined" v-tooltip:[tooltipPlacement]="`Go ${constants.modeTitle[mode]}`" :disabled="disabled" diff --git a/server/src/database/line.ts b/server/src/database/line.ts index 48a860a5..696f8cdc 100644 --- a/server/src/database/line.ts +++ b/server/src/database/line.ts @@ -192,19 +192,16 @@ export default class DatabaseLines { async createLine(padId: PadId, data: Line, trackPointsFromRoute?: Route): Promise { const type = await this._db.types.getType(padId, data.typeId); - if(type.defaultColour && !data.colour) - data.colour = type.defaultColour; - if(type.defaultWidth && !data.width) - data.width = type.defaultWidth; - if(type.defaultMode && !data.mode) - data.mode = type.defaultMode; - - const { trackPoints, ...routeInfo } = await calculateRouteForLine(data, trackPointsFromRoute); + const resolvedData = { + ...data, + colour: data.colour ?? type.defaultColour, + width: data.width ?? type.defaultWidth, + mode: data.mode ?? type.defaultMode + }; - const dataCopy = { ...data, ...routeInfo }; - delete dataCopy.trackPoints; // They came if mode is track + const { trackPoints, ...routeInfo } = await calculateRouteForLine(resolvedData, trackPointsFromRoute); - const createdLine = await this._db.helpers._createPadObject("Line", padId, dataCopy); + const createdLine = await this._db.helpers._createPadObject("Line", padId, omit({ ...resolvedData, ...routeInfo }, "trackPoints" /* Part of data if mode is track */)); await this._db.helpers._updateObjectStyles(createdLine); // We have to emit this before calling _setLinePoints so that this event is sent to the client first diff --git a/server/src/database/marker.ts b/server/src/database/marker.ts index 25ce0189..76e3056e 100644 --- a/server/src/database/marker.ts +++ b/server/src/database/marker.ts @@ -82,20 +82,15 @@ export default class DatabaseMarkers { async createMarker(padId: PadId, data: Marker): Promise { const type = await this._db.types.getType(padId, data.typeId); - const elevation = await getElevationForPoint(data); - if(type.defaultColour) - data.colour = type.defaultColour; - if(type.defaultSize) - data.size = type.defaultSize; - if(type.defaultSymbol) - data.symbol = type.defaultSymbol; - if(type.defaultShape) - data.shape = type.defaultShape; - - data.ele = elevation ?? null; - - const result = await this._db.helpers._createPadObject("Marker", padId, data); + const result = await this._db.helpers._createPadObject("Marker", padId, { + ...data, + colour: data.colour ?? type.defaultColour, + size: data.size ?? type.defaultSize, + symbol: data.symbol ?? type.defaultSymbol, + shape: data.shape ?? type.defaultShape, + ele: data.ele ?? await getElevationForPoint(data) + }); await this._db.helpers._updateObjectStyles(result); diff --git a/server/src/database/migrations.ts b/server/src/database/migrations.ts index 0dc81ff2..bc524ba7 100644 --- a/server/src/database/migrations.ts +++ b/server/src/database/migrations.ts @@ -175,8 +175,8 @@ export default class DatabaseMigrations { // Forbid null defaultSize if (typesAttributes.defaultSize.allowNull) { - // 35 is the old default size, now it is 40 - await this._db.types.TypeModel.update({ defaultSize: 35 }, { where: { defaultSize: null as any } }); + // 25 is the old default size, now it is 30 + await this._db.types.TypeModel.update({ defaultSize: 25 }, { where: { defaultSize: null as any } }); await queryInterface.changeColumn("Types", "defaultSize", this._db.types.TypeModel.getAttributes().defaultSize); } diff --git a/types/src/line.ts b/types/src/line.ts index 3fbd8246..3c3d5b1f 100644 --- a/types/src/line.ts +++ b/types/src/line.ts @@ -18,9 +18,9 @@ export const lineValidator = cruValidator({ routePoints: optionalUpdate(z.array(pointValidator).min(2)), typeId: optionalUpdate(idValidator), name: optionalCreate(z.string().trim(), ""), - mode: optionalCreate(routeModeValidator, ""), - colour: optionalCreate(colourValidator, "0000ff"), - width: optionalCreate(z.number(), 4), + mode: optionalCreate(routeModeValidator), // defaults to type.defaultMode + colour: optionalCreate(colourValidator), // defaults to type.defaultColour + width: optionalCreate(z.number()), // defaults to type.defaultWidth data: optionalCreate(z.record(z.string())), extraInfo: optionalCreate(extraInfoValidator.or(z.null()), null), diff --git a/types/src/marker.ts b/types/src/marker.ts index 70841eb6..4ad0b0d1 100644 --- a/types/src/marker.ts +++ b/types/src/marker.ts @@ -8,10 +8,10 @@ export const markerValidator = cruValidator({ ...mapValues(pointValidator.shape, optionalUpdate), typeId: optionalUpdate(idValidator), name: optionalCreate(z.string().trim(), ""), - symbol: optionalCreate(symbolValidator, ""), - shape: optionalCreate(shapeValidator, ""), - colour: optionalCreate(colourValidator, "ff0000"), - size: optionalCreate(sizeValidator, 30), + symbol: optionalCreate(symbolValidator), // defaults to type.defaultSymbol + shape: optionalCreate(shapeValidator), // defaults to type.defaultShape + colour: optionalCreate(colourValidator), // defaults to type.defaultColour + size: optionalCreate(sizeValidator), // defaults to type.defaultSize data: optionalCreate(z.record(z.string())), ele: optionalCreate(z.number().or(z.null()), null) }); diff --git a/types/src/type.ts b/types/src/type.ts index 8c69301b..0c14c539 100644 --- a/types/src/type.ts +++ b/types/src/type.ts @@ -52,7 +52,7 @@ const rawTypeValidator = cruValidator({ defaultColour: optionalCreate(colourValidator), // Default value is applied below colourFixed: optionalCreate(z.boolean(), false), - defaultSize: optionalCreate(sizeValidator, 40), + defaultSize: optionalCreate(sizeValidator, 30), sizeFixed: optionalCreate(z.boolean(), false), defaultSymbol: optionalCreate(symbolValidator, ""), symbolFixed: optionalCreate(z.boolean(), false),