@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,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers shared by the control kinds: property readers, part elements
|
|
3
|
+
* (`data-part` with the Open UI part names), the machine of a control and
|
|
4
|
+
* the `ui-change` event a control dispatches once per committed value.
|
|
5
|
+
*
|
|
6
|
+
* A control runs its machine (`machines/<kind>.lino`) with
|
|
7
|
+
* `interpret` and binds it to its parts with `bindMachine`. Element
|
|
8
|
+
* properties reach the machine through `control.assign`, and a value the
|
|
9
|
+
* user commits leaves through `control.commit`: a bubbling `ui-change`
|
|
10
|
+
* event whose detail `{source, property, value}` `mountControls` writes to
|
|
11
|
+
* the store in one `setProp` before the `(on change ...)` handlers run.
|
|
12
|
+
*
|
|
13
|
+
* In the static document of ui-dom `renderToHTML` (no window) and when the
|
|
14
|
+
* element names its own `(machine ...)` (run by `mountBehavior`), the
|
|
15
|
+
* machine only writes the attributes of its parts; it listens to nothing.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { bindMachine } from '../a11y/bind-machine.js';
|
|
19
|
+
import { interpret } from '../behavior/interpret.js';
|
|
20
|
+
import { createMachine } from '../behavior/parse-machine.js';
|
|
21
|
+
import { atomOf, flagOf, isLive, numberOf, setText } from '../layout/dom.js';
|
|
22
|
+
import { CONTROL_MACHINES } from './machines/index.js';
|
|
23
|
+
|
|
24
|
+
export { atomOf, flagOf, isLive, numberOf, setText };
|
|
25
|
+
|
|
26
|
+
/** The prefix of the DOM ids of control parts. */
|
|
27
|
+
export const ID_PREFIX = 'ui-control';
|
|
28
|
+
|
|
29
|
+
/** The DOM id of a part of the control of a store element. */
|
|
30
|
+
export const partId = (id, part = 'input') => `${ID_PREFIX}-${id}-${part}`;
|
|
31
|
+
|
|
32
|
+
/** The guards of the control machines (besides `not-disabled`). */
|
|
33
|
+
export const CONTROL_GUARDS = Object.freeze({
|
|
34
|
+
editable: (context) => context.disabled !== true && context.readonly !== true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const parsed = new Map();
|
|
38
|
+
|
|
39
|
+
/** The parsed machine of a control kind (read once). */
|
|
40
|
+
export function controlMachine(kind) {
|
|
41
|
+
if (!parsed.has(kind)) {
|
|
42
|
+
parsed.set(kind, createMachine(CONTROL_MACHINES[kind]));
|
|
43
|
+
}
|
|
44
|
+
return parsed.get(kind);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Create a part element: `<tag data-part="name" ...attrs>`.
|
|
49
|
+
* @param {Document} document
|
|
50
|
+
* @param {string} tag
|
|
51
|
+
* @param {string|null} name the part name (`null`: no part)
|
|
52
|
+
* @param {Record<string, string>} [attrs]
|
|
53
|
+
*/
|
|
54
|
+
export function partOf(document, tag, name, attrs = {}) {
|
|
55
|
+
const node = document.createElement(tag);
|
|
56
|
+
if (name !== null) {
|
|
57
|
+
node.setAttribute('data-part', name);
|
|
58
|
+
}
|
|
59
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
60
|
+
node.setAttribute(key, value);
|
|
61
|
+
}
|
|
62
|
+
return node;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Set an attribute, or remove it when the value is `undefined`/`null`. */
|
|
66
|
+
export function setAttr(node, name, value) {
|
|
67
|
+
if (value === undefined || value === null) {
|
|
68
|
+
node.removeAttribute(name);
|
|
69
|
+
} else if (node.getAttribute(name) !== String(value)) {
|
|
70
|
+
node.setAttribute(name, String(value));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Mirror boolean properties to boolean attributes of a native element. */
|
|
75
|
+
export function syncFlags(node, el, names) {
|
|
76
|
+
for (const name of names) {
|
|
77
|
+
node.toggleAttribute(name, flagOf(el, name));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The visible label of a control: `<label data-part="label" for=...>`.
|
|
83
|
+
* An empty label is hidden and reported (`missing-label`) once.
|
|
84
|
+
*/
|
|
85
|
+
export function updateLabel(label, el, ctx, property = 'label') {
|
|
86
|
+
const text = atomOf(el, property);
|
|
87
|
+
setText(label, text);
|
|
88
|
+
const empty = text === undefined || text === '';
|
|
89
|
+
label.toggleAttribute('hidden', empty);
|
|
90
|
+
if (empty) {
|
|
91
|
+
ctx.diagnostic('missing-label', 'warning', { property });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Write the machine attributes of every part (static rendering); `owned`
|
|
97
|
+
* remembers them, so an attribute that is gone is removed.
|
|
98
|
+
*/
|
|
99
|
+
function writeAttrs(service, parts, owned) {
|
|
100
|
+
for (const name of Object.keys(service.machine.parts)) {
|
|
101
|
+
const node = parts[name];
|
|
102
|
+
if (node) {
|
|
103
|
+
node.setAttribute('data-part', name);
|
|
104
|
+
const attrs = service.attrs(name);
|
|
105
|
+
for (const attr of owned.get(name) ?? []) {
|
|
106
|
+
if (!Object.hasOwn(attrs, attr)) {
|
|
107
|
+
node.removeAttribute(attr);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
for (const [attr, value] of Object.entries(attrs)) {
|
|
111
|
+
setAttr(node, attr, value);
|
|
112
|
+
}
|
|
113
|
+
owned.set(name, Object.keys(attrs));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const controls = new WeakMap();
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Start the control of a node.
|
|
122
|
+
* @param {HTMLElement} node the kind's node
|
|
123
|
+
* @param {object} el the element model
|
|
124
|
+
* @param {object} ctx the kind context
|
|
125
|
+
* @param {object} options
|
|
126
|
+
* @param {string} [options.kind] the control kind (its own machine)
|
|
127
|
+
* @param {object} [options.machine] a parsed shared machine instead
|
|
128
|
+
* @param {Record<string, HTMLElement>} options.parts part elements
|
|
129
|
+
* @param {HTMLElement} options.target where `ui-change` is dispatched
|
|
130
|
+
* (the kind's event target)
|
|
131
|
+
* @param {Record<string, Function>} [options.actions] machine actions
|
|
132
|
+
* @param {Record<string, unknown>} [options.context] the initial context
|
|
133
|
+
* @param {boolean} [options.ignoreInFields] passed to `bindMachine`
|
|
134
|
+
* @param {(control: object) => void} [options.render] called after every
|
|
135
|
+
* machine change
|
|
136
|
+
*/
|
|
137
|
+
export function startControl(node, el, ctx, options) {
|
|
138
|
+
const report = (d) => ctx.diagnostic(d.code, d.severity, d.at);
|
|
139
|
+
const service = interpret(options.machine ?? controlMachine(options.kind), {
|
|
140
|
+
actions: options.actions,
|
|
141
|
+
guards: CONTROL_GUARDS,
|
|
142
|
+
context: options.context,
|
|
143
|
+
onDiagnostic: report,
|
|
144
|
+
});
|
|
145
|
+
const control = {
|
|
146
|
+
node,
|
|
147
|
+
service,
|
|
148
|
+
parts: options.parts,
|
|
149
|
+
value: undefined,
|
|
150
|
+
assign: (patch) => service.assign(patch),
|
|
151
|
+
commit(property, value) {
|
|
152
|
+
const win = ctx.document.defaultView;
|
|
153
|
+
if (!win) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
options.target.dispatchEvent(
|
|
157
|
+
new win.CustomEvent('ui-change', {
|
|
158
|
+
bubbles: true,
|
|
159
|
+
composed: true,
|
|
160
|
+
detail: Object.freeze({
|
|
161
|
+
source: node.getAttribute('data-link-id'),
|
|
162
|
+
property,
|
|
163
|
+
value,
|
|
164
|
+
}),
|
|
165
|
+
})
|
|
166
|
+
);
|
|
167
|
+
},
|
|
168
|
+
unbind: () => {},
|
|
169
|
+
};
|
|
170
|
+
const live = isLive(ctx) && !el.props.has('machine');
|
|
171
|
+
const owned = new Map();
|
|
172
|
+
if (live) {
|
|
173
|
+
control.unbind = bindMachine(node, service, options.parts, {
|
|
174
|
+
onDiagnostic: report,
|
|
175
|
+
ignoreInFields: options.ignoreInFields ?? true,
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
writeAttrs(service, options.parts, owned);
|
|
179
|
+
}
|
|
180
|
+
const unsubscribe = service.subscribe(() => {
|
|
181
|
+
if (!live) {
|
|
182
|
+
writeAttrs(service, options.parts, owned);
|
|
183
|
+
}
|
|
184
|
+
options.render?.(control);
|
|
185
|
+
});
|
|
186
|
+
control.stop = () => {
|
|
187
|
+
unsubscribe();
|
|
188
|
+
control.unbind();
|
|
189
|
+
service.stop();
|
|
190
|
+
};
|
|
191
|
+
controls.set(node, control);
|
|
192
|
+
return control;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The control of a node (`undefined` before `create`). */
|
|
196
|
+
export const controlOf = (node) => controls.get(node);
|
|
197
|
+
|
|
198
|
+
/** Stop the control of a node (the kind's `destroy`). */
|
|
199
|
+
export function stopControl(node) {
|
|
200
|
+
controls.get(node)?.stop();
|
|
201
|
+
controls.delete(node);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Update the value a control holds from its element property: the value
|
|
206
|
+
* is remembered first, so the machine change it causes is not committed
|
|
207
|
+
* back to the store.
|
|
208
|
+
*/
|
|
209
|
+
export function receive(control, value, patch = {}) {
|
|
210
|
+
control.value = value;
|
|
211
|
+
control.assign({ value, ...patch });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Commit the context value of a control when the user changed it: one
|
|
216
|
+
* `ui-change` per new value.
|
|
217
|
+
*/
|
|
218
|
+
export function commitValue(
|
|
219
|
+
control,
|
|
220
|
+
property = 'value',
|
|
221
|
+
value = control.service.context.value
|
|
222
|
+
) {
|
|
223
|
+
if (value !== control.value) {
|
|
224
|
+
control.value = value;
|
|
225
|
+
control.commit(property, value);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Bring the machine of a control to a value its element property holds:
|
|
231
|
+
* nothing is committed back, and `disabled` (which only stops the user)
|
|
232
|
+
* does not stop the host.
|
|
233
|
+
*/
|
|
234
|
+
export function driveControl(control, event, value, disabled) {
|
|
235
|
+
control.driven = true;
|
|
236
|
+
control.assign({ disabled: false });
|
|
237
|
+
control.service.send(event);
|
|
238
|
+
control.assign({ disabled });
|
|
239
|
+
control.value = value;
|
|
240
|
+
control.driven = false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** The text of a property, `fallback` when it is missing. */
|
|
244
|
+
export const textOf = (el, name, fallback = '') => atomOf(el, name) ?? fallback;
|
|
245
|
+
|
|
246
|
+
/** The number of a property, or `null` when it is missing or not a number. */
|
|
247
|
+
export const maybeNumber = (el, name) => numberOf(el, name, null);
|
|
248
|
+
|
|
249
|
+
const updated = new WeakSet();
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Whether an update touches a property: every property on the first
|
|
253
|
+
* update of a node (ui-dom passes only the properties an element has, and
|
|
254
|
+
* a missing one still needs its default), then the changed ones.
|
|
255
|
+
* @returns {(name: string) => boolean}
|
|
256
|
+
*/
|
|
257
|
+
export function changesOf(node, changed) {
|
|
258
|
+
const first = !updated.has(node);
|
|
259
|
+
updated.add(node);
|
|
260
|
+
return (name) => first || changed.includes(name);
|
|
261
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `icon-button`: a button that shows only an icon. Its `label` is the
|
|
3
|
+
* accessible name (`aria-label`) and the tooltip (`title`); without one
|
|
4
|
+
* the button has no name, which is a `missing-label` error.
|
|
5
|
+
*
|
|
6
|
+
* (close: icon-button close (label 'Close') (on press (emit close)))
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createButton, updateButton } from './button.js';
|
|
10
|
+
import { atomOf, changesOf, setAttr, stopControl } from './dom.js';
|
|
11
|
+
|
|
12
|
+
export const ICON_BUTTON_KIND = Object.freeze({
|
|
13
|
+
tag: 'ui-icon-button',
|
|
14
|
+
childrenSlot: null,
|
|
15
|
+
attributes: { variant: 'string', disabled: 'boolean' },
|
|
16
|
+
create: (el, ctx) =>
|
|
17
|
+
createButton(el, ctx, { tag: 'ui-icon-button', kind: 'icon-button' }),
|
|
18
|
+
eventTarget: (node) => node.firstChild,
|
|
19
|
+
update(node, el, changed, ctx) {
|
|
20
|
+
const has = changesOf(node, changed);
|
|
21
|
+
updateButton(node, el, has);
|
|
22
|
+
if (has('label')) {
|
|
23
|
+
const label = atomOf(el, 'label');
|
|
24
|
+
const button = node.firstChild;
|
|
25
|
+
setAttr(button, 'aria-label', label || undefined);
|
|
26
|
+
setAttr(button, 'title', label || undefined);
|
|
27
|
+
if (!label) {
|
|
28
|
+
ctx.diagnostic('missing-label', 'error', { property: 'label' });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
destroy: stopControl,
|
|
33
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
|
|
3
|
+
import type { Vocabulary } from '@link-foundation/ui-core';
|
|
4
|
+
import type {
|
|
5
|
+
BehaviorHandle,
|
|
6
|
+
BehaviorOptions,
|
|
7
|
+
} from '@link-foundation/ui-components/behavior/dom';
|
|
8
|
+
|
|
9
|
+
/* Control kinds */
|
|
10
|
+
|
|
11
|
+
export type ControlKind =
|
|
12
|
+
| 'button'
|
|
13
|
+
| 'icon-button'
|
|
14
|
+
| 'label'
|
|
15
|
+
| 'link'
|
|
16
|
+
| 'text-field'
|
|
17
|
+
| 'text-area'
|
|
18
|
+
| 'number-field'
|
|
19
|
+
| 'color-field'
|
|
20
|
+
| 'checkbox'
|
|
21
|
+
| 'radio-group'
|
|
22
|
+
| 'radio'
|
|
23
|
+
| 'switch'
|
|
24
|
+
| 'slider'
|
|
25
|
+
| 'select'
|
|
26
|
+
| 'combobox'
|
|
27
|
+
| 'option'
|
|
28
|
+
| 'progress'
|
|
29
|
+
| 'badge'
|
|
30
|
+
| 'avatar'
|
|
31
|
+
| 'chip'
|
|
32
|
+
| 'accordion';
|
|
33
|
+
|
|
34
|
+
/** The machine and the ARIA role of a control kind (`null` when none). */
|
|
35
|
+
export interface ControlKindMeta {
|
|
36
|
+
machine: string | null;
|
|
37
|
+
role: string | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The control kinds in Links Notation (`kinds.lino`). */
|
|
41
|
+
export const CONTROL_KINDS_TEXT: string;
|
|
42
|
+
/** The control kind names, in kinds.lino order. */
|
|
43
|
+
export function controlKindNames(): ControlKind[];
|
|
44
|
+
/** `undefined` for a kind that is not a control. */
|
|
45
|
+
export function controlKindMeta(kind: string): ControlKindMeta | undefined;
|
|
46
|
+
/** The ui-dialect vocabulary with the control kinds. */
|
|
47
|
+
export function controlsVocabulary(): Vocabulary;
|
|
48
|
+
|
|
49
|
+
/** The machine text of every stateful control, by kind. */
|
|
50
|
+
export const CONTROL_MACHINES: Readonly<Partial<Record<ControlKind, string>>>;
|
|
51
|
+
/** The ui-dom kind renderer of every control, by kind. */
|
|
52
|
+
export const CONTROL_KIND_SPECS: Readonly<Record<ControlKind, object>>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Register the control kinds with ui-dom (no target) or add them to a
|
|
56
|
+
* kinds Map or object; returns the kind names.
|
|
57
|
+
*/
|
|
58
|
+
export function registerControls(
|
|
59
|
+
target?: Map<string, object> | Record<string, object>
|
|
60
|
+
): ControlKind[];
|
|
61
|
+
|
|
62
|
+
/** The `detail` of the `ui-change` event a control dispatches. */
|
|
63
|
+
export interface ControlChange {
|
|
64
|
+
source: string;
|
|
65
|
+
property: string;
|
|
66
|
+
value: string | null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Mount a document whose controls write every committed value to the
|
|
71
|
+
* store (one `setProp`) before the `(on change ...)` handlers run.
|
|
72
|
+
*/
|
|
73
|
+
export function mountControls(
|
|
74
|
+
container: Element,
|
|
75
|
+
text: string,
|
|
76
|
+
options?: BehaviorOptions & Record<string, unknown>
|
|
77
|
+
): BehaviorHandle;
|
|
78
|
+
|
|
79
|
+
/* Helpers */
|
|
80
|
+
|
|
81
|
+
/** The ID prefix of the control parts (`ui-control-<id>-<part>`). */
|
|
82
|
+
export const ID_PREFIX: string;
|
|
83
|
+
export function partId(id: string, part?: string): string;
|
|
84
|
+
|
|
85
|
+
export interface NumericRange {
|
|
86
|
+
min?: number;
|
|
87
|
+
max?: number;
|
|
88
|
+
step?: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Round to the step from the minimum and clamp to the range. */
|
|
92
|
+
export function clampStep(value: number, range?: NumericRange): number;
|
|
93
|
+
/** The number of decimals of a finite number. */
|
|
94
|
+
export function decimalsOf(value: number): number;
|
|
95
|
+
/** The value a key moves a numeric control to; `null` for other keys. */
|
|
96
|
+
export function nextValue(
|
|
97
|
+
key: string,
|
|
98
|
+
state?: NumericRange & {
|
|
99
|
+
value?: number;
|
|
100
|
+
shift?: boolean;
|
|
101
|
+
rtl?: boolean;
|
|
102
|
+
snapping?: 'nearest' | 'directional';
|
|
103
|
+
}
|
|
104
|
+
): number | null;
|
|
105
|
+
export function formatNumber(value: unknown): string;
|
|
106
|
+
export function readNumber(
|
|
107
|
+
text: string,
|
|
108
|
+
range?: NumericRange & { normalization?: 'snap' | 'clamp' }
|
|
109
|
+
): { value: number | null; invalid: false } | { invalid: true };
|
|
110
|
+
/** The fraction (0–1) of the range a value is at. */
|
|
111
|
+
export function fractionOf(
|
|
112
|
+
value: number,
|
|
113
|
+
range: { min: number; max: number }
|
|
114
|
+
): number;
|
|
115
|
+
/** The `value-text` template with `{value}` replaced, else the number. */
|
|
116
|
+
export function valueText(
|
|
117
|
+
template: string | null | undefined,
|
|
118
|
+
value: number
|
|
119
|
+
): string;
|
|
120
|
+
/** The normalised `#rrggbb` of a hex colour text, or `null`. */
|
|
121
|
+
export function formatHex(value: unknown): string | null;
|
|
122
|
+
export function parseHex(
|
|
123
|
+
value: unknown
|
|
124
|
+
): { r: number; g: number; b: number } | null;
|
|
125
|
+
/** The initials of a name (first and last word). */
|
|
126
|
+
export function initialsOf(name: string): string;
|
|
127
|
+
/** Whether a link target looks like a URL rather than a bare word. */
|
|
128
|
+
export function isUrl(href: string): boolean;
|
|
129
|
+
/** Hide the options of a listbox whose label does not contain the text. */
|
|
130
|
+
export function filterOptions(listbox: Element, text: string): void;
|
|
131
|
+
export const BADGE_TONES: readonly ['neutral', 'primary', 'error'];
|
|
132
|
+
export const CHIP_TONES: readonly ['neutral', 'primary', 'error'];
|
|
133
|
+
export const CHIP_VARIANTS: readonly ['filled', 'outlined'];
|
|
134
|
+
export const PROGRESS_SHAPES: readonly ['linear', 'circular'];
|
|
135
|
+
|
|
136
|
+
/* Stories and the catalog */
|
|
137
|
+
|
|
138
|
+
export interface StorySet {
|
|
139
|
+
id: string;
|
|
140
|
+
component: string;
|
|
141
|
+
stories: readonly { name: string; element: readonly unknown[] }[];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface CatalogEntry {
|
|
145
|
+
title: string | null;
|
|
146
|
+
stories: string | null;
|
|
147
|
+
apg: string | null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Read a story file (`(<kind>-stories: stories (component KIND) ...)`). */
|
|
151
|
+
export function parseStories(text: string): StorySet;
|
|
152
|
+
/** Read the control catalog: the entries by kind name, in file order. */
|
|
153
|
+
export function parseCatalog(text: string): Map<string, CatalogEntry>;
|
|
154
|
+
/** The document ID of a story: `<component>-<story>`. */
|
|
155
|
+
export function storyId(component: string, name: string): string;
|
|
156
|
+
/** Every story of the sets as one document. */
|
|
157
|
+
export function storiesDocument(
|
|
158
|
+
sets: readonly StorySet[],
|
|
159
|
+
options?: { id?: string }
|
|
160
|
+
): string;
|
|
161
|
+
|
|
162
|
+
/** Hex or integer rgb(r, g, b) normalized to #rrggbb. */
|
|
163
|
+
export function formatColor(value: unknown): string | null;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The basic controls of ui-components: one module per kind (button,
|
|
3
|
+
* text-field, select, ...), each a ui-dom kind renderer that runs the
|
|
4
|
+
* control's behaviour machine (`machines/<kind>.lino`) over native
|
|
5
|
+
* elements in light DOM, with Open UI part names in `data-part`.
|
|
6
|
+
*
|
|
7
|
+
* `registerControls()` registers every control kind with ui-dom
|
|
8
|
+
* (`defineKind`), or into a kinds Map/object for the `kinds` render
|
|
9
|
+
* option. `mountControls(container, text, options)` mounts a document with
|
|
10
|
+
* `mountBehavior` and writes every value a control commits to the store as
|
|
11
|
+
* one `setProp` before the element's `(on change ...)` handlers run.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { defineKind } from '@link-foundation/ui-dom';
|
|
15
|
+
import { mountBehavior } from '../behavior/dom.js';
|
|
16
|
+
import { ACCORDION_KIND } from './accordion.js';
|
|
17
|
+
import { AVATAR_KIND } from './avatar.js';
|
|
18
|
+
import { BADGE_KIND } from './badge.js';
|
|
19
|
+
import { BUTTON_KIND } from './button.js';
|
|
20
|
+
import { CHECKBOX_KIND } from './checkbox.js';
|
|
21
|
+
import { CHIP_KIND } from './chip.js';
|
|
22
|
+
import { COLOR_FIELD_KIND } from './color-field.js';
|
|
23
|
+
import { COMBOBOX_KIND } from './combobox.js';
|
|
24
|
+
import { ICON_BUTTON_KIND } from './icon-button.js';
|
|
25
|
+
import { LABEL_KIND } from './label.js';
|
|
26
|
+
import { LINK_KIND } from './link.js';
|
|
27
|
+
import { NUMBER_FIELD_KIND } from './number-field.js';
|
|
28
|
+
import { OPTION_KIND } from './option.js';
|
|
29
|
+
import { PROGRESS_KIND } from './progress.js';
|
|
30
|
+
import { RADIO_GROUP_KIND, RADIO_KIND } from './radio-group.js';
|
|
31
|
+
import { SELECT_KIND } from './select.js';
|
|
32
|
+
import { SLIDER_KIND } from './slider.js';
|
|
33
|
+
import { SWITCH_KIND } from './switch.js';
|
|
34
|
+
import { TEXT_AREA_KIND } from './text-area.js';
|
|
35
|
+
import { TEXT_FIELD_KIND } from './text-field.js';
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
controlKindMeta,
|
|
39
|
+
controlKindNames,
|
|
40
|
+
controlsVocabulary,
|
|
41
|
+
CONTROL_KINDS_TEXT,
|
|
42
|
+
} from './vocabulary.js';
|
|
43
|
+
export { CONTROL_MACHINES } from './machines/index.js';
|
|
44
|
+
export { formatHex, parseHex, formatColor } from './color.js';
|
|
45
|
+
export { clampStep, decimalsOf, nextValue } from './numeric-keys.js';
|
|
46
|
+
export { ID_PREFIX, partId } from './dom.js';
|
|
47
|
+
export { initialsOf } from './avatar.js';
|
|
48
|
+
export { isUrl } from './link.js';
|
|
49
|
+
export { formatNumber, readNumber } from './number-field.js';
|
|
50
|
+
export { fractionOf, valueText } from './slider.js';
|
|
51
|
+
export { filterOptions } from './combobox.js';
|
|
52
|
+
export { BADGE_TONES } from './badge.js';
|
|
53
|
+
export { CHIP_TONES, CHIP_VARIANTS } from './chip.js';
|
|
54
|
+
export { PROGRESS_SHAPES } from './progress.js';
|
|
55
|
+
export {
|
|
56
|
+
parseCatalog,
|
|
57
|
+
parseStories,
|
|
58
|
+
storiesDocument,
|
|
59
|
+
storyId,
|
|
60
|
+
} from './stories.js';
|
|
61
|
+
|
|
62
|
+
/** The kind renderer of every basic control, by kind name. */
|
|
63
|
+
export const CONTROL_KIND_SPECS = Object.freeze({
|
|
64
|
+
button: BUTTON_KIND,
|
|
65
|
+
'icon-button': ICON_BUTTON_KIND,
|
|
66
|
+
label: LABEL_KIND,
|
|
67
|
+
link: LINK_KIND,
|
|
68
|
+
'text-field': TEXT_FIELD_KIND,
|
|
69
|
+
'text-area': TEXT_AREA_KIND,
|
|
70
|
+
'number-field': NUMBER_FIELD_KIND,
|
|
71
|
+
'color-field': COLOR_FIELD_KIND,
|
|
72
|
+
checkbox: CHECKBOX_KIND,
|
|
73
|
+
'radio-group': RADIO_GROUP_KIND,
|
|
74
|
+
radio: RADIO_KIND,
|
|
75
|
+
switch: SWITCH_KIND,
|
|
76
|
+
slider: SLIDER_KIND,
|
|
77
|
+
select: SELECT_KIND,
|
|
78
|
+
combobox: COMBOBOX_KIND,
|
|
79
|
+
option: OPTION_KIND,
|
|
80
|
+
progress: PROGRESS_KIND,
|
|
81
|
+
chip: CHIP_KIND,
|
|
82
|
+
accordion: ACCORDION_KIND,
|
|
83
|
+
badge: BADGE_KIND,
|
|
84
|
+
avatar: AVATAR_KIND,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Register the control kinds. With no target they are registered with
|
|
89
|
+
* ui-dom (`defineKind`, for every render); with a Map or an object they
|
|
90
|
+
* are added to it (the `kinds` render option of one render).
|
|
91
|
+
* @param {Map<string, object>|Record<string, object>} [target]
|
|
92
|
+
* @returns {string[]} the registered kind names
|
|
93
|
+
*/
|
|
94
|
+
export function registerControls(target) {
|
|
95
|
+
for (const [kind, spec] of Object.entries(CONTROL_KIND_SPECS)) {
|
|
96
|
+
if (target === undefined) {
|
|
97
|
+
defineKind(kind, spec);
|
|
98
|
+
} else if (target instanceof Map) {
|
|
99
|
+
target.set(kind, spec);
|
|
100
|
+
} else {
|
|
101
|
+
target[kind] = spec;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return Object.keys(CONTROL_KIND_SPECS);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The kinds option of a mount: the controls under the caller's kinds. */
|
|
108
|
+
function kindsWith(kinds) {
|
|
109
|
+
if (kinds instanceof Map) {
|
|
110
|
+
return new Map([...Object.entries(CONTROL_KIND_SPECS), ...kinds]);
|
|
111
|
+
}
|
|
112
|
+
return { ...CONTROL_KIND_SPECS, ...(kinds ?? {}) };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Write a committed value to the store: one `setProp` (or `removeProp`
|
|
117
|
+
* for `null`), so it is one transaction and one patch.
|
|
118
|
+
*/
|
|
119
|
+
function writeChange(store, detail) {
|
|
120
|
+
const { source, property, value } = detail ?? {};
|
|
121
|
+
if (typeof source !== 'string' || !store.has(source)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (value === null || value === undefined) {
|
|
125
|
+
store.removeProp(source, property);
|
|
126
|
+
} else {
|
|
127
|
+
store.setProp(source, property, [String(value)]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Mount a document whose controls write their values to the store.
|
|
133
|
+
*
|
|
134
|
+
* The container listens to `ui-change` in the capture phase, so the store
|
|
135
|
+
* holds the new value when the `(on change ...)` handlers of the element
|
|
136
|
+
* run. A link click is offered to the `navigate` action when the host
|
|
137
|
+
* handles navigation (`host.navigate` or an own action registry); an
|
|
138
|
+
* unsafe URL is never followed.
|
|
139
|
+
* @param {HTMLElement} container
|
|
140
|
+
* @param {string} text the document (Links Notation)
|
|
141
|
+
* @param {object} [options] the options of `mountBehavior`
|
|
142
|
+
*/
|
|
143
|
+
export function mountControls(container, text, options = {}) {
|
|
144
|
+
const state = { store: null, registry: null };
|
|
145
|
+
const onChange = (event) => {
|
|
146
|
+
if (state.store !== null) {
|
|
147
|
+
writeChange(state.store, event.detail);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const routed =
|
|
151
|
+
typeof options.host?.navigate === 'function' ||
|
|
152
|
+
typeof options.actions?.dispatch === 'function';
|
|
153
|
+
const onNavigate = (event) => {
|
|
154
|
+
if (!routed || state.registry === null) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const { source, href } = event.detail ?? {};
|
|
158
|
+
const result = state.registry.dispatch('navigate', [href], {
|
|
159
|
+
source,
|
|
160
|
+
event: 'navigate',
|
|
161
|
+
});
|
|
162
|
+
if (result.ok || result.diagnostic?.code !== 'unhandled-action') {
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
container.addEventListener('ui-change', onChange, true);
|
|
167
|
+
container.addEventListener('ui-navigate', onNavigate);
|
|
168
|
+
const mounted = mountBehavior(container, text, {
|
|
169
|
+
...options,
|
|
170
|
+
kinds: kindsWith(options.kinds),
|
|
171
|
+
});
|
|
172
|
+
state.store = mounted.store;
|
|
173
|
+
state.registry = mounted.registry;
|
|
174
|
+
return {
|
|
175
|
+
...mounted,
|
|
176
|
+
destroy() {
|
|
177
|
+
container.removeEventListener('ui-change', onChange, true);
|
|
178
|
+
container.removeEventListener('ui-navigate', onNavigate);
|
|
179
|
+
mounted.destroy();
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|