improve Door integer coercion, remove PreviewDoor.editable

This commit is contained in:
Jörn-Michael Miehe 2023-09-07 00:34:42 +00:00
parent 3a4d2c1598
commit 560beb0a44
2 changed files with 12 additions and 15 deletions

View file

@ -26,16 +26,11 @@ import SVGRect from "../rects/SVGRect.vue";
}, },
props: { props: {
door: Door, door: Door,
editable: {
type: Boolean,
default: true,
},
}, },
emits: ["update:door"], emits: ["update:door"],
}) })
export default class extends Vue { export default class extends Vue {
public door!: Door; public door!: Door;
private editable!: boolean;
public day_str = ""; public day_str = "";
public editing = false; public editing = false;
@ -74,14 +69,9 @@ export default class extends Vue {
return result; return result;
} }
private get day_num(): number {
const result = Number(this.day_str);
return isNaN(result) ? -1 : result;
}
private toggle_editing() { private toggle_editing() {
this.day_str = String(this.door.day); this.day_str = String(this.door.day);
this.editing = this.editable && !this.editing; this.editing = !this.editing;
} }
public on_click(event: MouseEvent) { public on_click(event: MouseEvent) {
@ -100,7 +90,7 @@ export default class extends Vue {
}; };
day_input_focus(); day_input_focus();
} else { } else {
this.door.day = this.day_num; this.door.day = this.day_str;
} }
this.toggle_editing(); this.toggle_editing();
@ -112,7 +102,7 @@ export default class extends Vue {
} }
if (event.key === "Enter") { if (event.key === "Enter") {
this.door.day = this.day_num; this.door.day = this.day_str;
this.toggle_editing(); this.toggle_editing();
} else if (event.key === "Delete") { } else if (event.key === "Delete") {
this.door.day = -1; this.door.day = -1;

View file

@ -13,7 +13,14 @@ export class Door {
return this._day return this._day
} }
public set day(day: number) { public set day(day: unknown) {
this._day = Math.max(Math.floor(day), -1); // integer coercion
const result = Number(day);
if (isNaN(result)) {
this._day = -1;
} else {
this._day = Math.max(Math.floor(result), -1);
}
} }
} }