@ng-desktop/platform 0.5.0 → 0.6.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.
@@ -0,0 +1,301 @@
1
+ import { NgTemplateOutlet } from '@angular/common';
2
+ import { Component, computed, contentChild, effect, input, output, signal, TemplateRef, } from '@angular/core';
3
+ import { NativeContainerDirective, NativeElementDirective, NativeTextWidgetDirective, } from './native-directives.js';
4
+ import { computeVirtualGridSliceRange, FenwickOffsetTree, } from './virtual-layout.js';
5
+ import * as i0 from "@angular/core";
6
+ /**
7
+ * 2D virtualized native grid rendering only visible rows + overscan inside `<ng-scroll>` + `<ng-grid>`.
8
+ * Supports responsive column sizing from viewport width as well as variable row heights via `FenwickOffsetTree`.
9
+ */
10
+ export class NativeVirtualGridComponent {
11
+ items = input([], /* @ts-ignore */
12
+ ...(ngDevMode ? [{ debugName: "items" }] : /* istanbul ignore next */ []));
13
+ rowHeight = input(200, /* @ts-ignore */
14
+ ...(ngDevMode ? [{ debugName: "rowHeight" }] : /* istanbul ignore next */ []));
15
+ minColumnWidth = input(200, /* @ts-ignore */
16
+ ...(ngDevMode ? [{ debugName: "minColumnWidth" }] : /* istanbul ignore next */ []));
17
+ columns = input(null, /* @ts-ignore */
18
+ ...(ngDevMode ? [{ debugName: "columns" }] : /* istanbul ignore next */ []));
19
+ viewportHeight = input(480, /* @ts-ignore */
20
+ ...(ngDevMode ? [{ debugName: "viewportHeight" }] : /* istanbul ignore next */ []));
21
+ viewportWidth = input(960, /* @ts-ignore */
22
+ ...(ngDevMode ? [{ debugName: "viewportWidth" }] : /* istanbul ignore next */ []));
23
+ rowGap = input(12, /* @ts-ignore */
24
+ ...(ngDevMode ? [{ debugName: "rowGap" }] : /* istanbul ignore next */ []));
25
+ columnGap = input(12, /* @ts-ignore */
26
+ ...(ngDevMode ? [{ debugName: "columnGap" }] : /* istanbul ignore next */ []));
27
+ overscanRows = input(2, /* @ts-ignore */
28
+ ...(ngDevMode ? [{ debugName: "overscanRows" }] : /* istanbul ignore next */ []));
29
+ gridClass = input('', /* @ts-ignore */
30
+ ...(ngDevMode ? [{ debugName: "gridClass" }] : /* istanbul ignore next */ []));
31
+ gridId = input('', /* @ts-ignore */
32
+ ...(ngDevMode ? [{ debugName: "gridId" }] : /* istanbul ignore next */ []));
33
+ rowHeightResolver = input(null, /* @ts-ignore */
34
+ ...(ngDevMode ? [{ debugName: "rowHeightResolver" }] : /* istanbul ignore next */ []));
35
+ trackBy = input((index, _item) => index, /* @ts-ignore */
36
+ ...(ngDevMode ? [{ debugName: "trackBy" }] : /* istanbul ignore next */ []));
37
+ itemTemplateInput = input(null, { ...(ngDevMode ? { debugName: "itemTemplateInput" } : /* istanbul ignore next */ {}), alias: 'itemTemplate' });
38
+ projectedTemplate = contentChild(TemplateRef, /* @ts-ignore */
39
+ ...(ngDevMode ? [{ debugName: "projectedTemplate" }] : /* istanbul ignore next */ []));
40
+ rangeChange = output();
41
+ scrollBottom = output();
42
+ scrollTop = signal(0, /* @ts-ignore */
43
+ ...(ngDevMode ? [{ debugName: "scrollTop" }] : /* istanbul ignore next */ []));
44
+ measuredWidth = signal(null, /* @ts-ignore */
45
+ ...(ngDevMode ? [{ debugName: "measuredWidth" }] : /* istanbul ignore next */ []));
46
+ measuredHeight = signal(null, /* @ts-ignore */
47
+ ...(ngDevMode ? [{ debugName: "measuredHeight" }] : /* istanbul ignore next */ []));
48
+ scrollToAttr = signal(undefined, /* @ts-ignore */
49
+ ...(ngDevMode ? [{ debugName: "scrollToAttr" }] : /* istanbul ignore next */ []));
50
+ rowTree = new FenwickOffsetTree(0, 200);
51
+ rowOverrides = new Map();
52
+ measureRevision = signal(0, /* @ts-ignore */
53
+ ...(ngDevMode ? [{ debugName: "measureRevision" }] : /* istanbul ignore next */ []));
54
+ resolvedTemplate = computed(() => this.itemTemplateInput() ?? this.projectedTemplate() ?? null, /* @ts-ignore */
55
+ ...(ngDevMode ? [{ debugName: "resolvedTemplate" }] : /* istanbul ignore next */ []));
56
+ effectiveWidth = computed(() => this.measuredWidth() ?? this.viewportWidth(), /* @ts-ignore */
57
+ ...(ngDevMode ? [{ debugName: "effectiveWidth" }] : /* istanbul ignore next */ []));
58
+ effectiveHeight = computed(() => this.measuredHeight() ?? this.viewportHeight(), /* @ts-ignore */
59
+ ...(ngDevMode ? [{ debugName: "effectiveHeight" }] : /* istanbul ignore next */ []));
60
+ range = computed(() => {
61
+ const source = this.items();
62
+ const resolver = this.rowHeightResolver();
63
+ this.measureRevision();
64
+ const baseRowHeight = Math.max(1, this.rowHeight());
65
+ const effWidth = this.effectiveWidth();
66
+ const effHeight = this.effectiveHeight();
67
+ const baseSlice = computeVirtualGridSliceRange({
68
+ totalItems: source.length,
69
+ scrollTop: this.scrollTop(),
70
+ viewportHeight: effHeight,
71
+ viewportWidth: effWidth,
72
+ rowHeight: baseRowHeight,
73
+ minColumnWidth: this.minColumnWidth(),
74
+ columns: this.columns(),
75
+ rowGap: this.rowGap(),
76
+ columnGap: this.columnGap(),
77
+ overscanRows: this.overscanRows(),
78
+ });
79
+ if (resolver === null && this.rowOverrides.size === 0) {
80
+ return baseSlice;
81
+ }
82
+ const totalRows = baseSlice.totalRows;
83
+ const overrides = new Map();
84
+ if (resolver !== null) {
85
+ for (let r = 0; r < totalRows; r += 1) {
86
+ overrides.set(r, resolver(r));
87
+ }
88
+ }
89
+ for (const [r, h] of this.rowOverrides) {
90
+ if (r >= 0 && r < totalRows) {
91
+ overrides.set(r, h);
92
+ }
93
+ }
94
+ this.rowTree.reset(totalRows, baseRowHeight, overrides);
95
+ return computeVirtualGridSliceRange({
96
+ totalItems: source.length,
97
+ scrollTop: this.scrollTop(),
98
+ viewportHeight: effHeight,
99
+ viewportWidth: effWidth,
100
+ rowHeight: baseRowHeight,
101
+ minColumnWidth: this.minColumnWidth(),
102
+ columns: baseSlice.columns,
103
+ rowGap: this.rowGap(),
104
+ columnGap: this.columnGap(),
105
+ overscanRows: this.overscanRows(),
106
+ rowHeights: this.rowTree,
107
+ });
108
+ }, /* @ts-ignore */
109
+ ...(ngDevMode ? [{ debugName: "range" }] : /* istanbul ignore next */ []));
110
+ visibleEntries = computed(() => {
111
+ const source = this.items();
112
+ const { startIndex, endIndex, columns } = this.range();
113
+ const trackFn = this.trackBy();
114
+ const entries = [];
115
+ for (let idx = startIndex; idx < endIndex; idx += 1) {
116
+ const item = source[idx];
117
+ const localIdx = idx - startIndex;
118
+ const row = Math.floor(localIdx / columns);
119
+ const col = localIdx % columns;
120
+ entries.push({ item, index: idx, row, col, key: trackFn(idx, item) });
121
+ }
122
+ return entries;
123
+ }, /* @ts-ignore */
124
+ ...(ngDevMode ? [{ debugName: "visibleEntries" }] : /* istanbul ignore next */ []));
125
+ lastEmittedRangeKey = '';
126
+ constructor() {
127
+ effect(() => {
128
+ const next = this.range();
129
+ const key = `${next.startIndex}:${next.endIndex}:${next.columns}:${next.totalItems}:${next.topSpacerHeight}:${next.bottomSpacerHeight}`;
130
+ if (key !== this.lastEmittedRangeKey) {
131
+ this.lastEmittedRangeKey = key;
132
+ this.rangeChange.emit(next);
133
+ }
134
+ });
135
+ }
136
+ /** Overrides the height of grid row `rowIndex` in O(log R). */
137
+ measureRowHeight(rowIndex, height) {
138
+ const row = Math.floor(rowIndex);
139
+ if (!Number.isFinite(rowIndex) || row < 0 || !Number.isFinite(height)) {
140
+ return;
141
+ }
142
+ const clamped = Math.max(1, height);
143
+ if (this.rowOverrides.get(row) !== clamped) {
144
+ this.rowOverrides.set(row, clamped);
145
+ this.measureRevision.update((rev) => rev + 1);
146
+ }
147
+ }
148
+ /** Clears all dynamically measured row height overrides. */
149
+ resetMeasuredRows() {
150
+ if (this.rowOverrides.size === 0) {
151
+ return;
152
+ }
153
+ this.rowOverrides.clear();
154
+ this.measureRevision.update((rev) => rev + 1);
155
+ }
156
+ /** Scrolls the native grid viewport to bring item `index` into view. */
157
+ scrollToIndex(index) {
158
+ const safeTotal = this.items().length;
159
+ if (safeTotal === 0) {
160
+ return;
161
+ }
162
+ const clampedIdx = Math.max(0, Math.min(safeTotal - 1, Math.floor(index)));
163
+ const slice = this.range();
164
+ const targetRow = Math.floor(clampedIdx / Math.max(1, slice.columns));
165
+ const rowGap = Math.max(0, this.rowGap());
166
+ const y = this.rowHeightResolver() !== null || this.rowOverrides.size > 0
167
+ ? Math.round(this.rowTree.offsetOf(targetRow) + targetRow * rowGap)
168
+ : Math.round(targetRow * (Math.max(1, this.rowHeight()) + rowGap));
169
+ this.scrollTop.set(y);
170
+ this.scrollToAttr.set(`0,${y}`);
171
+ }
172
+ onNativeScroll(event) {
173
+ if (!event || typeof event !== 'object') {
174
+ return;
175
+ }
176
+ const obj = event;
177
+ const target = obj['target'] && typeof obj['target'] === 'object'
178
+ ? obj['target']
179
+ : undefined;
180
+ const top = typeof obj['scrollTop'] === 'number' ? obj['scrollTop'] : target?.['scrollTop'];
181
+ if (typeof top === 'number' && Number.isFinite(top)) {
182
+ this.scrollTop.set(Math.max(0, top));
183
+ }
184
+ const width = typeof obj['clientWidth'] === 'number' ? obj['clientWidth'] : target?.['clientWidth'];
185
+ if (typeof width === 'number' && Number.isFinite(width) && width > 0) {
186
+ this.measuredWidth.set(width);
187
+ }
188
+ const height = typeof obj['clientHeight'] === 'number' ? obj['clientHeight'] : target?.['clientHeight'];
189
+ if (typeof height === 'number' && Number.isFinite(height) && height > 0) {
190
+ this.measuredHeight.set(height);
191
+ }
192
+ }
193
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.0", ngImport: i0, type: NativeVirtualGridComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
194
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.3.0", type: NativeVirtualGridComponent, isStandalone: true, selector: "ng-virtual-grid", inputs: { items: { classPropertyName: "items", publicName: "items", isSignal: true, isRequired: false, transformFunction: null }, rowHeight: { classPropertyName: "rowHeight", publicName: "rowHeight", isSignal: true, isRequired: false, transformFunction: null }, minColumnWidth: { classPropertyName: "minColumnWidth", publicName: "minColumnWidth", isSignal: true, isRequired: false, transformFunction: null }, columns: { classPropertyName: "columns", publicName: "columns", isSignal: true, isRequired: false, transformFunction: null }, viewportHeight: { classPropertyName: "viewportHeight", publicName: "viewportHeight", isSignal: true, isRequired: false, transformFunction: null }, viewportWidth: { classPropertyName: "viewportWidth", publicName: "viewportWidth", isSignal: true, isRequired: false, transformFunction: null }, rowGap: { classPropertyName: "rowGap", publicName: "rowGap", isSignal: true, isRequired: false, transformFunction: null }, columnGap: { classPropertyName: "columnGap", publicName: "columnGap", isSignal: true, isRequired: false, transformFunction: null }, overscanRows: { classPropertyName: "overscanRows", publicName: "overscanRows", isSignal: true, isRequired: false, transformFunction: null }, gridClass: { classPropertyName: "gridClass", publicName: "gridClass", isSignal: true, isRequired: false, transformFunction: null }, gridId: { classPropertyName: "gridId", publicName: "gridId", isSignal: true, isRequired: false, transformFunction: null }, rowHeightResolver: { classPropertyName: "rowHeightResolver", publicName: "rowHeightResolver", isSignal: true, isRequired: false, transformFunction: null }, trackBy: { classPropertyName: "trackBy", publicName: "trackBy", isSignal: true, isRequired: false, transformFunction: null }, itemTemplateInput: { classPropertyName: "itemTemplateInput", publicName: "itemTemplate", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { rangeChange: "rangeChange", scrollBottom: "scrollBottom" }, queries: [{ propertyName: "projectedTemplate", first: true, predicate: TemplateRef, descendants: true, isSignal: true }], ngImport: i0, template: `
195
+ <ng-scroll
196
+ [hexpand]="true"
197
+ [vexpand]="true"
198
+ [min-height]="viewportHeight()"
199
+ [scroll-to]="scrollToAttr()"
200
+ (scroll)="onNativeScroll($event)"
201
+ (scrollBottom)="scrollBottom.emit()"
202
+ >
203
+ <ng-box orientation="vertical" [spacing]="0" [hexpand]="true">
204
+ @if (range().topSpacerHeight > 0) {
205
+ <ng-box [height]="range().topSpacerHeight"></ng-box>
206
+ }
207
+ @if (items().length === 0) {
208
+ <ng-content select="[ngVirtualEmpty]"></ng-content>
209
+ } @else {
210
+ <ng-grid
211
+ [attr.id]="gridId() || null"
212
+ [class]="gridClass()"
213
+ [columns]="range().columns"
214
+ [row-spacing]="rowGap()"
215
+ [column-spacing]="columnGap()"
216
+ [hexpand]="true"
217
+ >
218
+ @for (entry of visibleEntries(); track entry.key) {
219
+ @if (resolvedTemplate(); as tpl) {
220
+ <ng-container
221
+ [ngTemplateOutlet]="tpl"
222
+ [ngTemplateOutletContext]="{
223
+ $implicit: entry.item,
224
+ index: entry.index,
225
+ row: entry.row,
226
+ col: entry.col,
227
+ }"
228
+ ></ng-container>
229
+ } @else {
230
+ <ng-label [text]="'' + entry.item"></ng-label>
231
+ }
232
+ }
233
+ </ng-grid>
234
+ }
235
+ @if (range().bottomSpacerHeight > 0) {
236
+ <ng-box [height]="range().bottomSpacerHeight"></ng-box>
237
+ }
238
+ </ng-box>
239
+ </ng-scroll>
240
+ `, isInline: true, dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: NativeElementDirective, selector: "ng-window,adw-window,ng-dialog,gtk-dialog,adw-dialog,ng-header-bar,adw-header-bar,gtk-header-bar,ng-scroll,gtk-scrolled-window,ng-box,gtk-box,ng-grid,gtk-grid,ng-separator,gtk-separator,ng-button,gtk-button,ng-label,gtk-label,ng-entry,gtk-entry,ng-switch,gtk-switch,ng-checkbox,ng-check,ng-check-button,gtk-check-button,ng-slider,ng-scale,gtk-scale,ng-select,ng-dropdown,gtk-dropdown,gtk-drop-down,ng-image,adw-image,gtk-picture,ng-picture,ng-level-bar,gtk-level-bar,ng-flow,ng-flow-box,gtk-flow-box,ng-spinner,gtk-spinner,ng-progress-bar,gtk-progress-bar,ng-paned,gtk-paned,ng-stack,gtk-stack,adw-navigation-view,ng-expander,gtk-expander,ng-text-view,gtk-text-view,ng-password-entry,gtk-password-entry,ng-spin-button,gtk-spin-button,ng-search-entry,gtk-search-entry,ng-clamp,adw-clamp,ng-list,ng-table,gtk-list-box,gtk-list-view,ng-popover,gtk-popover,ng-menu,adw-entry-row,adw-password-entry-row,adw-switch-row,adw-combo-row,adw-spin-row", inputs: ["hexpand", "vexpand", "halign", "valign", "width", "height", "min-width", "min-height", "margin", "margin-top", "margin-bottom", "margin-start", "margin-end", "visible", "sensitive", "disabled", "tooltip", "event-mask", "drop-target", "col", "row"] }, { kind: "directive", type: NativeContainerDirective, selector: "ng-box,gtk-box,ng-grid,gtk-grid,ng-flow,ng-flow-box,gtk-flow-box,ng-paned,gtk-paned,ng-scroll,gtk-scrolled-window,ng-stack,gtk-stack,adw-navigation-view,ng-expander,gtk-expander,ng-clamp,adw-clamp,ng-separator,gtk-separator", inputs: ["orientation", "spacing", "gap", "row-spacing", "column-spacing", "min-children", "max-children", "min-per-row", "max-per-row", "columns", "homogeneous", "position", "visible-child", "active-page", "transition", "transition-type", "label", "title", "expanded", "open", "max-size", "maximum-size", "scroll-to", "scroll-anchor"] }, { kind: "directive", type: NativeTextWidgetDirective, selector: "ng-button,gtk-button,ng-label,gtk-label", inputs: ["label", "text", "wrap", "selectable", "ellipsize"] }] });
241
+ }
242
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.0", ngImport: i0, type: NativeVirtualGridComponent, decorators: [{
243
+ type: Component,
244
+ args: [{
245
+ selector: 'ng-virtual-grid',
246
+ standalone: true,
247
+ imports: [
248
+ NgTemplateOutlet,
249
+ NativeElementDirective,
250
+ NativeContainerDirective,
251
+ NativeTextWidgetDirective,
252
+ ],
253
+ template: `
254
+ <ng-scroll
255
+ [hexpand]="true"
256
+ [vexpand]="true"
257
+ [min-height]="viewportHeight()"
258
+ [scroll-to]="scrollToAttr()"
259
+ (scroll)="onNativeScroll($event)"
260
+ (scrollBottom)="scrollBottom.emit()"
261
+ >
262
+ <ng-box orientation="vertical" [spacing]="0" [hexpand]="true">
263
+ @if (range().topSpacerHeight > 0) {
264
+ <ng-box [height]="range().topSpacerHeight"></ng-box>
265
+ }
266
+ @if (items().length === 0) {
267
+ <ng-content select="[ngVirtualEmpty]"></ng-content>
268
+ } @else {
269
+ <ng-grid
270
+ [attr.id]="gridId() || null"
271
+ [class]="gridClass()"
272
+ [columns]="range().columns"
273
+ [row-spacing]="rowGap()"
274
+ [column-spacing]="columnGap()"
275
+ [hexpand]="true"
276
+ >
277
+ @for (entry of visibleEntries(); track entry.key) {
278
+ @if (resolvedTemplate(); as tpl) {
279
+ <ng-container
280
+ [ngTemplateOutlet]="tpl"
281
+ [ngTemplateOutletContext]="{
282
+ $implicit: entry.item,
283
+ index: entry.index,
284
+ row: entry.row,
285
+ col: entry.col,
286
+ }"
287
+ ></ng-container>
288
+ } @else {
289
+ <ng-label [text]="'' + entry.item"></ng-label>
290
+ }
291
+ }
292
+ </ng-grid>
293
+ }
294
+ @if (range().bottomSpacerHeight > 0) {
295
+ <ng-box [height]="range().bottomSpacerHeight"></ng-box>
296
+ }
297
+ </ng-box>
298
+ </ng-scroll>
299
+ `,
300
+ }]
301
+ }], ctorParameters: () => [], propDecorators: { items: [{ type: i0.Input, args: [{ isSignal: true, alias: "items", required: false }] }], rowHeight: [{ type: i0.Input, args: [{ isSignal: true, alias: "rowHeight", required: false }] }], minColumnWidth: [{ type: i0.Input, args: [{ isSignal: true, alias: "minColumnWidth", required: false }] }], columns: [{ type: i0.Input, args: [{ isSignal: true, alias: "columns", required: false }] }], viewportHeight: [{ type: i0.Input, args: [{ isSignal: true, alias: "viewportHeight", required: false }] }], viewportWidth: [{ type: i0.Input, args: [{ isSignal: true, alias: "viewportWidth", required: false }] }], rowGap: [{ type: i0.Input, args: [{ isSignal: true, alias: "rowGap", required: false }] }], columnGap: [{ type: i0.Input, args: [{ isSignal: true, alias: "columnGap", required: false }] }], overscanRows: [{ type: i0.Input, args: [{ isSignal: true, alias: "overscanRows", required: false }] }], gridClass: [{ type: i0.Input, args: [{ isSignal: true, alias: "gridClass", required: false }] }], gridId: [{ type: i0.Input, args: [{ isSignal: true, alias: "gridId", required: false }] }], rowHeightResolver: [{ type: i0.Input, args: [{ isSignal: true, alias: "rowHeightResolver", required: false }] }], trackBy: [{ type: i0.Input, args: [{ isSignal: true, alias: "trackBy", required: false }] }], itemTemplateInput: [{ type: i0.Input, args: [{ isSignal: true, alias: "itemTemplate", required: false }] }], projectedTemplate: [{ type: i0.ContentChild, args: [i0.forwardRef(() => TemplateRef), { isSignal: true }] }], rangeChange: [{ type: i0.Output, args: ["rangeChange"] }], scrollBottom: [{ type: i0.Output, args: ["scrollBottom"] }] } });
@@ -0,0 +1,71 @@
1
+ /** Inclusive start / exclusive end index range and spacer heights for a virtualized viewport. */
2
+ export interface VirtualSliceRange {
3
+ startIndex: number;
4
+ endIndex: number;
5
+ totalItems: number;
6
+ topSpacerHeight: number;
7
+ bottomSpacerHeight: number;
8
+ }
9
+ /** Input parameters for 2D responsive virtualized grid slicing. */
10
+ export interface VirtualGridSliceOptions {
11
+ totalItems: number;
12
+ scrollTop: number;
13
+ viewportHeight: number;
14
+ viewportWidth: number;
15
+ rowHeight: number;
16
+ minColumnWidth?: number;
17
+ columns?: number | null;
18
+ rowGap?: number;
19
+ columnGap?: number;
20
+ overscanRows?: number;
21
+ rowHeights?: FenwickOffsetTree | null;
22
+ }
23
+ /** Inclusive start / exclusive end slice metadata for a 2D virtualized grid viewport. */
24
+ export interface VirtualGridSliceRange {
25
+ startIndex: number;
26
+ endIndex: number;
27
+ startRow: number;
28
+ endRow: number;
29
+ totalRows: number;
30
+ columns: number;
31
+ totalItems: number;
32
+ topSpacerHeight: number;
33
+ bottomSpacerHeight: number;
34
+ }
35
+ /**
36
+ * Binary Indexed Tree (Fenwick tree) supporting O(log n) updates, prefix sums, and offset lookups
37
+ * for variable-height virtualized lists and grids.
38
+ */
39
+ export declare class FenwickOffsetTree {
40
+ private sizes;
41
+ private tree;
42
+ private itemCount;
43
+ private fallbackSize;
44
+ private cachedTotal;
45
+ constructor(count?: number, defaultSize?: number);
46
+ get length(): number;
47
+ /** Rebuilds the tree in O(n) time from `count`, `defaultSize`, and optional per-index overrides. */
48
+ reset(count: number, defaultSize: number, overrides?: ReadonlyMap<number, number>): void;
49
+ /** Returns the height/size recorded for `index` in O(1). */
50
+ sizeOf(index: number): number;
51
+ /** Updates the height/size at `index` in O(log n); returns `true` when it changed. */
52
+ setSize(index: number, size: number): boolean;
53
+ /** Returns the exclusive prefix sum covering `[0, index)` in O(log n). */
54
+ offsetOf(index: number): number;
55
+ /** Returns total size of all items in O(1). */
56
+ totalSize(): number;
57
+ /** Returns the first 0-based index covering `offset` via O(log n) binary lifting. */
58
+ indexAtOffset(offset: number): number;
59
+ }
60
+ /**
61
+ * Computes the visible item slice and spacer heights for a fixed-row-height viewport.
62
+ */
63
+ export declare function computeVirtualSliceRange(totalItems: number, scrollTop: number, viewportHeight: number, itemHeight: number, overscan?: number): VirtualSliceRange;
64
+ /**
65
+ * Computes the visible item slice and exact top/bottom spacer heights from a variable-height `FenwickOffsetTree`.
66
+ */
67
+ export declare function computeVariableVirtualSliceRange(tree: FenwickOffsetTree, scrollTop: number, viewportHeight: number, overscan?: number): VirtualSliceRange;
68
+ /**
69
+ * Computes the visible 2D item range, row span, responsive column count, and spacer heights for `<ng-virtual-grid>`.
70
+ */
71
+ export declare function computeVirtualGridSliceRange(options: VirtualGridSliceOptions): VirtualGridSliceRange;
@@ -0,0 +1,279 @@
1
+ function sanitizePositiveSize(value, fallback) {
2
+ if (!Number.isFinite(value)) {
3
+ return fallback;
4
+ }
5
+ return Math.max(1, value);
6
+ }
7
+ /**
8
+ * Binary Indexed Tree (Fenwick tree) supporting O(log n) updates, prefix sums, and offset lookups
9
+ * for variable-height virtualized lists and grids.
10
+ */
11
+ export class FenwickOffsetTree {
12
+ sizes;
13
+ tree;
14
+ itemCount;
15
+ fallbackSize;
16
+ cachedTotal;
17
+ constructor(count = 0, defaultSize = 48) {
18
+ this.sizes = new Float64Array(0);
19
+ this.tree = new Float64Array(1);
20
+ this.itemCount = 0;
21
+ this.fallbackSize = sanitizePositiveSize(defaultSize, 48);
22
+ this.cachedTotal = 0;
23
+ this.reset(count, this.fallbackSize);
24
+ }
25
+ get length() {
26
+ return this.itemCount;
27
+ }
28
+ /** Rebuilds the tree in O(n) time from `count`, `defaultSize`, and optional per-index overrides. */
29
+ reset(count, defaultSize, overrides) {
30
+ const safeCount = Math.max(0, Math.floor(Number.isFinite(count) ? count : 0));
31
+ const safeDefault = sanitizePositiveSize(defaultSize, 48);
32
+ this.itemCount = safeCount;
33
+ this.fallbackSize = safeDefault;
34
+ this.sizes = new Float64Array(safeCount);
35
+ this.tree = new Float64Array(safeCount + 1);
36
+ let total = 0;
37
+ for (let idx = 0; idx < safeCount; idx += 1) {
38
+ const override = overrides?.get(idx);
39
+ const size = override !== undefined ? sanitizePositiveSize(override, safeDefault) : safeDefault;
40
+ this.sizes[idx] = size;
41
+ total += size;
42
+ this.tree[idx + 1] = size;
43
+ }
44
+ this.cachedTotal = total;
45
+ for (let pos = 1; pos <= safeCount; pos += 1) {
46
+ const parent = pos + (pos & -pos);
47
+ if (parent <= safeCount) {
48
+ this.tree[parent] = (this.tree[parent] ?? 0) + (this.tree[pos] ?? 0);
49
+ }
50
+ }
51
+ }
52
+ /** Returns the height/size recorded for `index` in O(1). */
53
+ sizeOf(index) {
54
+ const idx = Math.floor(index);
55
+ if (idx < 0 || idx >= this.itemCount) {
56
+ return this.fallbackSize;
57
+ }
58
+ return this.sizes[idx] ?? this.fallbackSize;
59
+ }
60
+ /** Updates the height/size at `index` in O(log n); returns `true` when it changed. */
61
+ setSize(index, size) {
62
+ const idx = Math.floor(index);
63
+ if (!Number.isFinite(index) || idx < 0 || idx >= this.itemCount) {
64
+ return false;
65
+ }
66
+ const next = sanitizePositiveSize(size, this.fallbackSize);
67
+ const prev = this.sizes[idx] ?? this.fallbackSize;
68
+ if (Math.abs(next - prev) < 1e-6) {
69
+ return false;
70
+ }
71
+ const delta = next - prev;
72
+ this.sizes[idx] = next;
73
+ this.cachedTotal += delta;
74
+ for (let pos = idx + 1; pos <= this.itemCount; pos += pos & -pos) {
75
+ this.tree[pos] = (this.tree[pos] ?? 0) + delta;
76
+ }
77
+ return true;
78
+ }
79
+ /** Returns the exclusive prefix sum covering `[0, index)` in O(log n). */
80
+ offsetOf(index) {
81
+ if (this.itemCount === 0 || !Number.isFinite(index)) {
82
+ return 0;
83
+ }
84
+ const clamped = Math.max(0, Math.min(this.itemCount, Math.floor(index)));
85
+ let sum = 0;
86
+ for (let pos = clamped; pos > 0; pos -= pos & -pos) {
87
+ sum += this.tree[pos] ?? 0;
88
+ }
89
+ return sum;
90
+ }
91
+ /** Returns total size of all items in O(1). */
92
+ totalSize() {
93
+ return this.cachedTotal;
94
+ }
95
+ /** Returns the first 0-based index covering `offset` via O(log n) binary lifting. */
96
+ indexAtOffset(offset) {
97
+ if (this.itemCount === 0 || !Number.isFinite(offset) || offset <= 0) {
98
+ return 0;
99
+ }
100
+ if (offset >= this.cachedTotal) {
101
+ return this.itemCount - 1;
102
+ }
103
+ let maxBit = 1;
104
+ while (maxBit << 1 <= this.itemCount) {
105
+ maxBit <<= 1;
106
+ }
107
+ let pos = 0;
108
+ let acc = 0;
109
+ for (let bit = maxBit; bit > 0; bit >>= 1) {
110
+ const next = pos + bit;
111
+ if (next <= this.itemCount) {
112
+ const candidate = acc + (this.tree[next] ?? 0);
113
+ if (candidate <= offset) {
114
+ pos = next;
115
+ acc = candidate;
116
+ }
117
+ }
118
+ }
119
+ return Math.min(this.itemCount - 1, pos);
120
+ }
121
+ }
122
+ /**
123
+ * Computes the visible item slice and spacer heights for a fixed-row-height viewport.
124
+ */
125
+ export function computeVirtualSliceRange(totalItems, scrollTop, viewportHeight, itemHeight, overscan = 4) {
126
+ const safeTotal = Math.max(0, Math.floor(totalItems));
127
+ if (safeTotal === 0) {
128
+ return {
129
+ startIndex: 0,
130
+ endIndex: 0,
131
+ totalItems: 0,
132
+ topSpacerHeight: 0,
133
+ bottomSpacerHeight: 0,
134
+ };
135
+ }
136
+ const safeItemHeight = sanitizePositiveSize(itemHeight, 1);
137
+ const safeViewport = Math.max(safeItemHeight, Number.isFinite(viewportHeight) ? viewportHeight : safeItemHeight);
138
+ const safeOverscan = Math.max(0, Math.floor(overscan));
139
+ const maxScrollTop = Math.max(0, safeTotal * safeItemHeight - safeViewport);
140
+ const clampedScrollTop = Math.min(Math.max(0, Number.isFinite(scrollTop) ? scrollTop : 0), maxScrollTop);
141
+ const firstVisible = Math.floor(clampedScrollTop / safeItemHeight);
142
+ const visibleCount = Math.ceil(safeViewport / safeItemHeight);
143
+ const startIndex = Math.max(0, firstVisible - safeOverscan);
144
+ const endIndex = Math.min(safeTotal, firstVisible + visibleCount + safeOverscan);
145
+ return {
146
+ startIndex,
147
+ endIndex,
148
+ totalItems: safeTotal,
149
+ topSpacerHeight: startIndex * safeItemHeight,
150
+ bottomSpacerHeight: (safeTotal - endIndex) * safeItemHeight,
151
+ };
152
+ }
153
+ /**
154
+ * Computes the visible item slice and exact top/bottom spacer heights from a variable-height `FenwickOffsetTree`.
155
+ */
156
+ export function computeVariableVirtualSliceRange(tree, scrollTop, viewportHeight, overscan = 4) {
157
+ const safeTotal = tree.length;
158
+ if (safeTotal === 0) {
159
+ return {
160
+ startIndex: 0,
161
+ endIndex: 0,
162
+ totalItems: 0,
163
+ topSpacerHeight: 0,
164
+ bottomSpacerHeight: 0,
165
+ };
166
+ }
167
+ const totalHeight = tree.totalSize();
168
+ const safeViewport = sanitizePositiveSize(viewportHeight, 1);
169
+ const safeOverscan = Math.max(0, Math.floor(overscan));
170
+ const maxScrollTop = Math.max(0, totalHeight - safeViewport);
171
+ const clampedScrollTop = Math.min(Math.max(0, Number.isFinite(scrollTop) ? scrollTop : 0), maxScrollTop);
172
+ const firstVisible = tree.indexAtOffset(clampedScrollTop);
173
+ const viewportBottom = clampedScrollTop + safeViewport;
174
+ const endVisible = viewportBottom >= totalHeight
175
+ ? safeTotal
176
+ : Math.min(safeTotal, tree.indexAtOffset(Math.max(clampedScrollTop, viewportBottom - 1e-6)) + 1);
177
+ const startIndex = Math.max(0, firstVisible - safeOverscan);
178
+ const endIndex = Math.min(safeTotal, endVisible + safeOverscan);
179
+ return {
180
+ startIndex,
181
+ endIndex,
182
+ totalItems: safeTotal,
183
+ topSpacerHeight: Math.round(tree.offsetOf(startIndex)),
184
+ bottomSpacerHeight: Math.round(Math.max(0, totalHeight - tree.offsetOf(endIndex))),
185
+ };
186
+ }
187
+ function findRowAtVariableOffset(rowHeights, totalRows, rowGap, offset) {
188
+ if (offset <= 0) {
189
+ return 0;
190
+ }
191
+ let lo = 0;
192
+ let hi = totalRows - 1;
193
+ while (lo < hi) {
194
+ const mid = (lo + hi + 1) >> 1;
195
+ const rowStart = rowHeights.offsetOf(mid) + mid * rowGap;
196
+ if (rowStart <= offset) {
197
+ lo = mid;
198
+ }
199
+ else {
200
+ hi = mid - 1;
201
+ }
202
+ }
203
+ return lo;
204
+ }
205
+ /**
206
+ * Computes the visible 2D item range, row span, responsive column count, and spacer heights for `<ng-virtual-grid>`.
207
+ */
208
+ export function computeVirtualGridSliceRange(options) {
209
+ const safeTotal = Math.max(0, Math.floor(Number.isFinite(options.totalItems) ? options.totalItems : 0));
210
+ const safeColGap = Math.max(0, Number.isFinite(options.columnGap) ? options.columnGap : 0);
211
+ const safeRowGap = Math.max(0, Number.isFinite(options.rowGap) ? options.rowGap : 0);
212
+ const safeWidth = sanitizePositiveSize(options.viewportWidth, 320);
213
+ const safeMinColWidth = sanitizePositiveSize(options.minColumnWidth ?? 200, 200);
214
+ const safeRowHeight = sanitizePositiveSize(options.rowHeight, 48);
215
+ const safeViewportHeight = sanitizePositiveSize(options.viewportHeight, safeRowHeight);
216
+ const safeOverscan = Math.max(0, Math.floor(options.overscanRows ?? 2));
217
+ const columns = typeof options.columns === 'number' && Number.isFinite(options.columns) && options.columns >= 1
218
+ ? Math.floor(options.columns)
219
+ : Math.max(1, Math.floor((safeWidth + safeColGap) / (safeMinColWidth + safeColGap)));
220
+ if (safeTotal === 0) {
221
+ return {
222
+ startIndex: 0,
223
+ endIndex: 0,
224
+ startRow: 0,
225
+ endRow: 0,
226
+ totalRows: 0,
227
+ columns,
228
+ totalItems: 0,
229
+ topSpacerHeight: 0,
230
+ bottomSpacerHeight: 0,
231
+ };
232
+ }
233
+ const totalRows = Math.ceil(safeTotal / columns);
234
+ const rowHeights = options.rowHeights && options.rowHeights.length === totalRows ? options.rowHeights : null;
235
+ const gapTotal = Math.max(0, totalRows - 1) * safeRowGap;
236
+ const totalHeight = rowHeights
237
+ ? rowHeights.totalSize() + gapTotal
238
+ : totalRows * safeRowHeight + gapTotal;
239
+ const maxScrollTop = Math.max(0, totalHeight - safeViewportHeight);
240
+ const clampedScrollTop = Math.min(Math.max(0, Number.isFinite(options.scrollTop) ? options.scrollTop : 0), maxScrollTop);
241
+ let firstVisibleRow;
242
+ let endVisibleRow;
243
+ if (rowHeights) {
244
+ firstVisibleRow = findRowAtVariableOffset(rowHeights, totalRows, safeRowGap, clampedScrollTop);
245
+ const viewportBottom = clampedScrollTop + safeViewportHeight;
246
+ endVisibleRow =
247
+ viewportBottom >= totalHeight
248
+ ? totalRows
249
+ : Math.min(totalRows, findRowAtVariableOffset(rowHeights, totalRows, safeRowGap, Math.max(clampedScrollTop, viewportBottom - 1e-6)) + 1);
250
+ }
251
+ else {
252
+ const stride = safeRowHeight + safeRowGap;
253
+ firstVisibleRow = Math.min(totalRows - 1, Math.floor(clampedScrollTop / stride));
254
+ const lastOffset = Math.max(clampedScrollTop, clampedScrollTop + safeViewportHeight - 1e-6);
255
+ endVisibleRow = Math.min(totalRows, Math.floor(lastOffset / stride) + 1);
256
+ }
257
+ const startRow = Math.max(0, firstVisibleRow - safeOverscan);
258
+ const endRow = Math.min(totalRows, endVisibleRow + safeOverscan);
259
+ const startIndex = startRow * columns;
260
+ const endIndex = Math.min(safeTotal, endRow * columns);
261
+ const topSpacerHeight = rowHeights
262
+ ? Math.round(rowHeights.offsetOf(startRow) + startRow * safeRowGap)
263
+ : Math.round(startRow * (safeRowHeight + safeRowGap));
264
+ const renderedEndOffset = rowHeights
265
+ ? rowHeights.offsetOf(endRow) + Math.max(0, endRow - 1) * safeRowGap
266
+ : endRow * safeRowHeight + Math.max(0, endRow - 1) * safeRowGap;
267
+ const bottomSpacerHeight = Math.round(Math.max(0, totalHeight - renderedEndOffset));
268
+ return {
269
+ startIndex,
270
+ endIndex,
271
+ startRow,
272
+ endRow,
273
+ totalRows,
274
+ columns,
275
+ totalItems: safeTotal,
276
+ topSpacerHeight,
277
+ bottomSpacerHeight,
278
+ };
279
+ }