Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550/** Node-side HTML emitter for the deck.gl scene control panel. Pure string-builder: returns an HTML fragment ready for embedding in the page generated by `./render-html.ts`. No DOM access, no scripts; those live in `./script/controller.ts` and are bundled into a separate `<script>` block. Output IDs match what `./script/controller.ts` queries: `dim-<channel>`, `range-<channel>-<min|max>`, `toggle-<key>` (radio names), `search`, `display-*`, `name-labels`, `visibility-counter`, `reset`. Channel/toggle rows carry `data-channel` / `data-toggle` attributes so the controller can wire handlers via `querySelectorAll`. @example ```ts import { renderControls } from './render-controls.ts'; const fragment = renderControls({ probes, state: defaultState({ probes }) }); ``` */
import { acceptsDim, DIM_DISPLAY_NAMES, TOGGLE_LABELS,} from './dim-meta.ts';import type { PackageProbe, } from './probe.ts';import { type ChannelKey, type DataDimKey, DIM_UNKNOWN, extractDim, type ToggleKey, type ToggleValue,} from './script/filter.ts';import { type AppState, TOGGLE_KEYS,} from './script/state.ts';
//region Constants
/** All ten data dims, fixed order for dropdown rendering. */const DIM_KEYS: readonly DataDimKey[] = [ 'logSourceBytes', 'logDaysStale', 'logInstallSize', 'logDownloads', 'tsRatio', 'runtimeDepCount', 'transitiveDepCount', 'logPackageAge', 'isLeafNumeric', 'licenseClassNumeric',];
/** Channels in their dropdown render order. */const CHANNEL_KEYS: readonly ChannelKey[] = [ 'x', 'y', 'z', 'color', 'shape', 'size',];
/** Three-state toggle radio values, fixed order. */const TOGGLE_VALUES: readonly ToggleValue[] = [ 'any', 'yes', 'no',];
/** Name-labels select options, fixed order. */const NAME_LABEL_OPTIONS: readonly ('none' | 'topN' | 'all')[] = [ 'none', 'topN', 'all',];
/** Slider step in displayed-value units; fine enough for log-scale dims. */const RANGE_STEP = 0.01;
/** Display-channel labels for the dim picker rows. */const CHANNEL_LABELS: Record<ChannelKey, string> = { x: 'x', y: 'y', z: 'z', color: 'color', shape: 'shape', size: 'size',};
//endregion Constants
//region Helpers
/** Escapes runtime-controlled strings for embedding in HTML attribute values. Currently only the search field's initial value reaches the renderer with user-controlled content (via URL-hash bookmark). Order is critical: `&` must be replaced first or other replacements would double-escape it. @param raw - Source string from state. @returns Escaped string safe for double-quoted attribute embedding. */function escapeAttr(raw: string,): string { return raw .replaceAll( '&', '&', ) .replaceAll( '"', '"', ) .replaceAll( '<', '<', ) .replaceAll( '>', '>', );}
/** Computes inclusive `[min, max]` for one dim across the probe array, skipping unknowns. Falls back to `[0, 0]` when no probe has a known value so the slider can still render. @param probes - Source probes. @param dim - Dim whose extent to compute. @returns Inclusive `[min, max]` bounds. */function computeChannelExtent( { probes, dim, }: { readonly probes: readonly PackageProbe[]; readonly dim: DataDimKey; },): readonly [ number, number,] { /** Known values for `dim` (unknowns dropped), used to derive the slider's `[min, max]` extent. */ const values = probes .map(function pluck(probe,) { return extractDim({ probe, dim, },); },) .filter(function known(value,): value is number { return value !== DIM_UNKNOWN; },); if (values.length === 0) { return [ 0, 0, ]; } return [ Math.min(...values,), Math.max(...values,), ];}
//endregion Helpers
//region Dim dropdowns
/** Renders one `<select>` element binding a channel to its data dim. Options the channel doesn't accept (per {@link acceptsDim}) still render (but with the `disabled` attribute) so users can see what exists without reading docs. @param channel - Channel being bound (x, y, z, color, shape, size). @param selectedDim - Currently mapped dim. @returns HTML fragment for one dim row. @example ```ts renderDimDropdown({ channel: 'x', selectedDim: 'logSourceBytes' }); ``` */function renderDimDropdown( { channel, selectedDim, }: { readonly channel: ChannelKey; readonly selectedDim: DataDimKey; },): string { /** One `<option>` per dim, ordered to match {@link DIM_KEYS}; concatenated below into the dropdown body. */ const options = DIM_KEYS .map(function renderOption(dim,) { /** `true` when the channel accepts this dim type; gates the `disabled` attribute. */ const accepted = acceptsDim({ channel, dim, },); /** ` selected` suffix when this option matches the channel's current dim, empty otherwise. */ const selected = dim === selectedDim ? ' selected' : ''; /** ` disabled` suffix when the dim is not accepted by the channel; greys out the option. */ const disabled = accepted ? '' : ' disabled'; return `<option value="${dim}"${selected}${disabled}>${ DIM_DISPLAY_NAMES[dim] }</option>`; },) .join('',); return `<div class="dim-row" data-channel="${channel}"> <label for="dim-${channel}">${CHANNEL_LABELS[channel]}</label> <select id="dim-${channel}">${options}</select> </div>`;}
//endregion Dim dropdowns
//region Toggle rows
/** Renders one 3-state radio group for a boolean filter toggle. @param key - Toggle key (identifies the boolean attribute). @param current - Currently selected value. @returns HTML fragment for one toggle row. @example ```ts renderToggleRow({ key: 'isLeaf', current: 'any' }); ``` */function renderToggleRow( { key, current, }: { readonly key: ToggleKey; readonly current: ToggleValue; },): string { /** One `<input type="radio">` per allowed value, concatenated to form the toggle's radio group. */ const radios = TOGGLE_VALUES .map(function renderRadio(value,) { /** ` checked` suffix when this radio matches `current`, empty otherwise. */ const checked = value === current ? ' checked' : ''; return `<label><input type="radio" name="toggle-${key}" value="${value}"${checked}>${value}</label>`; },) .join('',); return `<fieldset class="toggle-row" data-toggle="${key}"> <legend>${TOGGLE_LABELS[key]}</legend>${radios} </fieldset>`;}
//endregion Toggle rows
//region Range sliders
/** Renders one min/max range-slider pair for a channel. Slider `min`/`max` attributes use the full data extent; `value` attributes use the current state. On first render both pairs coincide; after URL-hash restore they typically diverge. @param channel - Channel being filtered. @param fullExtent - Full data extent (slider bounds). @param current - Currently set `[min, max]` (initial handle positions). @returns HTML fragment for one range row. @example ```ts renderRangeRow({ channel: 'x', fullExtent: [0, 6], current: [0, 6] }); ``` */function renderRangeRow( { channel, fullExtent, current, }: { readonly channel: ChannelKey; readonly fullExtent: readonly [ number, number, ]; readonly current: readonly [ number, number, ]; },): string { /** Slider's `min`/`max` bounds from the dim's full data extent. */ const [ fullMin, fullMax, ] = fullExtent; /** Currently-set `[min, max]` from state; drives the two handle positions. */ const [ curMin, curMax, ] = current; /** String form of `fullMin` for the slider's `min` HTML attribute. */ const minAttr = fullMin.toString(); /** String form of `fullMax` for the slider's `max` HTML attribute. */ const maxAttr = fullMax.toString(); /** String form of {@link RANGE_STEP} for the slider's `step` HTML attribute. */ const stepAttr = RANGE_STEP.toString(); return `<div class="range-row control-row" data-channel="${channel}"> <label>${CHANNEL_LABELS[channel]} range</label> <div class="range-track"> <input type="range" id="range-${channel}-min" min="${minAttr}" max="${maxAttr}" step="${stepAttr}" value="${curMin.toString()}"> <input type="range" id="range-${channel}-max" min="${minAttr}" max="${maxAttr}" step="${stepAttr}" value="${curMax.toString()}"> </div> </div>`;}
//endregion Range sliders
//region Display section
/** Renders the display-toggle section (4 checkboxes plus a `name-labels` `<select>`). @param state - Current state (drives initial checked/selected state). @returns HTML fragment for the display section. @example ```ts renderDisplaySection({ state: defaultState({ probes }) }); ``` */function renderDisplaySection( { state, }: { readonly state: AppState; },): string { /** Local alias for the display-toggles sub-state so each control read below stays one line. */ const dt = state.displayToggles; /** ` checked` suffix when the wireframe toggle is on; renders the box's bounding wireframe. */ const wireframe = dt.showWireframe ? ' checked' : ''; /** ` checked` suffix when the threshold-planes toggle is on; renders the cube's 6 cutoff planes. */ const planes = dt.showThresholdPlanes ? ' checked' : ''; /** ` checked` suffix when the axis-labels toggle is on; renders dim names near each axis. */ const axisLabels = dt.showAxisLabels ? ' checked' : ''; /** ` checked` suffix when the unknown-cluster toggle is on; surfaces probes with missing dim values. */ const unknown = dt.showUnknownCluster ? ' checked' : ''; /** One `<option>` per name-label policy, concatenated to form the name-labels dropdown body. */ const nameOptions = NAME_LABEL_OPTIONS .map(function renderOption(value,) { /** ` selected` suffix when this option matches the current name-labels policy. */ const selected = dt.nameLabels === value ? ' selected' : ''; return `<option value="${value}"${selected}>${value}</option>`; },) .join('',); return `<section class="controls-section" data-section="display"> <h2>Display</h2> <label><input type="checkbox" id="display-wireframe"${wireframe}>Wireframe</label> <label><input type="checkbox" id="display-planes"${planes}>Threshold planes</label> <label><input type="checkbox" id="display-axis-labels"${axisLabels}>Axis labels</label> <label><input type="checkbox" id="display-unknown"${unknown}>Unknown cluster</label> <label>Name labels <select id="name-labels">${nameOptions}</select></label> </section>`;}
//endregion Display section
//region Public API
/** Renders the full control-panel HTML fragment. Output is meant to be inserted into `<body>` by `./render-html.ts`, sitting alongside the deck.gl canvas. The controller script (also embedded by render-html.ts) wires every control after the page loads. @param probes - Source probes; drives the per-channel slider extents. @param state - Initial state; every control's initial value reflects this. @returns HTML fragment containing the complete control panel. @example ```ts const fragment = renderControls({ probes, state }); // … embedded in render-html.ts's final HTML string … ``` */export function renderControls( { probes, state, }: { readonly probes: readonly PackageProbe[]; readonly state: AppState; },): string { /** Concatenated dim-dropdown rows, one per channel, in {@link CHANNEL_KEYS} order. */ const dimRows = CHANNEL_KEYS .map(function dropdownFor(channel,) { return renderDimDropdown({ channel, selectedDim: state.dimMapping[channel], },); },) .join('',); /** Concatenated toggle rows, one per boolean filter, in {@link TOGGLE_KEYS} order. */ const toggleRows = TOGGLE_KEYS .map(function toggleFor(key,) { return renderToggleRow({ key, current: state.toggles[key], },); },) .join('',); /** Concatenated range-slider rows, one per channel; computes the dim extent fresh each render. */ const rangeRows = CHANNEL_KEYS .map(function rangeFor(channel,) { return renderRangeRow({ channel, fullExtent: computeChannelExtent({ probes, dim: state.dimMapping[channel], },), current: state.ranges[channel], },); },) .join('',); /** Total probe count rendered into the visibility-counter; used for both numerator and denominator at first paint. */ const total = probes.length .toString(); /** HTML-escaped search string, safe for embedding in the search input's `value` attribute. */ const searchAttr = escapeAttr(state.search,); return `<aside id="controls" class="controls"> <section class="controls-section" data-section="camera"> <h2>Camera</h2> <dl class="camera-help"> <dt>Drag</dt><dd>rotate</dd> <dt>Shift + drag</dt><dd>pan</dd> <dt>Scroll</dt><dd>zoom</dd> <dt>Double-click</dt><dd>reset view</dd> </dl> </section> <section class="controls-section" data-section="dim"> <h2>Dimensions</h2>${dimRows} </section> <section class="controls-section" data-section="toggles"> <h2>Filters</h2>${toggleRows} </section> <section class="controls-section" data-section="ranges"> <h2>Range filters</h2>${rangeRows} </section> <section class="controls-section" data-section="search"> <h2>Search</h2> <input type="text" id="search" placeholder="substring or /regex/" value="${searchAttr}"> </section> ${ renderDisplaySection({ state, },) } <section class="controls-section" data-section="info"> <span id="visibility-counter">${total} of ${total} visible</span> <button id="reset" type="button">Reset filters</button> </section> </aside>`;}
//endregion Public API