All files / submodels/settings view.ts

89.09% Statements 49/55
100% Branches 0/0
79.31% Functions 23/29
88.67% Lines 47/53

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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428                                                              3x 128x                   3x 128x 128x               1152x 1152x                                                                                   3x   3x         3x             3x                     3x               66x             3x                             3x           3x 3x     3x       384x     384x                   1024x         640x 384x                         640x     3x 384x   3x 640x         3x     128x                             1x                                                                                           1x                                                                                                                                                 3x       128x 1024x     3x 3x     3x 106x                     3x 44x     3x 22x             22x                                     3x   22x           22x                                      
import clsx from 'clsx'
import { Array, Option } from 'effect'
import { Submodel } from 'foldkit'
import type { Html, HtmlBuilder } from 'foldkit/html'
 
import { Disclosure, RadioGroup } from '@foldkit/ui'
 
import type { MapStyle, RangeRings } from '../../core/domain'
import type { GarsPrecision } from '../../core/systems/gars'
import type { MgrsPrecision } from '../../core/systems/mgrs'
import {
  type ProfileId,
  gridSwitchesFor,
  profiles,
} from '../../core/systems/profiles'
import { isGridEnabled } from '../../core/systems/registry'
import { TileZoom } from '../../core/systems/tile'
import * as Icon from '../../views/ui/icon'
import * as Select from '../../views/ui/select'
import * as Swatch from '../../views/ui/swatch'
import * as Toggle from '../../views/ui/toggle'
import { Message } from './message'
import type { Model } from './model'
import {
  BasemapRadioGroup,
  ProfileRadioGroup,
  ProjectionRadioGroup,
} from './radioGroup'
 
// TOGGLES
 
const ringsSwatch = (isOn: boolean, h: HtmlBuilder<Message>): Html =>
  h.span([
    h.Class(
      clsx('inline-block h-2 w-2 shrink-0 rounded-full border-2 border-cue', {
        'opacity-30': !isOn,
      }),
    ),
    h.AriaHidden(true),
  ])
 
/** The overlay switches shown in the toolbar: each grid, its labels, and the range rings. */
export const togglesView = Submodel.defineView<Model, Message>((model, h) => {
  const { values } = model
  return h.div(
    [
      h.Role('group'),
      h.AriaLabel('Overlays'),
      h.Class('flex shrink-0 items-center gap-1'),
    ],
    [
      ...Array.map(gridSwitchesFor(values.profile), grid => {
        const isOn = isGridEnabled(values.grids, grid)
        return Toggle.view(
          {
            id: `grid-${grid.id}`,
            label: grid.label,
            description: grid.description,
            isOn,
            onToggle: Message.ToggledGrid({ gridId: grid.id }),
            swatch: Swatch.view(grid, !isOn, h),
          },
          h,
        )
      }),
      Toggle.view(
        {
          id: 'grid-labels',
          label: 'Labels',
          description: 'Grid labels on the map',
          isOn: values.isGridLabelsVisible,
          onToggle: Message.ToggledGridLabels(),
          swatch: h.empty,
        },
        h,
      ),
      h.span([h.Class('mx-1 h-5 w-px shrink-0 bg-line'), h.AriaHidden(true)]),
      Toggle.view(
        {
          id: 'range-rings',
          label: 'Rings',
          description:
            'Range rings around the cue; set their spacing and unit in Map settings',
          isOn: values.isRangeRingsVisible,
          onToggle: Message.ToggledRangeRings(),
          swatch: ringsSwatch(values.isRangeRingsVisible, h),
        },
        h,
      ),
    ],
  )
})
 
// MENU
 
const profileOptions: ReadonlyArray<ProfileId> = ['Land', 'Maritime', 'Air']
 
const basemapOptions: ReadonlyArray<MapStyle.Basemap> = [
  'Dark',
  'Light',
  'Satellite',
]
const projectionOptions: ReadonlyArray<MapStyle.Projection> = [
  'Globe',
  'Mercator',
]
 
const mgrsPrecisionOptions: Array.NonEmptyReadonlyArray<
  Select.SelectOption<MgrsPrecision>
> = [
  { value: 5, label: '1 m' },
  { value: 4, label: '10 m' },
  { value: 3, label: '100 m' },
  { value: 2, label: '1 km' },
  { value: 1, label: '10 km' },
  { value: 0, label: '100 km' },
]
 
const garsPrecisionOptions: Array.NonEmptyReadonlyArray<
  Select.SelectOption<GarsPrecision>
> = [
  { value: 5, label: '5′ keypad' },
  { value: 15, label: '15′ quadrant' },
  { value: 30, label: '30′ cell' },
]
 
const tileZoomOptions: Array.NonEmptyReadonlyArray<
  Select.SelectOption<TileZoom>
> = Array.map(TileZoom.literals, zoom => ({
  value: zoom,
  label: `Zoom ${zoom}`,
}))
 
const ringSpacingOptions: Array.NonEmptyReadonlyArray<
  Select.SelectOption<RangeRings.RingSpacing>
> = [
  { value: 'Auto', label: 'Auto (by zoom)' },
  { value: '0.25', label: '0.25' },
  { value: '0.5', label: '0.5' },
  { value: '1', label: '1' },
  { value: '2', label: '2' },
  { value: '5', label: '5' },
  { value: '10', label: '10' },
  { value: '25', label: '25' },
  { value: '50', label: '50' },
  { value: '100', label: '100' },
]
 
const ringUnitOptions: Array.NonEmptyReadonlyArray<
  Select.SelectOption<RangeRings.RingUnit>
> = [
  { value: 'NauticalMiles', label: 'Nautical miles' },
  { value: 'Kilometres', label: 'Kilometres' },
]
 
/** Every setting is a field with its label on top. The panel sets the width: one column beside the map, a wrapping grid on phones. */
const FIELD_CLASS = 'grid min-w-0 gap-1'
const FIELD_LABEL_CLASS = 'text-[11px] font-medium text-faint'
 
/** A row of radio pills under its label: one choice from a short list, with arrow-key movement. */
const pillsView =
  <Value extends string>(
    label: string,
    toTitle: (value: Value) => Option.Option<string>,
    h: HtmlBuilder<Message>,
  ) =>
  ({ group, options }: RadioGroup.RenderInfo<Value>): Html =>
    h.div(
      [h.Class(FIELD_CLASS)],
      [
        h.span([h.Class(FIELD_LABEL_CLASS), h.AriaHidden(true)], [label]),
        h.div(
          [
            ...group,
            h.Class('flex rounded-md border border-line bg-surface p-0.5'),
          ],
          Array.map(options, option =>
            h.keyed('div')(
              option.value,
              [
                ...option.option,
                ...Option.match(toTitle(option.value), {
                  onNone: () => [],
                  onSome: title => [h.Title(title)],
                }),
                h.Class(
                  'flex-1 cursor-pointer rounded px-1 py-0.5 text-center text-xs text-muted outline-hidden hover:text-ink focus-visible:ring-2 focus-visible:ring-focus data-[checked]:bg-ink/15 data-[checked]:text-ink [@media(hover:none)]:py-1.5',
                ),
              ],
              [h.span([...option.label], [option.value])],
            ),
          ),
        ),
      ],
    )
 
const noTitle = (): Option.Option<string> => Option.none()
 
/** A profile's pill shows its description on hover. */
const profileTitle = (profileId: ProfileId): Option.Option<string> =>
  Option.some(profiles[profileId].description)
 
const selectField = (control: Html, h: HtmlBuilder<Message>): Html =>
  h.div([h.Class(FIELD_CLASS)], [control])
 
type Field = Readonly<{ id: string; view: Html }>
 
/** Every setting, in order. Land, Maritime or Air (AGeoP-21 Table B-1) comes first, as it changes the most. */
const fields = (
  model: Model,
  h: HtmlBuilder<Message>,
): ReadonlyArray<Field> => [
  {
    id: 'profile',
    view: h.submodel({
      slotId: model.profileRadioGroup.id,
      model: model.profileRadioGroup,
      view: ProfileRadioGroup.view,
      viewInputs: {
        options: profileOptions,
        selectedValue: Option.some(model.values.profile),
        ariaLabel: 'Profile',
        orientation: 'Horizontal',
        toView: pillsView('Profile', profileTitle, h),
      },
      toParentMessage: message =>
        Message.GotProfileRadioGroupMessage({ message }),
    }),
  },
  {
    id: 'basemap',
    view: h.submodel({
      slotId: model.basemapRadioGroup.id,
      model: model.basemapRadioGroup,
      view: BasemapRadioGroup.view,
      viewInputs: {
        options: basemapOptions,
        selectedValue: Option.some(model.values.basemap),
        ariaLabel: 'Base map',
        orientation: 'Horizontal',
        toView: pillsView('Base map', noTitle, h),
      },
      toParentMessage: message =>
        Message.GotBasemapRadioGroupMessage({ message }),
    }),
  },
  {
    id: 'projection',
    view: h.submodel({
      slotId: model.projectionRadioGroup.id,
      model: model.projectionRadioGroup,
      view: ProjectionRadioGroup.view,
      viewInputs: {
        options: projectionOptions,
        selectedValue: Option.some(model.values.projection),
        ariaLabel: 'Projection',
        orientation: 'Horizontal',
        toView: pillsView('Projection', noTitle, h),
      },
      toParentMessage: message =>
        Message.GotProjectionRadioGroupMessage({ message }),
    }),
  },
  {
    id: 'mgrs-precision',
    view: selectField(
      Select.view(
        {
          id: 'mgrs-precision',
          label: 'MGRS precision',
          current: model.values.mgrsPrecision,
          options: mgrsPrecisionOptions,
          toMessage: precision => Message.SelectedMgrsPrecision({ precision }),
        },
        h,
      ),
      h,
    ),
  },
  {
    id: 'gars-precision',
    view: selectField(
      Select.view(
        {
          id: 'gars-precision',
          label: 'GARS precision',
          current: model.values.garsPrecision,
          options: garsPrecisionOptions,
          toMessage: precision => Message.SelectedGarsPrecision({ precision }),
        },
        h,
      ),
      h,
    ),
  },
  {
    id: 'tile-zoom',
    view: selectField(
      Select.view(
        {
          id: 'tile-zoom',
          label: 'Map tile zoom',
          current: model.values.tileZoom,
          options: tileZoomOptions,
          toMessage: zoom => Message.SelectedTileZoom({ zoom }),
        },
        h,
      ),
      h,
    ),
  },
  {
    id: 'ring-spacing',
    view: selectField(
      Select.view(
        {
          id: 'ring-spacing',
          label: 'Ring spacing',
          current: model.values.ringSpacing,
          options: ringSpacingOptions,
          toMessage: spacing => Message.SelectedRingSpacing({ spacing }),
        },
        h,
      ),
      h,
    ),
  },
  {
    id: 'ring-unit',
    view: selectField(
      Select.view(
        {
          id: 'ring-unit',
          label: 'Ring unit',
          current: model.values.ringUnit,
          options: ringUnitOptions,
          toMessage: unit => Message.SelectedRingUnit({ unit }),
        },
        h,
      ),
      h,
    ),
  },
]
 
const keyedFields = (
  model: Model,
  h: HtmlBuilder<Message>,
): ReadonlyArray<Html> =>
  Array.map(fields(model, h), field =>
    h.keyed('div')(field.id, [h.Class('contents')], [field.view]),
  )
 
const PANEL_LABEL = 'Map settings'
const DISCLOSURE_ID = 'map-settings'
 
/** Wide screens: the settings sit in a narrow column beside the map, one field under another. */
export const sidePanelView = Submodel.defineView<Model, Message>((model, h) =>
  h.aside(
    [
      h.AriaLabel(PANEL_LABEL),
      h.Class(
        'flex w-52 shrink-0 flex-col gap-3 overflow-y-auto border-l border-line bg-panel p-3 text-ink',
      ),
    ],
    keyedFields(model, h),
  ),
)
 
const toggledPanel = (isOpen: boolean): Message =>
  Message.ToggledPanel({ isOpen })
 
/** Compact screens: the toolbar button that opens and closes the settings strip. */
export const panelButtonView = Submodel.defineView<Model, Message>((model, h) =>
  Disclosure.view(
    {
      id: DISCLOSURE_ID,
      isOpen: model.values.isPanelOpen,
      onToggle: toggledPanel,
      ariaLabel: PANEL_LABEL,
      toView: ({ button }) =>
        h.button(
          [
            ...button,
            h.Title(PANEL_LABEL),
            h.Class(
              'shrink-0 rounded-md p-1.5 text-muted outline-hidden hover:bg-ink/10 hover:text-ink focus-visible:ring-2 focus-visible:ring-focus data-[open]:bg-ink/15 data-[open]:text-ink',
            ),
          ],
          [Icon.settings(h)],
        ),
    },
    h,
  ),
)
 
/**
 * Compact screens: the fields wrap under the toolbar in columns at least 10rem wide, two to a
 * row on most phones, and scroll if they are taller than the space allows. The toolbar button opens and closes them.
 */
export const compactPanelView = Submodel.defineView<Model, Message>(
  (model, h) =>
    Disclosure.view(
      {
        id: DISCLOSURE_ID,
        isOpen: model.values.isPanelOpen,
        onToggle: toggledPanel,
        toView: ({ panel, animatePanel }) =>
          h.section(
            [...panel, h.AriaLabel(PANEL_LABEL)],
            [
              animatePanel(
                h.div(
                  [
                    h.Class(
                      'grid max-h-[40vh] grid-cols-[repeat(auto-fill,minmax(10rem,1fr))] gap-x-3 gap-y-2 overflow-y-auto pb-1 pt-1 text-ink',
                    ),
                  ],
                  keyedFields(model, h),
                ),
              ),
            ],
          ),
      },
      h,
    ),
)