All files / views/readout view.ts

100% Statements 28/28
100% Branches 6/6
100% Functions 13/13
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209                          1x 1x 1x     1x 128x               1x         106x                 1x             2756x                       1x 106x 106x 106x                                   1378x                                 212x 212x                                   2756x                                     1x           286x                         442x                   1x 22x 22x 34x 22x                                   34x                               286x          
import clsx from 'clsx'
import { Array, Option } from 'effect'
import type { Html, HtmlBuilder } from 'foldkit/html'
 
import type { Message } from '@/message'
import type { Model } from '@/model'
 
import { DATUM_LABEL } from '../../core/geo/ellipsoid'
import * as Cell from './cell'
import { type ReadoutColumn, columnsFor } from './columns'
import { positionLabelView, systemLabelView } from './header'
import { type ReadoutRow, availableRows } from './rows'
 
const CAPTION = 'The cursor and cue positions in each coordinate system'
const stickyLeft = 'sticky left-0 z-10 bg-panel'
const headingText = 'font-sans text-[11px] font-medium text-muted'
 
/** Every reading is on one datum, so the readout names it once. */
const datumView = (h: HtmlBuilder<Message>): Html =>
  h.span(
    [
      h.Class(headingText),
      h.Title(`All positions are on the ${DATUM_LABEL} datum`),
    ],
    [DATUM_LABEL],
  )
 
const readoutTableView = (
  head: Html,
  body: ReadonlyArray<Html>,
  h: HtmlBuilder<Message>,
): Html =>
  h.table(
    [h.Class('w-full border-separate border-spacing-0 font-mono text-xs')],
    [
      h.caption([h.Class('sr-only')], [CAPTION]),
      h.thead([], [head]),
      h.tbody([], [...body]),
    ],
  )
 
const cellView = (
  row: ReadoutRow,
  column: ReadoutColumn,
  model: Model,
  classes: string,
  h: HtmlBuilder<Message>,
): Html =>
  h.keyed('td')(
    `${row.id}-${column.formatter.id}`,
    [h.Class(clsx('p-0', classes))],
    [Cell.view(row, column, model, h)],
  )
 
// WIDE: positions down, systems across
 
/**
 * The readout shows positions (rows.ts) in each coordinate system (columns.ts). Wide screens
 * get this table under the map.
 */
export const tableView = (model: Model, h: HtmlBuilder<Message>): Html => {
  const rows = availableRows(model)
  const columns = columnsFor(model.settings.values.profile)
  return h.section(
    [
      h.AriaLabel('Coordinate readout'),
      h.Class('shrink-0 overflow-x-auto border-t border-line bg-panel'),
    ],
    [
      readoutTableView(
        h.tr(
          [],
          [
            h.th(
              [
                h.Scope('col'),
                h.Class(clsx(stickyLeft, 'w-28 px-3 pb-1 pt-2 text-left')),
              ],
              [h.span([h.Class('sr-only')], ['Position. ']), datumView(h)],
            ),
            ...Array.map(columns, column =>
              h.keyed('th')(
                column.formatter.id,
                [
                  h.Scope('col'),
                  h.Class(
                    clsx(
                      'min-w-36 border-l border-line px-3 pb-1 pt-2 text-left',
                      headingText,
                    ),
                  ),
                ],
                [systemLabelView(column, model, h)],
              ),
            ),
          ],
        ),
        Array.map(rows, (row, index) => {
          const divider = index > 0 && 'border-t border-line-soft'
          return h.keyed('tr')(
            row.id,
            [],
            [
              h.th(
                [
                  h.Scope('row'),
                  h.Class(
                    clsx(
                      stickyLeft,
                      divider,
                      'px-3 text-left font-sans text-xs font-medium',
                    ),
                  ),
                ],
                [positionLabelView(row, model, h)],
              ),
              ...Array.map(columns, column =>
                cellView(
                  row,
                  column,
                  model,
                  clsx('border-l border-line', divider),
                  h,
                ),
              ),
            ],
          )
        }),
        h,
      ),
    ],
  )
}
 
// COMPACT: a sheet of system cards that scrolls sideways
 
const cardView = (
  column: ReadoutColumn,
  rows: ReadonlyArray<ReadoutRow>,
  model: Model,
  h: HtmlBuilder<Message>,
): Html =>
  h.keyed('li')(
    column.formatter.id,
    [
      h.Class(
        'w-44 shrink-0 snap-start overflow-hidden rounded-lg border border-line bg-surface',
      ),
    ],
    [
      h.div(
        [h.Class(clsx('px-3 pb-0.5 pt-2', headingText))],
        [systemLabelView(column, model, h)],
      ),
      ...Array.map(rows, row =>
        h.keyed('div')(
          row.id,
          [h.Class('pb-1 font-mono text-xs')],
          [Cell.view(row, column, model, h)],
        ),
      ),
    ],
  )
 
/** Compact screens get a sheet over the map, one card per system, which slides up while there is a position to show. */
export const sheetView = (model: Model, h: HtmlBuilder<Message>): Html => {
  const rows = availableRows(model)
  const columns = columnsFor(model.settings.values.profile)
  const isOpen = Array.some(rows, row => Option.isSome(row.point(model)))
  return h.section(
    [
      h.AriaLabel('Coordinate readout'),
      h.AriaHidden(!isOpen),
      ...(isOpen ? [] : [h.Attribute('inert', '')]),
      h.Class(
        clsx(
          'absolute inset-x-0 bottom-0 z-10 border-t border-line bg-panel/95 pb-[env(safe-area-inset-bottom)]',
          'shadow-[0_-8px_24px_rgba(0,0,0,0.35)] transition-transform duration-200 ease-out motion-reduce:transition-none',
          isOpen ? 'translate-y-0' : 'pointer-events-none translate-y-full',
        ),
      ),
    ],
    [
      h.div(
        [h.Class('flex items-center gap-4 px-3 pb-1 pt-2 text-xs font-medium')],
        [
          ...Array.map(rows, row =>
            h.keyed('div')(
              row.id,
              [h.Class('flex flex-1 items-center')],
              [positionLabelView(row, model, h)],
            ),
          ),
          datumView(h),
        ],
      ),
      h.ul(
        [
          h.AriaLabel('Coordinate systems'),
          h.Class(
            'flex snap-x snap-mandatory scroll-px-3 gap-2 overflow-x-auto px-3 pb-3 pt-1 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden',
          ),
        ],
        Array.map(columns, column => cardView(column, rows, model, h)),
      ),
    ],
  )
}