@link-foundation/ui-components 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONFIGURATIONS.md +74 -0
- package/ICONS.md +76 -0
- package/LICENSE +24 -0
- package/README.md +1499 -2
- package/bin/lino-network.mjs +71 -0
- package/dist/icons/ATTRIBUTION.md +602 -0
- package/dist/icons/NOTICE.lino +1261 -0
- package/dist/icons/brand.js +31 -0
- package/dist/icons/brand.svg +3 -0
- package/dist/icons/licenses/brand/DISCLAIMER.md +51 -0
- package/dist/icons/licenses/brand/LICENSE.md +30 -0
- package/dist/icons/licenses/lucide/LICENSE +43 -0
- package/dist/icons/licenses/material-symbols/LICENSE +202 -0
- package/dist/icons/licenses/material-symbols/NOTICE +12 -0
- package/dist/icons/licenses/tabler/LICENSE +21 -0
- package/dist/icons/lucide.js +681 -0
- package/dist/icons/lucide.svg +34 -0
- package/dist/icons/material-symbols.js +681 -0
- package/dist/icons/material-symbols.svg +34 -0
- package/dist/icons/own.js +521 -0
- package/dist/icons/own.svg +34 -0
- package/dist/icons/tabler.js +681 -0
- package/dist/icons/tabler.svg +34 -0
- package/dist/network-layout-NOTICE.md +6 -0
- package/dist/network-layout-d3-dispatch-LICENSE.txt +13 -0
- package/dist/network-layout-d3-force-LICENSE.txt +13 -0
- package/dist/network-layout-d3-quadtree-LICENSE.txt +13 -0
- package/dist/network-layout-d3-timer-LICENSE.txt +13 -0
- package/dist/network-layout-dagre-LICENSE.txt +19 -0
- package/dist/network-layout.worker.mjs +2 -0
- package/package.json +246 -4
- package/src/a11y/announce.js +88 -0
- package/src/a11y/bind-machine.js +181 -0
- package/src/a11y/focus.js +169 -0
- package/src/a11y/index.d.ts +89 -0
- package/src/a11y/index.js +19 -0
- package/src/a11y/keys.js +182 -0
- package/src/a11y/roving.js +167 -0
- package/src/a11y/typeahead.js +82 -0
- package/src/behavior/actions.js +235 -0
- package/src/behavior/diagnostics.js +73 -0
- package/src/behavior/dom.d.ts +38 -0
- package/src/behavior/dom.js +420 -0
- package/src/behavior/index.d.ts +211 -0
- package/src/behavior/index.js +29 -0
- package/src/behavior/interpret.js +454 -0
- package/src/behavior/machines/disclosure.lino +46 -0
- package/src/behavior/machines/index.js +91 -0
- package/src/behavior/machines/toggle.lino +25 -0
- package/src/behavior/parse-machine.js +367 -0
- package/src/charts/README.md +78 -0
- package/src/charts/activity-layout.js +350 -0
- package/src/charts/activity-model.js +130 -0
- package/src/charts/activity-validation.js +31 -0
- package/src/charts/chart-model.js +667 -0
- package/src/charts/chart.js +518 -0
- package/src/charts/echarts-adapter.d.ts +23 -0
- package/src/charts/echarts-adapter.js +240 -0
- package/src/charts/element.js +330 -0
- package/src/charts/errors.js +62 -0
- package/src/charts/gauge.js +27 -0
- package/src/charts/index.d.ts +344 -0
- package/src/charts/index.js +59 -0
- package/src/charts/layout.js +542 -0
- package/src/charts/lino.js +158 -0
- package/src/charts/mark-options.js +78 -0
- package/src/charts/read.js +101 -0
- package/src/charts/renderer-features.js +8 -0
- package/src/charts/scales.js +223 -0
- package/src/charts/small-model.js +223 -0
- package/src/charts/small-svg.js +269 -0
- package/src/charts/sparkline.js +27 -0
- package/src/charts/stat-tile.js +37 -0
- package/src/charts/summary.js +193 -0
- package/src/charts/svg-renderer.js +226 -0
- package/src/charts/svg.js +69 -0
- package/src/charts/tag-cloud.js +129 -0
- package/src/charts/tokens.js +171 -0
- package/src/charts/tooltip.js +47 -0
- package/src/charts/values.js +146 -0
- package/src/chat/README.md +142 -0
- package/src/chat/actions.js +75 -0
- package/src/chat/chat-contract.js +95 -0
- package/src/chat/chat-list.js +42 -0
- package/src/chat/chat-model.js +125 -0
- package/src/chat/chat.css +228 -0
- package/src/chat/composer.js +131 -0
- package/src/chat/day-separator.js +12 -0
- package/src/chat/dom.js +33 -0
- package/src/chat/follow.js +49 -0
- package/src/chat/index.d.ts +142 -0
- package/src/chat/index.js +13 -0
- package/src/chat/message-bubble.js +191 -0
- package/src/chat/message-stream.js +331 -0
- package/src/chat/reasoning-trace.js +70 -0
- package/src/chat/selection.js +39 -0
- package/src/chat/skins/bubbles.lino +2 -0
- package/src/chat/skins/cards.lino +4 -0
- package/src/chat/skins/compact.lino +4 -0
- package/src/chat/skins/signal.lino +25 -0
- package/src/chat/skins/t3.lino +25 -0
- package/src/chat/skins/telegram.lino +31 -0
- package/src/chat/skins/whatsapp.lino +25 -0
- package/src/chat/stream-keys.js +57 -0
- package/src/chat/stream-window.js +159 -0
- package/src/chat/vocabulary.js +19 -0
- package/src/controls/accordion.js +160 -0
- package/src/controls/avatar.js +80 -0
- package/src/controls/badge.js +48 -0
- package/src/controls/button-template.js +32 -0
- package/src/controls/button.js +97 -0
- package/src/controls/catalog.lino +84 -0
- package/src/controls/checkbox.js +112 -0
- package/src/controls/chip.js +140 -0
- package/src/controls/choices.js +45 -0
- package/src/controls/color-field.js +217 -0
- package/src/controls/color-popover.js +135 -0
- package/src/controls/color-rgb.js +80 -0
- package/src/controls/color.js +67 -0
- package/src/controls/combobox.js +192 -0
- package/src/controls/dom.js +261 -0
- package/src/controls/icon-button.js +33 -0
- package/src/controls/index.d.ts +163 -0
- package/src/controls/index.js +182 -0
- package/src/controls/kinds-text.js +358 -0
- package/src/controls/kinds.lino +350 -0
- package/src/controls/label.js +52 -0
- package/src/controls/link.js +101 -0
- package/src/controls/listbox.js +144 -0
- package/src/controls/machines/button.lino +20 -0
- package/src/controls/machines/checkbox.lino +17 -0
- package/src/controls/machines/chip.lino +20 -0
- package/src/controls/machines/color-field.lino +23 -0
- package/src/controls/machines/combobox.lino +82 -0
- package/src/controls/machines/icon-button.lino +20 -0
- package/src/controls/machines/index.js +586 -0
- package/src/controls/machines/number-field.lino +71 -0
- package/src/controls/machines/radio-group.lino +17 -0
- package/src/controls/machines/select.lino +122 -0
- package/src/controls/machines/slider.lino +99 -0
- package/src/controls/machines/switch.lino +19 -0
- package/src/controls/machines/text-area.lino +26 -0
- package/src/controls/machines/text-field.lino +26 -0
- package/src/controls/number-field.js +239 -0
- package/src/controls/numeric-keys.js +125 -0
- package/src/controls/option.js +54 -0
- package/src/controls/progress.js +76 -0
- package/src/controls/radio-group.js +234 -0
- package/src/controls/select.js +229 -0
- package/src/controls/slider.js +234 -0
- package/src/controls/stories.js +172 -0
- package/src/controls/styles.css +1060 -0
- package/src/controls/switch.js +95 -0
- package/src/controls/text-area.js +39 -0
- package/src/controls/text-field.js +131 -0
- package/src/controls/vocabulary.js +164 -0
- package/src/data/data.css +95 -0
- package/src/data/index.d.ts +83 -0
- package/src/data/index.js +10 -0
- package/src/data/keyboard.js +189 -0
- package/src/data/list.js +215 -0
- package/src/data/render-data.js +116 -0
- package/src/data/selection-dom.js +39 -0
- package/src/data/selection-model.js +43 -0
- package/src/data/sort-model.js +42 -0
- package/src/data/source.js +45 -0
- package/src/data/table.js +160 -0
- package/src/data/tree-model.js +41 -0
- package/src/data/tree.js +55 -0
- package/src/data/viewport.js +216 -0
- package/src/data/virtual-range.js +92 -0
- package/src/data/vocabulary.js +67 -0
- package/src/diagrams/diagram-model.js +511 -0
- package/src/diagrams/diagram.js +204 -0
- package/src/diagrams/index.d.ts +225 -0
- package/src/diagrams/index.js +48 -0
- package/src/diagrams/layout-dagre.js +164 -0
- package/src/diagrams/layout-plugins.js +131 -0
- package/src/diagrams/mermaid-export.js +232 -0
- package/src/diagrams/mermaid-view.js +229 -0
- package/src/diagrams/svg-diagram.js +215 -0
- package/src/editor/code-editor.js +206 -0
- package/src/editor/documents-model.js +170 -0
- package/src/editor/documents-persistence.js +85 -0
- package/src/editor/editor-engine.js +22 -0
- package/src/editor/editor-ui.js +203 -0
- package/src/editor/editor.css +106 -0
- package/src/editor/engines/codemirror.js +227 -0
- package/src/editor/engines/monaco.js +219 -0
- package/src/editor/index.d.ts +111 -0
- package/src/editor/index.js +14 -0
- package/src/editor/lino-highlight.js +106 -0
- package/src/editor/lino-structure.js +85 -0
- package/src/editor/validate-lino.js +43 -0
- package/src/embed/README.md +161 -0
- package/src/embed/controller.js +223 -0
- package/src/embed/element.js +141 -0
- package/src/embed/index.d.ts +75 -0
- package/src/embed/index.js +9 -0
- package/src/embed/providers/telegram.d.ts +3 -0
- package/src/embed/providers/telegram.js +38 -0
- package/src/embed/providers/vk.d.ts +3 -0
- package/src/embed/providers/vk.js +42 -0
- package/src/embed/providers/x.d.ts +3 -0
- package/src/embed/providers/x.js +31 -0
- package/src/embed/registry.js +35 -0
- package/src/embed/script.js +73 -0
- package/src/embed/view.js +122 -0
- package/src/icons/adapters/lucide.d.ts +15 -0
- package/src/icons/adapters/lucide.js +142 -0
- package/src/icons/adapters/material-symbols.d.ts +13 -0
- package/src/icons/adapters/material-symbols.js +113 -0
- package/src/icons/adapters/shared.d.ts +38 -0
- package/src/icons/adapters/shared.js +157 -0
- package/src/icons/adapters/simple-icons.d.ts +9 -0
- package/src/icons/adapters/simple-icons.js +38 -0
- package/src/icons/adapters/tabler.d.ts +9 -0
- package/src/icons/adapters/tabler.js +86 -0
- package/src/icons/icon-registry.js +215 -0
- package/src/icons/icon.js +356 -0
- package/src/icons/index.d.ts +123 -0
- package/src/icons/index.js +30 -0
- package/src/icons/names.js +51 -0
- package/src/icons/own-icons.js +521 -0
- package/src/icons/presentation.js +55 -0
- package/src/icons/svg.js +136 -0
- package/src/index.d.ts +14 -0
- package/src/index.js +40 -0
- package/src/layout/README.md +102 -0
- package/src/layout/app-shell.js +150 -0
- package/src/layout/catalog.lino +27 -0
- package/src/layout/dock-model.js +576 -0
- package/src/layout/dock-view.js +312 -0
- package/src/layout/dock.js +352 -0
- package/src/layout/dom.js +312 -0
- package/src/layout/grid.js +192 -0
- package/src/layout/index.d.ts +267 -0
- package/src/layout/index.js +60 -0
- package/src/layout/layer-manager.js +120 -0
- package/src/layout/layout.css +760 -0
- package/src/layout/menu.js +141 -0
- package/src/layout/nav-strip.js +174 -0
- package/src/layout/page-links.js +166 -0
- package/src/layout/panel.js +26 -0
- package/src/layout/pref-switches.js +209 -0
- package/src/layout/split-model.js +270 -0
- package/src/layout/split.js +430 -0
- package/src/layout/stack.js +86 -0
- package/src/layout/surfaces.js +119 -0
- package/src/layout/tab-group.js +151 -0
- package/src/layout/tab-strip.js +277 -0
- package/src/layout/vocabulary.js +229 -0
- package/src/layout/window-frame.js +222 -0
- package/src/layout/window.js +146 -0
- package/src/links-view/animator.js +144 -0
- package/src/links-view/configuration-classes.js +150 -0
- package/src/links-view/configuration-model.js +191 -0
- package/src/links-view/configuration-notations.js +230 -0
- package/src/links-view/configurations.d.ts +73 -0
- package/src/links-view/configurations.js +33 -0
- package/src/links-view/create-layout-runner.js +79 -0
- package/src/links-view/doublet-curves.js +239 -0
- package/src/links-view/doublet-layout.js +148 -0
- package/src/links-view/doublet-markers.js +109 -0
- package/src/links-view/doublet-options.js +369 -0
- package/src/links-view/doublet-svg.js +156 -0
- package/src/links-view/doublet.js +230 -0
- package/src/links-view/export.js +146 -0
- package/src/links-view/geometry.js +409 -0
- package/src/links-view/index.d.ts +1029 -0
- package/src/links-view/index.js +140 -0
- package/src/links-view/labels.js +28 -0
- package/src/links-view/layout-worker-entry.mjs +2 -0
- package/src/links-view/layout-worker.js +28 -0
- package/src/links-view/layouts/circle.js +7 -0
- package/src/links-view/layouts/force.js +76 -0
- package/src/links-view/layouts/grid.js +7 -0
- package/src/links-view/layouts/hex.js +8 -0
- package/src/links-view/layouts/index.js +15 -0
- package/src/links-view/layouts/layered.js +29 -0
- package/src/links-view/layouts/regular.js +27 -0
- package/src/links-view/layouts/spiral.js +10 -0
- package/src/links-view/link-view.js +494 -0
- package/src/links-view/links-gallery.js +306 -0
- package/src/links-view/links-network.js +496 -0
- package/src/links-view/lino-network-source.js +122 -0
- package/src/links-view/lino-network.js +233 -0
- package/src/links-view/lino-to-network.js +252 -0
- package/src/links-view/markers.js +192 -0
- package/src/links-view/model.js +233 -0
- package/src/links-view/network-editor/dom.js +36 -0
- package/src/links-view/network-editor/editor.js +202 -0
- package/src/links-view/network-editor/model.js +163 -0
- package/src/links-view/network-editor/options.js +265 -0
- package/src/links-view/network-editor/panels.js +423 -0
- package/src/links-view/network-editor/presets.js +76 -0
- package/src/links-view/network-editor/provider.js +93 -0
- package/src/links-view/network-editor/remap.js +45 -0
- package/src/links-view/network-editor/schema.js +224 -0
- package/src/links-view/network-editor/share.js +232 -0
- package/src/links-view/network-editor/styles.js +19 -0
- package/src/links-view/network-editor/url-sync.js +131 -0
- package/src/links-view/network-interaction.js +228 -0
- package/src/links-view/network-model.js +85 -0
- package/src/links-view/network-renderer.js +259 -0
- package/src/links-view/network-scene.js +245 -0
- package/src/links-view/network-source.js +82 -0
- package/src/links-view/network-vocabulary.js +18 -0
- package/src/links-view/presentation.js +199 -0
- package/src/links-view/render-canvas.js +102 -0
- package/src/links-view/render-svg.js +118 -0
- package/src/links-view/scene.js +233 -0
- package/src/links-view/solver-job.js +106 -0
- package/src/links-view/tokens.js +114 -0
- package/src/links-view/zoom-pan.js +203 -0
- package/src/overlays/catalog.lino +89 -0
- package/src/overlays/context-menu.js +110 -0
- package/src/overlays/dialog.js +117 -0
- package/src/overlays/dom.js +168 -0
- package/src/overlays/drawer.js +8 -0
- package/src/overlays/index.d.ts +80 -0
- package/src/overlays/index.js +25 -0
- package/src/overlays/kinds.lino +137 -0
- package/src/overlays/list-popover.js +129 -0
- package/src/overlays/machines/index.js +71 -0
- package/src/overlays/machines/surface.lino +11 -0
- package/src/overlays/machines/tabs.lino +7 -0
- package/src/overlays/machines/toast.lino +16 -0
- package/src/overlays/machines/tooltip.lino +29 -0
- package/src/overlays/menu.js +240 -0
- package/src/overlays/popover.js +194 -0
- package/src/overlays/position.js +84 -0
- package/src/overlays/radial-geometry.js +38 -0
- package/src/overlays/radial-menu.js +50 -0
- package/src/overlays/styles.css +155 -0
- package/src/overlays/tabs.js +125 -0
- package/src/overlays/toast-queue.js +105 -0
- package/src/overlays/toast.js +163 -0
- package/src/overlays/tooltip.js +110 -0
- package/src/overlays/vocabulary.js +3 -0
- package/src/settings/conditions.js +44 -0
- package/src/settings/control-for.js +62 -0
- package/src/settings/field-control.js +201 -0
- package/src/settings/index.d.ts +201 -0
- package/src/settings/index.js +3 -0
- package/src/settings/messages.js +112 -0
- package/src/settings/miniature.js +26 -0
- package/src/settings/panel-actions.js +234 -0
- package/src/settings/panel-model.js +49 -0
- package/src/settings/panel-refresh.js +56 -0
- package/src/settings/panel-rows.js +200 -0
- package/src/settings/preset-cards.js +43 -0
- package/src/settings/saved-looks.js +227 -0
- package/src/settings/section-layout.js +95 -0
- package/src/settings/section-tabs.js +175 -0
- package/src/settings/settings-panel.js +192 -0
- package/src/settings/settings.css +178 -0
- package/stories/accordion.lino +14 -0
- package/stories/alert-dialog.lino +4 -0
- package/stories/app-bar.lino +10 -0
- package/stories/avatar.lino +7 -0
- package/stories/badge.lino +12 -0
- package/stories/button.lino +17 -0
- package/stories/card.lino +10 -0
- package/stories/checkbox.lino +14 -0
- package/stories/chip.lino +19 -0
- package/stories/color-field.lino +9 -0
- package/stories/combobox.lino +10 -0
- package/stories/container.lino +11 -0
- package/stories/context-menu.lino +15 -0
- package/stories/dialog.lino +16 -0
- package/stories/drawer.lino +12 -0
- package/stories/icon-button.lino +13 -0
- package/stories/label.lino +7 -0
- package/stories/link-cards.lino +9 -0
- package/stories/link.lino +9 -0
- package/stories/menu-item.lino +6 -0
- package/stories/menu-separator.lino +7 -0
- package/stories/menu.lino +30 -0
- package/stories/nav-strip.lino +11 -0
- package/stories/number-field.lino +16 -0
- package/stories/option.lino +10 -0
- package/stories/paper.lino +11 -0
- package/stories/popover.lino +4 -0
- package/stories/pref-switches.lino +4 -0
- package/stories/progress.lino +14 -0
- package/stories/radial-menu.lino +19 -0
- package/stories/radio-group.lino +22 -0
- package/stories/radio.lino +10 -0
- package/stories/section-nav.lino +9 -0
- package/stories/select.lino +16 -0
- package/stories/slider.lino +14 -0
- package/stories/switch.lino +10 -0
- package/stories/tab-panel.lino +7 -0
- package/stories/tab.lino +7 -0
- package/stories/tabs.lino +25 -0
- package/stories/text-area.lino +12 -0
- package/stories/text-field.lino +23 -0
- package/stories/titled-section.lino +7 -0
- package/stories/toast.lino +3 -0
- package/stories/toaster.lino +2 -0
- package/stories/tooltip.lino +3 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { numberOf } from '../layout/dom.js';
|
|
2
|
+
import { itemKey } from './source.js';
|
|
3
|
+
|
|
4
|
+
function horizontal(state, item, index, right) {
|
|
5
|
+
if (state.kind === 'table') {
|
|
6
|
+
state.cell = Math.max(
|
|
7
|
+
0,
|
|
8
|
+
Math.min(state.columns.length - 1, state.cell + (right ? 1 : -1))
|
|
9
|
+
);
|
|
10
|
+
return index;
|
|
11
|
+
}
|
|
12
|
+
if (state.kind !== 'tree') {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
const open = state.expanded.has(state.active);
|
|
16
|
+
if (right && item._tree.branch && !open) {
|
|
17
|
+
state.expand(item, true);
|
|
18
|
+
} else if (
|
|
19
|
+
right &&
|
|
20
|
+
open &&
|
|
21
|
+
state.items[index + 1]?._tree.level > item._tree.level
|
|
22
|
+
) {
|
|
23
|
+
return index + 1;
|
|
24
|
+
} else if (!right && open) {
|
|
25
|
+
state.expand(item, false);
|
|
26
|
+
} else if (!right && item._tree.parent !== null) {
|
|
27
|
+
return state.items.findIndex((x) => itemKey(x) === item._tree.parent);
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function endpoints(state, index, event, end) {
|
|
33
|
+
const command = event.ctrlKey || event.metaKey;
|
|
34
|
+
if (state.kind === 'table') {
|
|
35
|
+
state.cell = end ? state.columns.length - 1 : 0;
|
|
36
|
+
if (!command) {
|
|
37
|
+
return index;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return end ? state.items.length - 1 : 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function specialKey(state, item, index, event) {
|
|
44
|
+
const command = event.ctrlKey || event.metaKey;
|
|
45
|
+
if (event.key === 'Enter') {
|
|
46
|
+
state.activate(event);
|
|
47
|
+
if (state.kind === 'tree') {
|
|
48
|
+
state.expand(item);
|
|
49
|
+
}
|
|
50
|
+
return { handled: true };
|
|
51
|
+
}
|
|
52
|
+
if (event.key === ' ') {
|
|
53
|
+
state.select(item, event);
|
|
54
|
+
return { handled: true };
|
|
55
|
+
}
|
|
56
|
+
if (event.key === '*' && state.kind === 'tree') {
|
|
57
|
+
state.items
|
|
58
|
+
.filter((x) => x._tree.parent === item._tree.parent && x._tree.branch)
|
|
59
|
+
.forEach((x) => state.expand(x, true));
|
|
60
|
+
return { handled: true };
|
|
61
|
+
}
|
|
62
|
+
if (event.key === 'a' && command && state.mode() === 'multi') {
|
|
63
|
+
state.selected = new Set(state.items.map(itemKey));
|
|
64
|
+
state.viewport.render();
|
|
65
|
+
state.send('select', { item, selected: [...state.selected] });
|
|
66
|
+
return { handled: true };
|
|
67
|
+
}
|
|
68
|
+
if (command || event.altKey || event.key.length !== 1) {
|
|
69
|
+
return { handled: false };
|
|
70
|
+
}
|
|
71
|
+
return { handled: true, target: state.typeahead.handleKey(event.key, index) };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function keyTarget(state, item, index, event) {
|
|
75
|
+
const page = () =>
|
|
76
|
+
Math.max(
|
|
77
|
+
1,
|
|
78
|
+
Math.floor(
|
|
79
|
+
state.root.clientHeight / numberOf(state.el, 'item-height', 32)
|
|
80
|
+
)
|
|
81
|
+
);
|
|
82
|
+
const targets = {
|
|
83
|
+
ArrowDown: () => index + 1,
|
|
84
|
+
ArrowUp: () => index - 1,
|
|
85
|
+
Home: () => endpoints(state, index, event, false),
|
|
86
|
+
End: () => endpoints(state, index, event, true),
|
|
87
|
+
PageDown: () => Math.min(state.items.length - 1, index + page()),
|
|
88
|
+
PageUp: () => Math.max(0, index - page()),
|
|
89
|
+
ArrowRight: () => horizontal(state, item, index, true),
|
|
90
|
+
ArrowLeft: () => horizontal(state, item, index, false),
|
|
91
|
+
};
|
|
92
|
+
if (Object.hasOwn(targets, event.key)) {
|
|
93
|
+
return { handled: true, target: targets[event.key]() };
|
|
94
|
+
}
|
|
95
|
+
return specialKey(state, item, index, event);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Grid headers participate in the same roving focus and arrow navigation. */
|
|
99
|
+
function headerKey(state, event) {
|
|
100
|
+
const header = event.target.closest('[role=columnheader]');
|
|
101
|
+
const cells = [...state.header.header.children];
|
|
102
|
+
const at = cells.indexOf(header);
|
|
103
|
+
const to = {
|
|
104
|
+
ArrowLeft: at - 1,
|
|
105
|
+
ArrowRight: at + 1,
|
|
106
|
+
Home: 0,
|
|
107
|
+
End: cells.length - 1,
|
|
108
|
+
}[event.key];
|
|
109
|
+
if (to !== undefined) {
|
|
110
|
+
state.cell = Math.max(0, Math.min(cells.length - 1, to));
|
|
111
|
+
cells[state.cell].firstChild.focus();
|
|
112
|
+
} else if (event.key === 'ArrowDown' || event.key === 'PageDown') {
|
|
113
|
+
state.focus(0);
|
|
114
|
+
} else {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
event.stopPropagation();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function gridEndpoint(state, event) {
|
|
122
|
+
if (
|
|
123
|
+
state.kind !== 'table' ||
|
|
124
|
+
!(event.ctrlKey || event.metaKey) ||
|
|
125
|
+
!['Home', 'End'].includes(event.key)
|
|
126
|
+
) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
const end = event.key === 'End';
|
|
130
|
+
state.cell = end ? Math.max(0, state.columns.length - 1) : 0;
|
|
131
|
+
if (end) {
|
|
132
|
+
state.focus(state.items.length - 1);
|
|
133
|
+
} else {
|
|
134
|
+
state.header.header.firstChild?.firstChild.focus();
|
|
135
|
+
}
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
event.stopPropagation();
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function firstRowUp(state, index, event) {
|
|
142
|
+
if (state.kind !== 'table' || index !== 0 || event.key !== 'ArrowUp') {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
event.preventDefault();
|
|
146
|
+
state.header.header.children[state.cell]?.firstChild.focus();
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function dataKeydown(state, event) {
|
|
151
|
+
if (
|
|
152
|
+
event.target.closest?.(
|
|
153
|
+
'input,textarea,select,[contenteditable=true],.ui-data-resize'
|
|
154
|
+
)
|
|
155
|
+
) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (gridEndpoint(state, event)) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (state.kind === 'table' && event.target.closest?.('.ui-data-header')) {
|
|
162
|
+
return headerKey(state, event);
|
|
163
|
+
}
|
|
164
|
+
if (event.target.closest?.('button')) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const index = state.items.findIndex((item) => itemKey(item) === state.active);
|
|
168
|
+
if (index < 0) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (firstRowUp(state, index, event)) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const result = keyTarget(state, state.items[index], index, event);
|
|
175
|
+
if (!result.handled) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
event.preventDefault();
|
|
179
|
+
event.stopPropagation();
|
|
180
|
+
if (result.target >= 0) {
|
|
181
|
+
state.focus(result.target, event, selectOnMove(state, event));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function selectOnMove(state, event) {
|
|
186
|
+
return (
|
|
187
|
+
state.kind !== 'table' && (state.mode() === 'single' || event.shiftKey)
|
|
188
|
+
);
|
|
189
|
+
}
|
package/src/data/list.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/** Shared controller of the listbox, tree and grid, preserving native scrolling. */
|
|
2
|
+
import { expandTree } from './tree.js';
|
|
3
|
+
import { drawRow, refreshData } from './render-data.js';
|
|
4
|
+
import { dataKeydown } from './keyboard.js';
|
|
5
|
+
import { defineKind } from '@link-foundation/ui-dom';
|
|
6
|
+
import {
|
|
7
|
+
atomOf,
|
|
8
|
+
controllerOf,
|
|
9
|
+
dropController,
|
|
10
|
+
flagOf,
|
|
11
|
+
isLive,
|
|
12
|
+
make,
|
|
13
|
+
numberOf,
|
|
14
|
+
} from '../layout/dom.js';
|
|
15
|
+
import { createTypeahead } from '../a11y/typeahead.js';
|
|
16
|
+
import { selectData, focusData } from './selection-dom.js';
|
|
17
|
+
import { itemKey, sourceOf } from './source.js';
|
|
18
|
+
import { createViewport } from './viewport.js';
|
|
19
|
+
import { createTableHeader } from './table.js';
|
|
20
|
+
|
|
21
|
+
const roles = { list: 'listbox', tree: 'tree', table: 'grid' };
|
|
22
|
+
|
|
23
|
+
function createData(node, el, ctx, kind) {
|
|
24
|
+
const document = ctx.document;
|
|
25
|
+
const root = make(document, 'div', { class: 'ui-data-scroll' });
|
|
26
|
+
const body = make(document, 'div', {
|
|
27
|
+
class: 'ui-data-body',
|
|
28
|
+
role: 'presentation',
|
|
29
|
+
});
|
|
30
|
+
const status = make(document, 'div', {
|
|
31
|
+
class: 'ui-data-status',
|
|
32
|
+
role: 'status',
|
|
33
|
+
'aria-live': 'polite',
|
|
34
|
+
});
|
|
35
|
+
const caption = make(document, 'div', { class: 'ui-data-caption' });
|
|
36
|
+
node.append(caption, root, status);
|
|
37
|
+
const state = {
|
|
38
|
+
node,
|
|
39
|
+
root,
|
|
40
|
+
body,
|
|
41
|
+
document,
|
|
42
|
+
el,
|
|
43
|
+
kind,
|
|
44
|
+
options: ctx.options.data ?? {},
|
|
45
|
+
active: null,
|
|
46
|
+
cell: 0,
|
|
47
|
+
selected: new Set(),
|
|
48
|
+
anchor: null,
|
|
49
|
+
expanded: new Set(),
|
|
50
|
+
loaded: new Map(),
|
|
51
|
+
pending: new Map(),
|
|
52
|
+
failures: new Map(),
|
|
53
|
+
widths: new Map(),
|
|
54
|
+
sort: null,
|
|
55
|
+
items: [],
|
|
56
|
+
columns: [],
|
|
57
|
+
disposed: false,
|
|
58
|
+
};
|
|
59
|
+
let header;
|
|
60
|
+
let unsubscribe;
|
|
61
|
+
const mode = () => atomOf(state.el, 'selection') ?? 'single';
|
|
62
|
+
const label = (item) => String(item.label ?? item.name ?? item.id);
|
|
63
|
+
const typeahead = createTypeahead(() => state.items.map(label), {
|
|
64
|
+
clock: state.options.clock,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
state.send = (type, detail) => {
|
|
68
|
+
const event = new document.defaultView.CustomEvent(`ui-${type}`, {
|
|
69
|
+
bubbles: true,
|
|
70
|
+
composed: true,
|
|
71
|
+
detail,
|
|
72
|
+
});
|
|
73
|
+
node.dispatchEvent(event);
|
|
74
|
+
};
|
|
75
|
+
const select = (item, event) => selectData(state, item, event);
|
|
76
|
+
const focus = (index, event, selectOnMove) =>
|
|
77
|
+
focusData(state, index, event, selectOnMove);
|
|
78
|
+
state.expand = (item, open) => expandTree(state, item, open);
|
|
79
|
+
|
|
80
|
+
state.trackSizes = () =>
|
|
81
|
+
state.columns
|
|
82
|
+
.map((c) => `${state.widths.get(c.name) ?? c.width}px`)
|
|
83
|
+
.join(' ');
|
|
84
|
+
if (kind === 'table') {
|
|
85
|
+
header = createTableHeader(state);
|
|
86
|
+
}
|
|
87
|
+
root.append(body);
|
|
88
|
+
const viewport = createViewport(root, body, {
|
|
89
|
+
estimate: numberOf(el, 'item-height', 32),
|
|
90
|
+
virtualized: flagOf(el, 'virtualized', true),
|
|
91
|
+
draw: (row, item, index) => drawRow(state, row, item, index),
|
|
92
|
+
changed: () => header?.render(),
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
Object.assign(state, {
|
|
96
|
+
viewport,
|
|
97
|
+
status,
|
|
98
|
+
caption,
|
|
99
|
+
header,
|
|
100
|
+
ctx,
|
|
101
|
+
mode,
|
|
102
|
+
label,
|
|
103
|
+
typeahead,
|
|
104
|
+
select,
|
|
105
|
+
focus,
|
|
106
|
+
});
|
|
107
|
+
state.refresh = () => refreshData(state);
|
|
108
|
+
|
|
109
|
+
const click = (event) => {
|
|
110
|
+
if (event.target.closest?.('[data-sort], .ui-data-resize')) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const row = event.target.closest?.('[data-key]');
|
|
114
|
+
const index = state.items.findIndex(
|
|
115
|
+
(item) => itemKey(item) === row?.dataset.key
|
|
116
|
+
);
|
|
117
|
+
if (index < 0) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
state.cell = Number(
|
|
121
|
+
event.target.closest?.('[data-cell]')?.dataset.cell ?? 0
|
|
122
|
+
);
|
|
123
|
+
focus(index);
|
|
124
|
+
select(state.items[index], event);
|
|
125
|
+
if (kind === 'tree' && state.items[index]._tree.branch) {
|
|
126
|
+
state.expand(state.items[index]);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const activate = (event) => activateData(state, event);
|
|
130
|
+
state.activate = activate;
|
|
131
|
+
const keydown = (event) => dataKeydown(state, event);
|
|
132
|
+
node.addEventListener('click', click);
|
|
133
|
+
node.addEventListener('dblclick', activate);
|
|
134
|
+
node.addEventListener('keydown', keydown);
|
|
135
|
+
const onFocus = (event) => focusChanged(state, event);
|
|
136
|
+
node.addEventListener('focusin', onFocus);
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
state,
|
|
140
|
+
viewport,
|
|
141
|
+
update(next) {
|
|
142
|
+
state.el = next;
|
|
143
|
+
const nextSource = sourceOf(next, state.options);
|
|
144
|
+
if (state.source !== nextSource) {
|
|
145
|
+
unsubscribe?.();
|
|
146
|
+
state.source = nextSource;
|
|
147
|
+
unsubscribe = nextSource?.subscribe?.(() => state.refresh());
|
|
148
|
+
}
|
|
149
|
+
state.refresh();
|
|
150
|
+
},
|
|
151
|
+
disconnect() {
|
|
152
|
+
state.disposed = true;
|
|
153
|
+
unsubscribe?.();
|
|
154
|
+
viewport.disconnect();
|
|
155
|
+
header?.disconnect();
|
|
156
|
+
typeahead.reset();
|
|
157
|
+
for (const abort of state.pending.values()) {
|
|
158
|
+
abort.abort();
|
|
159
|
+
}
|
|
160
|
+
node.removeEventListener('click', click);
|
|
161
|
+
node.removeEventListener('dblclick', activate);
|
|
162
|
+
node.removeEventListener('keydown', keydown);
|
|
163
|
+
node.removeEventListener('focusin', onFocus);
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Register one data kind. Import the data entry point to register all three. */
|
|
169
|
+
export function registerDataKind(kind) {
|
|
170
|
+
defineKind(kind, {
|
|
171
|
+
tag: `ui-${kind}`,
|
|
172
|
+
childrenSlot: null,
|
|
173
|
+
create: (el, ctx) => ctx.document.createElement(`ui-${kind}`),
|
|
174
|
+
update(node, el, changed, ctx) {
|
|
175
|
+
if (isLive(ctx)) {
|
|
176
|
+
controllerOf(node, () => createData(node, el, ctx, kind)).update(el);
|
|
177
|
+
} else {
|
|
178
|
+
node.setAttribute('role', roles[kind]);
|
|
179
|
+
node.setAttribute(
|
|
180
|
+
'aria-label',
|
|
181
|
+
atomOf(el, 'label') ?? atomOf(el, 'caption') ?? kind
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
destroy: dropController,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
registerDataKind('list');
|
|
189
|
+
|
|
190
|
+
function activateData(state, event) {
|
|
191
|
+
const item = state.items.find((item) => itemKey(item) === state.active);
|
|
192
|
+
if (item) {
|
|
193
|
+
state.send('activate', { item, originalEvent: event.type });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function focusChanged(state, event) {
|
|
198
|
+
const { node, viewport } = state;
|
|
199
|
+
if (event.target.closest?.('.ui-data-header')) {
|
|
200
|
+
state.inHeader = true;
|
|
201
|
+
state.cell = [...state.header.header.children].indexOf(
|
|
202
|
+
event.target.closest('[role=columnheader]')
|
|
203
|
+
);
|
|
204
|
+
viewport.render();
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const row = event.target.closest?.('[data-key]');
|
|
208
|
+
if (row && node.contains(row)) {
|
|
209
|
+
state.inHeader = false;
|
|
210
|
+
state.active = row.dataset.key;
|
|
211
|
+
state.cell = Number(event.target.dataset.cell ?? 0);
|
|
212
|
+
viewport.setActive(state.active);
|
|
213
|
+
viewport.render();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { atomOf, flagOf } from '../layout/dom.js';
|
|
2
|
+
import { itemKey, itemsOf } from './source.js';
|
|
3
|
+
import { sortItems } from './sort-model.js';
|
|
4
|
+
import { flattenTree } from './tree-model.js';
|
|
5
|
+
import { columnsOf, drawCells } from './table.js';
|
|
6
|
+
const roles = { list: 'listbox', tree: 'tree', table: 'grid' };
|
|
7
|
+
const rowRoles = { list: 'option', tree: 'treeitem', table: 'row' };
|
|
8
|
+
|
|
9
|
+
export function drawRow(state, row, item, index) {
|
|
10
|
+
const { kind, document, label } = state;
|
|
11
|
+
const key = itemKey(item);
|
|
12
|
+
row.setAttribute('role', rowRoles[kind]);
|
|
13
|
+
row.setAttribute('aria-selected', String(state.selected.has(key)));
|
|
14
|
+
row.tabIndex = kind !== 'table' && key === state.active ? 0 : -1;
|
|
15
|
+
if (kind === 'table') {
|
|
16
|
+
drawCells(state, row, item, index);
|
|
17
|
+
} else {
|
|
18
|
+
row.setAttribute('aria-setsize', item._tree?.size ?? state.items.length);
|
|
19
|
+
row.setAttribute('aria-posinset', item._tree?.position ?? index + 1);
|
|
20
|
+
if (kind === 'tree') {
|
|
21
|
+
row.setAttribute('aria-level', item._tree.level);
|
|
22
|
+
row.style.paddingInlineStart = `${(item._tree.level - 1) * 20 + 8}px`;
|
|
23
|
+
if (item._tree.branch) {
|
|
24
|
+
row.setAttribute('aria-expanded', String(state.expanded.has(key)));
|
|
25
|
+
} else {
|
|
26
|
+
row.removeAttribute('aria-expanded');
|
|
27
|
+
}
|
|
28
|
+
row.setAttribute('aria-busy', String(state.pending.has(key)));
|
|
29
|
+
}
|
|
30
|
+
const templateName = atomOf(state.el, 'template');
|
|
31
|
+
const template = state.options.templates?.[templateName];
|
|
32
|
+
if (row._item !== item || row._template !== templateName) {
|
|
33
|
+
const content = template ? template(item, { document }) : label(item);
|
|
34
|
+
row.replaceChildren(
|
|
35
|
+
typeof content === 'string' ? document.createTextNode(content) : content
|
|
36
|
+
);
|
|
37
|
+
row._item = item;
|
|
38
|
+
row._template = templateName;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function refreshData(state) {
|
|
44
|
+
const { kind, source, header, body, ctx } = state;
|
|
45
|
+
const keyList = () => state.items.map(itemKey);
|
|
46
|
+
if (state.disposed) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const raw = itemsOf(source) ?? [];
|
|
50
|
+
const collection = Array.isArray(raw) ? raw : [];
|
|
51
|
+
state.columns = kind === 'table' ? columnsOf(state.el) : [];
|
|
52
|
+
state.items =
|
|
53
|
+
kind === 'tree'
|
|
54
|
+
? flattenTree(collection, state.expanded, state.loaded)
|
|
55
|
+
: kind === 'table'
|
|
56
|
+
? sortItems(collection, state.sort ?? undefined)
|
|
57
|
+
: collection;
|
|
58
|
+
const keys = keyList();
|
|
59
|
+
const present = new Set(keys);
|
|
60
|
+
if (new Set(keys).size !== keys.length || keys.includes('undefined')) {
|
|
61
|
+
ctx.diagnostic('invalid-items', 'error', { property: 'items' });
|
|
62
|
+
state.items = []; // Stable, unique ids are required for focus and measurements.
|
|
63
|
+
}
|
|
64
|
+
if (!present.has(state.active)) {
|
|
65
|
+
state.active = state.items.length ? itemKey(state.items[0]) : null;
|
|
66
|
+
}
|
|
67
|
+
state.selected = new Set(
|
|
68
|
+
[...state.selected].filter((key) => present.has(key))
|
|
69
|
+
);
|
|
70
|
+
renderStatus(state);
|
|
71
|
+
if (header) {
|
|
72
|
+
header.render();
|
|
73
|
+
header.header.style.gridTemplateColumns = state.trackSizes();
|
|
74
|
+
state.root.setAttribute('aria-rowcount', state.items.length + 1);
|
|
75
|
+
body.style.minWidth = `${state.columns.reduce((sum, c) => sum + (state.widths.get(c.name) ?? c.width), 0)}px`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function renderStatus(state) {
|
|
80
|
+
const { root, node, status, caption, kind, mode } = state;
|
|
81
|
+
const loading = flagOf(state.el, 'loading');
|
|
82
|
+
const error = atomOf(state.el, 'error');
|
|
83
|
+
const unavailable = loading || Boolean(error);
|
|
84
|
+
state.viewport.setActive(state.active);
|
|
85
|
+
state.viewport.setItems(unavailable ? [] : state.items);
|
|
86
|
+
root.hidden = unavailable || !state.items.length;
|
|
87
|
+
node.setAttribute('aria-busy', String(loading || state.pending.size > 0));
|
|
88
|
+
root.setAttribute(
|
|
89
|
+
'aria-label',
|
|
90
|
+
atomOf(state.el, 'label') ?? atomOf(state.el, 'caption') ?? kind
|
|
91
|
+
);
|
|
92
|
+
root.setAttribute('role', roles[kind]);
|
|
93
|
+
node.removeAttribute('role');
|
|
94
|
+
root.setAttribute('aria-multiselectable', String(mode() === 'multi'));
|
|
95
|
+
node.tabIndex = unavailable || !state.items.length ? 0 : -1;
|
|
96
|
+
status.textContent = statusMessage(state, loading, error);
|
|
97
|
+
caption.textContent =
|
|
98
|
+
kind === 'table' ? (atomOf(state.el, 'caption') ?? 'Table') : '';
|
|
99
|
+
caption.hidden = kind !== 'table';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function statusMessage(state, loading, error) {
|
|
103
|
+
if (loading) {
|
|
104
|
+
return 'Loading…';
|
|
105
|
+
}
|
|
106
|
+
if (error) {
|
|
107
|
+
return `Error: ${error}`;
|
|
108
|
+
}
|
|
109
|
+
if (state.failures.size) {
|
|
110
|
+
return `Error loading children: ${[...state.failures.values()].join('; ')}`;
|
|
111
|
+
}
|
|
112
|
+
if (state.pending.size) {
|
|
113
|
+
return 'Loading children…';
|
|
114
|
+
}
|
|
115
|
+
return state.items.length ? '' : (atomOf(state.el, 'empty') ?? 'No items');
|
|
116
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { selectItem } from './selection-model.js';
|
|
2
|
+
import { itemKey } from './source.js';
|
|
3
|
+
export function selectData(state, item, event = {}) {
|
|
4
|
+
const { viewport, mode } = state;
|
|
5
|
+
const keyList = () => state.items.map(itemKey);
|
|
6
|
+
const next = selectItem({
|
|
7
|
+
keys: keyList(),
|
|
8
|
+
selected: state.selected,
|
|
9
|
+
anchor: state.anchor,
|
|
10
|
+
key: itemKey(item),
|
|
11
|
+
mode: mode(),
|
|
12
|
+
shift: event.shiftKey,
|
|
13
|
+
toggle: event.ctrlKey || event.metaKey || event.key === ' ',
|
|
14
|
+
});
|
|
15
|
+
state.selected = next.selected;
|
|
16
|
+
state.anchor = next.anchor;
|
|
17
|
+
viewport.render();
|
|
18
|
+
state.send('select', { item, selected: [...state.selected] });
|
|
19
|
+
}
|
|
20
|
+
export function focusData(state, index, event = {}, selectOnMove = false) {
|
|
21
|
+
const { viewport, kind } = state;
|
|
22
|
+
const item =
|
|
23
|
+
state.items[Math.max(0, Math.min(index, state.items.length - 1))];
|
|
24
|
+
if (!item) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
state.inHeader = false;
|
|
28
|
+
state.active = itemKey(item);
|
|
29
|
+
viewport.setActive(state.active);
|
|
30
|
+
viewport.scrollTo(state.items.indexOf(item));
|
|
31
|
+
viewport.render();
|
|
32
|
+
const row = viewport.row(state.active);
|
|
33
|
+
(kind === 'table' ? row?.children[state.cell] : row)?.focus({
|
|
34
|
+
preventScroll: true,
|
|
35
|
+
});
|
|
36
|
+
if (selectOnMove && !event.ctrlKey && !event.metaKey) {
|
|
37
|
+
state.select(item, event);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Immutable selection state. Keys are stable strings, ordered by the visible collection. */
|
|
2
|
+
export function selectItem({
|
|
3
|
+
keys,
|
|
4
|
+
selected = new Set(),
|
|
5
|
+
anchor = null,
|
|
6
|
+
key,
|
|
7
|
+
mode = 'single',
|
|
8
|
+
shift = false,
|
|
9
|
+
toggle = false,
|
|
10
|
+
}) {
|
|
11
|
+
if (!keys.includes(key) || mode === 'none') {
|
|
12
|
+
return { selected: new Set(selected), anchor };
|
|
13
|
+
}
|
|
14
|
+
if (mode === 'single') {
|
|
15
|
+
return singleSelection(selected, key, toggle);
|
|
16
|
+
}
|
|
17
|
+
if (shift && keys.includes(anchor)) {
|
|
18
|
+
return rangeSelection(keys, selected, anchor, key, toggle);
|
|
19
|
+
}
|
|
20
|
+
const next = toggle ? new Set(selected) : new Set();
|
|
21
|
+
if (toggle && next.has(key)) {
|
|
22
|
+
next.delete(key);
|
|
23
|
+
} else {
|
|
24
|
+
next.add(key);
|
|
25
|
+
}
|
|
26
|
+
return { selected: next, anchor: key };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function singleSelection(selected, key, toggle) {
|
|
30
|
+
return {
|
|
31
|
+
selected: new Set(toggle && selected.has(key) ? [] : [key]),
|
|
32
|
+
anchor: key,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function rangeSelection(keys, selected, anchor, key, toggle) {
|
|
36
|
+
const a = keys.indexOf(anchor);
|
|
37
|
+
const b = keys.indexOf(key);
|
|
38
|
+
const range = keys.slice(Math.min(a, b), Math.max(a, b) + 1);
|
|
39
|
+
return {
|
|
40
|
+
selected: new Set(toggle ? [...selected, ...range] : range),
|
|
41
|
+
anchor,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Three-state sort cycle; stable ordering also on runtimes with older sort implementations. */
|
|
2
|
+
export function nextSort(current, column) {
|
|
3
|
+
return {
|
|
4
|
+
column,
|
|
5
|
+
direction:
|
|
6
|
+
current?.column !== column || current.direction === 'none'
|
|
7
|
+
? 'ascending'
|
|
8
|
+
: current.direction === 'ascending'
|
|
9
|
+
? 'descending'
|
|
10
|
+
: 'none',
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function sortItems(items, { column, direction = 'none' } = {}, compare) {
|
|
15
|
+
if (direction === 'none') {
|
|
16
|
+
return [...items];
|
|
17
|
+
}
|
|
18
|
+
const comparator =
|
|
19
|
+
compare ??
|
|
20
|
+
((a, b) => {
|
|
21
|
+
const left = a[column];
|
|
22
|
+
const right = b[column];
|
|
23
|
+
if (left === right) {
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
if (left === null || left === undefined) {
|
|
27
|
+
return -1;
|
|
28
|
+
}
|
|
29
|
+
if (right === null || right === undefined) {
|
|
30
|
+
return 1;
|
|
31
|
+
}
|
|
32
|
+
return left < right ? -1 : 1;
|
|
33
|
+
});
|
|
34
|
+
return items
|
|
35
|
+
.map((item, index) => ({ item, index }))
|
|
36
|
+
.sort(
|
|
37
|
+
(a, b) =>
|
|
38
|
+
(direction === 'descending' ? -1 : 1) * comparator(a.item, b.item) ||
|
|
39
|
+
a.index - b.index
|
|
40
|
+
)
|
|
41
|
+
.map(({ item }) => item);
|
|
42
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** A tiny observable adapter; hosts may also supply any {get, subscribe} source. */
|
|
2
|
+
export function createDataSource(initial = []) {
|
|
3
|
+
let items = initial;
|
|
4
|
+
const listeners = new Set();
|
|
5
|
+
return {
|
|
6
|
+
get: () => items,
|
|
7
|
+
set(next) {
|
|
8
|
+
items = next;
|
|
9
|
+
for (const listener of listeners) {
|
|
10
|
+
listener(items);
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
subscribe(listener) {
|
|
14
|
+
listeners.add(listener);
|
|
15
|
+
return () => listeners.delete(listener);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Read scalar/record values already parsed by ui-core, never text. */
|
|
21
|
+
function valueOf(value) {
|
|
22
|
+
if (!Array.isArray(value)) {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
return Object.fromEntries(
|
|
26
|
+
value
|
|
27
|
+
.filter(Array.isArray)
|
|
28
|
+
.map(([name, ...values]) => [
|
|
29
|
+
name,
|
|
30
|
+
values.length === 1 ? valueOf(values[0]) : values.map(valueOf),
|
|
31
|
+
])
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function sourceOf(el, options) {
|
|
36
|
+
const values = el.props.get('items') ?? [];
|
|
37
|
+
if (Array.isArray(values[0]) && values[0][0] === 'param') {
|
|
38
|
+
return options.sources?.[values[0][1]] ?? [];
|
|
39
|
+
}
|
|
40
|
+
return values.map(valueOf);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const itemsOf = (source) =>
|
|
44
|
+
typeof source?.get === 'function' ? source.get() : source;
|
|
45
|
+
export const itemKey = (item) => String(item.id);
|