Skip to content

Mapping Table

A table that pairs each source with one target picked from a select, such as remote libraries to local libraries or remote users to local users. It uses the Static Table styling and stacks the two columns on a narrow screen.

Decision

When Use
Pairing a fixed list of sources with a list of targets, one target per source Mapping Table
Two sources that must never share a target Mapping Table with unique
Both sides typed by the user, or rows the user adds Key Value Editor
Read only rows Static Table

Usage

import { createMappingTable } from './configurationpage?name=myplugin_stolon.js';

var map = createMappingTable({
    rows: remoteLibraries.map(function (l) { return { value: l.Id, label: l.Name }; }),
    targets: localLibraries.map(function (l) { return { value: l.Id, label: l.Name }; }),
    value: config.LibraryMap,
    sourceHeader: 'Remote Library',
    targetHeader: 'Local Library',
    unique: true,
    onChange: function (value) { config.LibraryMap = value; }
});
container.appendChild(map.element);

Classes: stolon-mapping-table, stolon-mapping-table-grid on a stolon-static-table inside a stolon-static-table-scroll. The handle is { element, getValue, setValue, setRows, setTargets, getDetails, setDetails, isValid, destroy }.

Parts: stolon-mapping-table-row, stolon-mapping-table-source, stolon-mapping-table-name, stolon-mapping-table-description, stolon-mapping-table-target, stolon-mapping-table-empty, stolon-mapping-table-toggle, stolon-mapping-table-extra, stolon-mapping-table-remove, stolon-mapping-table-remove-btn.

A row can carry more than its target:

var map = createMappingTable({
    rows: sources, targets: targets, value: config.LibraryMap,
    toggle: true,
    removable: true,
    fields: [{ key: 'mode', label: 'Mode', type: 'select', options: ['Copy', 'Link'] }],
    details: config.LibraryDetails,
    onChange: function () { config.LibraryMap = map.getValue(); config.LibraryDetails = map.getDetails(); }
});

States and Modifiers

  • rows is an array of { value, label, description }, targets an array of { value, label }, and the value an object from source value to target value. A label defaults to its value.
  • getValue() returns only the shown sources whose target exists. A source on "None" is left out.
  • unique: true disables a target in the other rows once one row picks it.
  • The labels default to "Source", "Target", "None", and "Nothing to map.". targetLabel is the aria-label of each select, "Target for {value}", where the value is the source label.
  • The empty sentence shows in place of the table while there is no source. It takes a List's empty look, stolon-list-empty outside.
  • At 600px and below the header row hides and each source sits above its select.
  • size is small, medium (default), or large, and sizes its fields on the size scale.
  • fields adds a column per Repeater field to each row, with the same types and validation. Their values come back from getDetails(), by source, as { target, enabled, ...fields }, and details or setDetails starts them.
  • toggle: true adds an on and off control first in each row, labeled "Enable {value}" by enabledLabel, under the "Enabled" header. A row that is off takes off, dims, and is left out of getValue(), and its pair is kept.
  • removable: true adds a remove button last in each row, "Remove {value}" by removeLabel. It drops the source and its pair, calls onRemove(source), and moves focus to the next remove button.
  • A table with fields, toggle, or removable takes extended.
  • isValid() checks every extra field and shows each message.

Gotchas

  • A pair whose target is missing from targets shows as unmapped and is left out of getValue(). The pair is kept, so it returns once setTargets brings the target back. A pair for a source that is not shown is kept the same way.
  • With unique on, a target that several sources hold stays with the first row, and the later rows show as unmapped.
  • setValue, setRows, and setTargets do not call onChange.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createMappingTable

function createMappingTable(options?: MappingTableOptions): MappingTableHandle

A table that pairs each source with one target picked from a select, such as remote libraries to local libraries or remote users to local users. It stacks the two columns on a narrow screen.

Option Type What it does
rows MappingTableRow[] | null array of { value, label, description } sources [optional]
targets MappingTableTarget[] | null array of { value, label } choices [optional]
value MappingTableValue | null object from source value to target value [optional]
sourceHeader string header of the source column (default 'Source')
targetHeader string header of the target column (default 'Target')
noneLabel string the choice that leaves a source unmapped (default 'None')
emptyText string sentence shown while there is no source (default 'Nothing to map.')
targetLabel string aria-label of a select, where {value} is the source label (default 'Target for {value}')
unique boolean true disables a target in the other rows once one row picks it (default false)
size 'small' | 'medium' | 'large' 'small', 'medium', or 'large' for its fields (default 'medium')
fields FieldSpec[] | null Repeater fields drawn as more columns on each row, as { key, label, type, options, required, validate } [optional]
toggle boolean true adds an on and off control at the start of each row, and a row that is off is left out of getValue (default false)
removable boolean true adds a remove button at the end of each row, which drops the source (default false)
details MappingTableDetails | null object from source value to { enabled, and a value per field key } [optional]
enabledHeader string header of the toggle column (default 'Enabled')
enabledLabel string aria-label of a toggle, where {value} is the source label (default 'Enable {value}')
removeLabel string aria-label of a remove button, where {value} is the source label (default 'Remove {value}')
onChange (value: MappingTableValue): void function(value) after any change [optional]
onRemove (source: string): void function(source) after a row is removed [optional]
Handle member Type
getValue (): MappingTableValue
setValue (next: MappingTableValue | null): void
setRows (next: MappingTableRow[] | null): void
setTargets (next: MappingTableTarget[] | null): void
getDetails (): MappingTableDetails
setDetails (next: MappingTableDetails | null): void
isValid (): boolean
destroy (): void
Type What it is
MappingTableRow A source row of the table, such as a remote library or a remote user.
MappingTableTarget A target a source can be paired with, such as a local library or a local user.
MappingTableValue The pairs, as an object from source value to target value.
MappingTableDetail A row's extra state: whether it is on, and the values of its extra fields by key.
MappingTableDetails Every row's extra state, by source value.
Class What it looks like
stolon-mapping-table Static table that pairs each source row with a target select, with a sentence in place of the table while there is no source
stolon-mapping-table-grid Source cell with its name and an optional dimmed description under it
stolon-mapping-table-target Target cell whose select fills it
stolon-mapping-table-grid.stolon-extended A table with extra columns: a toggle first, a cell per extra field, and a remove button last. A row switched off dims its source and target

Example

Example Code

<!-- title: Mapping Table -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
const note = document.createElement('div');
note.className = 'stolon-status-text';
const map = base.createMappingTable({
    rows: [
        { value: 'r1', label: 'Movies', description: 'source-a' },
        { value: 'r2', label: 'Shows', description: 'source-a' },
        { value: 'r3', label: 'Music', description: 'source-a' }
    ],
    targets: [{ value: 'l1', label: 'Local Movies' }, { value: 'l2', label: 'Local Shows' }, { value: 'l3', label: 'Local Music' }],
    value: { r1: 'l1', r3: 'removed' },
    sourceHeader: 'Remote Library',
    targetHeader: 'Local Library',
    unique: true,
    onChange: (value) => { note.textContent = JSON.stringify(value); }
});
mount.appendChild(map.element);
mount.appendChild(note);
</script>