@universal-material/data-grid 0.0.0-stage → 0.2.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 (237) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +172 -2
  3. package/custom-elements.json +4652 -0
  4. package/dist/attribute-converters.d.ts +53 -0
  5. package/dist/attribute-converters.js +98 -0
  6. package/dist/attribute-converters.js.map +1 -0
  7. package/dist/change-source.d.ts +3 -0
  8. package/dist/change-source.js +2 -0
  9. package/dist/change-source.js.map +1 -0
  10. package/dist/data/data-source.d.ts +164 -0
  11. package/dist/data/data-source.js +75 -0
  12. package/dist/data/data-source.js.map +1 -0
  13. package/dist/data/local-source.d.ts +20 -0
  14. package/dist/data/local-source.js +94 -0
  15. package/dist/data/local-source.js.map +1 -0
  16. package/dist/data/remote-loader.d.ts +190 -0
  17. package/dist/data/remote-loader.js +608 -0
  18. package/dist/data/remote-loader.js.map +1 -0
  19. package/dist/export/csv.d.ts +28 -0
  20. package/dist/export/csv.js +61 -0
  21. package/dist/export/csv.js.map +1 -0
  22. package/dist/export/download.d.ts +7 -0
  23. package/dist/export/download.js +17 -0
  24. package/dist/export/download.js.map +1 -0
  25. package/dist/export/rows.d.ts +75 -0
  26. package/dist/export/rows.js +134 -0
  27. package/dist/export/rows.js.map +1 -0
  28. package/dist/export/slice.d.ts +15 -0
  29. package/dist/export/slice.js +35 -0
  30. package/dist/export/slice.js.map +1 -0
  31. package/dist/export/types.d.ts +155 -0
  32. package/dist/export/types.js +2 -0
  33. package/dist/export/types.js.map +1 -0
  34. package/dist/export/write.d.ts +38 -0
  35. package/dist/export/write.js +178 -0
  36. package/dist/export/write.js.map +1 -0
  37. package/dist/export-pdf/client.d.ts +75 -0
  38. package/dist/export-pdf/client.js +155 -0
  39. package/dist/export-pdf/client.js.map +1 -0
  40. package/dist/export-pdf/engine.d.ts +73 -0
  41. package/dist/export-pdf/engine.js +131 -0
  42. package/dist/export-pdf/engine.js.map +1 -0
  43. package/dist/export-pdf/grid-pdf.wasm +0 -0
  44. package/dist/export-pdf/index.d.ts +27 -0
  45. package/dist/export-pdf/index.js +566 -0
  46. package/dist/export-pdf/index.js.map +1 -0
  47. package/dist/export-pdf/serialize.d.ts +23 -0
  48. package/dist/export-pdf/serialize.js +127 -0
  49. package/dist/export-pdf/serialize.js.map +1 -0
  50. package/dist/export-pdf/types.d.ts +150 -0
  51. package/dist/export-pdf/types.js +2 -0
  52. package/dist/export-pdf/types.js.map +1 -0
  53. package/dist/export-pdf/worker.d.ts +2 -0
  54. package/dist/export-pdf/worker.js +81 -0
  55. package/dist/export-pdf/worker.js.map +1 -0
  56. package/dist/export-xlsx/formats.d.ts +14 -0
  57. package/dist/export-xlsx/formats.js +40 -0
  58. package/dist/export-xlsx/formats.js.map +1 -0
  59. package/dist/export-xlsx/index.d.ts +74 -0
  60. package/dist/export-xlsx/index.js +137 -0
  61. package/dist/export-xlsx/index.js.map +1 -0
  62. package/dist/features/adaptive.d.ts +25 -0
  63. package/dist/features/adaptive.js +47 -0
  64. package/dist/features/adaptive.js.map +1 -0
  65. package/dist/features/cell-content-focus.d.ts +29 -0
  66. package/dist/features/cell-content-focus.js +95 -0
  67. package/dist/features/cell-content-focus.js.map +1 -0
  68. package/dist/features/cell-range.d.ts +97 -0
  69. package/dist/features/cell-range.js +75 -0
  70. package/dist/features/cell-range.js.map +1 -0
  71. package/dist/features/clipboard.d.ts +28 -0
  72. package/dist/features/clipboard.js +48 -0
  73. package/dist/features/clipboard.js.map +1 -0
  74. package/dist/features/column-chooser.d.ts +91 -0
  75. package/dist/features/column-chooser.js +451 -0
  76. package/dist/features/column-chooser.js.map +1 -0
  77. package/dist/features/column-dnd.d.ts +10 -0
  78. package/dist/features/column-dnd.js +126 -0
  79. package/dist/features/column-dnd.js.map +1 -0
  80. package/dist/features/context-menu.d.ts +122 -0
  81. package/dist/features/context-menu.js +153 -0
  82. package/dist/features/context-menu.js.map +1 -0
  83. package/dist/features/drag-helper.d.ts +39 -0
  84. package/dist/features/drag-helper.js +108 -0
  85. package/dist/features/drag-helper.js.map +1 -0
  86. package/dist/features/drop-position.d.ts +15 -0
  87. package/dist/features/drop-position.js +24 -0
  88. package/dist/features/drop-position.js.map +1 -0
  89. package/dist/features/editing.d.ts +111 -0
  90. package/dist/features/editing.js +244 -0
  91. package/dist/features/editing.js.map +1 -0
  92. package/dist/features/filter-row.d.ts +80 -0
  93. package/dist/features/filter-row.js +538 -0
  94. package/dist/features/filter-row.js.map +1 -0
  95. package/dist/features/filtering.d.ts +55 -0
  96. package/dist/features/filtering.js +209 -0
  97. package/dist/features/filtering.js.map +1 -0
  98. package/dist/features/group-dnd.d.ts +11 -0
  99. package/dist/features/group-dnd.js +119 -0
  100. package/dist/features/group-dnd.js.map +1 -0
  101. package/dist/features/header-filter.d.ts +100 -0
  102. package/dist/features/header-filter.js +588 -0
  103. package/dist/features/header-filter.js.map +1 -0
  104. package/dist/features/host-label.d.ts +36 -0
  105. package/dist/features/host-label.js +184 -0
  106. package/dist/features/host-label.js.map +1 -0
  107. package/dist/features/keyboard.d.ts +59 -0
  108. package/dist/features/keyboard.js +127 -0
  109. package/dist/features/keyboard.js.map +1 -0
  110. package/dist/features/menu.d.ts +113 -0
  111. package/dist/features/menu.js +491 -0
  112. package/dist/features/menu.js.map +1 -0
  113. package/dist/features/resize.d.ts +82 -0
  114. package/dist/features/resize.js +214 -0
  115. package/dist/features/resize.js.map +1 -0
  116. package/dist/features/scroll-port.d.ts +51 -0
  117. package/dist/features/scroll-port.js +329 -0
  118. package/dist/features/scroll-port.js.map +1 -0
  119. package/dist/features/search.d.ts +45 -0
  120. package/dist/features/search.js +123 -0
  121. package/dist/features/search.js.map +1 -0
  122. package/dist/features/selection.d.ts +37 -0
  123. package/dist/features/selection.js +103 -0
  124. package/dist/features/selection.js.map +1 -0
  125. package/dist/features/sorting.d.ts +39 -0
  126. package/dist/features/sorting.js +53 -0
  127. package/dist/features/sorting.js.map +1 -0
  128. package/dist/features/toolbar.d.ts +125 -0
  129. package/dist/features/toolbar.js +135 -0
  130. package/dist/features/toolbar.js.map +1 -0
  131. package/dist/features/tree.d.ts +53 -0
  132. package/dist/features/tree.js +94 -0
  133. package/dist/features/tree.js.map +1 -0
  134. package/dist/features/validation.d.ts +48 -0
  135. package/dist/features/validation.js +172 -0
  136. package/dist/features/validation.js.map +1 -0
  137. package/dist/grid-column-def.d.ts +521 -0
  138. package/dist/grid-column-def.js +32 -0
  139. package/dist/grid-column-def.js.map +1 -0
  140. package/dist/grid-column.d.ts +173 -0
  141. package/dist/grid-column.js +382 -0
  142. package/dist/grid-column.js.map +1 -0
  143. package/dist/grid-i18n.d.ts +469 -0
  144. package/dist/grid-i18n.js +3222 -0
  145. package/dist/grid-i18n.js.map +1 -0
  146. package/dist/grid.d.ts +1510 -0
  147. package/dist/grid.js +15972 -0
  148. package/dist/grid.js.map +1 -0
  149. package/dist/grid.min.js +3966 -0
  150. package/dist/grid.styles.d.ts +2 -0
  151. package/dist/grid.styles.js +2849 -0
  152. package/dist/grid.styles.js.map +1 -0
  153. package/dist/render/cell.d.ts +16 -0
  154. package/dist/render/cell.js +78 -0
  155. package/dist/render/cell.js.map +1 -0
  156. package/dist/render/column-window.d.ts +18 -0
  157. package/dist/render/column-window.js +50 -0
  158. package/dist/render/column-window.js.map +1 -0
  159. package/dist/render/content.d.ts +45 -0
  160. package/dist/render/content.js +140 -0
  161. package/dist/render/content.js.map +1 -0
  162. package/dist/render/editors.d.ts +28 -0
  163. package/dist/render/editors.js +41 -0
  164. package/dist/render/editors.js.map +1 -0
  165. package/dist/render/footer.d.ts +61 -0
  166. package/dist/render/footer.js +61 -0
  167. package/dist/render/footer.js.map +1 -0
  168. package/dist/render/group-row.d.ts +35 -0
  169. package/dist/render/group-row.js +60 -0
  170. package/dist/render/group-row.js.map +1 -0
  171. package/dist/render/header.d.ts +108 -0
  172. package/dist/render/header.js +124 -0
  173. package/dist/render/header.js.map +1 -0
  174. package/dist/render/keyed-rows.d.ts +25 -0
  175. package/dist/render/keyed-rows.js +98 -0
  176. package/dist/render/keyed-rows.js.map +1 -0
  177. package/dist/render/virtualizer.d.ts +158 -0
  178. package/dist/render/virtualizer.js +721 -0
  179. package/dist/render/virtualizer.js.map +1 -0
  180. package/dist/row-model/added-rows.d.ts +32 -0
  181. package/dist/row-model/added-rows.js +95 -0
  182. package/dist/row-model/added-rows.js.map +1 -0
  183. package/dist/row-model/cell-range.d.ts +71 -0
  184. package/dist/row-model/cell-range.js +187 -0
  185. package/dist/row-model/cell-range.js.map +1 -0
  186. package/dist/row-model/columns.d.ts +102 -0
  187. package/dist/row-model/columns.js +222 -0
  188. package/dist/row-model/columns.js.map +1 -0
  189. package/dist/row-model/compare.d.ts +40 -0
  190. package/dist/row-model/compare.js +590 -0
  191. package/dist/row-model/compare.js.map +1 -0
  192. package/dist/row-model/distinct.d.ts +39 -0
  193. package/dist/row-model/distinct.js +105 -0
  194. package/dist/row-model/distinct.js.map +1 -0
  195. package/dist/row-model/filter.d.ts +80 -0
  196. package/dist/row-model/filter.js +582 -0
  197. package/dist/row-model/filter.js.map +1 -0
  198. package/dist/row-model/flatten.d.ts +129 -0
  199. package/dist/row-model/flatten.js +511 -0
  200. package/dist/row-model/flatten.js.map +1 -0
  201. package/dist/row-model/fold.d.ts +25 -0
  202. package/dist/row-model/fold.js +104 -0
  203. package/dist/row-model/fold.js.map +1 -0
  204. package/dist/row-model/format.d.ts +58 -0
  205. package/dist/row-model/format.js +239 -0
  206. package/dist/row-model/format.js.map +1 -0
  207. package/dist/row-model/group.d.ts +30 -0
  208. package/dist/row-model/group.js +173 -0
  209. package/dist/row-model/group.js.map +1 -0
  210. package/dist/row-model/lru.d.ts +19 -0
  211. package/dist/row-model/lru.js +48 -0
  212. package/dist/row-model/lru.js.map +1 -0
  213. package/dist/row-model/sort.d.ts +30 -0
  214. package/dist/row-model/sort.js +107 -0
  215. package/dist/row-model/sort.js.map +1 -0
  216. package/dist/row-model/summary.d.ts +64 -0
  217. package/dist/row-model/summary.js +213 -0
  218. package/dist/row-model/summary.js.map +1 -0
  219. package/dist/row-model/tree.d.ts +116 -0
  220. package/dist/row-model/tree.js +478 -0
  221. package/dist/row-model/tree.js.map +1 -0
  222. package/dist/row-model/types.d.ts +109 -0
  223. package/dist/row-model/types.js +5 -0
  224. package/dist/row-model/types.js.map +1 -0
  225. package/dist/row-model/value.d.ts +20 -0
  226. package/dist/row-model/value.js +57 -0
  227. package/dist/row-model/value.js.map +1 -0
  228. package/dist/state/grid-state.d.ts +147 -0
  229. package/dist/state/grid-state.js +295 -0
  230. package/dist/state/grid-state.js.map +1 -0
  231. package/dist/state/storage.d.ts +33 -0
  232. package/dist/state/storage.js +62 -0
  233. package/dist/state/storage.js.map +1 -0
  234. package/dist/state/versioned.d.ts +18 -0
  235. package/dist/state/versioned.js +56 -0
  236. package/dist/state/versioned.js.map +1 -0
  237. package/package.json +145 -6
@@ -0,0 +1,608 @@
1
+ import { RowList } from '../row-model/flatten.js';
2
+ import { groupKeyOf } from '../row-model/group.js';
3
+ import { LruCache } from '../row-model/lru.js';
4
+ import { MISSING } from '../row-model/types.js';
5
+ import { deepFreeze, } from './data-source.js';
6
+ export { MISSING };
7
+ /** Limite de blocos guardados por geração (o resto volta do servidor se reaparecer). */
8
+ export const BLOCK_CACHE_LIMIT = 200;
9
+ /** Pedido em voo ou falho: chave do nó + bloco. */
10
+ const NODE_SEP = '\u0000';
11
+ const pathKey = (path) => path ? JSON.stringify(path.map(serialize)) : '';
12
+ const serialize = (value) => value instanceof Date ? { date: value.getTime() } : value === undefined ? null : value;
13
+ /** Linhas de dados de um nó (a raiz sem grupos, ou um grupo do último nível). */
14
+ class DataNode {
15
+ #generation;
16
+ constructor(generation, id, path, count) {
17
+ this.lazy = true;
18
+ /** Fim da última linha carregada (infinito: a lista cresce a partir dele). */
19
+ this.loadedEnd = 0;
20
+ this.#generation = generation;
21
+ this.id = id;
22
+ this.path = path;
23
+ this.count = count;
24
+ }
25
+ get length() {
26
+ if (this.count !== undefined) {
27
+ return this.count;
28
+ }
29
+ // Infinito: o que já veio + um bloco de esqueleto (pede o próximo).
30
+ return this.loadedEnd + this.#generation.blockSizeFor(this);
31
+ }
32
+ /** Item na posição; marca o bloco como desejado quando falta. */
33
+ get(position) {
34
+ const item = this.peek(position);
35
+ if (item === MISSING) {
36
+ this.#generation.want(this, this.#generation.blockOf(position));
37
+ }
38
+ return item;
39
+ }
40
+ find(match) {
41
+ return this.#generation.find(this, match);
42
+ }
43
+ loadedPositions(match) {
44
+ return this.#generation.loadedPositions(this, match);
45
+ }
46
+ /** Item na posição sem pedir nada (`MISSING` se o bloco não está em cache). */
47
+ peek(position) {
48
+ const size = this.#generation.blockSizeFor(this);
49
+ const block = this.#generation.block(this, Math.floor(position / size));
50
+ if (!block) {
51
+ return MISSING;
52
+ }
53
+ const offset = position % size;
54
+ return offset < block.length ? block[offset] : MISSING;
55
+ }
56
+ }
57
+ /** Lista de grupos de um nível (filhos da raiz ou de um grupo). */
58
+ class GroupListNode {
59
+ #generation;
60
+ constructor(generation, id, path, level) {
61
+ this.lazy = true;
62
+ this.#generation = generation;
63
+ this.id = id;
64
+ this.path = path;
65
+ this.level = level;
66
+ }
67
+ /** Antes de carregar: uma linha de esqueleto. */
68
+ get length() {
69
+ return this.groups ? 0 : 1;
70
+ }
71
+ get() {
72
+ this.#generation.want(this, 0);
73
+ return MISSING;
74
+ }
75
+ peek() {
76
+ return MISSING;
77
+ }
78
+ find() {
79
+ return -1;
80
+ }
81
+ loadedPositions() {
82
+ return [];
83
+ }
84
+ }
85
+ /** Uma consulta e tudo o que veio dela. */
86
+ class Generation {
87
+ #host;
88
+ #onSettled;
89
+ #blocks;
90
+ #lists;
91
+ #data;
92
+ #pending;
93
+ #wanted;
94
+ #failed;
95
+ /** O 1º pedido da raiz (o que traz total e resumos) já saiu. */
96
+ #headAsked;
97
+ constructor(query, host, onSettled) {
98
+ this.#blocks = new LruCache(BLOCK_CACHE_LIMIT);
99
+ this.#lists = new Map();
100
+ this.#data = new Map();
101
+ this.#pending = new Map();
102
+ this.#wanted = new Map();
103
+ this.#failed = new Set();
104
+ /** A raiz respondeu (contagem e resumos conhecidos). */
105
+ this.ready = false;
106
+ this.summary = [];
107
+ this.error = undefined;
108
+ this.hasError = false;
109
+ this.disposed = false;
110
+ /** O 1º pedido da raiz (o que traz total e resumos) já saiu. */
111
+ this.#headAsked = false;
112
+ /** Muda a cada resposta aplicada (o grid refaz as linhas). */
113
+ this.version = 0;
114
+ this.query = query;
115
+ this.#host = host;
116
+ this.#onSettled = onSettled;
117
+ this.root = query.group.length
118
+ ? this.#list(null, 0)
119
+ : this.#dataNode(null, undefined);
120
+ }
121
+ get blocksCached() {
122
+ return this.#blocks.size;
123
+ }
124
+ get pendingCount() {
125
+ return this.#pending.size;
126
+ }
127
+ blockSizeFor(node) {
128
+ return Number.isFinite(this.query.blockSize) ? this.query.blockSize : Math.max(node.count ?? node.loadedEnd, 1);
129
+ }
130
+ blockOf(position) {
131
+ return Number.isFinite(this.query.blockSize) ? Math.floor(position / this.query.blockSize) : 0;
132
+ }
133
+ /** Posição do 1º item carregado do nó que satisfaz `match` (só blocos em cache). */
134
+ find(node, match) {
135
+ const prefix = node.id + NODE_SEP;
136
+ const size = this.blockSizeFor(node);
137
+ let best = -1;
138
+ for (const [key, block] of this.#blocks.entries()) {
139
+ if (!key.startsWith(prefix)) {
140
+ continue;
141
+ }
142
+ const start = Number(key.slice(prefix.length)) * (Number.isFinite(this.query.blockSize) ? size : 0);
143
+ const offset = block.findIndex(match);
144
+ if (offset >= 0 && (best < 0 || start + offset < best)) {
145
+ best = start + offset;
146
+ }
147
+ }
148
+ return best;
149
+ }
150
+ /** Posições (crescentes) dos itens carregados do nó que satisfazem `match` (linhas de detalhe). */
151
+ loadedPositions(node, match) {
152
+ const prefix = node.id + NODE_SEP;
153
+ const size = Number.isFinite(this.query.blockSize) ? this.blockSizeFor(node) : 0;
154
+ const positions = [];
155
+ for (const [key, block] of this.#blocks.entries()) {
156
+ if (!key.startsWith(prefix)) {
157
+ continue;
158
+ }
159
+ const start = Number(key.slice(prefix.length)) * size;
160
+ block.forEach((item, offset) => {
161
+ if (match(item)) {
162
+ positions.push(start + offset);
163
+ }
164
+ });
165
+ }
166
+ return positions.sort((a, b) => a - b);
167
+ }
168
+ /** Itens da raiz numa carga única (modo sem paginação nem grupos remotos). */
169
+ rootItems() {
170
+ return this.root instanceof DataNode ? this.block(this.root, 0) : undefined;
171
+ }
172
+ block(node, index) {
173
+ return this.#blocks.get(node.id + NODE_SEP + index);
174
+ }
175
+ #list(path, level) {
176
+ const id = `l${pathKey(path)}`;
177
+ let node = this.#lists.get(id);
178
+ if (!node) {
179
+ node = new GroupListNode(this, id, path, level);
180
+ this.#lists.set(id, node);
181
+ }
182
+ return node;
183
+ }
184
+ #dataNode(path, count) {
185
+ const id = `d${pathKey(path)}`;
186
+ let node = this.#data.get(id);
187
+ if (!node) {
188
+ node = new DataNode(this, id, path, count);
189
+ this.#data.set(id, node);
190
+ }
191
+ return node;
192
+ }
193
+ /** Lista de subgrupos JÁ criada de um grupo (sem criar). */
194
+ loadedList(group) {
195
+ return this.#lists.get(`l${pathKey(group.path)}`);
196
+ }
197
+ /** Filhos de um grupo expandido: o próximo nível ou as linhas. */
198
+ childrenOf(group) {
199
+ return group.path.length < this.query.group.length
200
+ ? this.#list(group.path, group.path.length)
201
+ : this.#dataNode(group.path, group.count);
202
+ }
203
+ want(node, block) {
204
+ const key = node.id + NODE_SEP + block;
205
+ if (!this.disposed && !this.#pending.has(key) && !this.#failed.has(key)) {
206
+ this.#wanted.set(key, { node, block });
207
+ }
208
+ }
209
+ /** Pede os blocos desejados desde o último `flush`. */
210
+ flush() {
211
+ const wanted = [...this.#wanted.values()];
212
+ this.#wanted.clear();
213
+ for (const { node, block } of wanted) {
214
+ this.#load(node, block);
215
+ }
216
+ }
217
+ #options(node, block, head) {
218
+ const { query } = this;
219
+ const isList = node instanceof GroupListNode;
220
+ const paged = !isList && Number.isFinite(query.blockSize);
221
+ const options = {
222
+ sort: query.sort,
223
+ filter: query.filter,
224
+ filterValue: query.filterValue,
225
+ searchText: query.searchText,
226
+ group: query.group,
227
+ groupPath: node.path,
228
+ requireTotalCount: head && !query.infinite,
229
+ requireGroupCount: isList,
230
+ totalSummary: head ? query.totalSummary : [],
231
+ groupSummary: isList ? query.groupSummary : [],
232
+ tree: query.tree ?? null,
233
+ };
234
+ if (paged) {
235
+ options.skip = block * query.blockSize;
236
+ options.take = query.blockSize;
237
+ }
238
+ return deepFreeze(options);
239
+ }
240
+ #load(node, block) {
241
+ const key = node.id + NODE_SEP + block;
242
+ if (this.disposed || this.#pending.has(key)) {
243
+ return;
244
+ }
245
+ const controller = new AbortController();
246
+ const head = node === this.root && !this.#headAsked;
247
+ const options = this.#options(node, block, head);
248
+ if (head) {
249
+ this.#headAsked = true;
250
+ }
251
+ let request;
252
+ try {
253
+ request = this.#host.request(options, controller.signal);
254
+ }
255
+ catch (error) {
256
+ request = Promise.reject(error);
257
+ }
258
+ if (!request) {
259
+ // Cancelada pelo hospedeiro (`u-grid-loading`): fica como está.
260
+ if (head) {
261
+ this.#headAsked = false;
262
+ }
263
+ this.#settle();
264
+ return;
265
+ }
266
+ this.#pending.set(key, controller);
267
+ request.then(result => {
268
+ if (this.#pending.get(key) !== controller || controller.signal.aborted || this.disposed) {
269
+ return;
270
+ }
271
+ this.#pending.delete(key);
272
+ this.#apply(node, block, result, head);
273
+ this.version++;
274
+ this.#settle();
275
+ this.#host.changed();
276
+ }, (error) => {
277
+ if (this.#pending.get(key) !== controller || controller.signal.aborted || this.disposed) {
278
+ return;
279
+ }
280
+ this.#pending.delete(key);
281
+ this.#failed.add(key);
282
+ if (head) {
283
+ this.#headAsked = false;
284
+ }
285
+ this.error = error;
286
+ this.hasError = true;
287
+ this.version++;
288
+ this.#settle();
289
+ this.#host.changed();
290
+ });
291
+ }
292
+ #settle() {
293
+ this.#onSettled(this);
294
+ }
295
+ #apply(node, block, result, head) {
296
+ const data = Array.isArray(result?.data) ? result.data : [];
297
+ if (head) {
298
+ this.ready = true;
299
+ this.summary = result.summary ?? [];
300
+ if (typeof result.totalCount === 'number' && result.totalCount >= 0) {
301
+ this.totalCount = result.totalCount;
302
+ }
303
+ }
304
+ if (node instanceof GroupListNode) {
305
+ const groups = data.map(group => ({
306
+ value: group?.key,
307
+ count: Math.max(0, Number(group?.count) || 0),
308
+ summary: group?.summary ?? [],
309
+ path: [...node.path ?? [], group?.key],
310
+ }));
311
+ node.groups = groups;
312
+ if (node === this.root && this.totalCount === undefined) {
313
+ this.totalCount = groups.reduce((sum, group) => sum + group.count, 0);
314
+ }
315
+ return;
316
+ }
317
+ const size = this.query.blockSize;
318
+ this.#blocks.set(node.id + NODE_SEP + block, data);
319
+ if (!Number.isFinite(size)) {
320
+ node.count = data.length;
321
+ node.loadedEnd = data.length;
322
+ return;
323
+ }
324
+ const end = block * size + data.length;
325
+ node.loadedEnd = Math.max(node.loadedEnd, end);
326
+ if (node === this.root && !this.query.infinite && this.totalCount !== undefined) {
327
+ node.count = this.totalCount;
328
+ }
329
+ // Bloco curto: acabou (infinito, ou servidor sem `totalCount`).
330
+ if (data.length < size && (node.count === undefined || node.count > end)) {
331
+ node.count = end;
332
+ if (node === this.root) {
333
+ this.totalCount = end;
334
+ }
335
+ }
336
+ }
337
+ /** Tenta de novo o que falhou. */
338
+ retry() {
339
+ this.hasError = false;
340
+ this.error = undefined;
341
+ this.#failed.clear();
342
+ }
343
+ /** Itens carregados (seleção, `applyChanges`, `getSelectedItems`). */
344
+ *loadedItems() {
345
+ for (const [, block] of this.#blocks.entries()) {
346
+ yield* block;
347
+ }
348
+ }
349
+ /**
350
+ * Itens carregados na ordem da tela (exportação): grupos carregados na
351
+ * ordem deles e, em cada nó de dados, os blocos em cache por posição. Não
352
+ * pede nada nem mexe na ordem do LRU.
353
+ */
354
+ loadedInOrder() {
355
+ const byNode = new Map();
356
+ for (const [key, block] of this.#blocks.entries()) {
357
+ const separator = key.lastIndexOf(NODE_SEP);
358
+ const id = key.slice(0, separator);
359
+ let blocks = byNode.get(id);
360
+ if (!blocks) {
361
+ blocks = [];
362
+ byNode.set(id, blocks);
363
+ }
364
+ blocks.push([Number(key.slice(separator + 1)), block]);
365
+ }
366
+ const items = [];
367
+ const visit = (node) => {
368
+ if (!node) {
369
+ return;
370
+ }
371
+ if (node instanceof DataNode) {
372
+ for (const [, block] of (byNode.get(node.id) ?? []).sort((a, b) => a[0] - b[0])) {
373
+ for (const item of block) {
374
+ items.push(item);
375
+ }
376
+ }
377
+ return;
378
+ }
379
+ for (const group of node.groups ?? []) {
380
+ visit(group.path.length < this.query.group.length
381
+ ? this.#lists.get(`l${pathKey(group.path)}`)
382
+ : this.#data.get(`d${pathKey(group.path)}`));
383
+ }
384
+ };
385
+ visit(this.root);
386
+ return items;
387
+ }
388
+ /** Troca, nos blocos em cache, os itens cuja chave está em `updates`. */
389
+ updateItems(keyOf, updates) {
390
+ let count = 0;
391
+ for (const [id, block] of this.#blocks.entries()) {
392
+ let copy;
393
+ block.forEach((item, index) => {
394
+ const next = updates.get(keyOf(item));
395
+ if (next !== undefined) {
396
+ copy ??= [...block];
397
+ copy[index] = next;
398
+ count++;
399
+ }
400
+ });
401
+ if (copy) {
402
+ this.#blocks.replace(id, copy);
403
+ }
404
+ }
405
+ if (count) {
406
+ this.version++;
407
+ }
408
+ return count;
409
+ }
410
+ /** Aborta o que está em voo; a geração continua usável (pede de novo se precisar). */
411
+ abortPending() {
412
+ for (const controller of this.#pending.values()) {
413
+ controller.abort();
414
+ }
415
+ this.#pending.clear();
416
+ if (!this.ready) {
417
+ this.#headAsked = false;
418
+ }
419
+ }
420
+ dispose() {
421
+ this.disposed = true;
422
+ this.#wanted.clear();
423
+ this.abortPending();
424
+ }
425
+ }
426
+ /**
427
+ * Gerações da fonte remota: a exibida (`current`) e a que está carregando
428
+ * (`next`). O grid lê sempre `current`.
429
+ */
430
+ export class RemoteLoader {
431
+ #host;
432
+ #current;
433
+ #next;
434
+ /** Promessas de `refresh()` esperando a geração em carga responder. */
435
+ #waiting = [];
436
+ constructor(host) {
437
+ this.#host = host;
438
+ }
439
+ /** Geração exibida (`undefined` antes da 1ª resposta). */
440
+ get current() {
441
+ return this.#current;
442
+ }
443
+ /** Há carga da consulta inteira em andamento (painel de carregamento). */
444
+ get loading() {
445
+ return !!this.#next && !this.#next.hasError;
446
+ }
447
+ /** Pedidos em voo (diagnóstico e testes de vazamento). */
448
+ get pending() {
449
+ return (this.#current?.pendingCount ?? 0) + (this.#next?.pendingCount ?? 0);
450
+ }
451
+ get blocksCached() {
452
+ return (this.#current?.blocksCached ?? 0) + (this.#next?.blocksCached ?? 0);
453
+ }
454
+ /** A geração em carga falhou antes de responder (linha de erro sem dados novos). */
455
+ get error() {
456
+ const generation = this.#next?.hasError ? this.#next : this.#current?.hasError ? this.#current : undefined;
457
+ return generation ? { error: generation.error } : undefined;
458
+ }
459
+ /**
460
+ * Nova consulta: aborta tudo o que está pendente e começa a carregar.
461
+ * `hintRow`: 1ª linha visível (o 1º pedido já traz o bloco dela).
462
+ */
463
+ reset(query, hintRow = 0) {
464
+ this.#next?.dispose();
465
+ // A exibida segue na tela (e rolável) até a nova responder; o que ela
466
+ // tinha em voo é da consulta velha.
467
+ this.#current?.abortPending();
468
+ const generation = new Generation(query, this.#host, settled => this.#settled(settled));
469
+ this.#next = generation;
470
+ const { root } = generation;
471
+ if (root instanceof GroupListNode || !Number.isFinite(query.blockSize) || query.infinite) {
472
+ generation.want(root, 0);
473
+ }
474
+ else {
475
+ // O total vem com o 1º bloco; o da linha visível vem junto se for outro.
476
+ generation.want(root, 0);
477
+ const block = Math.floor(Math.max(0, hintRow) / query.blockSize);
478
+ if (block > 0) {
479
+ generation.want(root, block);
480
+ }
481
+ }
482
+ generation.flush();
483
+ }
484
+ #settled(generation) {
485
+ if (generation !== this.#next) {
486
+ return;
487
+ }
488
+ if (generation.ready) {
489
+ this.#current?.dispose();
490
+ this.#current = generation;
491
+ this.#next = undefined;
492
+ this.#resolveWaiting();
493
+ }
494
+ else if (generation.hasError) {
495
+ // Falhou antes de responder: a anterior segue na tela, com a linha de erro.
496
+ this.#resolveWaiting();
497
+ }
498
+ else if (!generation.pendingCount) {
499
+ // Cancelada pelo hospedeiro: a anterior segue na tela.
500
+ generation.dispose();
501
+ this.#next = undefined;
502
+ this.#resolveWaiting();
503
+ this.#host.changed();
504
+ }
505
+ }
506
+ /** Resolve quando a carga em andamento termina (ou já, sem carga). */
507
+ whenSettled() {
508
+ if (!this.#next || (!this.#next.pendingCount && !this.#next.ready)) {
509
+ return Promise.resolve();
510
+ }
511
+ return new Promise(resolve => this.#waiting.push(resolve));
512
+ }
513
+ #resolveWaiting() {
514
+ const waiting = this.#waiting;
515
+ this.#waiting = [];
516
+ waiting.forEach(resolve => resolve());
517
+ }
518
+ /** Pede o que o último render marcou. */
519
+ flush() {
520
+ this.#current?.flush();
521
+ this.#next?.flush();
522
+ }
523
+ /** "Tentar de novo": refaz os pedidos que falharam. */
524
+ retry() {
525
+ const next = this.#next;
526
+ if (next?.hasError && !next.ready) {
527
+ next.retry();
528
+ next.want(next.root, 0);
529
+ next.flush();
530
+ this.#host.changed();
531
+ return;
532
+ }
533
+ this.#current?.retry();
534
+ this.#host.changed();
535
+ }
536
+ /** Aborta tudo (grid desconectado); `reset` recomeça. */
537
+ dispose() {
538
+ this.#next?.dispose();
539
+ this.#current?.dispose();
540
+ this.#next = undefined;
541
+ this.#current = undefined;
542
+ this.#resolveWaiting();
543
+ }
544
+ }
545
+ /**
546
+ * Linhas da geração: grupos carregados (os expandidos com filhos) e blocos
547
+ * preguiçosos de dados. O(grupos), nunca O(linhas).
548
+ */
549
+ export const flattenRemote = (generation, groupColumns, isExpanded, keyOf, groupSummaries, groupFooters = false, detailsOf) => {
550
+ const rows = new RowList(keyOf);
551
+ const visit = (node) => {
552
+ if (node instanceof DataNode) {
553
+ rows.pushItems(node, detailsOf?.(node));
554
+ return;
555
+ }
556
+ if (!node.groups) {
557
+ rows.pushItems(node);
558
+ return;
559
+ }
560
+ for (const group of node.groups) {
561
+ const level = group.path.length - 1;
562
+ const column = groupColumns[level];
563
+ const key = groupKeyOf(group.path, groupColumns) ?? JSON.stringify(group.path.map(serialize));
564
+ const expanded = isExpanded(key, level);
565
+ const summaries = groupSummaries(group.summary);
566
+ rows.push({
567
+ isGroup: true,
568
+ key: `g:${key}`,
569
+ groupKey: key,
570
+ level,
571
+ column,
572
+ value: group.value,
573
+ expanded,
574
+ itemCount: group.count,
575
+ firstItem: undefined,
576
+ path: group.path,
577
+ summaries,
578
+ });
579
+ if (!expanded) {
580
+ continue;
581
+ }
582
+ visit(generation.childrenOf(group));
583
+ if (groupFooters) {
584
+ rows.push({ isGroup: false, isFooter: true, key: `f:${key}`, groupKey: key, level, path: group.path, summaries });
585
+ }
586
+ }
587
+ };
588
+ visit(generation.root);
589
+ return rows;
590
+ };
591
+ /**
592
+ * Árvore de grupos carregados como `GroupNode` (sem itens): a API de
593
+ * expansão e `getSummaryValues(groupPath)` do grid andam nela.
594
+ */
595
+ export const remoteGroupNodes = (generation, groupColumns, groupSummaries) => {
596
+ const build = (node) => node?.groups?.map(group => ({
597
+ key: groupKeyOf(group.path, groupColumns) ?? JSON.stringify(group.path.map(serialize)),
598
+ level: group.path.length - 1,
599
+ column: groupColumns[group.path.length - 1],
600
+ value: group.value,
601
+ path: group.path,
602
+ items: [],
603
+ children: build(generation.loadedList(group)),
604
+ summaries: groupSummaries(group.summary),
605
+ })) ?? [];
606
+ return generation.root instanceof GroupListNode ? build(generation.root) : [];
607
+ };
608
+ //# sourceMappingURL=remote-loader.js.map