@viewkitjs/views 0.0.0-stage → 0.1.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.
Files changed (270) hide show
  1. package/CHANGELOG.md +420 -0
  2. package/LICENSE.md +7 -0
  3. package/README.md +50 -2
  4. package/dist/atoms/CommandMenu.d.ts +2 -0
  5. package/dist/atoms/CommandMenu.js +20 -0
  6. package/dist/atoms/List.d.ts +2 -0
  7. package/dist/atoms/List.js +6 -0
  8. package/dist/atoms/LoadMore.d.ts +2 -0
  9. package/dist/atoms/LoadMore.js +25 -0
  10. package/dist/atoms/Loading.d.ts +2 -0
  11. package/dist/atoms/Loading.js +7 -0
  12. package/dist/atoms/PlatformMark.d.ts +2 -0
  13. package/dist/atoms/PlatformMark.js +4 -0
  14. package/dist/atoms/RelationLink.d.ts +2 -0
  15. package/dist/atoms/RelationLink.js +6 -0
  16. package/dist/atoms/Row.d.ts +3 -0
  17. package/dist/atoms/Row.js +30 -0
  18. package/dist/atoms/RowMenu.d.ts +2 -0
  19. package/dist/atoms/RowMenu.js +101 -0
  20. package/dist/atoms/StatusBadge.d.ts +2 -0
  21. package/dist/atoms/StatusBadge.js +7 -0
  22. package/dist/atoms/StatusMarker.d.ts +2 -0
  23. package/dist/atoms/StatusMarker.js +19 -0
  24. package/dist/atoms/Tag.d.ts +2 -0
  25. package/dist/atoms/Tag.js +13 -0
  26. package/dist/atoms/TickBox.d.ts +2 -0
  27. package/dist/atoms/TickBox.js +8 -0
  28. package/dist/atoms/defaults.d.ts +2 -0
  29. package/dist/atoms/defaults.js +27 -0
  30. package/dist/atoms/tones.d.ts +5 -0
  31. package/dist/atoms/tones.js +28 -0
  32. package/dist/atoms/types.d.ts +178 -0
  33. package/dist/atoms/types.js +3 -0
  34. package/dist/cells/cell-format.d.ts +138 -0
  35. package/dist/cells/cell-format.js +16 -0
  36. package/dist/cells/cell-title.d.ts +17 -0
  37. package/dist/cells/cell-title.js +8 -0
  38. package/dist/cells/cell-values.d.ts +28 -0
  39. package/dist/cells/cell-values.js +87 -0
  40. package/dist/cells/index.d.ts +4 -0
  41. package/dist/cells/index.js +4 -0
  42. package/dist/cells/legacy-field.d.ts +52 -0
  43. package/dist/cells/legacy-field.js +164 -0
  44. package/dist/cells/relation-wrap.d.ts +3 -0
  45. package/dist/cells/relation-wrap.js +24 -0
  46. package/dist/cells/use-cell-format.d.ts +5 -0
  47. package/dist/cells/use-cell-format.js +10 -0
  48. package/dist/contract.d.ts +431 -0
  49. package/dist/contract.js +663 -0
  50. package/dist/display/actor.d.ts +4 -0
  51. package/dist/display/actor.js +26 -0
  52. package/dist/display/boolean.d.ts +18 -0
  53. package/dist/display/boolean.js +43 -0
  54. package/dist/display/breakdown.d.ts +15 -0
  55. package/dist/display/breakdown.js +32 -0
  56. package/dist/display/currency.d.ts +9 -0
  57. package/dist/display/currency.js +29 -0
  58. package/dist/display/date.d.ts +12 -0
  59. package/dist/display/date.js +26 -0
  60. package/dist/display/display-cores.d.ts +31 -0
  61. package/dist/display/display-cores.js +57 -0
  62. package/dist/display/entity-chip.d.ts +26 -0
  63. package/dist/display/entity-chip.js +9 -0
  64. package/dist/display/format.d.ts +51 -0
  65. package/dist/display/format.js +181 -0
  66. package/dist/display/index.d.ts +8 -0
  67. package/dist/display/index.js +7 -0
  68. package/dist/display/link.d.ts +29 -0
  69. package/dist/display/link.js +48 -0
  70. package/dist/display/location.d.ts +7 -0
  71. package/dist/display/location.js +15 -0
  72. package/dist/display/multi-value.d.ts +18 -0
  73. package/dist/display/multi-value.js +28 -0
  74. package/dist/display/name.d.ts +6 -0
  75. package/dist/display/name.js +14 -0
  76. package/dist/display/number.d.ts +11 -0
  77. package/dist/display/number.js +27 -0
  78. package/dist/display/options.d.ts +153 -0
  79. package/dist/display/options.js +1 -0
  80. package/dist/display/percent.d.ts +4 -0
  81. package/dist/display/percent.js +33 -0
  82. package/dist/display/phone.d.ts +10 -0
  83. package/dist/display/phone.js +38 -0
  84. package/dist/display/range.d.ts +6 -0
  85. package/dist/display/range.js +29 -0
  86. package/dist/display/relation.d.ts +4 -0
  87. package/dist/display/relation.js +29 -0
  88. package/dist/display/rich-text.d.ts +10 -0
  89. package/dist/display/rich-text.js +34 -0
  90. package/dist/display/select.d.ts +6 -0
  91. package/dist/display/select.js +38 -0
  92. package/dist/display/text-class.d.ts +23 -0
  93. package/dist/display/text-class.js +5 -0
  94. package/dist/display/text.d.ts +19 -0
  95. package/dist/display/text.js +8 -0
  96. package/dist/display/use-display-options.d.ts +2 -0
  97. package/dist/display/use-display-options.js +14 -0
  98. package/dist/display/user.d.ts +11 -0
  99. package/dist/display/user.js +10 -0
  100. package/dist/display/value-list.d.ts +10 -0
  101. package/dist/display/value-list.js +12 -0
  102. package/dist/display/viewer.d.ts +4 -0
  103. package/dist/display/viewer.js +11 -0
  104. package/dist/display/web-href.d.ts +8 -0
  105. package/dist/display/web-href.js +12 -0
  106. package/dist/editors/ChipsEditor.d.ts +59 -0
  107. package/dist/editors/ChipsEditor.js +73 -0
  108. package/dist/editors/DateEditor.d.ts +25 -0
  109. package/dist/editors/DateEditor.js +122 -0
  110. package/dist/editors/LocationEditor.d.ts +22 -0
  111. package/dist/editors/LocationEditor.js +229 -0
  112. package/dist/editors/NameEditor.d.ts +22 -0
  113. package/dist/editors/NameEditor.js +91 -0
  114. package/dist/editors/NumberEditor.d.ts +67 -0
  115. package/dist/editors/NumberEditor.js +86 -0
  116. package/dist/editors/RangeEditor.d.ts +22 -0
  117. package/dist/editors/RangeEditor.js +70 -0
  118. package/dist/editors/RelationEditor.d.ts +65 -0
  119. package/dist/editors/RelationEditor.js +64 -0
  120. package/dist/editors/SelectEditor.d.ts +72 -0
  121. package/dist/editors/SelectEditor.js +59 -0
  122. package/dist/editors/ValueListEditor.d.ts +38 -0
  123. package/dist/editors/ValueListEditor.js +217 -0
  124. package/dist/editors/edit-cores.d.ts +83 -0
  125. package/dist/editors/edit-cores.js +38 -0
  126. package/dist/editors/edit-session.d.ts +74 -0
  127. package/dist/editors/edit-session.js +35 -0
  128. package/dist/editors/open-values.d.ts +28 -0
  129. package/dist/editors/open-values.js +26 -0
  130. package/dist/editors/relation-source.d.ts +66 -0
  131. package/dist/editors/relation-source.js +102 -0
  132. package/dist/fields/binding.d.ts +87 -0
  133. package/dist/fields/binding.js +117 -0
  134. package/dist/fields/currency.d.ts +92 -0
  135. package/dist/fields/currency.js +55 -0
  136. package/dist/fields/denomination.d.ts +42 -0
  137. package/dist/fields/denomination.js +11 -0
  138. package/dist/fields/edit-core-types.d.ts +40 -0
  139. package/dist/fields/edit-core-types.js +29 -0
  140. package/dist/fields/format-knobs.d.ts +34 -0
  141. package/dist/fields/format-knobs.js +36 -0
  142. package/dist/fields/menus.d.ts +28 -0
  143. package/dist/fields/menus.js +1 -0
  144. package/dist/fields/money.d.ts +59 -0
  145. package/dist/fields/money.js +77 -0
  146. package/dist/fields/number-edit.d.ts +11 -0
  147. package/dist/fields/number-edit.js +17 -0
  148. package/dist/fields/prepare-write.d.ts +55 -0
  149. package/dist/fields/prepare-write.js +67 -0
  150. package/dist/fields/relation-source-types.d.ts +54 -0
  151. package/dist/fields/relation-source-types.js +1 -0
  152. package/dist/figures.d.ts +52 -0
  153. package/dist/figures.js +63 -0
  154. package/dist/filters/date-filter.d.ts +2 -0
  155. package/dist/filters/date-filter.js +1 -0
  156. package/dist/filters/filter-model.d.ts +177 -0
  157. package/dist/filters/filter-model.js +41 -0
  158. package/dist/filters/number-filter.d.ts +2 -0
  159. package/dist/filters/number-filter.js +1 -0
  160. package/dist/filters/range-filter.d.ts +2 -0
  161. package/dist/filters/range-filter.js +1 -0
  162. package/dist/filters/registry.d.ts +136 -0
  163. package/dist/filters/registry.js +46 -0
  164. package/dist/filters/relation-filter.d.ts +16 -0
  165. package/dist/filters/relation-filter.js +53 -0
  166. package/dist/filters/text-filter.d.ts +2 -0
  167. package/dist/filters/text-filter.js +1 -0
  168. package/dist/filters/types.d.ts +27 -0
  169. package/dist/filters/types.js +1 -0
  170. package/dist/grouping/sort-groups.d.ts +18 -0
  171. package/dist/grouping/sort-groups.js +33 -0
  172. package/dist/grouping/types.d.ts +10 -0
  173. package/dist/grouping/types.js +1 -0
  174. package/dist/index.d.ts +29 -0
  175. package/dist/index.js +29 -0
  176. package/dist/internal/chip.d.ts +4 -0
  177. package/dist/internal/chip.js +3 -0
  178. package/dist/internal/cx.d.ts +3 -0
  179. package/dist/internal/cx.js +3 -0
  180. package/dist/model/aggregates.d.ts +59 -0
  181. package/dist/model/aggregates.js +186 -0
  182. package/dist/model/card-density.d.ts +8 -0
  183. package/dist/model/card-density.js +10 -0
  184. package/dist/model/card-layout.d.ts +85 -0
  185. package/dist/model/card-layout.js +90 -0
  186. package/dist/model/column-capabilities.d.ts +162 -0
  187. package/dist/model/column-capabilities.js +233 -0
  188. package/dist/model/column-filters.d.ts +106 -0
  189. package/dist/model/column-filters.js +321 -0
  190. package/dist/model/column-format.d.ts +103 -0
  191. package/dist/model/column-format.js +1 -0
  192. package/dist/model/column-from.d.ts +28 -0
  193. package/dist/model/column-from.js +160 -0
  194. package/dist/model/column-inspect.d.ts +18 -0
  195. package/dist/model/column-inspect.js +6 -0
  196. package/dist/model/column-knobs.d.ts +20 -0
  197. package/dist/model/column-knobs.js +16 -0
  198. package/dist/model/column-money.d.ts +13 -0
  199. package/dist/model/column-money.js +27 -0
  200. package/dist/model/column-order.d.ts +36 -0
  201. package/dist/model/column-order.js +68 -0
  202. package/dist/model/column-schema.d.ts +335 -0
  203. package/dist/model/column-schema.js +1 -0
  204. package/dist/model/column-sort.d.ts +17 -0
  205. package/dist/model/column-sort.js +11 -0
  206. package/dist/model/column-state.d.ts +77 -0
  207. package/dist/model/column-state.js +95 -0
  208. package/dist/model/column-types.d.ts +109 -0
  209. package/dist/model/column-types.js +50 -0
  210. package/dist/model/column-warnings.d.ts +44 -0
  211. package/dist/model/column-warnings.js +24 -0
  212. package/dist/model/density.d.ts +3 -0
  213. package/dist/model/density.js +13 -0
  214. package/dist/model/field-type-of.d.ts +21 -0
  215. package/dist/model/field-type-of.js +11 -0
  216. package/dist/model/filter-capabilities.d.ts +1 -0
  217. package/dist/model/filter-capabilities.js +1 -0
  218. package/dist/model/filter-pushdown.d.ts +106 -0
  219. package/dist/model/filter-pushdown.js +147 -0
  220. package/dist/model/index.d.ts +40 -0
  221. package/dist/model/index.js +21 -0
  222. package/dist/model/part-rows.d.ts +28 -0
  223. package/dist/model/part-rows.js +40 -0
  224. package/dist/model/preferences.d.ts +151 -0
  225. package/dist/model/preferences.js +84 -0
  226. package/dist/model/prefs-buckets.d.ts +173 -0
  227. package/dist/model/prefs-buckets.js +71 -0
  228. package/dist/model/relation-filters.d.ts +45 -0
  229. package/dist/model/relation-filters.js +78 -0
  230. package/dist/model/render-count.d.ts +2 -0
  231. package/dist/model/render-count.js +24 -0
  232. package/dist/model/row-icon.d.ts +23 -0
  233. package/dist/model/row-icon.js +5 -0
  234. package/dist/model/view-rules.d.ts +73 -0
  235. package/dist/model/view-rules.js +216 -0
  236. package/dist/provider/ViewsProvider.d.ts +26 -0
  237. package/dist/provider/ViewsProvider.js +12 -0
  238. package/dist/provider/components.d.ts +222 -0
  239. package/dist/provider/components.js +3 -0
  240. package/dist/provider/context.d.ts +19 -0
  241. package/dist/provider/context.js +10 -0
  242. package/dist/provider/icons.d.ts +6 -0
  243. package/dist/provider/icons.js +1 -0
  244. package/dist/provider/ports.d.ts +35 -0
  245. package/dist/provider/ports.js +8 -0
  246. package/dist/react.d.ts +9 -0
  247. package/dist/react.js +4 -0
  248. package/dist/selection/SelectCheckbox.d.ts +17 -0
  249. package/dist/selection/SelectCheckbox.js +11 -0
  250. package/dist/selection/index.d.ts +7 -0
  251. package/dist/selection/index.js +6 -0
  252. package/dist/selection/model.d.ts +55 -0
  253. package/dist/selection/model.js +86 -0
  254. package/dist/selection/row-click.d.ts +35 -0
  255. package/dist/selection/row-click.js +20 -0
  256. package/dist/selection/types.d.ts +17 -0
  257. package/dist/selection/types.js +1 -0
  258. package/dist/selection/useCheckDragSelect.d.ts +61 -0
  259. package/dist/selection/useCheckDragSelect.js +165 -0
  260. package/dist/selection/useListSelection.d.ts +39 -0
  261. package/dist/selection/useListSelection.js +48 -0
  262. package/dist/selection/useListSelectionShortcuts.d.ts +6 -0
  263. package/dist/selection/useListSelectionShortcuts.js +14 -0
  264. package/dist/selection/useRestIntent.d.ts +18 -0
  265. package/dist/selection/useRestIntent.js +35 -0
  266. package/docs/contract.md +453 -0
  267. package/docs/install.md +105 -0
  268. package/docs/reshaping.md +19 -0
  269. package/package.json +114 -3
  270. package/skills/views/SKILL.md +87 -0
@@ -0,0 +1,233 @@
1
+ import { acceptFieldCommit, isMultipleType, lockReason, valueTypeOf } from "@viewkitjs/fields";
2
+ import { resolveViewsMessage } from "../contract.js";
3
+ import { prettifyOptionValue, resolveColumnOrder } from "./column-state.js";
4
+ import { typeRule } from "./column-types.js";
5
+ import { fieldKeyOf } from "./view-rules.js";
6
+ export function gridEditorKind(col) {
7
+ if (!col.editable || !col.type)
8
+ return null;
9
+ if (col.multiple)
10
+ return col.field && col.type && isMultipleType(col.type) ? "values" : null;
11
+ if (col.type === "title" && col.field && valueTypeOf(col.field) === "name")
12
+ return "name";
13
+ return typeRule(col.type)?.editor ?? null;
14
+ }
15
+ export function gridColumnHasEditor(col) {
16
+ return gridEditorKind(col) != null;
17
+ }
18
+ export function toggleListColumnLock(config, columnId) {
19
+ const cur = config ?? {};
20
+ const cols = { ...(cur.columns ?? {}) };
21
+ if (cols[columnId]?.locked)
22
+ delete cols[columnId];
23
+ else
24
+ cols[columnId] = { locked: true };
25
+ const next = { ...cur };
26
+ if (Object.keys(cols).length === 0)
27
+ delete next.columns;
28
+ else
29
+ next.columns = cols;
30
+ return next;
31
+ }
32
+ export function toggleListReadOnly(config) {
33
+ const cur = config ?? {};
34
+ const next = { ...cur };
35
+ if (next.readOnly)
36
+ delete next.readOnly;
37
+ else
38
+ next.readOnly = true;
39
+ return next;
40
+ }
41
+ export function gridBooleanMode(col) {
42
+ if (col.type !== "boolean")
43
+ return null;
44
+ return col.format?.booleanDisplay === "marker" ? "marker" : "toggle";
45
+ }
46
+ export function columnWritable(col) {
47
+ if (col.custom)
48
+ return false;
49
+ if (col.command)
50
+ return false;
51
+ if (!col.editable)
52
+ return false;
53
+ return gridColumnHasEditor(col) || gridBooleanMode(col) != null;
54
+ }
55
+ export function columnLabel(col) {
56
+ return typeof col.header === "string" && col.header ? col.header : prettifyOptionValue(col.id);
57
+ }
58
+ export function columnManagedByView(col) {
59
+ return col.field != null;
60
+ }
61
+ export function columnPlacedByView(col) {
62
+ return col.field != null || col.custom != null;
63
+ }
64
+ export const VIEW_FIELDLESS_FILTER_REASON = "Not a field of this list; a view cannot filter on it.";
65
+ export function resolveViewColumnOrder(columns, requested = []) {
66
+ const managedIds = new Set(columns.filter(columnPlacedByView).map((column) => column.id));
67
+ const seen = new Set();
68
+ const managed = requested.filter((id) => {
69
+ if (!managedIds.has(id) || seen.has(id))
70
+ return false;
71
+ seen.add(id);
72
+ return true;
73
+ });
74
+ for (const column of columns) {
75
+ if (column.field != null && managedIds.has(column.id) && !seen.has(column.id)) {
76
+ seen.add(column.id);
77
+ managed.push(column.id);
78
+ }
79
+ }
80
+ let managedIndex = 0;
81
+ return columns.map((column) => (seen.has(column.id) ? managed[managedIndex++] : column.id));
82
+ }
83
+ export function baseColumnOrder(columns, order, viewBacked) {
84
+ return viewBacked
85
+ ? resolveViewColumnOrder(columns, order)
86
+ : resolveColumnOrder(columns, order ? [...order] : undefined).map((column) => column.id);
87
+ }
88
+ export function lockKeyOf(col) {
89
+ return fieldKeyOf(col);
90
+ }
91
+ export function gridColumnLocked(col, config) {
92
+ if (!config)
93
+ return false;
94
+ return !!config.readOnly || !!config.columns?.[lockKeyOf(col)]?.locked;
95
+ }
96
+ export function columnEffectiveWritable(col, config) {
97
+ return columnWritable(col) && !gridColumnLocked(col, config);
98
+ }
99
+ export function gridColumnEffectiveClearable(col, config) {
100
+ return columnEffectiveWritable(col, config) && !!col.clearable;
101
+ }
102
+ export function gridClearRefusal(col, config, messages = resolveViewsMessage) {
103
+ const declared = columnWritable(col) && !!col.clearable;
104
+ if (declared && gridColumnLocked(col, config)) {
105
+ return messages({ key: "views.grid.locked", values: { field: columnLabel(col) } });
106
+ }
107
+ return columnClearRefusal(col, messages);
108
+ }
109
+ export function columnClearRefusal(col, messages = resolveViewsMessage) {
110
+ return col.reasons?.clearable ?? messages({ key: "views.grid.not-clearable" });
111
+ }
112
+ export function columnEditRefusal(col, conventions, messages = resolveViewsMessage) {
113
+ return (col.reasons?.editable ??
114
+ (col.field ? lockReason(col.field, conventions) : undefined) ??
115
+ (col.custom ? messages({ key: "views.grid.computed", values: { from: col.custom.from } }) : undefined));
116
+ }
117
+ export function gridEditRefusal(col, config, conventions, messages = resolveViewsMessage) {
118
+ if (gridColumnLocked(col, config))
119
+ return messages({ key: "views.grid.locked", values: { field: columnLabel(col) } });
120
+ return columnEditRefusal(col, conventions, messages);
121
+ }
122
+ function parseBooleanish(next) {
123
+ if (typeof next === "boolean")
124
+ return next;
125
+ if (next == null)
126
+ return false;
127
+ if (typeof next === "number")
128
+ return next !== 0;
129
+ if (typeof next !== "string")
130
+ return undefined;
131
+ const s = next.trim().toLowerCase();
132
+ if (s === "" || s === "false" || s === "0" || s === "no" || s === "n")
133
+ return false;
134
+ if (s === "true" || s === "1" || s === "yes" || s === "y")
135
+ return true;
136
+ return undefined;
137
+ }
138
+ const EMAIL_SHAPE = /^\S+@\S+\.\S+$/;
139
+ const URL_SHAPE = /^https?:\/\/\S+\.\S+/i;
140
+ function isCleared(next) {
141
+ return next == null || (typeof next === "string" && next.trim() === "");
142
+ }
143
+ function coerceWrite(col, next, messages) {
144
+ if (col.multiple) {
145
+ if (isCleared(next) || Array.isArray(next))
146
+ return { ok: true, value: next };
147
+ if (typeof next !== "string")
148
+ return { ok: false, reason: messages({ key: "views.grid.write-not-list" }) };
149
+ return {
150
+ ok: true,
151
+ value: next
152
+ .split(col.type === "url" || col.type === "domain" ? /\n|,\s/ : ",")
153
+ .map((part) => part.trim())
154
+ .filter((part) => part !== ""),
155
+ };
156
+ }
157
+ switch (typeRule(col.type)?.value ?? "any") {
158
+ case "boolean": {
159
+ const parsed = parseBooleanish(next);
160
+ if (parsed === undefined)
161
+ return { ok: false, reason: messages({ key: "views.grid.write-not-boolean" }) };
162
+ if (gridBooleanMode(col) === "marker" && parsed) {
163
+ return { ok: false, reason: messages({ key: "views.grid.write-marker" }) };
164
+ }
165
+ return { ok: true, value: parsed };
166
+ }
167
+ case "email": {
168
+ if (isCleared(next))
169
+ return { ok: true, value: next };
170
+ if (typeof next !== "string")
171
+ return { ok: true, value: next };
172
+ const trimmed = next.trim();
173
+ if (!EMAIL_SHAPE.test(trimmed))
174
+ return { ok: false, reason: messages({ key: "views.grid.write-not-email" }) };
175
+ return { ok: true, value: trimmed };
176
+ }
177
+ case "url": {
178
+ if (isCleared(next))
179
+ return { ok: true, value: next };
180
+ if (typeof next !== "string")
181
+ return { ok: true, value: next };
182
+ const trimmed = next.trim();
183
+ if (!URL_SHAPE.test(trimmed)) {
184
+ return { ok: false, reason: messages({ key: "views.grid.write-not-url" }) };
185
+ }
186
+ return { ok: true, value: trimmed };
187
+ }
188
+ case "relation": {
189
+ if (isCleared(next))
190
+ return { ok: true, value: null };
191
+ if (typeof next !== "string")
192
+ return { ok: false, reason: messages({ key: "views.grid.write-not-link" }) };
193
+ const known = col.relation?.options;
194
+ if (known && !known.some((option) => option.id === next)) {
195
+ return { ok: false, reason: messages({ key: "views.grid.write-not-linkable" }) };
196
+ }
197
+ return { ok: true, value: next };
198
+ }
199
+ case "list": {
200
+ if (isCleared(next))
201
+ return { ok: true, value: [] };
202
+ if (!Array.isArray(next) && typeof next !== "string") {
203
+ return { ok: false, reason: messages({ key: "views.grid.write-not-list" }) };
204
+ }
205
+ const raw = Array.isArray(next) ? next : next.split(",");
206
+ const values = [];
207
+ for (const entry of raw) {
208
+ if (entry == null)
209
+ continue;
210
+ const trimmed = String(entry).trim();
211
+ if (trimmed === "" || values.includes(trimmed))
212
+ continue;
213
+ values.push(trimmed);
214
+ }
215
+ return { ok: true, value: values };
216
+ }
217
+ case "any":
218
+ return { ok: true, value: next };
219
+ }
220
+ }
221
+ export function gridNormalizeWrite(col, next, conventions, options = {}) {
222
+ const coerced = coerceWrite(col, next, options.messages ?? resolveViewsMessage);
223
+ if (!coerced.ok || !col.field)
224
+ return coerced;
225
+ if (isCleared(coerced.value))
226
+ return coerced;
227
+ return acceptFieldCommit(col.field, coerced.value, {
228
+ unitOptions: col.editUnit?.options,
229
+ conventions,
230
+ region: options.region,
231
+ stored: options.stored,
232
+ });
233
+ }
@@ -0,0 +1,106 @@
1
+ import type { ReactNode } from "react";
2
+ import { type FieldConventions } from "@viewkitjs/fields";
3
+ import type { ViewsMessageResolver } from "../contract.js";
4
+ import type { FilterConfig } from "../filters/filter-model.js";
5
+ import type { ListColumn, ListRelationValue } from "./column-schema.js";
6
+ import { type ListFilterEditor } from "./column-types.js";
7
+ import type { ListColumnState } from "./column-state.js";
8
+ import type { ListFilterRule, ListFilterRuleState } from "./preferences.js";
9
+ import type { ListFilterCapabilities } from "./filter-capabilities.js";
10
+ /**
11
+ * The Filter list, derived from the table's own columns.
12
+ *
13
+ * A list used to hand-author its filter dimensions, so the Filter drill-in
14
+ * showed whatever that one page happened to declare and nothing else — a table
15
+ * with a Name column had no way to filter by name unless someone wrote that
16
+ * dimension by hand. Here the columns ARE the inventory: every column is
17
+ * listed, in the user's column order, carrying its own type. What a row DOES
18
+ * when opened is the type's business (`filterEditor`), which is what lets
19
+ * one editor per type serve every table in the app.
20
+ *
21
+ * Pure and renderer-free — the icon arrives as a resolver so `chrome/` keeps
22
+ * owning the glyphs.
23
+ */
24
+ /** Which editor a column's type opens in the Filter drill-in. Declared once per
25
+ * type in `column-types.ts`, beside that type's editor and paste rules. */
26
+ export type { ListFilterEditor };
27
+ export declare function filterEditor<Row>(col: ListColumn<Row>): ListFilterEditor | null;
28
+ /** Section label for filters whose column is currently hidden from the table. */
29
+ export declare const HIDDEN_COLUMN_SECTION = "Hidden columns";
30
+ /** The decimals the cell is showing: the workspace's per-FIELD override wins
31
+ * over the column's declared default, so equality rounds to what is on
32
+ * screen — except on an integer field, which shows whole numbers whatever
33
+ * the reader set (spec §8.2). Keyed by field key; a field-less column's key
34
+ * is its id. */
35
+ export declare function numberDecimals<Row>(col: ListColumn<Row>, overrides?: Record<string, number>): number;
36
+ /**
37
+ * A boolean column's two choices, worded the way its CELL is worded — a filter
38
+ * offering "True / False" against a column that draws the word "flagged" makes
39
+ * you translate between the two. `format.booleanDisplay` already decides how
40
+ * the cell reads, so it decides how the filter reads.
41
+ */
42
+ export declare function booleanFilterOptions<Row>(col: ListColumn<Row>): {
43
+ value: string;
44
+ label: string;
45
+ }[];
46
+ /**
47
+ * Merge the columns with any dimensions the list declares by hand.
48
+ *
49
+ * Hand-declared dimensions do not disappear: something can be a real thing to
50
+ * filter on WITHOUT being a column (Transactions filters by direction and by
51
+ * currency, but the grid folds both into the Amount cell — there is no column
52
+ * to derive either from). So:
53
+ * - a declared filter whose `key` matches a column id REPLACES that column's
54
+ * derived row and keeps the column's position in the order;
55
+ * - a declared filter matching no column is appended, in declared order.
56
+ */
57
+ export declare function buildColumnFilters<Row>({ columns, order, declared, icon, rules, onRuleChange, selectValues, relationValues, decimals, columnState, viewId, capabilities, knownValues, knownLabels, onValuesWanted, parts, messages, conventions, }: {
58
+ columns: ListColumn<Row>[];
59
+ /** The user's column order, so Filter reads in the same order as the table. */
60
+ order?: string[];
61
+ declared?: FilterConfig<string>[];
62
+ icon?: (col: ListColumn<Row>) => ReactNode;
63
+ /** The persisted rules a wired editor reads and writes. */
64
+ rules?: ListFilterRuleState;
65
+ onRuleChange?: (columnId: string, rule: ListFilterRule | null) => void;
66
+ /** columnId → option values in display order, from `deriveGridSelectValues`
67
+ * (declared first, then whatever the data actually contains). The SAME list
68
+ * the cell editor offers, so you can never filter for a value you can't set
69
+ * — or fail to filter for one that's sitting in the table. */
70
+ selectValues?: Record<string, string[]>;
71
+ /** columnId → the relations PRESENT in the rows, from
72
+ * `deriveGridRelationValues`. Deliberately not the column's pickable
73
+ * candidates: the filter answers "which of the ones I can see", so it stays
74
+ * the size of the table rather than the size of the entity it points at. */
75
+ relationValues?: Record<string, ListRelationValue[]>;
76
+ /** columnId → the decimals the cell is currently showing (the user's
77
+ * per-column override), so equality rounds the way the eye does. */
78
+ decimals?: Record<string, number>;
79
+ /** The user's column state, so a filter can say whether its column is
80
+ * currently hidden. Hidden columns stay LISTED — a rule on one still
81
+ * narrows the list — they just group separately. */
82
+ columnState?: ListColumnState;
83
+ /** The view being drawn, so "hidden" means what THAT view hides — a column
84
+ * the rows look hides by default must not read as shown here. */
85
+ viewId?: string;
86
+ capabilities?: ListFilterCapabilities;
87
+ /** columnId → an open field's values as the host knows them (POR-845) — every value its records hold, read
88
+ * from the database — offered ahead of the values on screen. */
89
+ knownValues?: Record<string, string[]>;
90
+ /** columnId → value → its name, for a relation's, a user's or an actor's known values (POR-1480): a value the
91
+ * host did not name is not offered unless it is on screen. */
92
+ knownLabels?: Record<string, Readonly<Record<string, string>>>;
93
+ /** An open field's, a relation's, a user's or an actor's row opened: the host may ask for its values. */
94
+ onValuesWanted?: (columnId: string) => void;
95
+ /** One row per part of a value, right under its column (VK-57): "Location · Country", "Name · Given name"
96
+ * (`FILTER_PART_ROWS`). Keyed `partKey(columnId, part)` in `rules`, `onRuleChange`, `selectValues`, `knownValues`
97
+ * and `onValuesWanted`. A place's inert whole-value row gives way to its parts. Off by default. */
98
+ parts?: boolean;
99
+ /** Words for part rows; the package's English by default. */
100
+ messages?: ViewsMessageResolver;
101
+ /** The locale a country's name is read in (`conventions.locale`); English by default. */
102
+ conventions?: FieldConventions;
103
+ }): FilterConfig<string>[];
104
+ export declare function rowMatchesRules<Row>(row: Row, rules: ListFilterRuleState, columns: ListColumn<Row>[],
105
+ /** Per-column displayed decimals — see `numberDecimals`. */
106
+ decimals: Record<string, number> | undefined, conventions: FieldConventions): boolean;
@@ -0,0 +1,321 @@
1
+ import { actorKey, COUNTRY_CODES, countryName, isIntegerField, isMultipleType, isOpenVocabulary, presentedDef, relationKey, scalarOf, valueItems, valueTypeOf, } from "@viewkitjs/fields";
2
+ import { matchesRule, operatorsFor } from "@viewkitjs/fields/query";
3
+ import { FILTER_PART_ROWS, parsePartKey, partKey, partLabel } from "./part-rows.js";
4
+ import { phoneFilterOperand, phoneMatchText, readPhone } from "@viewkitjs/fields/phone";
5
+ import { isTextRuleActive, matchesTextRule } from "../filters/text-filter.js";
6
+ import { matchesNameRule } from "@viewkitjs/fields/query";
7
+ import { matchesNumberRule } from "../filters/number-filter.js";
8
+ import { matchesRangeRule } from "../filters/range-filter.js";
9
+ import { matchesDateRule } from "../filters/date-filter.js";
10
+ import { typeRule } from "./column-types.js";
11
+ import { columnLabel } from "./column-capabilities.js";
12
+ import { isColumnHidden, prettifyOptionValue, resolveColumnOrder } from "./column-state.js";
13
+ import { fieldTypeOf } from "./field-type-of.js";
14
+ import { fieldKeyOf, fromListFilterRule } from "./view-rules.js";
15
+ export function filterEditor(col) {
16
+ return typeRule(col.type)?.filter ?? null;
17
+ }
18
+ const WIRED_EDITORS = new Set(["text", "options", "boolean", "number", "date", "range"]);
19
+ const NAMED_VALUE_TYPES = new Set(["relation", "user", "actor"]);
20
+ function relationOptions(known, names, onScreen) {
21
+ const byId = new Map();
22
+ for (const id of known ?? []) {
23
+ const name = names?.[id];
24
+ if (name !== undefined)
25
+ byId.set(id, name);
26
+ }
27
+ for (const value of onScreen ?? [])
28
+ if (!byId.has(value.id))
29
+ byId.set(value.id, value.label);
30
+ return [...byId]
31
+ .map(([value, label]) => ({ value, label }))
32
+ .sort((a, b) => a.label.localeCompare(b.label));
33
+ }
34
+ export const HIDDEN_COLUMN_SECTION = "Hidden columns";
35
+ export function numberDecimals(col, overrides) {
36
+ if (col.field && isIntegerField(col.field))
37
+ return 0;
38
+ return overrides?.[fieldKeyOf(col)] ?? col.format?.decimals ?? 2;
39
+ }
40
+ function numberUnit(col) {
41
+ if (fieldTypeOf(col) === "percent")
42
+ return "%";
43
+ if (col.format?.style === "duration")
44
+ return "s";
45
+ if (col.currencyAccessor)
46
+ return undefined;
47
+ if (fieldTypeOf(col) === "currency")
48
+ return col.format?.currency;
49
+ return undefined;
50
+ }
51
+ export function booleanFilterOptions(col) {
52
+ const display = col.format?.booleanDisplay ?? "checkbox";
53
+ if (display === "marker") {
54
+ const label = col.marker?.label ?? columnLabel(col).toLowerCase();
55
+ return [
56
+ { value: "true", label: label.charAt(0).toUpperCase() + label.slice(1) },
57
+ { value: "false", label: `Not ${label}` },
58
+ ];
59
+ }
60
+ if (display === "text") {
61
+ const labelFor = (value, fallback) => col.options?.find((o) => o.value === value)?.label ?? fallback;
62
+ return [
63
+ { value: "true", label: labelFor("true", "True") },
64
+ { value: "false", label: labelFor("false", "False") },
65
+ ];
66
+ }
67
+ return [
68
+ { value: "true", label: "Checked" },
69
+ { value: "false", label: "Unchecked" },
70
+ ];
71
+ }
72
+ export function buildColumnFilters({ columns, order, declared = [], icon, rules = {}, onRuleChange, selectValues = {}, relationValues = {}, decimals, columnState, viewId, capabilities, knownValues = {}, knownLabels = {}, onValuesWanted, parts = false, messages, conventions, }) {
73
+ const declaredByKey = new Map(declared.map((f) => [f.key, f]));
74
+ const claimed = new Set();
75
+ const buildOne = (col) => {
76
+ const capability = capabilities?.[col.id];
77
+ if (capability?.mode === "unavailable") {
78
+ return {
79
+ key: col.id,
80
+ label: columnLabel(col),
81
+ icon: icon?.(col),
82
+ editor: "unavailable",
83
+ reason: capability.reason,
84
+ stale: rules[col.id] !== undefined,
85
+ onClear: () => onRuleChange?.(col.id, null),
86
+ };
87
+ }
88
+ const own = declaredByKey.get(col.id);
89
+ if (own) {
90
+ claimed.add(col.id);
91
+ return { ...own, icon: own.icon ?? icon?.(col) };
92
+ }
93
+ const editor = filterEditor(col);
94
+ const wired = editor != null && WIRED_EDITORS.has(editor) && !!col.accessor;
95
+ const stored = rules[col.id];
96
+ if (wired && editor === "text") {
97
+ const rule = stored?.editor === "text" ? stored : null;
98
+ return {
99
+ key: col.id,
100
+ label: columnLabel(col),
101
+ icon: icon?.(col),
102
+ editor: "text",
103
+ value: rule ? { operator: rule.operator, query: rule.query } : null,
104
+ onChange: (next) => onRuleChange?.(col.id, next ? { editor: "text", ...next } : null),
105
+ };
106
+ }
107
+ if (wired && editor === "number") {
108
+ const rule = stored?.editor === "number" ? stored : null;
109
+ return {
110
+ key: col.id,
111
+ label: columnLabel(col),
112
+ icon: icon?.(col),
113
+ editor: "number",
114
+ value: rule ? { operator: rule.operator, value: rule.value, value2: rule.value2 } : null,
115
+ decimals: numberDecimals(col, decimals),
116
+ unit: numberUnit(col),
117
+ ...(col.format?.style === "duration"
118
+ ? { numberStyle: "duration", durationUnit: col.format.unit ?? "ms" }
119
+ : {}),
120
+ onChange: (next) => onRuleChange?.(col.id, next ? { editor: "number", ...next } : null),
121
+ };
122
+ }
123
+ if (wired && editor === "range") {
124
+ const rule = stored?.editor === "range" ? stored : null;
125
+ const def = col.field ? presentedDef(col.field) : undefined;
126
+ const range = def?.type === "range" ? def : undefined;
127
+ return {
128
+ key: col.id,
129
+ label: columnLabel(col),
130
+ icon: icon?.(col),
131
+ editor: "range",
132
+ value: rule ? { operator: rule.operator, value: rule.value, value2: rule.value2 } : null,
133
+ unit: range?.of === "percent" ? "%" : range?.of === "currency" ? range.code?.fixed : undefined,
134
+ presets: range?.presets,
135
+ onChange: (next) => onRuleChange?.(col.id, next ? { editor: "range", ...next } : null),
136
+ };
137
+ }
138
+ if (wired && editor === "date") {
139
+ const rule = stored?.editor === "date" ? stored : null;
140
+ return {
141
+ key: col.id,
142
+ label: columnLabel(col),
143
+ icon: icon?.(col),
144
+ editor: "date",
145
+ value: rule
146
+ ? {
147
+ operator: rule.operator,
148
+ from: rule.from,
149
+ to: rule.to,
150
+ direction: rule.direction,
151
+ unit: rule.unit,
152
+ }
153
+ : null,
154
+ onChange: (next) => onRuleChange?.(col.id, next ? { editor: "date", ...next } : null),
155
+ };
156
+ }
157
+ if (wired && (editor === "options" || editor === "boolean")) {
158
+ const shown = col.field ? presentedDef(col.field) : undefined;
159
+ const open = !!shown && isOpenVocabulary(shown);
160
+ const openSelect = open && shown?.type === "select";
161
+ const onScreen = selectValues[col.id] ?? (col.options ?? []).map((o) => o.value);
162
+ const values = open ? [...new Set([...(knownValues[col.id] ?? []), ...onScreen])] : onScreen;
163
+ const named = !!col.field && NAMED_VALUE_TYPES.has(col.field.type);
164
+ const options = editor === "boolean"
165
+ ? booleanFilterOptions(col)
166
+ :
167
+ col.type === "relation" || col.type === "user" || col.type === "actor"
168
+ ? relationOptions(knownValues[col.id], knownLabels[col.id], relationValues[col.id])
169
+ : values.map((value) => ({
170
+ value,
171
+ label: col.options?.find((o) => o.value === value)?.label ??
172
+ (openSelect ? value : prettifyOptionValue(value)),
173
+ }));
174
+ return {
175
+ key: col.id,
176
+ label: columnLabel(col),
177
+ icon: icon?.(col),
178
+ ...((open || named) && onValuesWanted ? { onOpen: () => onValuesWanted(col.id) } : {}),
179
+ multi: true,
180
+ value: stored?.editor === "options" ? stored.values : [],
181
+ options,
182
+ onChange: (next) => onRuleChange?.(col.id, next.length ? { editor: "options", values: next } : null),
183
+ emptyLabel: `All ${columnLabel(col).toLowerCase()}`,
184
+ };
185
+ }
186
+ return {
187
+ key: col.id,
188
+ label: columnLabel(col),
189
+ icon: icon?.(col),
190
+ multi: true,
191
+ value: [],
192
+ options: [],
193
+ onChange: () => { },
194
+ disabled: !wired,
195
+ ...(wired ? {} : { valueLabel: "—" }),
196
+ };
197
+ };
198
+ const buildPartRows = (col) => {
199
+ if (!parts || !col.field || !col.accessor)
200
+ return [];
201
+ if (declaredByKey.has(col.id) || capabilities?.[col.id]?.mode === "unavailable")
202
+ return [];
203
+ const type = valueTypeOf(col.field);
204
+ return (FILTER_PART_ROWS[type] ?? []).map((part) => {
205
+ const key = partKey(col.id, part);
206
+ const label = partLabel(columnLabel(col), type, part, messages);
207
+ const stored = rules[key];
208
+ if (operatorsFor(type, part)[0] === "is_any_of") {
209
+ const offered = [...new Set([...(knownValues[key] ?? []), ...(selectValues[key] ?? [])])];
210
+ const locale = conventions?.locale;
211
+ return {
212
+ key,
213
+ label,
214
+ icon: icon?.(col),
215
+ ...(onValuesWanted ? { onOpen: () => onValuesWanted(key) } : {}),
216
+ multi: true,
217
+ value: stored?.editor === "options" ? stored.values : [],
218
+ options: (offered.length ? offered : COUNTRY_CODES)
219
+ .map((value) => ({ value, label: countryName(value, locale) }))
220
+ .sort((a, b) => a.label.localeCompare(b.label, locale)),
221
+ onChange: (next) => onRuleChange?.(key, next.length ? { editor: "options", values: next } : null),
222
+ };
223
+ }
224
+ const rule = stored?.editor === "text" ? stored : null;
225
+ return {
226
+ key,
227
+ label,
228
+ icon: icon?.(col),
229
+ editor: "text",
230
+ value: rule ? { operator: rule.operator, query: rule.query } : null,
231
+ onChange: (next) => onRuleChange?.(key, next ? { editor: "text", ...next } : null),
232
+ };
233
+ });
234
+ };
235
+ const derived = resolveColumnOrder(columns, order).flatMap((col) => {
236
+ const partRows = buildPartRows(col);
237
+ const own = partRows.length && col.field && valueTypeOf(col.field) === "location" ? [] : [buildOne(col)];
238
+ const hidden = isColumnHidden(col, columnState, viewId);
239
+ return [...own, ...partRows].map((built) => built.section || !hidden ? built : { ...built, section: HIDDEN_COLUMN_SECTION });
240
+ });
241
+ return [...derived, ...declared.filter((f) => !claimed.has(f.key))];
242
+ }
243
+ function phoneTextRule(col, value, rule, conventions) {
244
+ if (fieldTypeOf(col) !== "phone" || readPhone(value)?.kind !== "international")
245
+ return [value, rule];
246
+ return [
247
+ phoneMatchText(value),
248
+ { ...rule, query: phoneFilterOperand(rule.operator, rule.query, conventions.region) },
249
+ ];
250
+ }
251
+ function textRuleHolds(col, value, rule, holds) {
252
+ if (!isMultipleType(fieldTypeOf(col)) || !Array.isArray(value))
253
+ return holds(value);
254
+ if (!isTextRuleActive(rule))
255
+ return true;
256
+ const values = valueItems(value).map((item) => item.value);
257
+ if (rule.operator === "is_empty")
258
+ return values.length === 0;
259
+ if (rule.operator === "is_not_empty")
260
+ return values.length > 0;
261
+ return rule.operator === "is_not" || rule.operator === "not_contains" ? values.every(holds) : values.some(holds);
262
+ }
263
+ function partRuleMatches(row, key, rule, columns, conventions) {
264
+ const { id, part } = parsePartKey(key);
265
+ if (part === undefined)
266
+ return true;
267
+ const col = columns.find((c) => c.id === id);
268
+ if (!col?.field || !col.accessor)
269
+ return true;
270
+ const type = valueTypeOf(col.field);
271
+ if (!FILTER_PART_ROWS[type]?.includes(part))
272
+ return true;
273
+ const filterRule = fromListFilterRule(fieldKeyOf(col), type, rule, part);
274
+ return !filterRule || matchesRule(type, col.accessor(row), filterRule, { conventions });
275
+ }
276
+ export function rowMatchesRules(row, rules, columns, decimals, conventions) {
277
+ const ids = Object.keys(rules);
278
+ if (ids.length === 0)
279
+ return true;
280
+ for (const id of ids) {
281
+ const rule = rules[id];
282
+ const col = columns.find((c) => c.id === id);
283
+ if (!col) {
284
+ if (!partRuleMatches(row, id, rule, columns, conventions))
285
+ return false;
286
+ continue;
287
+ }
288
+ if (!col.accessor)
289
+ continue;
290
+ const raw = col.field ? scalarOf(col.field, col.accessor(row)) : col.accessor(row);
291
+ const value = col.type === "boolean" ? String(raw === true) : raw;
292
+ const textMatch = fieldTypeOf(col) === "name" ? matchesNameRule : matchesTextRule;
293
+ if (rule.editor === "text") {
294
+ const holds = (one) => textMatch(...phoneTextRule(col, one, rule, conventions));
295
+ if (!textRuleHolds(col, value, rule, holds))
296
+ return false;
297
+ }
298
+ if (rule.editor === "options" && !matchesOptionsRule(value, rule.values))
299
+ return false;
300
+ if (rule.editor === "number" && !matchesNumberRule(value, rule, numberDecimals(col, decimals)))
301
+ return false;
302
+ if (rule.editor === "range" && !matchesRangeRule(value, rule))
303
+ return false;
304
+ if (rule.editor === "date" &&
305
+ !matchesDateRule(value, rule, undefined, undefined, conventions))
306
+ return false;
307
+ }
308
+ return true;
309
+ }
310
+ function matchesOptionsRule(value, picked) {
311
+ if (picked.length === 0)
312
+ return true;
313
+ if (Array.isArray(value))
314
+ return value.some((v) => picked.includes(optionKey(v)));
315
+ if (value == null)
316
+ return false;
317
+ return picked.includes(optionKey(value));
318
+ }
319
+ function optionKey(value) {
320
+ return actorKey(value) ?? relationKey(value) ?? String(value);
321
+ }