@ironflock/widget-toolbar 0.0.0-stage → 1.0.1
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/README.md +110 -2
- package/dist/widget-toolbar.js +1331 -0
- package/dist/widget-toolbar.js.map +1 -0
- package/package.json +53 -5
- package/src/colors.ts +102 -0
- package/src/default-data.json +45 -0
- package/src/definition-schema.d.ts +192 -0
- package/src/definition-schema.json +317 -0
- package/src/layout.ts +153 -0
- package/src/tools.ts +430 -0
- package/src/widget-toolbar.ts +1289 -0
- package/thumbnail.png +0 -0
- package/thumbnail.webp +0 -0
package/src/layout.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which tools the bar shows, at which density, and which move into the ⋮ menu.
|
|
3
|
+
* Pure: it works on sizes the element measured, so it can be tested without a
|
|
4
|
+
* browser.
|
|
5
|
+
*
|
|
6
|
+
* Main axis: along the bar (width when horizontal, height when vertical).
|
|
7
|
+
* Cross axis: across it.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type Extent = { main: number; cross: number }
|
|
11
|
+
|
|
12
|
+
export type Item = {
|
|
13
|
+
kind: 'tool' | 'separator' | 'spacer'
|
|
14
|
+
/** The tool drawn with its label. */
|
|
15
|
+
full: Extent
|
|
16
|
+
/** The tool drawn as small as it gets: icon-only buttons and choices, a shorter slider. */
|
|
17
|
+
compact: Extent
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type LayoutInput = {
|
|
21
|
+
items: Item[]
|
|
22
|
+
/** The bar's content box. */
|
|
23
|
+
available: Extent
|
|
24
|
+
/** Space between two items on the main axis. */
|
|
25
|
+
gap: number
|
|
26
|
+
/** The ⋮ button. */
|
|
27
|
+
more: Extent
|
|
28
|
+
/**
|
|
29
|
+
* Whether the cross axis limits the tools: true for a vertical strip, where
|
|
30
|
+
* a switch with its label can be wider than the strip. Each tool then gets
|
|
31
|
+
* its labels where it fits across, is compact where only that fits, and
|
|
32
|
+
* goes to the menu where neither does. A horizontal bar centres its tools
|
|
33
|
+
* and lets the tile clip them.
|
|
34
|
+
*/
|
|
35
|
+
checkCross: boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type Density = 'full' | 'compact'
|
|
39
|
+
|
|
40
|
+
export type Placement = {
|
|
41
|
+
/** Indexes of the items in the bar, in order. */
|
|
42
|
+
visible: number[]
|
|
43
|
+
/** Indexes of the visible items drawn compact. */
|
|
44
|
+
compact: number[]
|
|
45
|
+
/** Indexes of the items in the menu, in order. Empty: no ⋮ button. */
|
|
46
|
+
overflow: number[]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Separators only between tools: none at either end, none next to another.
|
|
51
|
+
* Spacers don't count as tools, and in the menu they mean nothing.
|
|
52
|
+
*/
|
|
53
|
+
function tidy(items: Item[], indexes: number[], keepSpacers: boolean): number[] {
|
|
54
|
+
const kept = indexes.filter((i) => keepSpacers || items[i].kind !== 'spacer')
|
|
55
|
+
const result: number[] = []
|
|
56
|
+
// The last tool or separator kept; spacers sit between them unseen.
|
|
57
|
+
let last: 'none' | 'tool' | 'separator' = 'none'
|
|
58
|
+
kept.forEach((i, n) => {
|
|
59
|
+
const kind = items[i].kind
|
|
60
|
+
if (kind === 'separator') {
|
|
61
|
+
const toolAfter = kept.slice(n + 1).some((j) => items[j].kind === 'tool')
|
|
62
|
+
if (last !== 'tool' || !toolAfter) return
|
|
63
|
+
last = 'separator'
|
|
64
|
+
} else if (kind === 'tool') {
|
|
65
|
+
last = 'tool'
|
|
66
|
+
}
|
|
67
|
+
result.push(i)
|
|
68
|
+
})
|
|
69
|
+
return result
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function place(input: LayoutInput): Placement {
|
|
73
|
+
const { items, available, gap, more, checkCross } = input
|
|
74
|
+
const fitsAcross = (extent: Extent) => !checkCross || extent.cross <= available.cross
|
|
75
|
+
|
|
76
|
+
// A density per item, or undefined where the item fits across at neither.
|
|
77
|
+
type Choice = (Density | undefined)[]
|
|
78
|
+
const preferred: Choice = items.map((item) =>
|
|
79
|
+
item.kind !== 'tool' || fitsAcross(item.full) ? 'full' : fitsAcross(item.compact) ? 'compact' : undefined
|
|
80
|
+
)
|
|
81
|
+
const allCompact: Choice = items.map((item) =>
|
|
82
|
+
item.kind !== 'tool' || fitsAcross(item.compact) ? 'compact' : undefined
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
const done = (choice: Choice, visible: number[], overflow: number[]): Placement => {
|
|
86
|
+
const bar = tidy(items, visible, true)
|
|
87
|
+
const menu = tidy(items, overflow, false)
|
|
88
|
+
// A menu of separators alone is no menu.
|
|
89
|
+
const hasTools = menu.some((i) => items[i].kind === 'tool')
|
|
90
|
+
return {
|
|
91
|
+
visible: bar,
|
|
92
|
+
compact: bar.filter((i) => items[i].kind === 'tool' && choice[i] === 'compact'),
|
|
93
|
+
overflow: hasTools ? menu : []
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const length = (choice: Choice, indexes: number[]): number =>
|
|
98
|
+
indexes.reduce((sum, i, n) => sum + items[i][choice[i] ?? 'compact'].main + (n > 0 ? gap : 0), 0)
|
|
99
|
+
|
|
100
|
+
// Everything in the bar: with labels where possible; else compact the
|
|
101
|
+
// tools that save the most first, so the labels go where they cost most
|
|
102
|
+
// and the rest keep theirs.
|
|
103
|
+
if (!preferred.some((density) => density === undefined)) {
|
|
104
|
+
const shown = tidy(items, items.map((_, i) => i), true)
|
|
105
|
+
const choice = [...preferred]
|
|
106
|
+
const savings = shown
|
|
107
|
+
.filter((i) => items[i].kind === 'tool' && choice[i] === 'full')
|
|
108
|
+
.map((i) => ({ i, saving: items[i].full.main - items[i].compact.main }))
|
|
109
|
+
.filter(({ saving }) => saving > 0)
|
|
110
|
+
.sort((a, b) => b.saving - a.saving || a.i - b.i)
|
|
111
|
+
let total = length(choice, shown)
|
|
112
|
+
for (const { i, saving } of savings) {
|
|
113
|
+
if (total <= available.main) break
|
|
114
|
+
choice[i] = 'compact'
|
|
115
|
+
total -= saving
|
|
116
|
+
}
|
|
117
|
+
if (total <= available.main) return done(choice, shown, [])
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// The ⋮ menu: the first tools that fit, then the rest. Once one tool does
|
|
121
|
+
// not fit, every tool after it goes to the menu too, so the bar keeps the
|
|
122
|
+
// configured order. Tools that fit across at neither density go anyway.
|
|
123
|
+
const budget = available.main - more.main - gap
|
|
124
|
+
const fill = (choice: Choice) => {
|
|
125
|
+
const visible: number[] = []
|
|
126
|
+
const overflow: number[] = []
|
|
127
|
+
let used = 0
|
|
128
|
+
let full = false
|
|
129
|
+
items.forEach((item, i) => {
|
|
130
|
+
const density = choice[i]
|
|
131
|
+
if (density === undefined) {
|
|
132
|
+
overflow.push(i)
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
const add = (visible.length > 0 ? gap : 0) + item[density].main
|
|
136
|
+
if (!full && used + add <= budget) {
|
|
137
|
+
visible.push(i)
|
|
138
|
+
used += add
|
|
139
|
+
} else {
|
|
140
|
+
full = true
|
|
141
|
+
overflow.push(i)
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
return { visible, overflow }
|
|
145
|
+
}
|
|
146
|
+
// Keep the labels when that costs no tool, else go compact.
|
|
147
|
+
const labelled = fill(preferred)
|
|
148
|
+
const small = fill(allCompact)
|
|
149
|
+
const count = (indexes: number[]) => indexes.filter((i) => items[i].kind === 'tool').length
|
|
150
|
+
return count(labelled.visible) >= count(small.visible)
|
|
151
|
+
? done(preferred, labelled.visible, labelled.overflow)
|
|
152
|
+
: done(allCompact, small.visible, small.overflow)
|
|
153
|
+
}
|
package/src/tools.ts
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the toolbar draws and writes, derived from the config. Everything here
|
|
3
|
+
* is pure: the config belongs to the host (which shares bound rows with other
|
|
4
|
+
* tiles), so nothing writes to it, and every field is checked rather than
|
|
5
|
+
* trusted, because boards keep configs written by hand or for older versions.
|
|
6
|
+
*/
|
|
7
|
+
import type { ToolbarConfiguration } from './definition-schema.js'
|
|
8
|
+
|
|
9
|
+
export type Control = 'button' | 'switch' | 'slider' | 'choice' | 'dropdown' | 'label' | 'separator' | 'spacer'
|
|
10
|
+
export type Variant = 'filled' | 'tonal' | 'outlined' | 'text'
|
|
11
|
+
|
|
12
|
+
const CONTROLS: readonly Control[] = ['button', 'switch', 'slider', 'choice', 'dropdown', 'label', 'separator', 'spacer']
|
|
13
|
+
const VARIANTS: readonly Variant[] = ['filled', 'tonal', 'outlined', 'text']
|
|
14
|
+
|
|
15
|
+
/** Tools that write a value; label, separator and spacer don't. */
|
|
16
|
+
export const isInteractive = (control: Control): boolean =>
|
|
17
|
+
control === 'button' || control === 'switch' || control === 'slider' || control === 'choice' || control === 'dropdown'
|
|
18
|
+
|
|
19
|
+
/** A table column as the targetColumn picker stores it. Studio adds the app's backend key. */
|
|
20
|
+
export type ColumnRef = {
|
|
21
|
+
swarm_app_databackend_key: string | number
|
|
22
|
+
tablename: string
|
|
23
|
+
column: string
|
|
24
|
+
dataType?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** One column value, as `data-submit` carries it. Studio groups the items into one row per table. */
|
|
28
|
+
export type ValueItem = {
|
|
29
|
+
swarm_app_databackend_key: string | number
|
|
30
|
+
table_name: string
|
|
31
|
+
column_name: string
|
|
32
|
+
value: unknown
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type Option = { label: string; value: string; icon?: string }
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The value an operator set, held until the reported value agrees with it.
|
|
39
|
+
* `expected` is what the tool's state source should report once the write
|
|
40
|
+
* has landed. Without a timer (nothing reports the value) it is the state.
|
|
41
|
+
*/
|
|
42
|
+
export type Request = { expected: unknown; timer?: ReturnType<typeof setTimeout> }
|
|
43
|
+
|
|
44
|
+
/** Where a tool's shown value comes from. */
|
|
45
|
+
export type Source = 'url' | 'data' | 'local'
|
|
46
|
+
|
|
47
|
+
export type ToolModel = {
|
|
48
|
+
/** Stable per position and type; changing a tool's type starts it afresh. */
|
|
49
|
+
key: string
|
|
50
|
+
index: number
|
|
51
|
+
control: Control
|
|
52
|
+
label: string
|
|
53
|
+
icon?: string
|
|
54
|
+
variant: Variant
|
|
55
|
+
/** The tool's own accent colour, if configured. */
|
|
56
|
+
color?: string
|
|
57
|
+
// Interactive tools
|
|
58
|
+
source: Source
|
|
59
|
+
/** The value the tool shows: reported, or held after a click. */
|
|
60
|
+
state: unknown
|
|
61
|
+
disabled: boolean
|
|
62
|
+
confirmText?: string
|
|
63
|
+
parameterKey?: string
|
|
64
|
+
target?: ColumnRef
|
|
65
|
+
// button
|
|
66
|
+
buttonValue: string
|
|
67
|
+
active: boolean
|
|
68
|
+
// switch
|
|
69
|
+
on: boolean
|
|
70
|
+
// slider
|
|
71
|
+
min: number
|
|
72
|
+
max: number
|
|
73
|
+
step: number
|
|
74
|
+
decimals: number
|
|
75
|
+
unit: string
|
|
76
|
+
sliderValue: number
|
|
77
|
+
// choice, dropdown
|
|
78
|
+
options: Option[]
|
|
79
|
+
selected: number
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** What one interaction writes. */
|
|
83
|
+
export type Commit = {
|
|
84
|
+
/** URL parameters for `param-submit`, or undefined when the tool sets none. */
|
|
85
|
+
params?: Record<string, string | undefined>
|
|
86
|
+
/** Items for `data-submit`, or undefined when the tool writes no column. */
|
|
87
|
+
items?: ValueItem[]
|
|
88
|
+
/** What the tool's state source should report afterwards. */
|
|
89
|
+
expected: unknown
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Written into the URL and into a column for "on" when nothing else is set.
|
|
93
|
+
const DEFAULT_ON = 'true'
|
|
94
|
+
|
|
95
|
+
const text = (value: unknown): string =>
|
|
96
|
+
typeof value === 'string' ? value : typeof value === 'number' && Number.isFinite(value) ? String(value) : ''
|
|
97
|
+
|
|
98
|
+
export const isEmpty = (value: unknown): boolean =>
|
|
99
|
+
value === undefined || value === null || (typeof value === 'string' && value.trim() === '')
|
|
100
|
+
|
|
101
|
+
export const toNumber = (value: unknown): number | undefined => {
|
|
102
|
+
if (value === null || value === undefined || value === '' || typeof value === 'boolean') return undefined
|
|
103
|
+
const number = typeof value === 'number' ? value : Number(String(value).trim())
|
|
104
|
+
return Number.isFinite(number) ? number : undefined
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A picked column, or undefined unless it names a backend, a table and a column. */
|
|
108
|
+
export function columnRef(target: unknown): ColumnRef | undefined {
|
|
109
|
+
if (!target || typeof target !== 'object') return undefined
|
|
110
|
+
const ref = target as Record<string, unknown>
|
|
111
|
+
const key = ref.swarm_app_databackend_key
|
|
112
|
+
const hasKey = (typeof key === 'number' && Number.isFinite(key)) || (typeof key === 'string' && key.trim() !== '')
|
|
113
|
+
const named = (part: unknown) => typeof part === 'string' && part.trim() !== ''
|
|
114
|
+
if (!hasKey || !named(ref.tablename) || !named(ref.column)) return undefined
|
|
115
|
+
return {
|
|
116
|
+
swarm_app_databackend_key: key as string | number,
|
|
117
|
+
tablename: ref.tablename as string,
|
|
118
|
+
column: ref.column as string,
|
|
119
|
+
dataType: typeof ref.dataType === 'string' ? ref.dataType : undefined
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const sameTable = (a: ColumnRef, b: ColumnRef): boolean =>
|
|
124
|
+
String(a.swarm_app_databackend_key) === String(b.swarm_app_databackend_key) && a.tablename === b.tablename
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A value as its column's type wants it. The editor stores constants as text,
|
|
128
|
+
* so '12' or 'true' becomes a number or a boolean for those columns. What
|
|
129
|
+
* does not convert is written as it is, and Studio reports the failed row.
|
|
130
|
+
* (The same conversion as widget-switch's.)
|
|
131
|
+
*/
|
|
132
|
+
export function columnValue(value: unknown, dataType: unknown): unknown {
|
|
133
|
+
if (value === undefined || value === null) return null
|
|
134
|
+
if (dataType === 'boolean') {
|
|
135
|
+
if (typeof value === 'boolean') return value
|
|
136
|
+
if (typeof value === 'number') return value !== 0
|
|
137
|
+
const t = String(value).trim().toLowerCase()
|
|
138
|
+
if (t === 'true' || t === '1') return true
|
|
139
|
+
if (t === 'false' || t === '0') return false
|
|
140
|
+
return value
|
|
141
|
+
}
|
|
142
|
+
if (dataType === 'numeric' || dataType === 'bigint') {
|
|
143
|
+
if (typeof value === 'boolean') return value ? 1 : 0
|
|
144
|
+
if (typeof value === 'number') return value
|
|
145
|
+
const t = String(value).trim()
|
|
146
|
+
const n = Number(t)
|
|
147
|
+
if (t === '' || !Number.isFinite(n)) return value
|
|
148
|
+
// A bigint id beyond 2^53 would lose digits as a number.
|
|
149
|
+
return dataType === 'bigint' && !Number.isSafeInteger(n) ? t : n
|
|
150
|
+
}
|
|
151
|
+
if (dataType === 'string' && (typeof value === 'boolean' || typeof value === 'number')) return String(value)
|
|
152
|
+
return value
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Only used against a boolean, so 1 and 0 count as true and false.
|
|
156
|
+
const asBoolean = (value: unknown): boolean | undefined => {
|
|
157
|
+
if (typeof value === 'boolean') return value
|
|
158
|
+
const t = String(value).trim().toLowerCase()
|
|
159
|
+
if (t === 'true' || t === '1') return true
|
|
160
|
+
if (t === 'false' || t === '0') return false
|
|
161
|
+
return undefined
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Whether two values mean the same, after the column's conversion on both
|
|
166
|
+
* sides: true written to a numeric column comes back as 1, '5' as 5. Empty
|
|
167
|
+
* values (missing, null, '') are all the same.
|
|
168
|
+
*/
|
|
169
|
+
export function sameValue(a: unknown, b: unknown, dataType?: unknown): boolean {
|
|
170
|
+
if (isEmpty(a) || isEmpty(b)) return isEmpty(a) && isEmpty(b)
|
|
171
|
+
const ca = columnValue(a, dataType)
|
|
172
|
+
const cb = columnValue(b, dataType)
|
|
173
|
+
if (typeof ca === 'boolean' || typeof cb === 'boolean') {
|
|
174
|
+
const ba = asBoolean(ca)
|
|
175
|
+
const bb = asBoolean(cb)
|
|
176
|
+
if (ba !== undefined && bb !== undefined) return ba === bb
|
|
177
|
+
}
|
|
178
|
+
if (dataType !== 'string') {
|
|
179
|
+
const na = toNumber(ca)
|
|
180
|
+
const nb = toNumber(cb)
|
|
181
|
+
if (na !== undefined && nb !== undefined) return na === nb
|
|
182
|
+
}
|
|
183
|
+
return String(ca).trim() === String(cb).trim()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The value the URL holds for a parameter, undefined while it is absent. */
|
|
187
|
+
export function paramsOf(route: unknown): URLSearchParams {
|
|
188
|
+
if (typeof route !== 'string') return new URLSearchParams()
|
|
189
|
+
const query = route.indexOf('?')
|
|
190
|
+
return new URLSearchParams(query < 0 ? '' : route.slice(query + 1))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
type RawTool = Record<string, unknown>
|
|
194
|
+
|
|
195
|
+
const rawTools = (config: unknown): (RawTool | undefined)[] => {
|
|
196
|
+
const tools = (config as ToolbarConfiguration | undefined)?.tools
|
|
197
|
+
if (!Array.isArray(tools)) return []
|
|
198
|
+
// A null slot (a stray '-' in a board template) keeps its position, so the
|
|
199
|
+
// keys of the tools after it do not shift.
|
|
200
|
+
return tools.map((tool) => (tool && typeof tool === 'object' ? (tool as RawTool) : undefined))
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const controlOf = (tool: RawTool): Control => {
|
|
204
|
+
const control = tool.control
|
|
205
|
+
return CONTROLS.includes(control as Control) ? (control as Control) : 'button'
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export const toolKey = (index: number, control: Control): string => `${index}:${control}`
|
|
209
|
+
|
|
210
|
+
/** The key and the reported value of every interactive tool, for settling held requests. */
|
|
211
|
+
export type Report = { key: string; source: Source; reported: unknown; dataType?: string }
|
|
212
|
+
|
|
213
|
+
/** What each interactive tool's source reports right now. */
|
|
214
|
+
export function reports(config: unknown, params: URLSearchParams): Report[] {
|
|
215
|
+
return rawTools(config).flatMap((tool, index): Report[] => {
|
|
216
|
+
if (!tool) return []
|
|
217
|
+
const control = controlOf(tool)
|
|
218
|
+
if (!isInteractive(control)) return []
|
|
219
|
+
const key = toolKey(index, control)
|
|
220
|
+
const parameterKey = text(tool.parameterKey).trim()
|
|
221
|
+
if (parameterKey) {
|
|
222
|
+
return [{ key, source: 'url', reported: params.get(parameterKey) ?? undefined }]
|
|
223
|
+
}
|
|
224
|
+
// A bound value that has delivered nothing yet (null) reports nothing.
|
|
225
|
+
if (tool.value !== undefined && tool.value !== null) {
|
|
226
|
+
return [{ key, source: 'data', reported: tool.value, dataType: columnRef(tool.targetColumn)?.dataType }]
|
|
227
|
+
}
|
|
228
|
+
return [{ key, source: 'local', reported: undefined }]
|
|
229
|
+
})
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Whether a held request can go: its source now reports what it expects, or reports at all without a timer. */
|
|
233
|
+
export function settled(report: Report, request: Request): boolean {
|
|
234
|
+
if (report.source === 'local') return false
|
|
235
|
+
if (request.timer === undefined) return true
|
|
236
|
+
return sameValue(report.reported, request.expected, report.source === 'data' ? report.dataType : undefined)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const decimalsOf = (step: number): number => {
|
|
240
|
+
const [, fraction = ''] = String(step).split('.')
|
|
241
|
+
return Math.min(fraction.length, 6)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const options = (raw: unknown): Option[] => {
|
|
245
|
+
if (!Array.isArray(raw)) return []
|
|
246
|
+
const seen = new Set<string>()
|
|
247
|
+
return raw.flatMap((option) => {
|
|
248
|
+
if (!option || typeof option !== 'object') return []
|
|
249
|
+
const o = option as Record<string, unknown>
|
|
250
|
+
const value = text(o.value)
|
|
251
|
+
// Two options with one value would both be "selected"; the first wins.
|
|
252
|
+
if (seen.has(value)) return []
|
|
253
|
+
seen.add(value)
|
|
254
|
+
const label = text(o.label) || value
|
|
255
|
+
const icon = text(o.icon).trim() || undefined
|
|
256
|
+
if (!label && !icon) return []
|
|
257
|
+
return [{ label, value, icon }]
|
|
258
|
+
})
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Shared columns that go into the same table as `target`. */
|
|
262
|
+
function sharedFor(config: unknown, target: ColumnRef): { ref: ColumnRef; value: unknown }[] {
|
|
263
|
+
const shared = (config as ToolbarConfiguration | undefined)?.sharedColumns
|
|
264
|
+
if (!Array.isArray(shared)) return []
|
|
265
|
+
return shared.flatMap((entry) => {
|
|
266
|
+
const ref = columnRef(entry?.targetColumn)
|
|
267
|
+
return ref && sameTable(ref, target) ? [{ ref, value: entry?.value }] : []
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function extrasOf(tool: RawTool): { ref: ColumnRef; value: unknown }[] {
|
|
272
|
+
if (!Array.isArray(tool.extraColumns)) return []
|
|
273
|
+
return tool.extraColumns.flatMap((extra) => {
|
|
274
|
+
const ref = columnRef((extra as RawTool | null)?.targetColumn)
|
|
275
|
+
return ref ? [{ ref, value: (extra as RawTool).value }] : []
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const valueItem = (ref: ColumnRef, value: unknown): ValueItem => ({
|
|
280
|
+
swarm_app_databackend_key: ref.swarm_app_databackend_key,
|
|
281
|
+
table_name: ref.tablename,
|
|
282
|
+
column_name: ref.column,
|
|
283
|
+
value: columnValue(value, ref.dataType)
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The row a tool writes with `value` in its own column, or undefined while a
|
|
288
|
+
* key column has no value: a row without its record's key would land nowhere.
|
|
289
|
+
* The tool's own column comes last, so it wins over a shared or extra column
|
|
290
|
+
* naming the same column (Studio keeps the later item).
|
|
291
|
+
*/
|
|
292
|
+
export function rowItems(config: unknown, tool: RawTool, value: unknown): ValueItem[] | undefined {
|
|
293
|
+
const target = columnRef(tool.targetColumn)
|
|
294
|
+
if (!target) return undefined
|
|
295
|
+
const keys = [...sharedFor(config, target), ...extrasOf(tool)]
|
|
296
|
+
if (keys.some((key) => isEmpty(key.value))) return undefined
|
|
297
|
+
return [...keys.map((key) => valueItem(key.ref, key.value)), valueItem(target, value)]
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* The tool models, in configuration order. `requests` holds what the operator
|
|
302
|
+
* set; each request is either still waiting for its source (it has a timer) or
|
|
303
|
+
* stands in for a source that reports nothing.
|
|
304
|
+
*/
|
|
305
|
+
export function toTools(config: unknown, route: unknown, requests: ReadonlyMap<string, Request>): ToolModel[] {
|
|
306
|
+
const params = paramsOf(route)
|
|
307
|
+
const reported = new Map(reports(config, params).map((report) => [report.key, report]))
|
|
308
|
+
return rawTools(config).flatMap((tool, index) => {
|
|
309
|
+
if (!tool) return []
|
|
310
|
+
const control = controlOf(tool)
|
|
311
|
+
const key = toolKey(index, control)
|
|
312
|
+
const label = text(tool.label).trim()
|
|
313
|
+
const icon = text(tool.icon).trim() || undefined
|
|
314
|
+
const color = typeof tool.color === 'string' && tool.color.trim() ? tool.color.trim() : undefined
|
|
315
|
+
const report = reported.get(key)
|
|
316
|
+
const request = requests.get(key)
|
|
317
|
+
const source: Source = report?.source ?? 'local'
|
|
318
|
+
// A held request wins until its source agrees (the element drops it then).
|
|
319
|
+
const state = request ? request.expected : report?.reported
|
|
320
|
+
const target = columnRef(tool.targetColumn)
|
|
321
|
+
const parameterKey = text(tool.parameterKey).trim() || undefined
|
|
322
|
+
const dataType = source === 'data' ? target?.dataType : undefined
|
|
323
|
+
|
|
324
|
+
// A tool that names a column but misses a key value is disabled, even
|
|
325
|
+
// when it also sets a URL parameter: half a write is worse than none.
|
|
326
|
+
const writable = target ? rowItems(config, tool, true) !== undefined : false
|
|
327
|
+
const disabled = isInteractive(control) && (target ? !writable : !parameterKey)
|
|
328
|
+
|
|
329
|
+
const buttonValue = text(tool.buttonValue)
|
|
330
|
+
// Compared with what the switch writes for "on": a configured On Value,
|
|
331
|
+
// else true, which a numeric column stores (and reports back) as 1.
|
|
332
|
+
const onValue: unknown = text(tool.onValue).trim() || (source === 'url' ? DEFAULT_ON : true)
|
|
333
|
+
|
|
334
|
+
let min = toNumber(tool.min) ?? 0
|
|
335
|
+
let max = toNumber(tool.max) ?? 100
|
|
336
|
+
if (!(max > min)) max = min + 100
|
|
337
|
+
let step = toNumber(tool.step) ?? 1
|
|
338
|
+
if (!(step > 0)) step = 1
|
|
339
|
+
if (step > max - min) step = max - min
|
|
340
|
+
const decimals = decimalsOf(step)
|
|
341
|
+
const number = toNumber(state)
|
|
342
|
+
const sliderValue = number === undefined ? min : Math.min(max, Math.max(min, number))
|
|
343
|
+
|
|
344
|
+
const opts = options(tool.options)
|
|
345
|
+
const selected = control === 'choice' || control === 'dropdown'
|
|
346
|
+
? opts.findIndex((option) => sameValue(state, option.value, dataType))
|
|
347
|
+
: -1
|
|
348
|
+
|
|
349
|
+
return [
|
|
350
|
+
{
|
|
351
|
+
key,
|
|
352
|
+
index,
|
|
353
|
+
control,
|
|
354
|
+
label,
|
|
355
|
+
icon,
|
|
356
|
+
variant: VARIANTS.includes(tool.variant as Variant) ? (tool.variant as Variant) : 'filled',
|
|
357
|
+
color,
|
|
358
|
+
source,
|
|
359
|
+
state,
|
|
360
|
+
disabled,
|
|
361
|
+
confirmText: text(tool.confirmText).trim() || undefined,
|
|
362
|
+
parameterKey,
|
|
363
|
+
target,
|
|
364
|
+
buttonValue,
|
|
365
|
+
active: control === 'button' && buttonValue.trim() !== '' && sameValue(state, buttonValue, dataType),
|
|
366
|
+
on: control === 'switch' && sameValue(state, onValue, dataType),
|
|
367
|
+
min,
|
|
368
|
+
max,
|
|
369
|
+
step,
|
|
370
|
+
decimals,
|
|
371
|
+
unit: text(tool.unit),
|
|
372
|
+
sliderValue,
|
|
373
|
+
options: opts,
|
|
374
|
+
selected
|
|
375
|
+
}
|
|
376
|
+
]
|
|
377
|
+
})
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* What an interaction writes. `next` is the new position: a switch's on/off,
|
|
382
|
+
* a slider's number, an option's value; a button ignores it.
|
|
383
|
+
*/
|
|
384
|
+
export function commitFor(config: unknown, model: ToolModel, next?: unknown): Commit | undefined {
|
|
385
|
+
if (!isInteractive(model.control) || model.disabled) return undefined
|
|
386
|
+
const tool = rawTools(config)[model.index]
|
|
387
|
+
if (!tool) return undefined
|
|
388
|
+
|
|
389
|
+
// What goes into the URL (undefined removes the parameter) and into the column.
|
|
390
|
+
let urlValue: string | undefined
|
|
391
|
+
let column: unknown
|
|
392
|
+
switch (model.control) {
|
|
393
|
+
case 'button': {
|
|
394
|
+
const value = model.buttonValue.trim() === '' ? undefined : model.buttonValue
|
|
395
|
+
urlValue = value
|
|
396
|
+
column = value ?? true
|
|
397
|
+
break
|
|
398
|
+
}
|
|
399
|
+
case 'switch': {
|
|
400
|
+
const onValue = text(tool.onValue).trim()
|
|
401
|
+
const offValue = text(tool.offValue).trim()
|
|
402
|
+
if (next) {
|
|
403
|
+
urlValue = onValue || DEFAULT_ON
|
|
404
|
+
column = onValue || true
|
|
405
|
+
} else {
|
|
406
|
+
urlValue = offValue || undefined
|
|
407
|
+
column = offValue || false
|
|
408
|
+
}
|
|
409
|
+
break
|
|
410
|
+
}
|
|
411
|
+
case 'slider': {
|
|
412
|
+
const n = toNumber(next) ?? model.sliderValue
|
|
413
|
+
const value = Number(Math.min(model.max, Math.max(model.min, n)).toFixed(model.decimals))
|
|
414
|
+
urlValue = String(value)
|
|
415
|
+
column = value
|
|
416
|
+
break
|
|
417
|
+
}
|
|
418
|
+
default: {
|
|
419
|
+
const value = text(next)
|
|
420
|
+
urlValue = value.trim() === '' ? undefined : value
|
|
421
|
+
column = value.trim() === '' ? null : value
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const items = model.target ? rowItems(config, tool, column) : undefined
|
|
426
|
+
if (model.target && !items) return undefined
|
|
427
|
+
const params = model.parameterKey ? { [model.parameterKey]: urlValue } : undefined
|
|
428
|
+
const expected = model.parameterKey ? urlValue : model.target ? columnValue(column, model.target.dataType) : column
|
|
429
|
+
return { params, items, expected }
|
|
430
|
+
}
|