@svgrid/grid 3.0.0 → 3.0.2

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 (304) hide show
  1. package/CHANGELOG.md +116 -88
  2. package/README.md +199 -199
  3. package/dist/FlexRender.svelte +96 -96
  4. package/dist/GridFooter.svelte +190 -181
  5. package/dist/GridFooter.svelte.d.ts +8 -0
  6. package/dist/GridMenus.svelte +18 -10
  7. package/dist/GridMenus.svelte.d.ts +2 -1
  8. package/dist/SvAutoComplete.svelte +169 -169
  9. package/dist/SvAvatar.svelte +75 -75
  10. package/dist/SvCalendar.svelte +503 -503
  11. package/dist/SvCarousel.svelte +141 -141
  12. package/dist/SvCheckBox.svelte +102 -102
  13. package/dist/SvCircularProgress.svelte +109 -109
  14. package/dist/SvColorInput.svelte +181 -181
  15. package/dist/SvComboBox.svelte +279 -279
  16. package/dist/SvContextMenu.svelte +116 -116
  17. package/dist/SvCountryInput.svelte +163 -163
  18. package/dist/SvDrawer.svelte +254 -254
  19. package/dist/SvDropDownList.svelte +378 -378
  20. package/dist/SvDurationInput.svelte +126 -126
  21. package/dist/SvField.svelte +293 -293
  22. package/dist/SvForm.svelte +437 -437
  23. package/dist/SvGrid.controller.svelte.d.ts +17 -0
  24. package/dist/SvGrid.controller.svelte.js +128 -14
  25. package/dist/SvGrid.css +79 -10
  26. package/dist/SvGrid.svelte +191 -186
  27. package/dist/SvGrid.types.d.ts +42 -1
  28. package/dist/SvGridCellEditor.svelte +748 -744
  29. package/dist/SvGridChart.svelte +2246 -1716
  30. package/dist/SvGridChart.svelte.d.ts +63 -1
  31. package/dist/SvGridChartPanel.svelte +633 -485
  32. package/dist/SvGridChartView.svelte +79 -70
  33. package/dist/SvGridChartView.svelte.d.ts +7 -0
  34. package/dist/SvGridDropdown.svelte +728 -728
  35. package/dist/SvGridSelect.svelte +270 -270
  36. package/dist/SvGroupCell.svelte +141 -116
  37. package/dist/SvGroupCell.svelte.d.ts +8 -0
  38. package/dist/SvListBox.svelte +334 -334
  39. package/dist/SvMaskedInput.svelte +122 -122
  40. package/dist/SvMenu.svelte +124 -124
  41. package/dist/SvMenuList.svelte +146 -146
  42. package/dist/SvMultiSelect.svelte +293 -293
  43. package/dist/SvNumberInput.svelte +172 -172
  44. package/dist/SvOtpInput.svelte +158 -158
  45. package/dist/SvPasswordInput.svelte +151 -151
  46. package/dist/SvPhoneInput.svelte +133 -133
  47. package/dist/SvPopover.svelte +197 -197
  48. package/dist/SvProgress.svelte +116 -116
  49. package/dist/SvRadioGroup.svelte +107 -107
  50. package/dist/SvRating.svelte +112 -112
  51. package/dist/SvResult.svelte +73 -73
  52. package/dist/SvRichText.svelte +211 -211
  53. package/dist/SvRowGroupPanel.svelte +186 -170
  54. package/dist/SvRowGroupPanel.svelte.d.ts +8 -0
  55. package/dist/SvScrollArea.svelte +61 -61
  56. package/dist/SvSlider.svelte +203 -203
  57. package/dist/SvSwitchButton.svelte +108 -108
  58. package/dist/SvTagsInput.svelte +115 -115
  59. package/dist/SvTextInput.svelte +147 -147
  60. package/dist/SvTimePicker.svelte +245 -245
  61. package/dist/SvToaster.svelte +159 -159
  62. package/dist/SvToggleButton.svelte +85 -85
  63. package/dist/SvTooltip.svelte +161 -161
  64. package/dist/SvTour.svelte +208 -208
  65. package/dist/SvTree.svelte +444 -444
  66. package/dist/SvTreeSelect.svelte +239 -239
  67. package/dist/ai.d.ts +11 -2
  68. package/dist/ai.js +51 -7
  69. package/dist/cdn/GridMenus-DyFA-QxA.js +602 -0
  70. package/dist/cdn/GridMenus-M2XP9t0_.js +603 -0
  71. package/dist/cdn/{SvDateRangeInput-DflbiP7N.js → SvDateRangeInput-CaOuMs8O.js} +1 -1
  72. package/dist/cdn/{SvDateRangeInput-Bh0A0JkF.js → SvDateRangeInput-DMLKmGEc.js} +1 -1
  73. package/dist/cdn/{SvDateTimePicker-Bivn8dAP.js → SvDateTimePicker-CCbDZNZB.js} +1 -1
  74. package/dist/cdn/{SvDateTimePicker-BWwpfB_o.js → SvDateTimePicker-sonaH0oh.js} +1 -1
  75. package/dist/cdn/{SvGridCellEditor-Ba7rY3eu.js → SvGridCellEditor-BWnTi2N7.js} +2 -2
  76. package/dist/cdn/{SvGridCellEditor-AXL8cHGO.js → SvGridCellEditor-CjEJMJKc.js} +2 -2
  77. package/dist/cdn/SvGridChart-DAb9RbRs.js +1480 -0
  78. package/dist/cdn/SvGridChart-Yl-IXZFO.js +1481 -0
  79. package/dist/cdn/SvGridChartPanel-DayQpDsR.js +834 -0
  80. package/dist/cdn/SvGridChartPanel-aqf8n-vJ.js +810 -0
  81. package/dist/cdn/{SvGridChartView-eSPuJE6g.js → SvGridChartView-CxfakxR0.js} +5 -3
  82. package/dist/cdn/{SvGridChartView-SmPW10dI.js → SvGridChartView-Dm_YEx9L.js} +5 -3
  83. package/dist/cdn/{SvGridDropdown-B1ZcLpgs.js → SvGridDropdown-D0VdjeR8.js} +1 -1
  84. package/dist/cdn/{SvGridDropdown-CtdbHcIS.js → SvGridDropdown-D13MtJ2j.js} +1 -1
  85. package/dist/cdn/cell-formatting-C2Hf5gqd.js +86 -0
  86. package/dist/cdn/chart-CKJRCPOk.js +1652 -0
  87. package/dist/cdn/{date-format-DZT7T1wf.js → date-format-BNii4zeD.js} +2 -2
  88. package/dist/cdn/{date-format-CcP1tafP.js → date-format-BnnHlqGw.js} +2 -2
  89. package/dist/cdn/{export-format-Cv1Dll6k.js → export-format-CUDLy2yn.js} +84 -168
  90. package/dist/cdn/{src-B_YS5AOc.js → src-BJ2opd0j.js} +3642 -3450
  91. package/dist/cdn/{src-DYXXpuSk.js → src-DNbz94Ct.js} +8649 -8457
  92. package/dist/cdn/svgrid.js +16 -15
  93. package/dist/cdn/svgrid.svelte-external.js +16 -15
  94. package/dist/cell-formatting.d.ts +7 -0
  95. package/dist/cell-formatting.js +7 -1
  96. package/dist/chart-export.d.ts +23 -2
  97. package/dist/chart-export.js +88 -14
  98. package/dist/chart.d.ts +410 -18
  99. package/dist/chart.js +824 -24
  100. package/dist/editing.js +22 -13
  101. package/dist/grid-icons.d.ts +49 -0
  102. package/dist/grid-icons.js +86 -0
  103. package/dist/index.d.ts +3 -2
  104. package/dist/index.js +3 -2
  105. package/package.json +1 -1
  106. package/src/FlexRender.svelte +96 -96
  107. package/src/GridFooter.svelte +190 -181
  108. package/src/GridMenus.svelte +18 -10
  109. package/src/SvAutoComplete.svelte +169 -169
  110. package/src/SvAvatar.svelte +75 -75
  111. package/src/SvCalendar.svelte +503 -503
  112. package/src/SvCalendar.test.ts +226 -226
  113. package/src/SvCarousel.svelte +141 -141
  114. package/src/SvCheckBox.svelte +102 -102
  115. package/src/SvCircularProgress.svelte +109 -109
  116. package/src/SvColorInput.svelte +181 -181
  117. package/src/SvComboBox.svelte +279 -279
  118. package/src/SvContextMenu.svelte +116 -116
  119. package/src/SvCountryInput.svelte +163 -163
  120. package/src/SvDrawer.svelte +254 -254
  121. package/src/SvDropDownList.svelte +378 -378
  122. package/src/SvDurationInput.svelte +126 -126
  123. package/src/SvField.svelte +293 -293
  124. package/src/SvForm.svelte +437 -437
  125. package/src/SvForm.test.ts +411 -411
  126. package/src/SvGrid.controller.svelte.ts +146 -13
  127. package/src/SvGrid.css +79 -10
  128. package/src/SvGrid.svelte +191 -186
  129. package/src/SvGrid.types.ts +2133 -2092
  130. package/src/SvGridCellEditor.svelte +748 -744
  131. package/src/SvGridChart.svelte +2246 -1716
  132. package/src/SvGridChart.test.ts +243 -1
  133. package/src/SvGridChartPanel.svelte +633 -485
  134. package/src/SvGridChartView.svelte +79 -70
  135. package/src/SvGridDropdown.svelte +728 -728
  136. package/src/SvGridSelect.svelte +270 -270
  137. package/src/SvGroupCell.svelte +141 -116
  138. package/src/SvListBox.svelte +334 -334
  139. package/src/SvMaskedInput.svelte +122 -122
  140. package/src/SvMenu.svelte +124 -124
  141. package/src/SvMenu.test.ts +97 -97
  142. package/src/SvMenuList.svelte +146 -146
  143. package/src/SvMultiSelect.svelte +293 -293
  144. package/src/SvNumberInput.svelte +172 -172
  145. package/src/SvOtpInput.svelte +158 -158
  146. package/src/SvPasswordInput.svelte +151 -151
  147. package/src/SvPhoneInput.svelte +133 -133
  148. package/src/SvPopover.svelte +197 -197
  149. package/src/SvProgress.svelte +116 -116
  150. package/src/SvRadioGroup.svelte +107 -107
  151. package/src/SvRating.svelte +112 -112
  152. package/src/SvResult.svelte +73 -73
  153. package/src/SvRichText.svelte +211 -211
  154. package/src/SvRowGroupPanel.svelte +186 -170
  155. package/src/SvScrollArea.svelte +61 -61
  156. package/src/SvSlider.svelte +203 -203
  157. package/src/SvSwitchButton.svelte +108 -108
  158. package/src/SvTagsInput.svelte +115 -115
  159. package/src/SvTextInput.svelte +147 -147
  160. package/src/SvTimePicker.svelte +245 -245
  161. package/src/SvToaster.svelte +159 -159
  162. package/src/SvToaster.test.ts +95 -95
  163. package/src/SvToggleButton.svelte +85 -85
  164. package/src/SvTooltip.svelte +161 -161
  165. package/src/SvTour.svelte +208 -208
  166. package/src/SvTree.svelte +444 -444
  167. package/src/SvTreeSelect.svelte +239 -239
  168. package/src/a11y/dismissable.test.ts +119 -119
  169. package/src/a11y/dismissable.ts +114 -114
  170. package/src/a11y.contract.test.ts +49 -49
  171. package/src/a11y.test.ts +59 -59
  172. package/src/a11y.ts +61 -61
  173. package/src/ai.test.ts +561 -502
  174. package/src/ai.ts +1472 -1419
  175. package/src/build-api.coverage.test.ts +633 -633
  176. package/src/build-api.ts +846 -846
  177. package/src/builtin-editors.grid.test.ts +83 -83
  178. package/src/cell-formatting.ts +177 -171
  179. package/src/cell-render.test.ts +513 -513
  180. package/src/cell-render.ts +496 -496
  181. package/src/cell-values.ts +148 -148
  182. package/src/chart-export.test.ts +112 -0
  183. package/src/chart-export.ts +270 -202
  184. package/src/chart-view.svelte.ts +36 -36
  185. package/src/chart.test.ts +830 -1
  186. package/src/chart.ts +3342 -2321
  187. package/src/collaboration.test.ts +104 -104
  188. package/src/collaboration.ts +167 -167
  189. package/src/column-groups.ts +78 -78
  190. package/src/core.performance.test.ts +30 -30
  191. package/src/core.ts +1865 -1865
  192. package/src/createAutocomplete.svelte.ts +132 -132
  193. package/src/createCombobox.svelte.ts +191 -191
  194. package/src/createCountryInput.svelte.ts +157 -157
  195. package/src/createDropdownList.svelte.ts +168 -168
  196. package/src/createForm.svelte.ts +386 -386
  197. package/src/createGrid.svelte.ts +42 -42
  198. package/src/createGrid.test.ts +10 -10
  199. package/src/createGridState.svelte.ts +17 -17
  200. package/src/createListbox.svelte.ts +250 -250
  201. package/src/createMenu.svelte.ts +224 -224
  202. package/src/createPopoverSelect.svelte.ts +213 -213
  203. package/src/createSlider.svelte.ts +191 -191
  204. package/src/createTooltip.svelte.ts +144 -144
  205. package/src/createTree.svelte.ts +322 -322
  206. package/src/datetime/date-core.ts +206 -206
  207. package/src/datetime/date-restrict.ts +61 -61
  208. package/src/datetime/timezone.ts +135 -135
  209. package/src/dock-manager-model.ts +596 -596
  210. package/src/dock-model.ts +374 -374
  211. package/src/editing.test.ts +974 -974
  212. package/src/editing.ts +618 -609
  213. package/src/editor-contract.ts +171 -171
  214. package/src/editor-registry.grid.test.ts +144 -144
  215. package/src/editor-registry.ts +122 -122
  216. package/src/export-data-api.test.ts +126 -126
  217. package/src/export-format.test.ts +107 -107
  218. package/src/export-format.ts +601 -601
  219. package/src/filter-operators.ts +160 -160
  220. package/src/filtering/excel-filters.ts +325 -325
  221. package/src/flex-render.ts +3 -3
  222. package/src/form-field.ts +127 -127
  223. package/src/grid-icons.test.ts +46 -0
  224. package/src/grid-icons.ts +184 -0
  225. package/src/group-display.test.ts +167 -167
  226. package/src/group-display.ts +200 -200
  227. package/src/headless.ts +87 -87
  228. package/src/icon-overrides.grid.test.ts +188 -0
  229. package/src/icon-seam.test.ts +100 -0
  230. package/src/index.ts +19 -0
  231. package/src/js-scroller.svelte.ts +173 -173
  232. package/src/keyboard-handlers.ts +270 -270
  233. package/src/keyboard.test.ts +59 -59
  234. package/src/keyboard.ts +97 -97
  235. package/src/list-nav.test.ts +49 -49
  236. package/src/list-nav.ts +29 -29
  237. package/src/list-option.test.ts +56 -56
  238. package/src/list-option.ts +179 -179
  239. package/src/menus.ts +597 -597
  240. package/src/merge-objects.ts +48 -48
  241. package/src/overlays.test.ts +90 -90
  242. package/src/positioning.ts +268 -268
  243. package/src/render-component.ts +28 -28
  244. package/src/row-drag.test.ts +401 -401
  245. package/src/row-drag.ts +419 -419
  246. package/src/row-resize.test.ts +524 -524
  247. package/src/row-resize.ts +228 -228
  248. package/src/scheduler-ical.ts +181 -181
  249. package/src/scheduler-model.test.ts +562 -562
  250. package/src/scheduler-model.ts +873 -873
  251. package/src/selection.test.ts +885 -885
  252. package/src/server-data-source.test.ts +383 -383
  253. package/src/server-data-source.ts +469 -469
  254. package/src/sparkline.test.ts +68 -68
  255. package/src/sparkline.ts +169 -169
  256. package/src/spreadsheet.test.ts +488 -488
  257. package/src/spreadsheet.ts +312 -312
  258. package/src/static-functions.ts +11 -11
  259. package/src/subscribe.ts +38 -38
  260. package/src/summaries.ts +113 -113
  261. package/src/svgrid-wrapper.types.ts +563 -563
  262. package/src/svgrid.async-editor-options.test.ts +273 -273
  263. package/src/svgrid.auto-row-height.test.ts +204 -204
  264. package/src/svgrid.behavior.test.ts +910 -910
  265. package/src/svgrid.charting.test.ts +654 -534
  266. package/src/svgrid.comments-autocomplete.test.ts +127 -127
  267. package/src/svgrid.context-menu.test.ts +147 -147
  268. package/src/svgrid.features.test.ts +157 -157
  269. package/src/svgrid.filter-depth.test.ts +163 -163
  270. package/src/svgrid.filter-menu-listbox.svelte.test.ts +382 -377
  271. package/src/svgrid.filter-menu-scroll.test.ts +112 -112
  272. package/src/svgrid.grand-total.test.ts +188 -188
  273. package/src/svgrid.group-display-mode.test.ts +171 -171
  274. package/src/svgrid.group-footers.test.ts +121 -121
  275. package/src/svgrid.group-pagination.test.ts +153 -153
  276. package/src/svgrid.new-features.wrapper.test.ts +251 -251
  277. package/src/svgrid.tree-data.test.ts +186 -186
  278. package/src/svgrid.wrapper.test.ts +63 -63
  279. package/src/svgriddropdown.async-panel.svelte.test.ts +195 -195
  280. package/src/test-setup.ts +62 -62
  281. package/src/themes/index.ts +288 -288
  282. package/src/toast-store.svelte.ts +250 -250
  283. package/src/toast-store.test.ts +147 -147
  284. package/src/tree-row-model.test.ts +168 -168
  285. package/src/ui-buttons.test.ts +144 -144
  286. package/src/ui-inputs.test.ts +118 -118
  287. package/src/ui-localization.test.ts +113 -113
  288. package/src/ui-range.test.ts +70 -70
  289. package/src/ui-selection.test.ts +155 -155
  290. package/src/ui-tier1.test.ts +142 -142
  291. package/src/virtual.test.ts +88 -88
  292. package/src/virtualization/column-virtualizer.test.ts +27 -27
  293. package/src/virtualization/column-virtualizer.ts +30 -30
  294. package/src/virtualization/svelte-virtualizer.svelte.ts +26 -26
  295. package/src/virtualization/types.ts +30 -30
  296. package/src/virtualization/virtualizer.test.ts +47 -47
  297. package/src/virtualization/virtualizer.ts +322 -322
  298. package/dist/cdn/GridMenus-DMihUtma.js +0 -600
  299. package/dist/cdn/GridMenus-nmNDj1a3.js +0 -601
  300. package/dist/cdn/SvGridChart-Bs2GIR2Q.js +0 -1202
  301. package/dist/cdn/SvGridChart-CwhFz7GV.js +0 -1203
  302. package/dist/cdn/SvGridChartPanel-1WXuStzM.js +0 -561
  303. package/dist/cdn/SvGridChartPanel-e5_nqNP9.js +0 -585
  304. package/dist/cdn/chart-i4XcXyHJ.js +0 -1214
package/src/menus.ts CHANGED
@@ -1,597 +1,597 @@
1
- // menus handlers extracted from the controller. Imperative event handlers
2
- // reading/writing controller state via the `ctx` handle; the reactive core
3
- // ($state/$derived/$effect) stays in the controller.
4
- import {
5
- type RowData,
6
- type TableFeatures,
7
- } from "./index";
8
- import "./sv-grid-scrollbar";
9
- import { untrack } from "svelte";
10
- import type {
11
- FilterOperator,
12
- } from "./SvGrid.types";
13
- import {
14
- clampMenuX,
15
- cssEscape,
16
- } from "./SvGrid.helpers";
17
- import { splitInTokens, joinInTokens } from "./filtering/excel-filters";
18
-
19
- export function createMenus<
20
- TFeatures extends TableFeatures = TableFeatures,
21
- TData extends RowData = RowData,
22
- >(ctx: any) {
23
- function updateFilterRow(columnId: string, value: string) {
24
- ctx.filterRowValues = { ...ctx.filterRowValues, [columnId]: value };
25
- const current = ctx.filterMenuValues[columnId] ?? {
26
- operator: "contains" as const,
27
- value: "",
28
- };
29
- ctx.filterMenuValues = {
30
- ...ctx.filterMenuValues,
31
- [columnId]: { ...current, value },
32
- };
33
- }
34
-
35
- function updateFilterOperator(columnId: string, operator: FilterOperator) {
36
- const current = ctx.filterMenuValues[columnId] ?? {
37
- operator: "contains" as const,
38
- value: "",
39
- };
40
- ctx.filterMenuValues = {
41
- ...ctx.filterMenuValues,
42
- [columnId]: { ...current, operator },
43
- };
44
- }
45
-
46
- function updateFilterMenuValue(columnId: string, value: string) {
47
- const current = ctx.filterMenuValues[columnId] ?? {
48
- operator: "contains" as const,
49
- value: "",
50
- };
51
- ctx.filterMenuValues = {
52
- ...ctx.filterMenuValues,
53
- [columnId]: { ...current, value },
54
- };
55
- ctx.filterRowValues = { ...ctx.filterRowValues, [columnId]: value };
56
- }
57
-
58
- /** Upper-bound input for the `between` operator. Only relevant when
59
- * the column's current operator is `between`; harmless to call
60
- * otherwise. */
61
- function updateFilterMenuValueTo(columnId: string, valueTo: string) {
62
- const current = ctx.filterMenuValues[columnId] ?? {
63
- operator: "between" as const,
64
- value: "",
65
- };
66
- ctx.filterMenuValues = {
67
- ...ctx.filterMenuValues,
68
- [columnId]: { ...current, valueTo },
69
- };
70
- }
71
-
72
- /** Current `in` / `notIn` token list for a column (from the filter-row value). */
73
- function filterTokens(columnId: string): string[] {
74
- return splitInTokens(ctx.filterRowValues[columnId] ?? "");
75
- }
76
-
77
- /** Add a value to a column's `in` / `notIn` list (no-op if already present). */
78
- function addFilterToken(columnId: string, token: string) {
79
- const t = token.trim();
80
- if (!t) return;
81
- const tokens = filterTokens(columnId);
82
- if (!tokens.includes(t)) tokens.push(t);
83
- updateFilterRow(columnId, joinInTokens(tokens));
84
- }
85
-
86
- /** Remove a value from a column's `in` / `notIn` list. */
87
- function removeFilterToken(columnId: string, token: string) {
88
- updateFilterRow(
89
- columnId,
90
- joinInTokens(filterTokens(columnId).filter((t) => t !== token)),
91
- );
92
- }
93
-
94
- /** Toggle a value in a column's `in` / `notIn` list (checklist behaviour). */
95
- function toggleFilterToken(columnId: string, token: string) {
96
- if (filterTokens(columnId).includes(token)) removeFilterToken(columnId, token);
97
- else addFilterToken(columnId, token);
98
- }
99
-
100
- /**
101
- * Replace a column's `in` / `notIn` list wholesale. The suggestions dropdown
102
- * hands back the full token set after a click, which keeps tokens the user
103
- * typed by hand (or that the search box is currently hiding) intact.
104
- */
105
- function setFilterTokens(columnId: string, tokens: Iterable<string>) {
106
- const list = [...tokens].map((t) => t.trim()).filter(Boolean);
107
- updateFilterRow(columnId, joinInTokens(list));
108
- }
109
-
110
- function toggleCheckboxWithKeyboard(
111
- event: KeyboardEvent,
112
- toggle: () => void,
113
- ) {
114
- if (event.key !== "Enter" && event.key !== " ") return;
115
- event.preventDefault();
116
- toggle();
117
- }
118
-
119
- function isColumnFiltered(columnId: string) {
120
- if (ctx.valueFilters[columnId]) return true;
121
- const menuFilter = ctx.filterMenuValues[columnId];
122
- return Boolean(
123
- menuFilter &&
124
- (menuFilter.operator === "isBlank" ||
125
- menuFilter.operator === "isNotBlank" ||
126
- menuFilter.value.trim()),
127
- );
128
- }
129
-
130
- function closeMenus() {
131
- ctx.columnMenuFor = null;
132
- ctx.filterMenuFor = null;
133
- ctx.operatorMenuFor = null;
134
- ctx.inSuggestFor = null;
135
- ctx.chooseColumnsPos = null;
136
- }
137
-
138
- /** Open the `in` / `notIn` value-suggestions dropdown under a chip input. */
139
- function openInSuggest(anchor: HTMLElement, columnId: string) {
140
- const rect = anchor.getBoundingClientRect();
141
- ctx.inSuggestPos = { x: clampMenuX(rect.left, 220), y: rect.bottom + 4 };
142
- ctx.inSuggestQuery = "";
143
- ctx.inSuggestFor = columnId;
144
- }
145
-
146
- function closeInSuggest() {
147
- ctx.inSuggestFor = null;
148
- ctx.inSuggestQuery = "";
149
- }
150
-
151
- function openChooseColumns(event: MouseEvent) {
152
- event.stopPropagation();
153
- // Submenu behavior: keep the parent operations menu open and float the
154
- // Choose Columns panel out to the right of it, submenu-style. Falls
155
- // back to below the item if the popover would clip the viewport.
156
- const trigger = event.currentTarget as HTMLElement;
157
- const menuEl = trigger.closest(".sv-grid-menu") as HTMLElement | null;
158
- const anchor = (menuEl ?? trigger).getBoundingClientRect();
159
- const panelWidth = 240;
160
- const wantRight = anchor.right + 4;
161
- const x =
162
- wantRight + panelWidth + 8 > window.innerWidth
163
- ? clampMenuX(anchor.left - panelWidth - 4, panelWidth)
164
- : clampMenuX(wantRight, panelWidth);
165
- ctx.chooseColumnsPos = { x, y: anchor.top };
166
- }
167
-
168
- function openColumnMenu(event: MouseEvent, columnId: string) {
169
- event.stopPropagation();
170
- const wasOpen = ctx.columnMenuFor === columnId;
171
- closeMenus();
172
- if (wasOpen) return;
173
- const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
174
- ctx.columnMenuPos = {
175
- x: clampMenuX(rect.right - 240, 240),
176
- y: rect.bottom + 4,
177
- };
178
- ctx.columnMenuTab = "general";
179
- ctx.columnMenuFor = columnId;
180
- }
181
-
182
- function openFilterMenu(event: MouseEvent, columnId: string) {
183
- event.stopPropagation();
184
- const wasOpen = ctx.filterMenuFor === columnId;
185
- closeMenus();
186
- if (wasOpen) return;
187
-
188
- // If the consumer is running in row-mode (or any mode where the
189
- // funnel popover wouldn't render), redirect the click to the
190
- // inline filter input for this column. Without this the funnel
191
- // would be a dead button - it visibly clicks but nothing happens
192
- // because the menu is gated behind `showColumnFiltersEffective`.
193
- if (!ctx.showColumnFiltersEffective && ctx.showFilterRowEffective) {
194
- // Find the row-mode filter input and focus it. The input is
195
- // tagged with `data-svgrid-filter-col` so we can target by id
196
- // without depending on column position.
197
- const input = (ctx.scrollContainer as HTMLElement | null)?.querySelector<HTMLInputElement>(
198
- `[data-svgrid-filter-col="${cssEscape(columnId)}"]`,
199
- );
200
- if (input) {
201
- input.focus();
202
- input.select();
203
- // Brief pulse so the user sees where focus landed.
204
- input.classList.add("sv-grid-filter-value-pulse");
205
- setTimeout(() => input.classList.remove("sv-grid-filter-value-pulse"), 700);
206
- }
207
- return;
208
- }
209
-
210
- const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
211
- ctx.filterMenuPos = {
212
- x: clampMenuX(rect.right - 260, 260),
213
- y: rect.bottom + 4,
214
- };
215
- ctx.columnMenuSearch = "";
216
- ctx.filterMenuFor = columnId;
217
- }
218
-
219
- function openOperatorMenu(event: MouseEvent, columnId: string) {
220
- event.stopPropagation();
221
- const wasOpen = ctx.operatorMenuFor === columnId;
222
- closeMenus();
223
- if (wasOpen) return;
224
- const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
225
- ctx.operatorMenuPos = { x: clampMenuX(rect.left, 184), y: rect.bottom + 4 };
226
- ctx.operatorMenuFor = columnId;
227
- }
228
-
229
- function sortColumnFromMenu(columnId: string, desc: boolean) {
230
- ctx.grid.store.setState((prev: any) => ({
231
- ...prev,
232
- sorting: [{ id: columnId, desc }],
233
- }));
234
- closeMenus();
235
- }
236
-
237
- function clearColumnSort(columnId: string) {
238
- ctx.grid.store.setState((prev: any) => ({
239
- ...prev,
240
- sorting: (prev.sorting ?? []).filter(
241
- (entry: { id: string }) => entry.id !== columnId,
242
- ),
243
- }));
244
- closeMenus();
245
- }
246
-
247
- function groupByColumnFromMenu(columnId: string) {
248
- const current = ctx.grid.getState().grouping ?? [];
249
- if (current.includes(columnId)) {
250
- closeMenus();
251
- return;
252
- }
253
- ctx.grid.setGrouping([...current, columnId]);
254
- closeMenus();
255
- }
256
-
257
- function clearGroupingFromMenu(columnId: string) {
258
- const current = ctx.grid.getState().grouping ?? [];
259
- ctx.grid.setGrouping(current.filter((id: string) => id !== columnId));
260
- closeMenus();
261
- }
262
-
263
- function isFacetChecked(columnId: string, value: string) {
264
- const selected = ctx.valueFilters[columnId];
265
- return selected ? selected.has(value) : true;
266
- }
267
-
268
- function toggleFacetValue(columnId: string, value: string) {
269
- const allValues = ctx.columnMenuFacetValues;
270
- const next = new Set(ctx.valueFilters[columnId] ?? allValues);
271
- if (next.has(value)) next.delete(value);
272
- else next.add(value);
273
- if (next.size === allValues.length) {
274
- const copy = { ...ctx.valueFilters };
275
- delete copy[columnId];
276
- ctx.valueFilters = copy;
277
- } else {
278
- ctx.valueFilters = { ...ctx.valueFilters, [columnId]: next };
279
- }
280
- }
281
-
282
- /**
283
- * Replace a column's checked-facet set. "Everything checked" is stored as an
284
- * ABSENT entry (so an unfiltered high-cardinality column never holds a set of
285
- * every value), which is why the full-size case deletes instead of writing.
286
- */
287
- function setFacetSelection(columnId: string, next: ReadonlySet<string>) {
288
- if (next.size >= ctx.columnMenuFacetValues.length) {
289
- const copy = { ...ctx.valueFilters };
290
- delete copy[columnId];
291
- ctx.valueFilters = copy;
292
- return;
293
- }
294
- ctx.valueFilters = { ...ctx.valueFilters, [columnId]: new Set(next) };
295
- }
296
-
297
- /**
298
- * With a search query typed, "select all" means the RESULTS - the same thing
299
- * it means in a spreadsheet. It used to mean the whole column regardless, so
300
- * the standard flow (type a query, tick select-all, get just the matches)
301
- * cleared the entire selection instead: `isAllFacetsChecked` saw every value
302
- * ticked, read that as "on", and turned everything off.
303
- */
304
- function isAllFacetsChecked(columnId: string) {
305
- const selected = ctx.valueFilters[columnId];
306
- const visible = ctx.columnMenuVisibleFacets as Array<string>;
307
- const searching = visible.length !== ctx.columnMenuFacetValues.length;
308
- if (!selected) return true;
309
- if (searching) return visible.every((value) => selected.has(value));
310
- return selected.size >= ctx.columnMenuFacetValues.length;
311
- }
312
-
313
- function toggleAllFacets(columnId: string) {
314
- const visible = ctx.columnMenuVisibleFacets as Array<string>;
315
- const all = ctx.columnMenuFacetValues as Array<string>;
316
- const searching = visible.length !== all.length;
317
- const on = isAllFacetsChecked(columnId);
318
-
319
- if (!searching) {
320
- if (on) {
321
- ctx.valueFilters = { ...ctx.valueFilters, [columnId]: new Set<string>() };
322
- } else {
323
- const copy = { ...ctx.valueFilters };
324
- delete copy[columnId];
325
- ctx.valueFilters = copy;
326
- }
327
- return;
328
- }
329
- // Searching: add or remove just the matches, leaving the rest of the
330
- // selection alone. An absent entry means "everything", so it has to be
331
- // materialized before a subtraction can mean anything.
332
- const current = ctx.valueFilters[columnId]
333
- ? new Set<string>(ctx.valueFilters[columnId])
334
- : new Set<string>(all);
335
- for (const value of visible) {
336
- if (on) current.delete(value);
337
- else current.add(value);
338
- }
339
- ctx.valueFilters = { ...ctx.valueFilters, [columnId]: current };
340
- }
341
-
342
- function clearColumnFilter(columnId: string) {
343
- // `untrack`: read-modify-write on the filter maps. Keeps the method safe to
344
- // call from a consumer's reactive `$effect` (e.g. via `api.clearFilter`)
345
- // without capturing these reads as a dependency and looping. See
346
- // `build-api`'s `setFilter` for the full rationale.
347
- untrack(() => {
348
- if (ctx.valueFilters[columnId]) {
349
- const next = { ...ctx.valueFilters };
350
- delete next[columnId];
351
- ctx.valueFilters = next;
352
- }
353
- if (ctx.filterMenuValues[columnId]) {
354
- const next = { ...ctx.filterMenuValues };
355
- delete next[columnId];
356
- ctx.filterMenuValues = next;
357
- }
358
- if (ctx.filterRowValues[columnId]) {
359
- const next = { ...ctx.filterRowValues };
360
- delete next[columnId];
361
- ctx.filterRowValues = next;
362
- }
363
- });
364
- }
365
-
366
- // External pagination is controlled: emit the requested page/size and let
367
- // the consumer fetch + update props. Never mutate local pagination state.
368
- function emitExternalPagination(pageIndex: number, pageSize: number): boolean {
369
- if (ctx.props.externalPagination !== true) return false;
370
- ctx.props.onPaginationChange?.({ pageIndex: Math.max(0, pageIndex), pageSize });
371
- ctx.scrollContainer?.scrollTo?.({ top: 0 });
372
- return true;
373
- }
374
-
375
- function changePage(delta: number) {
376
- if (
377
- emitExternalPagination(
378
- (ctx.props.pageIndex ?? 0) + delta,
379
- ctx.props.pageSize ?? 10,
380
- )
381
- )
382
- return;
383
- ctx.grid.setPagination((prev: any) => ({
384
- ...prev,
385
- pageIndex: Math.max((prev?.pageIndex ?? 0) + delta, 0),
386
- }));
387
- ctx.scrollContainer?.scrollTo?.({ top: 0 });
388
- }
389
-
390
- function goToPage(pageIndex: number) {
391
- if (emitExternalPagination(pageIndex, ctx.props.pageSize ?? 10)) return;
392
- ctx.grid.setPagination((prev: any) => ({
393
- ...prev,
394
- pageIndex: Math.max(0, pageIndex),
395
- }));
396
- ctx.scrollContainer?.scrollTo?.({ top: 0 });
397
- }
398
-
399
- function setPageSize(pageSize: number) {
400
- // External mode: changing page size resets to the first page.
401
- if (emitExternalPagination(0, pageSize)) return;
402
- ctx.grid.setPagination((prev: any) => {
403
- const oldSize = prev?.pageSize ?? 10;
404
- const oldIndex = prev?.pageIndex ?? 0;
405
- // Keep the first visible row in view when the page size changes.
406
- const firstVisibleRow = oldIndex * oldSize;
407
- return {
408
- ...prev,
409
- pageSize,
410
- pageIndex: Math.max(0, Math.floor(firstVisibleRow / pageSize)),
411
- };
412
- });
413
- }
414
-
415
- // ---- Right-click context menu --------------------------------------
416
- type CtxTarget = { rowIndex: number; colIndex: number; columnId: string; rowId: string; row: TData | null };
417
- type ResolvedItem = { key: string; label: string; separator?: boolean; disabled?: boolean; run?: () => void };
418
-
419
- function insertRowAt(t: CtxTarget, offset: number) {
420
- const data = ctx.internalData.slice() as TData[];
421
- const idx = t.row ? data.indexOf(t.row) : -1;
422
- const at = idx < 0 ? data.length : idx + offset;
423
- data.splice(at, 0, {} as TData);
424
- ctx.internalData = data;
425
- }
426
- function removeRowAt(t: CtxTarget) {
427
- if (!t.row) return;
428
- ctx.internalData = (ctx.internalData as TData[]).filter((r) => r !== t.row);
429
- }
430
- function removeColAt(t: CtxTarget) {
431
- ctx.internalColumns = ctx.internalColumns.filter(
432
- (d: any) => (d.id ?? d.field) !== t.columnId,
433
- );
434
- }
435
-
436
- // Built-in items, keyed. `run` reads the live target passed at click time.
437
- // `msg` names the GridMessages key so the label localizes (falls back to the
438
- // English `label` when no `localeText` is set).
439
- const builtins: Record<string, { label: string; msg?: string; run: (t: CtxTarget) => void }> = {
440
- copy: { label: "Copy", msg: "menuCopy", run: () => ctx.copySelectionToClipboard() },
441
- cut: { label: "Cut", msg: "menuCut", run: () => ctx.cutSelectionToClipboard() },
442
- paste: { label: "Paste", msg: "menuPaste", run: () => void ctx.pasteFromClipboard() },
443
- clear: { label: "Clear", msg: "menuClear", run: () => ctx.clearSelectedCells() },
444
- row_above: { label: "Insert row above", msg: "menuInsertRowAbove", run: (t) => insertRowAt(t, 0) },
445
- row_below: { label: "Insert row below", msg: "menuInsertRowBelow", run: (t) => insertRowAt(t, 1) },
446
- remove_row: { label: "Remove row", msg: "menuRemoveRow", run: removeRowAt },
447
- remove_col: { label: "Remove column", msg: "menuRemoveColumn", run: removeColAt },
448
- comment: { label: "Edit comment", msg: "menuEditComment", run: (t) => openCommentEditor(t) },
449
- // Charting: open the chart panel scoped to the current cell selection. Gated
450
- // on `chartingEnabled` in the resolver below.
451
- chart: { label: "Chart selected range", msg: "chartRange", run: () => { ctx.chartPanelOpen = true; } },
452
- };
453
- const DEFAULT_ITEMS = [
454
- "copy", "cut", "paste", "clear", "separator",
455
- "row_above", "row_below", "remove_row", "separator", "remove_col",
456
- ];
457
-
458
- function openContextMenu(
459
- event: MouseEvent,
460
- rowIndex: number,
461
- colIndex: number,
462
- columnId: string,
463
- ) {
464
- if (!ctx.props.contextMenu) return;
465
- event.preventDefault();
466
- closeMenus();
467
- ctx.contextMenuFor = null;
468
- const rowModel = ctx.allRows[rowIndex];
469
- const row = rowModel?.original ?? null;
470
- const rowId = rowModel?.id ?? String(rowIndex);
471
- // Reserve ~280px for the menu body. If the click is in the bottom third,
472
- // flip the menu above the cursor so it stays fully in the viewport.
473
- const menuHeight = 280;
474
- const y = event.clientY + menuHeight > window.innerHeight
475
- ? Math.max(8, event.clientY - menuHeight)
476
- : event.clientY;
477
- ctx.contextMenuPos = {
478
- x: clampMenuX(event.clientX, 220),
479
- y,
480
- };
481
- ctx.contextMenuFor = { rowIndex, colIndex, columnId, rowId, row };
482
- }
483
-
484
- function closeContextMenu() {
485
- ctx.contextMenuFor = null;
486
- }
487
-
488
- // ---- Editable comments ---------------------------------------------
489
- function openCommentEditor(t: CtxTarget) {
490
- const cur =
491
- ctx.noteOverrides?.[t.rowId]?.[t.columnId] ??
492
- ctx.props.notes?.[t.rowId]?.[t.columnId] ??
493
- "";
494
- ctx.commentDraft = cur;
495
- ctx.commentEditFor = { rowId: t.rowId, columnId: t.columnId, x: ctx.contextMenuPos.x, y: ctx.contextMenuPos.y };
496
- ctx.contextMenuFor = null;
497
- }
498
- function writeNote(rowId: string, columnId: string, note: string) {
499
- const next = { ...ctx.noteOverrides };
500
- next[rowId] = { ...(next[rowId] ?? {}), [columnId]: note };
501
- ctx.noteOverrides = next;
502
- ctx.props.onNoteChange?.({ rowId, columnId, note });
503
- }
504
- function saveComment() {
505
- const e = ctx.commentEditFor;
506
- if (!e) return;
507
- writeNote(e.rowId, e.columnId, ctx.commentDraft);
508
- ctx.commentEditFor = null;
509
- }
510
- function removeComment() {
511
- const e = ctx.commentEditFor;
512
- if (!e) return;
513
- writeNote(e.rowId, e.columnId, "");
514
- ctx.commentEditFor = null;
515
- }
516
- function closeCommentEditor() {
517
- ctx.commentEditFor = null;
518
- }
519
-
520
- /** Resolve the configured items against the current target for rendering. */
521
- function contextMenuItems(): ResolvedItem[] {
522
- const target = ctx.contextMenuFor as CtxTarget | null;
523
- if (!target) return [];
524
- const cfg = ctx.props.contextMenu;
525
- const messages = ctx.messages;
526
- const entries: any[] = Array.isArray(cfg)
527
- ? cfg
528
- : [
529
- ...DEFAULT_ITEMS,
530
- ...(ctx.chartingEnabled ? ["separator", "chart"] : []),
531
- ...(ctx.props.editableComments ? ["separator", "comment"] : []),
532
- ];
533
- const out: ResolvedItem[] = [];
534
- for (const entry of entries) {
535
- if (entry === "separator") { out.push({ key: "sep" + out.length, label: "", separator: true }); continue; }
536
- if (typeof entry === "string") {
537
- // The chart item only makes sense when charting is enabled.
538
- if (entry === "chart" && !ctx.chartingEnabled) continue;
539
- const b = builtins[entry];
540
- if (!b) continue;
541
- const label = (b.msg && messages?.[b.msg]) || b.label;
542
- out.push({ key: entry, label, run: () => b.run(target) });
543
- continue;
544
- }
545
- if (entry.hidden?.(target)) continue;
546
- out.push({
547
- key: entry.key,
548
- label: entry.label,
549
- disabled: entry.disabled?.(target) ?? false,
550
- run: () => entry.action(target),
551
- });
552
- }
553
- // Drop leading/trailing/double separators.
554
- return out.filter((it, i, a) =>
555
- !it.separator || (i > 0 && i < a.length - 1 && !a[i - 1]?.separator),
556
- );
557
- }
558
-
559
- return {
560
- updateFilterRow,
561
- updateFilterOperator,
562
- updateFilterMenuValue,
563
- updateFilterMenuValueTo,
564
- addFilterToken,
565
- removeFilterToken,
566
- toggleFilterToken,
567
- toggleCheckboxWithKeyboard,
568
- isColumnFiltered,
569
- closeMenus,
570
- openInSuggest,
571
- closeInSuggest,
572
- openChooseColumns,
573
- openColumnMenu,
574
- openFilterMenu,
575
- openOperatorMenu,
576
- sortColumnFromMenu,
577
- clearColumnSort,
578
- groupByColumnFromMenu,
579
- clearGroupingFromMenu,
580
- isFacetChecked,
581
- toggleFacetValue,
582
- setFacetSelection,
583
- setFilterTokens,
584
- isAllFacetsChecked,
585
- toggleAllFacets,
586
- clearColumnFilter,
587
- changePage,
588
- goToPage,
589
- setPageSize,
590
- openContextMenu,
591
- closeContextMenu,
592
- contextMenuItems,
593
- saveComment,
594
- removeComment,
595
- closeCommentEditor,
596
- };
597
- }
1
+ // menus handlers extracted from the controller. Imperative event handlers
2
+ // reading/writing controller state via the `ctx` handle; the reactive core
3
+ // ($state/$derived/$effect) stays in the controller.
4
+ import {
5
+ type RowData,
6
+ type TableFeatures,
7
+ } from "./index";
8
+ import "./sv-grid-scrollbar";
9
+ import { untrack } from "svelte";
10
+ import type {
11
+ FilterOperator,
12
+ } from "./SvGrid.types";
13
+ import {
14
+ clampMenuX,
15
+ cssEscape,
16
+ } from "./SvGrid.helpers";
17
+ import { splitInTokens, joinInTokens } from "./filtering/excel-filters";
18
+
19
+ export function createMenus<
20
+ TFeatures extends TableFeatures = TableFeatures,
21
+ TData extends RowData = RowData,
22
+ >(ctx: any) {
23
+ function updateFilterRow(columnId: string, value: string) {
24
+ ctx.filterRowValues = { ...ctx.filterRowValues, [columnId]: value };
25
+ const current = ctx.filterMenuValues[columnId] ?? {
26
+ operator: "contains" as const,
27
+ value: "",
28
+ };
29
+ ctx.filterMenuValues = {
30
+ ...ctx.filterMenuValues,
31
+ [columnId]: { ...current, value },
32
+ };
33
+ }
34
+
35
+ function updateFilterOperator(columnId: string, operator: FilterOperator) {
36
+ const current = ctx.filterMenuValues[columnId] ?? {
37
+ operator: "contains" as const,
38
+ value: "",
39
+ };
40
+ ctx.filterMenuValues = {
41
+ ...ctx.filterMenuValues,
42
+ [columnId]: { ...current, operator },
43
+ };
44
+ }
45
+
46
+ function updateFilterMenuValue(columnId: string, value: string) {
47
+ const current = ctx.filterMenuValues[columnId] ?? {
48
+ operator: "contains" as const,
49
+ value: "",
50
+ };
51
+ ctx.filterMenuValues = {
52
+ ...ctx.filterMenuValues,
53
+ [columnId]: { ...current, value },
54
+ };
55
+ ctx.filterRowValues = { ...ctx.filterRowValues, [columnId]: value };
56
+ }
57
+
58
+ /** Upper-bound input for the `between` operator. Only relevant when
59
+ * the column's current operator is `between`; harmless to call
60
+ * otherwise. */
61
+ function updateFilterMenuValueTo(columnId: string, valueTo: string) {
62
+ const current = ctx.filterMenuValues[columnId] ?? {
63
+ operator: "between" as const,
64
+ value: "",
65
+ };
66
+ ctx.filterMenuValues = {
67
+ ...ctx.filterMenuValues,
68
+ [columnId]: { ...current, valueTo },
69
+ };
70
+ }
71
+
72
+ /** Current `in` / `notIn` token list for a column (from the filter-row value). */
73
+ function filterTokens(columnId: string): string[] {
74
+ return splitInTokens(ctx.filterRowValues[columnId] ?? "");
75
+ }
76
+
77
+ /** Add a value to a column's `in` / `notIn` list (no-op if already present). */
78
+ function addFilterToken(columnId: string, token: string) {
79
+ const t = token.trim();
80
+ if (!t) return;
81
+ const tokens = filterTokens(columnId);
82
+ if (!tokens.includes(t)) tokens.push(t);
83
+ updateFilterRow(columnId, joinInTokens(tokens));
84
+ }
85
+
86
+ /** Remove a value from a column's `in` / `notIn` list. */
87
+ function removeFilterToken(columnId: string, token: string) {
88
+ updateFilterRow(
89
+ columnId,
90
+ joinInTokens(filterTokens(columnId).filter((t) => t !== token)),
91
+ );
92
+ }
93
+
94
+ /** Toggle a value in a column's `in` / `notIn` list (checklist behaviour). */
95
+ function toggleFilterToken(columnId: string, token: string) {
96
+ if (filterTokens(columnId).includes(token)) removeFilterToken(columnId, token);
97
+ else addFilterToken(columnId, token);
98
+ }
99
+
100
+ /**
101
+ * Replace a column's `in` / `notIn` list wholesale. The suggestions dropdown
102
+ * hands back the full token set after a click, which keeps tokens the user
103
+ * typed by hand (or that the search box is currently hiding) intact.
104
+ */
105
+ function setFilterTokens(columnId: string, tokens: Iterable<string>) {
106
+ const list = [...tokens].map((t) => t.trim()).filter(Boolean);
107
+ updateFilterRow(columnId, joinInTokens(list));
108
+ }
109
+
110
+ function toggleCheckboxWithKeyboard(
111
+ event: KeyboardEvent,
112
+ toggle: () => void,
113
+ ) {
114
+ if (event.key !== "Enter" && event.key !== " ") return;
115
+ event.preventDefault();
116
+ toggle();
117
+ }
118
+
119
+ function isColumnFiltered(columnId: string) {
120
+ if (ctx.valueFilters[columnId]) return true;
121
+ const menuFilter = ctx.filterMenuValues[columnId];
122
+ return Boolean(
123
+ menuFilter &&
124
+ (menuFilter.operator === "isBlank" ||
125
+ menuFilter.operator === "isNotBlank" ||
126
+ menuFilter.value.trim()),
127
+ );
128
+ }
129
+
130
+ function closeMenus() {
131
+ ctx.columnMenuFor = null;
132
+ ctx.filterMenuFor = null;
133
+ ctx.operatorMenuFor = null;
134
+ ctx.inSuggestFor = null;
135
+ ctx.chooseColumnsPos = null;
136
+ }
137
+
138
+ /** Open the `in` / `notIn` value-suggestions dropdown under a chip input. */
139
+ function openInSuggest(anchor: HTMLElement, columnId: string) {
140
+ const rect = anchor.getBoundingClientRect();
141
+ ctx.inSuggestPos = { x: clampMenuX(rect.left, 220), y: rect.bottom + 4 };
142
+ ctx.inSuggestQuery = "";
143
+ ctx.inSuggestFor = columnId;
144
+ }
145
+
146
+ function closeInSuggest() {
147
+ ctx.inSuggestFor = null;
148
+ ctx.inSuggestQuery = "";
149
+ }
150
+
151
+ function openChooseColumns(event: MouseEvent) {
152
+ event.stopPropagation();
153
+ // Submenu behavior: keep the parent operations menu open and float the
154
+ // Choose Columns panel out to the right of it, submenu-style. Falls
155
+ // back to below the item if the popover would clip the viewport.
156
+ const trigger = event.currentTarget as HTMLElement;
157
+ const menuEl = trigger.closest(".sv-grid-menu") as HTMLElement | null;
158
+ const anchor = (menuEl ?? trigger).getBoundingClientRect();
159
+ const panelWidth = 240;
160
+ const wantRight = anchor.right + 4;
161
+ const x =
162
+ wantRight + panelWidth + 8 > window.innerWidth
163
+ ? clampMenuX(anchor.left - panelWidth - 4, panelWidth)
164
+ : clampMenuX(wantRight, panelWidth);
165
+ ctx.chooseColumnsPos = { x, y: anchor.top };
166
+ }
167
+
168
+ function openColumnMenu(event: MouseEvent, columnId: string) {
169
+ event.stopPropagation();
170
+ const wasOpen = ctx.columnMenuFor === columnId;
171
+ closeMenus();
172
+ if (wasOpen) return;
173
+ const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
174
+ ctx.columnMenuPos = {
175
+ x: clampMenuX(rect.right - 240, 240),
176
+ y: rect.bottom + 4,
177
+ };
178
+ ctx.columnMenuTab = "general";
179
+ ctx.columnMenuFor = columnId;
180
+ }
181
+
182
+ function openFilterMenu(event: MouseEvent, columnId: string) {
183
+ event.stopPropagation();
184
+ const wasOpen = ctx.filterMenuFor === columnId;
185
+ closeMenus();
186
+ if (wasOpen) return;
187
+
188
+ // If the consumer is running in row-mode (or any mode where the
189
+ // funnel popover wouldn't render), redirect the click to the
190
+ // inline filter input for this column. Without this the funnel
191
+ // would be a dead button - it visibly clicks but nothing happens
192
+ // because the menu is gated behind `showColumnFiltersEffective`.
193
+ if (!ctx.showColumnFiltersEffective && ctx.showFilterRowEffective) {
194
+ // Find the row-mode filter input and focus it. The input is
195
+ // tagged with `data-svgrid-filter-col` so we can target by id
196
+ // without depending on column position.
197
+ const input = (ctx.scrollContainer as HTMLElement | null)?.querySelector<HTMLInputElement>(
198
+ `[data-svgrid-filter-col="${cssEscape(columnId)}"]`,
199
+ );
200
+ if (input) {
201
+ input.focus();
202
+ input.select();
203
+ // Brief pulse so the user sees where focus landed.
204
+ input.classList.add("sv-grid-filter-value-pulse");
205
+ setTimeout(() => input.classList.remove("sv-grid-filter-value-pulse"), 700);
206
+ }
207
+ return;
208
+ }
209
+
210
+ const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
211
+ ctx.filterMenuPos = {
212
+ x: clampMenuX(rect.right - 260, 260),
213
+ y: rect.bottom + 4,
214
+ };
215
+ ctx.columnMenuSearch = "";
216
+ ctx.filterMenuFor = columnId;
217
+ }
218
+
219
+ function openOperatorMenu(event: MouseEvent, columnId: string) {
220
+ event.stopPropagation();
221
+ const wasOpen = ctx.operatorMenuFor === columnId;
222
+ closeMenus();
223
+ if (wasOpen) return;
224
+ const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
225
+ ctx.operatorMenuPos = { x: clampMenuX(rect.left, 184), y: rect.bottom + 4 };
226
+ ctx.operatorMenuFor = columnId;
227
+ }
228
+
229
+ function sortColumnFromMenu(columnId: string, desc: boolean) {
230
+ ctx.grid.store.setState((prev: any) => ({
231
+ ...prev,
232
+ sorting: [{ id: columnId, desc }],
233
+ }));
234
+ closeMenus();
235
+ }
236
+
237
+ function clearColumnSort(columnId: string) {
238
+ ctx.grid.store.setState((prev: any) => ({
239
+ ...prev,
240
+ sorting: (prev.sorting ?? []).filter(
241
+ (entry: { id: string }) => entry.id !== columnId,
242
+ ),
243
+ }));
244
+ closeMenus();
245
+ }
246
+
247
+ function groupByColumnFromMenu(columnId: string) {
248
+ const current = ctx.grid.getState().grouping ?? [];
249
+ if (current.includes(columnId)) {
250
+ closeMenus();
251
+ return;
252
+ }
253
+ ctx.grid.setGrouping([...current, columnId]);
254
+ closeMenus();
255
+ }
256
+
257
+ function clearGroupingFromMenu(columnId: string) {
258
+ const current = ctx.grid.getState().grouping ?? [];
259
+ ctx.grid.setGrouping(current.filter((id: string) => id !== columnId));
260
+ closeMenus();
261
+ }
262
+
263
+ function isFacetChecked(columnId: string, value: string) {
264
+ const selected = ctx.valueFilters[columnId];
265
+ return selected ? selected.has(value) : true;
266
+ }
267
+
268
+ function toggleFacetValue(columnId: string, value: string) {
269
+ const allValues = ctx.columnMenuFacetValues;
270
+ const next = new Set(ctx.valueFilters[columnId] ?? allValues);
271
+ if (next.has(value)) next.delete(value);
272
+ else next.add(value);
273
+ if (next.size === allValues.length) {
274
+ const copy = { ...ctx.valueFilters };
275
+ delete copy[columnId];
276
+ ctx.valueFilters = copy;
277
+ } else {
278
+ ctx.valueFilters = { ...ctx.valueFilters, [columnId]: next };
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Replace a column's checked-facet set. "Everything checked" is stored as an
284
+ * ABSENT entry (so an unfiltered high-cardinality column never holds a set of
285
+ * every value), which is why the full-size case deletes instead of writing.
286
+ */
287
+ function setFacetSelection(columnId: string, next: ReadonlySet<string>) {
288
+ if (next.size >= ctx.columnMenuFacetValues.length) {
289
+ const copy = { ...ctx.valueFilters };
290
+ delete copy[columnId];
291
+ ctx.valueFilters = copy;
292
+ return;
293
+ }
294
+ ctx.valueFilters = { ...ctx.valueFilters, [columnId]: new Set(next) };
295
+ }
296
+
297
+ /**
298
+ * With a search query typed, "select all" means the RESULTS - the same thing
299
+ * it means in a spreadsheet. It used to mean the whole column regardless, so
300
+ * the standard flow (type a query, tick select-all, get just the matches)
301
+ * cleared the entire selection instead: `isAllFacetsChecked` saw every value
302
+ * ticked, read that as "on", and turned everything off.
303
+ */
304
+ function isAllFacetsChecked(columnId: string) {
305
+ const selected = ctx.valueFilters[columnId];
306
+ const visible = ctx.columnMenuVisibleFacets as Array<string>;
307
+ const searching = visible.length !== ctx.columnMenuFacetValues.length;
308
+ if (!selected) return true;
309
+ if (searching) return visible.every((value) => selected.has(value));
310
+ return selected.size >= ctx.columnMenuFacetValues.length;
311
+ }
312
+
313
+ function toggleAllFacets(columnId: string) {
314
+ const visible = ctx.columnMenuVisibleFacets as Array<string>;
315
+ const all = ctx.columnMenuFacetValues as Array<string>;
316
+ const searching = visible.length !== all.length;
317
+ const on = isAllFacetsChecked(columnId);
318
+
319
+ if (!searching) {
320
+ if (on) {
321
+ ctx.valueFilters = { ...ctx.valueFilters, [columnId]: new Set<string>() };
322
+ } else {
323
+ const copy = { ...ctx.valueFilters };
324
+ delete copy[columnId];
325
+ ctx.valueFilters = copy;
326
+ }
327
+ return;
328
+ }
329
+ // Searching: add or remove just the matches, leaving the rest of the
330
+ // selection alone. An absent entry means "everything", so it has to be
331
+ // materialized before a subtraction can mean anything.
332
+ const current = ctx.valueFilters[columnId]
333
+ ? new Set<string>(ctx.valueFilters[columnId])
334
+ : new Set<string>(all);
335
+ for (const value of visible) {
336
+ if (on) current.delete(value);
337
+ else current.add(value);
338
+ }
339
+ ctx.valueFilters = { ...ctx.valueFilters, [columnId]: current };
340
+ }
341
+
342
+ function clearColumnFilter(columnId: string) {
343
+ // `untrack`: read-modify-write on the filter maps. Keeps the method safe to
344
+ // call from a consumer's reactive `$effect` (e.g. via `api.clearFilter`)
345
+ // without capturing these reads as a dependency and looping. See
346
+ // `build-api`'s `setFilter` for the full rationale.
347
+ untrack(() => {
348
+ if (ctx.valueFilters[columnId]) {
349
+ const next = { ...ctx.valueFilters };
350
+ delete next[columnId];
351
+ ctx.valueFilters = next;
352
+ }
353
+ if (ctx.filterMenuValues[columnId]) {
354
+ const next = { ...ctx.filterMenuValues };
355
+ delete next[columnId];
356
+ ctx.filterMenuValues = next;
357
+ }
358
+ if (ctx.filterRowValues[columnId]) {
359
+ const next = { ...ctx.filterRowValues };
360
+ delete next[columnId];
361
+ ctx.filterRowValues = next;
362
+ }
363
+ });
364
+ }
365
+
366
+ // External pagination is controlled: emit the requested page/size and let
367
+ // the consumer fetch + update props. Never mutate local pagination state.
368
+ function emitExternalPagination(pageIndex: number, pageSize: number): boolean {
369
+ if (ctx.props.externalPagination !== true) return false;
370
+ ctx.props.onPaginationChange?.({ pageIndex: Math.max(0, pageIndex), pageSize });
371
+ ctx.scrollContainer?.scrollTo?.({ top: 0 });
372
+ return true;
373
+ }
374
+
375
+ function changePage(delta: number) {
376
+ if (
377
+ emitExternalPagination(
378
+ (ctx.props.pageIndex ?? 0) + delta,
379
+ ctx.props.pageSize ?? 10,
380
+ )
381
+ )
382
+ return;
383
+ ctx.grid.setPagination((prev: any) => ({
384
+ ...prev,
385
+ pageIndex: Math.max((prev?.pageIndex ?? 0) + delta, 0),
386
+ }));
387
+ ctx.scrollContainer?.scrollTo?.({ top: 0 });
388
+ }
389
+
390
+ function goToPage(pageIndex: number) {
391
+ if (emitExternalPagination(pageIndex, ctx.props.pageSize ?? 10)) return;
392
+ ctx.grid.setPagination((prev: any) => ({
393
+ ...prev,
394
+ pageIndex: Math.max(0, pageIndex),
395
+ }));
396
+ ctx.scrollContainer?.scrollTo?.({ top: 0 });
397
+ }
398
+
399
+ function setPageSize(pageSize: number) {
400
+ // External mode: changing page size resets to the first page.
401
+ if (emitExternalPagination(0, pageSize)) return;
402
+ ctx.grid.setPagination((prev: any) => {
403
+ const oldSize = prev?.pageSize ?? 10;
404
+ const oldIndex = prev?.pageIndex ?? 0;
405
+ // Keep the first visible row in view when the page size changes.
406
+ const firstVisibleRow = oldIndex * oldSize;
407
+ return {
408
+ ...prev,
409
+ pageSize,
410
+ pageIndex: Math.max(0, Math.floor(firstVisibleRow / pageSize)),
411
+ };
412
+ });
413
+ }
414
+
415
+ // ---- Right-click context menu --------------------------------------
416
+ type CtxTarget = { rowIndex: number; colIndex: number; columnId: string; rowId: string; row: TData | null };
417
+ type ResolvedItem = { key: string; label: string; separator?: boolean; disabled?: boolean; run?: () => void };
418
+
419
+ function insertRowAt(t: CtxTarget, offset: number) {
420
+ const data = ctx.internalData.slice() as TData[];
421
+ const idx = t.row ? data.indexOf(t.row) : -1;
422
+ const at = idx < 0 ? data.length : idx + offset;
423
+ data.splice(at, 0, {} as TData);
424
+ ctx.internalData = data;
425
+ }
426
+ function removeRowAt(t: CtxTarget) {
427
+ if (!t.row) return;
428
+ ctx.internalData = (ctx.internalData as TData[]).filter((r) => r !== t.row);
429
+ }
430
+ function removeColAt(t: CtxTarget) {
431
+ ctx.internalColumns = ctx.internalColumns.filter(
432
+ (d: any) => (d.id ?? d.field) !== t.columnId,
433
+ );
434
+ }
435
+
436
+ // Built-in items, keyed. `run` reads the live target passed at click time.
437
+ // `msg` names the GridMessages key so the label localizes (falls back to the
438
+ // English `label` when no `localeText` is set).
439
+ const builtins: Record<string, { label: string; msg?: string; run: (t: CtxTarget) => void }> = {
440
+ copy: { label: "Copy", msg: "menuCopy", run: () => ctx.copySelectionToClipboard() },
441
+ cut: { label: "Cut", msg: "menuCut", run: () => ctx.cutSelectionToClipboard() },
442
+ paste: { label: "Paste", msg: "menuPaste", run: () => void ctx.pasteFromClipboard() },
443
+ clear: { label: "Clear", msg: "menuClear", run: () => ctx.clearSelectedCells() },
444
+ row_above: { label: "Insert row above", msg: "menuInsertRowAbove", run: (t) => insertRowAt(t, 0) },
445
+ row_below: { label: "Insert row below", msg: "menuInsertRowBelow", run: (t) => insertRowAt(t, 1) },
446
+ remove_row: { label: "Remove row", msg: "menuRemoveRow", run: removeRowAt },
447
+ remove_col: { label: "Remove column", msg: "menuRemoveColumn", run: removeColAt },
448
+ comment: { label: "Edit comment", msg: "menuEditComment", run: (t) => openCommentEditor(t) },
449
+ // Charting: open the chart panel scoped to the current cell selection. Gated
450
+ // on `chartingEnabled` in the resolver below.
451
+ chart: { label: "Chart selected range", msg: "chartRange", run: () => { ctx.chartPanelOpen = true; } },
452
+ };
453
+ const DEFAULT_ITEMS = [
454
+ "copy", "cut", "paste", "clear", "separator",
455
+ "row_above", "row_below", "remove_row", "separator", "remove_col",
456
+ ];
457
+
458
+ function openContextMenu(
459
+ event: MouseEvent,
460
+ rowIndex: number,
461
+ colIndex: number,
462
+ columnId: string,
463
+ ) {
464
+ if (!ctx.props.contextMenu) return;
465
+ event.preventDefault();
466
+ closeMenus();
467
+ ctx.contextMenuFor = null;
468
+ const rowModel = ctx.allRows[rowIndex];
469
+ const row = rowModel?.original ?? null;
470
+ const rowId = rowModel?.id ?? String(rowIndex);
471
+ // Reserve ~280px for the menu body. If the click is in the bottom third,
472
+ // flip the menu above the cursor so it stays fully in the viewport.
473
+ const menuHeight = 280;
474
+ const y = event.clientY + menuHeight > window.innerHeight
475
+ ? Math.max(8, event.clientY - menuHeight)
476
+ : event.clientY;
477
+ ctx.contextMenuPos = {
478
+ x: clampMenuX(event.clientX, 220),
479
+ y,
480
+ };
481
+ ctx.contextMenuFor = { rowIndex, colIndex, columnId, rowId, row };
482
+ }
483
+
484
+ function closeContextMenu() {
485
+ ctx.contextMenuFor = null;
486
+ }
487
+
488
+ // ---- Editable comments ---------------------------------------------
489
+ function openCommentEditor(t: CtxTarget) {
490
+ const cur =
491
+ ctx.noteOverrides?.[t.rowId]?.[t.columnId] ??
492
+ ctx.props.notes?.[t.rowId]?.[t.columnId] ??
493
+ "";
494
+ ctx.commentDraft = cur;
495
+ ctx.commentEditFor = { rowId: t.rowId, columnId: t.columnId, x: ctx.contextMenuPos.x, y: ctx.contextMenuPos.y };
496
+ ctx.contextMenuFor = null;
497
+ }
498
+ function writeNote(rowId: string, columnId: string, note: string) {
499
+ const next = { ...ctx.noteOverrides };
500
+ next[rowId] = { ...(next[rowId] ?? {}), [columnId]: note };
501
+ ctx.noteOverrides = next;
502
+ ctx.props.onNoteChange?.({ rowId, columnId, note });
503
+ }
504
+ function saveComment() {
505
+ const e = ctx.commentEditFor;
506
+ if (!e) return;
507
+ writeNote(e.rowId, e.columnId, ctx.commentDraft);
508
+ ctx.commentEditFor = null;
509
+ }
510
+ function removeComment() {
511
+ const e = ctx.commentEditFor;
512
+ if (!e) return;
513
+ writeNote(e.rowId, e.columnId, "");
514
+ ctx.commentEditFor = null;
515
+ }
516
+ function closeCommentEditor() {
517
+ ctx.commentEditFor = null;
518
+ }
519
+
520
+ /** Resolve the configured items against the current target for rendering. */
521
+ function contextMenuItems(): ResolvedItem[] {
522
+ const target = ctx.contextMenuFor as CtxTarget | null;
523
+ if (!target) return [];
524
+ const cfg = ctx.props.contextMenu;
525
+ const messages = ctx.messages;
526
+ const entries: any[] = Array.isArray(cfg)
527
+ ? cfg
528
+ : [
529
+ ...DEFAULT_ITEMS,
530
+ ...(ctx.chartingEnabled ? ["separator", "chart"] : []),
531
+ ...(ctx.props.editableComments ? ["separator", "comment"] : []),
532
+ ];
533
+ const out: ResolvedItem[] = [];
534
+ for (const entry of entries) {
535
+ if (entry === "separator") { out.push({ key: "sep" + out.length, label: "", separator: true }); continue; }
536
+ if (typeof entry === "string") {
537
+ // The chart item only makes sense when charting is enabled.
538
+ if (entry === "chart" && !ctx.chartingEnabled) continue;
539
+ const b = builtins[entry];
540
+ if (!b) continue;
541
+ const label = (b.msg && messages?.[b.msg]) || b.label;
542
+ out.push({ key: entry, label, run: () => b.run(target) });
543
+ continue;
544
+ }
545
+ if (entry.hidden?.(target)) continue;
546
+ out.push({
547
+ key: entry.key,
548
+ label: entry.label,
549
+ disabled: entry.disabled?.(target) ?? false,
550
+ run: () => entry.action(target),
551
+ });
552
+ }
553
+ // Drop leading/trailing/double separators.
554
+ return out.filter((it, i, a) =>
555
+ !it.separator || (i > 0 && i < a.length - 1 && !a[i - 1]?.separator),
556
+ );
557
+ }
558
+
559
+ return {
560
+ updateFilterRow,
561
+ updateFilterOperator,
562
+ updateFilterMenuValue,
563
+ updateFilterMenuValueTo,
564
+ addFilterToken,
565
+ removeFilterToken,
566
+ toggleFilterToken,
567
+ toggleCheckboxWithKeyboard,
568
+ isColumnFiltered,
569
+ closeMenus,
570
+ openInSuggest,
571
+ closeInSuggest,
572
+ openChooseColumns,
573
+ openColumnMenu,
574
+ openFilterMenu,
575
+ openOperatorMenu,
576
+ sortColumnFromMenu,
577
+ clearColumnSort,
578
+ groupByColumnFromMenu,
579
+ clearGroupingFromMenu,
580
+ isFacetChecked,
581
+ toggleFacetValue,
582
+ setFacetSelection,
583
+ setFilterTokens,
584
+ isAllFacetsChecked,
585
+ toggleAllFacets,
586
+ clearColumnFilter,
587
+ changePage,
588
+ goToPage,
589
+ setPageSize,
590
+ openContextMenu,
591
+ closeContextMenu,
592
+ contextMenuItems,
593
+ saveComment,
594
+ removeComment,
595
+ closeCommentEditor,
596
+ };
597
+ }