import m, {MithrilComponent} from '@garage44/mithril' import {Badge, Button, Card, Field, FieldNumber, FieldSelect, FieldText, uid} from '@garage44/mithril/components' import {required, validation, validations} from '@garage44/mithril/validation' import type {BuildingOverride} from '@/types' import {$s} from '@/app' import {closeContext, contextShows, openContext} from '@/lib/layout' import { deleteOverride, loadOverrides, pandOf, requestedPand, saveOverride, uploadModel, validPand, } from '@/lib/overrides' import {bind, local} from '@/lib/reactive' const COLLISION: {value: BuildingOverride['collision']; label: string}[] = [ {label: 'Every part (each one a solid block)', value: 'parts'}, {label: 'Only parts named wall, column or pillar', value: 'walls'}, {label: 'Nothing — walk through it', value: 'none'}, ] const INTERIOR: {value: BuildingOverride['interior']; label: string}[] = [ {label: 'None (the model is the inside)', value: 'none'}, {label: "The building's rooms and storeys", value: 'design'}, ] /** The context panel's key for one building's override form. */ const editKey = (pand: string) => `override:${pand}` interface UploadButtonAttrs { pand: string label: string disabled?: boolean id?: string variant?: 'primary' | 'secondary' } /** * A model file picker that uploads as soon as a file is chosen. * * The label is what is pressed, drawn as a library button; the input stays in * the page, visually hidden rather than `display: none`, so it can still take * keyboard focus and the label shows the focus ring for it. */ class UploadButton extends MithrilComponent { s = local({busy: false}) view(vnode: m.Vnode) { const {pand, label, disabled, id, variant} = vnode.attrs const s = this.s const off = s.busy || !!disabled return ( ) } } /** * The override being edited in the context panel: a draft of the stored one, so * "Undo changes" has something to go back to and Save knows what changed. */ interface Draft { busy: boolean collision: BuildingOverride['collision'] confirming: boolean interior: BuildingOverride['interior'] name: string pand: string placement: BuildingOverride['placement'] } interface NewOverrideAttrs { onClose: () => void } class NewOverride extends MithrilComponent { // Arriving from the viewer's building card fills in that building. s = local({text: requestedPand()}) v = validations({ text: validation(bind(this.s, 'text'), [ required, (text: string) => (validPand(pandOf(text)) ? null : 'That is not a BAG building number'), ]), }) id = uid('override-model') view(vnode: m.Vnode) { const s = this.s const pand = pandOf(s.text) const exists = ($s.overrides ?? []).some((o) => o.pand === pand) return ( Cancel } title="Replace a building" >
) } } /** * The overrides page (`docs/notes/building-models.md`): buildings whose * surveyed shell is replaced by a model. Every save republishes the one cell the * building is in; viewers pick it up within half a minute. * * A row opens its override's form in the context panel; the draft lives here, * on the page, because the panel is drawn by the frame and outlives no row of * its own. */ export class Overrides extends MithrilComponent { // Arriving from the viewer's building card opens the form on that building. s = local({adding: requestedPand() !== ''}) draft: Draft = local({ busy: false, collision: 'parts', confirming: false, interior: 'none', name: '', pand: '', placement: {base_m: 0, heading_deg: 0, lat: null, lng: null, scale: 1}, }) /** The `revision` the draft was last reset from. */ private seenRevision = 0 /** One placement number as a field model; empty falls back to `fallback`. */ private placementModel( key: K, fallback: (v: number | null) => BuildingOverride['placement'][K], ) { const draft = this.draft return { get value(): number | null { return draft.placement[key] as number | null }, set value(v: number | null) { draft.placement = {...draft.placement, [key]: fallback(v)} }, } } heading = this.placementModel('heading_deg', (v) => v ?? 0) scale = this.placementModel('scale', (v) => v ?? 1) base = this.placementModel('base_m', (v) => v ?? 0) lat = this.placementModel('lat', (v) => v) lng = this.placementModel('lng', (v) => v) v = validations({ name: validation(bind(this.draft, 'name'), [required]), // Both or neither: a centre with only a latitude is not a place. pin: validation(this.lng, [ () => (this.draft.placement.lat === null) !== (this.draft.placement.lng === null) ? 'Give both a latitude and a longitude, or leave both empty.' : null, ]), scale: validation(this.scale, [ (v: number | null) => (v !== null && Number.isFinite(v) && v > 0 ? null : 'The scale must be more than 0'), ]), }) oninit() { void loadOverrides().then(() => { // A building that already has an override: go to its form instead. const pand = requestedPand() const o = ($s.overrides ?? []).find((x) => x.pand === pand) if (o) { this.s.adding = false this.open(o) document.getElementById(`override-${pand}`)?.scrollIntoView({block: 'start'}) } }) } // A save or an upload comes back as a new revision; show what was stored. onupdate() { const draft = this.draft if (!draft.pand) { return } const o = ($s.overrides ?? []).find((x) => x.pand === draft.pand) if (o && o.revision !== this.seenRevision) { this.reset(o) } } onremove() { if (this.draft.pand) { closeContext(editKey(this.draft.pand)) } } reset(o: BuildingOverride) { const draft = this.draft this.seenRevision = o.revision draft.pand = o.pand draft.name = o.name draft.collision = o.collision draft.interior = o.interior draft.placement = {...o.placement} this.v.$reset() } open(o: BuildingOverride) { const key = editKey(o.pand) if (contextShows(key)) { closeContext(key) return } this.reset(o) this.draft.busy = false this.draft.confirming = false openContext({ body: () => this.editorBody(o.pand), footer: () => this.editorFooter(o.pand), key, title: () => this.find(o.pand)?.name ?? o.name, }) } find(pand: string): BuildingOverride | undefined { return ($s.overrides ?? []).find((x) => x.pand === pand) } changed(o: BuildingOverride): boolean { const {name, collision, interior, placement} = this.draft return ( name !== o.name || collision !== o.collision || interior !== o.interior || JSON.stringify(placement) !== JSON.stringify({...o.placement}) ) } async act(fn: () => Promise): Promise { this.draft.busy = true const ok = await fn() this.draft.busy = false return ok } editorBody(pand: string) { const o = this.find(pand) if (!o) { return

This building is no longer replaced.

} const draft = this.draft const busy = draft.busy return (
{o.source.kind === 'stadium' ? 'generated stadium' : 'uploaded model'}
Building
{o.source.kind === 'model' && (
Placement {/* The pin's error at once rather than on blur, as the hint it replaces was: typing a latitude is what causes it. */}
)}
) } editorFooter(pand: string) { const o = this.find(pand) if (!o) { return ( ) } const draft = this.draft const {busy, confirming} = draft const changed = this.changed(o) return (
{confirming ? ( <> ) : ( )}
) } view() { const s = this.s return (
{$s.overrides === null ? '' : `${$s.overrides.length} replaced`} {!s.adding && ( )}

A building listed here is drawn from its model instead of the 3DBAG survey. Deleting an override brings the surveyed building back. Viewers see a change within about half a minute.

{s.adding && ( { s.adding = false }} /> )} {$s.overrides === null ? (

Loading…

) : !$s.overrides.length ? (

No building is replaced.

) : (
    {$s.overrides.map((o) => { const open = contextShows(editKey(o.pand)) return (
  • {o.name} {o.source.kind === 'stadium' ? 'generated stadium' : 'uploaded model'}
  • ) })}
)}
) } } /** What is stored about an override, as one meta line. */ class OverrideFacts extends MithrilComponent<{o: BuildingOverride}> { view(vnode: m.Vnode<{o: BuildingOverride}>) { const o = vnode.attrs.o return ( BAG {o.pand} {o.cell && ` · cell ${o.cell[0]}/${o.cell[1]}`} · saved{' '} {new Date(o.revision).toLocaleString(undefined, {dateStyle: 'medium', timeStyle: 'short'})} {o.author && ` · by ${o.author}`} {o.source.kind === 'model' && ` · ${o.source.triangles.toLocaleString()} triangles`} ) } }