@jupyterlab/ui-components 4.0.0-alpha.27 → 4.0.0-alpha.29

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 (53) hide show
  1. package/lib/components/button.js +2 -13
  2. package/lib/components/button.js.map +1 -1
  3. package/lib/components/htmlselect.js +10 -15
  4. package/lib/components/htmlselect.js.map +1 -1
  5. package/lib/components/iframe.d.ts +1 -1
  6. package/lib/components/iframe.js +1 -1
  7. package/lib/components/index.d.ts +2 -2
  8. package/lib/components/index.js +2 -2
  9. package/lib/components/index.js.map +1 -1
  10. package/lib/components/inputgroup.d.ts +4 -0
  11. package/lib/components/inputgroup.js +2 -13
  12. package/lib/components/inputgroup.js.map +1 -1
  13. package/lib/components/menu.js +1 -1
  14. package/lib/components/menu.js.map +1 -1
  15. package/lib/components/search.d.ts +21 -12
  16. package/lib/components/search.js +1 -1
  17. package/lib/components/search.js.map +1 -1
  18. package/lib/components/sidepanel.js +4 -1
  19. package/lib/components/sidepanel.js.map +1 -1
  20. package/lib/components/toolbar.d.ts +1 -2
  21. package/lib/components/toolbar.js +6 -9
  22. package/lib/components/toolbar.js.map +1 -1
  23. package/lib/components/windowedlist.d.ts +533 -0
  24. package/lib/components/windowedlist.js +957 -0
  25. package/lib/components/windowedlist.js.map +1 -0
  26. package/lib/hoverbox.d.ts +20 -2
  27. package/lib/hoverbox.js +146 -11
  28. package/lib/hoverbox.js.map +1 -1
  29. package/lib/icon/iconimports.d.ts +1 -1
  30. package/lib/icon/iconimports.js +2 -2
  31. package/lib/icon/iconimports.js.map +1 -1
  32. package/lib/icon/labicon.js +25 -31
  33. package/lib/icon/labicon.js.map +1 -1
  34. package/lib/icon/widgets/commandpalettesvg.js +1 -13
  35. package/lib/icon/widgets/commandpalettesvg.js.map +1 -1
  36. package/lib/icon/widgets/menusvg.js +1 -12
  37. package/lib/icon/widgets/menusvg.js.map +1 -1
  38. package/lib/icon/widgets/tabbarsvg.d.ts +7 -15
  39. package/lib/icon/widgets/tabbarsvg.js +12 -6
  40. package/lib/icon/widgets/tabbarsvg.js.map +1 -1
  41. package/lib/style/icon.js +16 -15
  42. package/lib/style/icon.js.map +1 -1
  43. package/package.json +16 -15
  44. package/style/base.css +1 -0
  45. package/style/deprecated.css +5 -5
  46. package/style/icons/filetype/python.svg +3 -5
  47. package/style/icons/{listings/listings-info.svg → info.svg} +4 -4
  48. package/style/iframe.css +1 -1
  49. package/style/sidepanel.css +4 -0
  50. package/style/windowedlist.css +20 -0
  51. package/lib/components/collapse.d.ts +0 -19
  52. package/lib/components/collapse.js +0 -28
  53. package/lib/components/collapse.js.map +0 -1
@@ -0,0 +1,957 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+ import { ArrayExt } from '@lumino/algorithm';
6
+ import { PromiseDelegate } from '@lumino/coreutils';
7
+ import { MessageLoop } from '@lumino/messaging';
8
+ import { Signal } from '@lumino/signaling';
9
+ import { PanelLayout, Widget } from '@lumino/widgets';
10
+ /**
11
+ * Maximal remaining time for idle callback
12
+ *
13
+ * Ref: https://developer.mozilla.org/en-US/docs/Web/API/Background_Tasks_API#getting_the_most_out_of_idle_callbacks
14
+ */
15
+ const MAXIMUM_TIME_REMAINING = 50;
16
+ /*
17
+ * Feature detection
18
+ *
19
+ * Ref: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#improving_scrolling_performance_with_passive_listeners
20
+ */
21
+ let passiveIfSupported = false;
22
+ try {
23
+ // @ts-expect-error unknown signature
24
+ window.addEventListener('test', null, Object.defineProperty({}, 'passive', {
25
+ get: function () {
26
+ passiveIfSupported = { passive: true };
27
+ }
28
+ }));
29
+ }
30
+ catch (err) {
31
+ // pass no-op
32
+ }
33
+ /**
34
+ * Windowed list abstract model.
35
+ */
36
+ export class WindowedListModel {
37
+ /**
38
+ * Constructor
39
+ *
40
+ * @param options Constructor options
41
+ */
42
+ constructor(options = {}) {
43
+ var _a, _b, _c, _d, _e, _f;
44
+ /**
45
+ * Default widget size estimation
46
+ */
47
+ this._estimatedWidgetSize = WindowedList.DEFAULT_WIDGET_SIZE;
48
+ this._stateChanged = new Signal(this);
49
+ this._currentWindow = [-1, -1, -1, -1];
50
+ this._height = 0;
51
+ this._isDisposed = false;
52
+ this._itemsList = null;
53
+ this._lastMeasuredIndex = -1;
54
+ this._overscanCount = 1;
55
+ this._scrollOffset = 0;
56
+ this._widgetCount = 0;
57
+ this._widgetSizers = [];
58
+ this._windowingActive = true;
59
+ this._widgetCount = (_c = (_b = (_a = options.itemsList) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : options.count) !== null && _c !== void 0 ? _c : 0;
60
+ this._overscanCount = (_d = options.overscanCount) !== null && _d !== void 0 ? _d : 1;
61
+ this._windowingActive = (_e = options.windowingActive) !== null && _e !== void 0 ? _e : true;
62
+ this.itemsList = (_f = options.itemsList) !== null && _f !== void 0 ? _f : null;
63
+ }
64
+ /**
65
+ * List widget height
66
+ */
67
+ get height() {
68
+ return this._height;
69
+ }
70
+ set height(h) {
71
+ this._height = h;
72
+ }
73
+ /**
74
+ * Test whether the model is disposed.
75
+ */
76
+ get isDisposed() {
77
+ return this._isDisposed;
78
+ }
79
+ /**
80
+ * Items list to be rendered
81
+ */
82
+ get itemsList() {
83
+ return this._itemsList;
84
+ }
85
+ set itemsList(v) {
86
+ var _a, _b, _c;
87
+ if (this._itemsList !== v) {
88
+ if (this._itemsList) {
89
+ this._itemsList.changed.disconnect(this.onListChanged, this);
90
+ }
91
+ const oldValue = this._itemsList;
92
+ this._itemsList = v;
93
+ if (this._itemsList) {
94
+ this._itemsList.changed.connect(this.onListChanged, this);
95
+ }
96
+ else {
97
+ this._widgetCount = 0;
98
+ }
99
+ this._stateChanged.emit({
100
+ name: 'list',
101
+ newValue: this._itemsList,
102
+ oldValue
103
+ });
104
+ this._stateChanged.emit({
105
+ name: 'count',
106
+ newValue: (_b = (_a = this._itemsList) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0,
107
+ oldValue: (_c = oldValue === null || oldValue === void 0 ? void 0 : oldValue.length) !== null && _c !== void 0 ? _c : 0
108
+ });
109
+ }
110
+ }
111
+ /**
112
+ * Number of widgets to render in addition to those
113
+ * visible in the viewport.
114
+ */
115
+ get overscanCount() {
116
+ return this._overscanCount;
117
+ }
118
+ set overscanCount(newValue) {
119
+ if (newValue >= 1) {
120
+ if (this._overscanCount !== newValue) {
121
+ const oldValue = this._overscanCount;
122
+ this._overscanCount = newValue;
123
+ this._stateChanged.emit({ name: 'overscanCount', newValue, oldValue });
124
+ }
125
+ }
126
+ else {
127
+ console.error(`Forbidden non-positive overscan count: got ${newValue}`);
128
+ }
129
+ }
130
+ /**
131
+ * Viewport scroll offset.
132
+ */
133
+ get scrollOffset() {
134
+ return this._scrollOffset;
135
+ }
136
+ set scrollOffset(offset) {
137
+ this._scrollOffset = offset;
138
+ }
139
+ /**
140
+ * Total number of widgets in the list
141
+ */
142
+ get widgetCount() {
143
+ return this._itemsList ? this._itemsList.length : this._widgetCount;
144
+ }
145
+ set widgetCount(newValue) {
146
+ if (this.itemsList) {
147
+ console.error('It is not allow to change the widgets count of a windowed list if a items list is used.');
148
+ return;
149
+ }
150
+ if (newValue >= 0) {
151
+ if (this._widgetCount !== newValue) {
152
+ const oldValue = this._widgetCount;
153
+ this._widgetCount = newValue;
154
+ this._stateChanged.emit({ name: 'count', newValue, oldValue });
155
+ }
156
+ }
157
+ else {
158
+ console.error(`Forbidden negative widget count: got ${newValue}`);
159
+ }
160
+ }
161
+ /**
162
+ * Whether windowing is active or not.
163
+ *
164
+ * This is true by default.
165
+ */
166
+ get windowingActive() {
167
+ return this._windowingActive;
168
+ }
169
+ set windowingActive(newValue) {
170
+ if (newValue !== this._windowingActive) {
171
+ const oldValue = this._windowingActive;
172
+ this._windowingActive = newValue;
173
+ this._currentWindow = [-1, -1, -1, -1];
174
+ this._lastMeasuredIndex = -1;
175
+ this._widgetSizers = [];
176
+ this._stateChanged.emit({ name: 'windowingActive', newValue, oldValue });
177
+ }
178
+ }
179
+ /**
180
+ * A signal emitted when any model state changes.
181
+ */
182
+ get stateChanged() {
183
+ return this._stateChanged;
184
+ }
185
+ /**
186
+ * Dispose the model.
187
+ */
188
+ dispose() {
189
+ if (this.isDisposed) {
190
+ return;
191
+ }
192
+ this._isDisposed = true;
193
+ Signal.clearData(this);
194
+ }
195
+ /**
196
+ * Get the total list size.
197
+ *
198
+ * @returns Total estimated size
199
+ */
200
+ getEstimatedTotalSize() {
201
+ let totalSizeOfMeasuredItems = 0;
202
+ if (this._lastMeasuredIndex >= this.widgetCount) {
203
+ this._lastMeasuredIndex = this.widgetCount - 1;
204
+ }
205
+ if (this._lastMeasuredIndex >= 0) {
206
+ const itemMetadata = this._widgetSizers[this._lastMeasuredIndex];
207
+ totalSizeOfMeasuredItems = itemMetadata.offset + itemMetadata.size;
208
+ }
209
+ // We can do better than this
210
+ const numUnmeasuredItems = this.widgetCount - this._lastMeasuredIndex - 1;
211
+ const totalSizeOfUnmeasuredItems = numUnmeasuredItems * this._estimatedWidgetSize;
212
+ return totalSizeOfMeasuredItems + totalSizeOfUnmeasuredItems;
213
+ }
214
+ /**
215
+ * Get the scroll offset to display an item in the viewport.
216
+ *
217
+ * @param index Item index
218
+ * @param align Where to align the item in the viewport
219
+ * @returns The needed scroll offset
220
+ */
221
+ getOffsetForIndexAndAlignment(index, align = 'auto') {
222
+ const size = this._height;
223
+ const itemMetadata = this._getItemMetadata(index);
224
+ // Get estimated total size after ItemMetadata is computed,
225
+ // To ensure it reflects actual measurements instead of just estimates.
226
+ const estimatedTotalSize = this.getEstimatedTotalSize();
227
+ const maxOffset = Math.max(0, Math.min(estimatedTotalSize - size, itemMetadata.offset));
228
+ const minOffset = Math.max(0, itemMetadata.offset - size + itemMetadata.size);
229
+ if (align === 'smart') {
230
+ if (this._scrollOffset >= minOffset - size &&
231
+ this._scrollOffset <= maxOffset + size) {
232
+ align = 'auto';
233
+ }
234
+ else {
235
+ align = 'center';
236
+ }
237
+ }
238
+ switch (align) {
239
+ case 'start':
240
+ return maxOffset;
241
+ case 'end':
242
+ return minOffset;
243
+ case 'center':
244
+ return Math.round(minOffset + (maxOffset - minOffset) / 2);
245
+ case 'auto':
246
+ default:
247
+ if (this._scrollOffset >= minOffset &&
248
+ this._scrollOffset <= maxOffset) {
249
+ return this._scrollOffset;
250
+ }
251
+ else if (this._scrollOffset < minOffset) {
252
+ return minOffset;
253
+ }
254
+ else {
255
+ return maxOffset;
256
+ }
257
+ }
258
+ }
259
+ /**
260
+ * Compute the items range to display.
261
+ *
262
+ * It returns ``null`` if the range does not need to be updated.
263
+ *
264
+ * @returns The current items range to display
265
+ */
266
+ getRangeToRender() {
267
+ let newWindowIndex = [
268
+ 0,
269
+ Math.max(this.widgetCount - 1, -1),
270
+ 0,
271
+ Math.max(this.widgetCount - 1, -1)
272
+ ];
273
+ const previousLastMeasuredIndex = this._lastMeasuredIndex;
274
+ if (this.windowingActive) {
275
+ newWindowIndex = this._getRangeToRender();
276
+ }
277
+ const [startIndex, stopIndex] = newWindowIndex;
278
+ if (previousLastMeasuredIndex <= stopIndex ||
279
+ this._currentWindow[0] !== startIndex ||
280
+ this._currentWindow[1] !== stopIndex) {
281
+ this._currentWindow = newWindowIndex;
282
+ return newWindowIndex;
283
+ }
284
+ return null;
285
+ }
286
+ /**
287
+ * Return the viewport top position and height for range spanning from
288
+ * ``startIndex`` to ``stopIndex``.
289
+ *
290
+ * @param startIndex First item in viewport index
291
+ * @param stopIndex Last item in viewport index
292
+ * @returns The viewport top position and its height
293
+ */
294
+ getSpan(startIndex, stopIndex) {
295
+ const startSize = this._getItemMetadata(startIndex);
296
+ const top = startSize.offset;
297
+ const stopSize = this._getItemMetadata(stopIndex);
298
+ const height = stopSize.offset - startSize.offset + stopSize.size;
299
+ return [top, height];
300
+ }
301
+ /**
302
+ * WindowedListModel caches offsets and measurements for each index for performance purposes.
303
+ * This method clears that cached data for all items after (and including) the specified index.
304
+ *
305
+ * The list will automatically re-render after the index is reset.
306
+ *
307
+ * @param index
308
+ */
309
+ resetAfterIndex(index) {
310
+ const oldValue = this._lastMeasuredIndex;
311
+ this._lastMeasuredIndex = Math.min(index, this._lastMeasuredIndex);
312
+ if (this._lastMeasuredIndex !== oldValue) {
313
+ this._stateChanged.emit({ name: 'index', newValue: index, oldValue });
314
+ }
315
+ }
316
+ /**
317
+ * Update item sizes.
318
+ *
319
+ * This should be called when the real item sizes has been
320
+ * measured.
321
+ *
322
+ * @param sizes New sizes per item index
323
+ * @returns Whether some sizes changed or not
324
+ */
325
+ setWidgetSize(sizes) {
326
+ if (this._currentWindow[0] >= 0) {
327
+ let minIndex = Infinity;
328
+ for (const item of sizes) {
329
+ const key = item.index;
330
+ const size = item.size;
331
+ if (!this._widgetSizers[key]) {
332
+ this._getItemMetadata(key);
333
+ }
334
+ if (this._widgetSizers[key].size != size) {
335
+ this._widgetSizers[key].size = size;
336
+ minIndex = Math.min(minIndex, key);
337
+ }
338
+ // Always set the flag in case the size estimator provides perfect result
339
+ this._widgetSizers[key].measured = true;
340
+ }
341
+ // If some sizes changed
342
+ if (minIndex != Infinity) {
343
+ // Invalid follow-up index
344
+ this._lastMeasuredIndex = Math.min(this._lastMeasuredIndex, minIndex);
345
+ return true;
346
+ }
347
+ }
348
+ return false;
349
+ }
350
+ /**
351
+ * Callback on list changes
352
+ *
353
+ * @param list List items
354
+ * @param changes List change
355
+ */
356
+ onListChanged(list, changes) {
357
+ switch (changes.type) {
358
+ case 'add':
359
+ this._widgetSizers.splice(changes.newIndex, 0, ...new Array(changes.newValues.length).map(() => {
360
+ return { offset: 0, size: this._estimatedWidgetSize };
361
+ }));
362
+ this.resetAfterIndex(changes.newIndex - 1);
363
+ break;
364
+ case 'move':
365
+ ArrayExt.move(this._widgetSizers, changes.oldIndex, changes.newIndex);
366
+ this.resetAfterIndex(Math.min(changes.newIndex, changes.oldIndex) - 1);
367
+ break;
368
+ case 'remove':
369
+ this._widgetSizers.splice(changes.oldIndex, changes.oldValues.length);
370
+ this.resetAfterIndex(changes.oldIndex - 1);
371
+ break;
372
+ case 'set':
373
+ this.resetAfterIndex(changes.newIndex - 1);
374
+ break;
375
+ }
376
+ }
377
+ _getItemMetadata(index) {
378
+ var _a, _b;
379
+ if (index > this._lastMeasuredIndex) {
380
+ let offset = 0;
381
+ if (this._lastMeasuredIndex >= 0) {
382
+ const itemMetadata = this._widgetSizers[this._lastMeasuredIndex];
383
+ offset = itemMetadata.offset + itemMetadata.size;
384
+ }
385
+ for (let i = this._lastMeasuredIndex + 1; i <= index; i++) {
386
+ let size = ((_a = this._widgetSizers[i]) === null || _a === void 0 ? void 0 : _a.measured)
387
+ ? this._widgetSizers[i].size
388
+ : this.estimateWidgetSize(i);
389
+ this._widgetSizers[i] = {
390
+ offset,
391
+ size,
392
+ measured: (_b = this._widgetSizers[i]) === null || _b === void 0 ? void 0 : _b.measured
393
+ };
394
+ offset += size;
395
+ }
396
+ this._lastMeasuredIndex = index;
397
+ }
398
+ for (let i = 0; i <= this._lastMeasuredIndex; i++) {
399
+ const sizer = this._widgetSizers[i];
400
+ if (i === 0) {
401
+ if (sizer.offset !== 0) {
402
+ throw new Error('First offset is not null');
403
+ }
404
+ }
405
+ else {
406
+ const previous = this._widgetSizers[i - 1];
407
+ if (sizer.offset !== previous.offset + previous.size) {
408
+ throw new Error(`Sizer ${i} has incorrect offset.`);
409
+ }
410
+ }
411
+ }
412
+ return this._widgetSizers[index];
413
+ }
414
+ _findNearestItem(offset) {
415
+ const lastMeasuredItemOffset = this._lastMeasuredIndex > 0
416
+ ? this._widgetSizers[this._lastMeasuredIndex].offset
417
+ : 0;
418
+ if (lastMeasuredItemOffset >= offset) {
419
+ // If we've already measured items within this range just use a binary search as it's faster.
420
+ return this._findNearestItemBinarySearch(this._lastMeasuredIndex, 0, offset);
421
+ }
422
+ else {
423
+ // If we haven't yet measured this high, fallback to an exponential search with an inner binary search.
424
+ // The exponential search avoids pre-computing sizes for the full set of items as a binary search would.
425
+ // The overall complexity for this approach is O(log n).
426
+ return this._findNearestItemExponentialSearch(Math.max(0, this._lastMeasuredIndex), offset);
427
+ }
428
+ }
429
+ _findNearestItemBinarySearch(high, low, offset) {
430
+ while (low <= high) {
431
+ const middle = low + Math.floor((high - low) / 2);
432
+ const currentOffset = this._getItemMetadata(middle).offset;
433
+ if (currentOffset === offset) {
434
+ return middle;
435
+ }
436
+ else if (currentOffset < offset) {
437
+ low = middle + 1;
438
+ }
439
+ else if (currentOffset > offset) {
440
+ high = middle - 1;
441
+ }
442
+ }
443
+ if (low > 0) {
444
+ return low - 1;
445
+ }
446
+ else {
447
+ return 0;
448
+ }
449
+ }
450
+ _findNearestItemExponentialSearch(index, offset) {
451
+ let interval = 1;
452
+ while (index < this.widgetCount &&
453
+ this._getItemMetadata(index).offset < offset) {
454
+ index += interval;
455
+ interval *= 2;
456
+ }
457
+ return this._findNearestItemBinarySearch(Math.min(index, this.widgetCount - 1), Math.floor(index / 2), offset);
458
+ }
459
+ _getRangeToRender() {
460
+ const widgetCount = this.widgetCount;
461
+ if (widgetCount === 0) {
462
+ return [-1, -1, -1, -1];
463
+ }
464
+ const startIndex = this._getStartIndexForOffset(this._scrollOffset);
465
+ const stopIndex = this._getStopIndexForStartIndex(startIndex, this._scrollOffset);
466
+ const overscanBackward = Math.max(1, this.overscanCount);
467
+ const overscanForward = Math.max(1, this.overscanCount);
468
+ return [
469
+ Math.max(0, startIndex - overscanBackward),
470
+ Math.max(0, Math.min(widgetCount - 1, stopIndex + overscanForward)),
471
+ startIndex,
472
+ stopIndex
473
+ ];
474
+ }
475
+ _getStartIndexForOffset(offset) {
476
+ return this._findNearestItem(offset);
477
+ }
478
+ _getStopIndexForStartIndex(startIndex, scrollOffset) {
479
+ const size = this._height;
480
+ const itemMetadata = this._getItemMetadata(startIndex);
481
+ const maxOffset = scrollOffset + size;
482
+ let offset = itemMetadata.offset + itemMetadata.size;
483
+ let stopIndex = startIndex;
484
+ while (stopIndex < this.widgetCount - 1 && offset < maxOffset) {
485
+ stopIndex++;
486
+ offset += this._getItemMetadata(stopIndex).size;
487
+ }
488
+ return stopIndex;
489
+ }
490
+ }
491
+ /**
492
+ * Windowed list widget
493
+ */
494
+ export class WindowedList extends Widget {
495
+ /**
496
+ * Constructor
497
+ *
498
+ * @param options Constructor options
499
+ */
500
+ constructor(options) {
501
+ var _a;
502
+ // TODO probably needs to be able to customize outer HTML tag (could be ul / ol / table, ...)
503
+ const node = document.createElement('div');
504
+ node.className = 'jp-WindowedPanel-outer';
505
+ const innerElement = node.appendChild(document.createElement('div'));
506
+ innerElement.className = 'jp-WindowedPanel-inner';
507
+ const windowContainer = innerElement.appendChild(document.createElement('div'));
508
+ windowContainer.className = 'jp-WindowedPanel-window';
509
+ super({ node });
510
+ super.layout = (_a = options.layout) !== null && _a !== void 0 ? _a : new WindowedLayout();
511
+ this._viewModel = options.model;
512
+ this._innerElement = innerElement;
513
+ this._isScrolling = null;
514
+ this._windowElement = windowContainer;
515
+ this._scrollToItem = null;
516
+ this._scrollRepaint = null;
517
+ this._scrollUpdateWasRequested = false;
518
+ this._resizeObserver = null;
519
+ this._viewModel.stateChanged.connect(this.onStateChanged, this);
520
+ }
521
+ /**
522
+ * Widget layout
523
+ */
524
+ get layout() {
525
+ return super.layout;
526
+ }
527
+ /**
528
+ * Viewport
529
+ */
530
+ get viewportNode() {
531
+ return this._windowElement;
532
+ }
533
+ /**
534
+ * Windowed list view model
535
+ */
536
+ get viewModel() {
537
+ return this._viewModel;
538
+ }
539
+ /**
540
+ * Callback on event.
541
+ *
542
+ * @param event Event
543
+ */
544
+ handleEvent(event) {
545
+ switch (event.type) {
546
+ case 'scroll':
547
+ this.onScroll(event);
548
+ break;
549
+ }
550
+ }
551
+ /**
552
+ * Scroll to the specified offset `scrollTop`.
553
+ *
554
+ * @param scrollOffset Offset to scroll
555
+ *
556
+ * @deprecated This is an internal helper. Prefer calling `scrollToItem`.
557
+ */
558
+ scrollTo(scrollOffset) {
559
+ if (!this.viewModel.windowingActive) {
560
+ this.node.scrollTo({ top: scrollOffset });
561
+ return;
562
+ }
563
+ scrollOffset = Math.max(0, scrollOffset);
564
+ if (scrollOffset !== this.viewModel.scrollOffset) {
565
+ this.viewModel.scrollOffset = scrollOffset;
566
+ this._scrollUpdateWasRequested = true;
567
+ this.update();
568
+ }
569
+ }
570
+ /**
571
+ * Scroll to the specified item.
572
+ *
573
+ * By default, the List will scroll as little as possible to ensure the item is visible. You can control the alignment of the item though by specifying a second alignment parameter. Acceptable values are:
574
+ *
575
+ * auto (default) - Scroll as little as possible to ensure the item is visible. (If the item is already visible, it won't scroll at all.)
576
+ * smart - If the item is already visible, don't scroll at all. If it is less than one viewport away, scroll as little as possible so that it becomes visible. If it is more than one viewport away, scroll so that it is centered within the list.
577
+ * center - Center align the item within the list.
578
+ * end - Align the item to the end of the list
579
+ * start - Align the item to the beginning of the list
580
+ *
581
+ * @param index Item index to scroll to
582
+ * @param align Type of alignment
583
+ */
584
+ scrollToItem(index, align = 'auto') {
585
+ if (!this.viewModel.windowingActive) {
586
+ const widget = this.layout.widgets[index];
587
+ if (widget === null || widget === void 0 ? void 0 : widget.node.scrollIntoView) {
588
+ widget.node.scrollIntoView({
589
+ block: ['auto', 'smart'].includes(align)
590
+ ? 'nearest'
591
+ : align
592
+ });
593
+ }
594
+ return Promise.resolve();
595
+ }
596
+ if (!this._isScrolling ||
597
+ this._scrollToItem === null ||
598
+ this._scrollToItem[0] !== index ||
599
+ this._scrollToItem[1] !== align) {
600
+ if (this._isScrolling) {
601
+ this._isScrolling.reject('Scrolling to a new item is requested.');
602
+ }
603
+ this._isScrolling = new PromiseDelegate();
604
+ }
605
+ this._scrollToItem = [index, align];
606
+ this._resetScrollToItem();
607
+ this.scrollTo(this.viewModel.getOffsetForIndexAndAlignment(Math.max(0, Math.min(index, this._viewModel.widgetCount - 1)), align));
608
+ return this._isScrolling.promise;
609
+ }
610
+ /**
611
+ * A message handler invoked on an `'after-attach'` message.
612
+ */
613
+ onAfterAttach(msg) {
614
+ super.onAfterAttach(msg);
615
+ if (this._viewModel.windowingActive) {
616
+ this._addListeners();
617
+ }
618
+ this.viewModel.height = this.node.getBoundingClientRect().height;
619
+ }
620
+ /**
621
+ * A message handler invoked on an `'before-detach'` message.
622
+ */
623
+ onBeforeDetach(msg) {
624
+ if (this._viewModel.windowingActive) {
625
+ this._removeListeners();
626
+ }
627
+ }
628
+ /**
629
+ * Callback on scroll event
630
+ *
631
+ * @param event Scroll event
632
+ */
633
+ onScroll(event) {
634
+ const { clientHeight, scrollHeight, scrollTop } = event.currentTarget;
635
+ // TBC Firefox is emitting two events one with 1px diff then the _real_ scroll
636
+ if (!this._scrollUpdateWasRequested &&
637
+ Math.abs(this.viewModel.scrollOffset - scrollTop) > 1) {
638
+ // Test if the scroll event is jumping to the list bottom
639
+ // if (Math.abs(scrollHeight - clientHeight - scrollTop) < 1) {
640
+ // // FIXME Does not work because it happens in multiple segments in between which the sizing is changing
641
+ // // due to widget resizing. A possible fix would be to keep track of the "old" scrollHeight - clientHeight
642
+ // // up to some quiet activity.
643
+ // this.scrollToItem(this.widgetCount, 'end');
644
+ // } else {
645
+ const scrollOffset = Math.max(0, Math.min(scrollTop, scrollHeight - clientHeight));
646
+ this.viewModel.scrollOffset = scrollOffset;
647
+ this._scrollUpdateWasRequested = false;
648
+ this.update();
649
+ // }
650
+ }
651
+ }
652
+ /**
653
+ * A message handler invoked on an `'resize-request'` message.
654
+ */
655
+ onResize(msg) {
656
+ this.viewModel.height =
657
+ msg.height >= 0 ? msg.height : this.node.getBoundingClientRect().height;
658
+ super.onResize(msg);
659
+ }
660
+ /**
661
+ * Callback on view model change
662
+ *
663
+ * @param model Windowed list model
664
+ * @param changes Change
665
+ */
666
+ onStateChanged(model, changes) {
667
+ switch (changes.name) {
668
+ case 'windowingActive':
669
+ if (this.viewModel.windowingActive) {
670
+ this._addListeners();
671
+ this.onScroll({ currentTarget: this.node });
672
+ // Bail as onScroll will trigger update
673
+ return;
674
+ }
675
+ else {
676
+ this._removeListeners();
677
+ }
678
+ break;
679
+ }
680
+ this.update();
681
+ }
682
+ /**
683
+ * A message handler invoked on an `'update-request'` message.
684
+ *
685
+ * #### Notes
686
+ * The default implementation of this handler is a no-op.
687
+ */
688
+ onUpdateRequest(msg) {
689
+ if (this.viewModel.windowingActive) {
690
+ // Throttle update request
691
+ if (this._scrollRepaint === null) {
692
+ this._scrollRepaint = window.requestAnimationFrame(() => {
693
+ this._scrollRepaint = null;
694
+ this._update();
695
+ });
696
+ }
697
+ }
698
+ else {
699
+ this._update();
700
+ }
701
+ }
702
+ _addListeners() {
703
+ if (!this._resizeObserver) {
704
+ this._resizeObserver = new ResizeObserver(this._onWidgetResize.bind(this));
705
+ }
706
+ for (const widget of this.layout.widgets) {
707
+ this._resizeObserver.observe(widget.node);
708
+ }
709
+ this.node.addEventListener('scroll', this, passiveIfSupported);
710
+ this._windowElement.style.position = 'absolute';
711
+ }
712
+ _removeListeners() {
713
+ var _a;
714
+ this.node.removeEventListener('scroll', this);
715
+ (_a = this._resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
716
+ this._resizeObserver = null;
717
+ this._innerElement.style.height = '100%';
718
+ this._windowElement.style.position = 'relative';
719
+ this._windowElement.style.top = '0px';
720
+ }
721
+ _update() {
722
+ var _a, _b;
723
+ if (this.isDisposed || !this.layout) {
724
+ return;
725
+ }
726
+ const newWindowIndex = this.viewModel.getRangeToRender();
727
+ if (newWindowIndex !== null) {
728
+ const [startIndex, stopIndex] = newWindowIndex;
729
+ const toAdd = [];
730
+ if (stopIndex >= 0) {
731
+ for (let index = startIndex; index <= stopIndex; index++) {
732
+ const widget = this.viewModel.widgetRenderer(index);
733
+ widget.dataset.windowedListIndex = `${index}`;
734
+ toAdd.push(widget);
735
+ }
736
+ }
737
+ const nWidgets = this.layout.widgets.length;
738
+ // Remove not needed widgets
739
+ for (let itemIdx = nWidgets - 1; itemIdx >= 0; itemIdx--) {
740
+ if (!toAdd.includes(this.layout.widgets[itemIdx])) {
741
+ (_a = this._resizeObserver) === null || _a === void 0 ? void 0 : _a.unobserve(this.layout.widgets[itemIdx].node);
742
+ this.layout.removeWidget(this.layout.widgets[itemIdx]);
743
+ }
744
+ }
745
+ for (let index = 0; index < toAdd.length; index++) {
746
+ const item = toAdd[index];
747
+ if (!this.layout.widgets.includes(item)) {
748
+ (_b = this._resizeObserver) === null || _b === void 0 ? void 0 : _b.observe(item.node);
749
+ }
750
+ // The widget may have moved due to drag-and-drop
751
+ this.layout.insertWidget(index, item);
752
+ }
753
+ if (this.viewModel.windowingActive) {
754
+ if (stopIndex >= 0) {
755
+ // Read this value after creating the cells.
756
+ // So their actual sizes are taken into account
757
+ const estimatedTotalHeight = this.viewModel.getEstimatedTotalSize();
758
+ // Update inner container height
759
+ this._innerElement.style.height = `${estimatedTotalHeight}px`;
760
+ // Update position of window container
761
+ const [top, minHeight] = this.viewModel.getSpan(startIndex, stopIndex);
762
+ this._windowElement.style.top = `${top}px`;
763
+ this._windowElement.style.minHeight = `${minHeight}px`;
764
+ }
765
+ else {
766
+ // Update inner container height
767
+ this._innerElement.style.height = `0px`;
768
+ // Update position of window container
769
+ this._windowElement.style.top = `0px`;
770
+ this._windowElement.style.minHeight = `0px`;
771
+ }
772
+ // Update scroll
773
+ if (this._scrollUpdateWasRequested) {
774
+ this.node.scrollTop = this.viewModel.scrollOffset;
775
+ this._scrollUpdateWasRequested = false;
776
+ }
777
+ }
778
+ }
779
+ let index2 = -1;
780
+ for (const w of this.viewportNode.children) {
781
+ const currentIdx = parseInt(w.dataset.windowedListIndex, 10);
782
+ if (currentIdx < index2) {
783
+ throw new Error('Inconsistent dataset index');
784
+ }
785
+ else {
786
+ index2 = currentIdx;
787
+ }
788
+ }
789
+ }
790
+ _onWidgetResize(entries) {
791
+ this._resetScrollToItem();
792
+ const newSizes = [];
793
+ for (let entry of entries) {
794
+ // Rely on the data attribute as some nodes may be hidden instead of detach
795
+ // to preserve state.
796
+ newSizes.push({
797
+ index: parseInt(entry.target.dataset.windowedListIndex, 10),
798
+ size: entry.borderBoxSize[0].blockSize
799
+ });
800
+ }
801
+ // If some sizes changed
802
+ if (this.viewModel.setWidgetSize(newSizes)) {
803
+ // Update the list
804
+ if (this._scrollToItem) {
805
+ this.scrollToItem(...this._scrollToItem).catch(reason => {
806
+ console.log(reason);
807
+ });
808
+ }
809
+ else {
810
+ this.update();
811
+ }
812
+ }
813
+ }
814
+ _resetScrollToItem() {
815
+ if (this._resetScrollToItemTimeout) {
816
+ clearTimeout(this._resetScrollToItemTimeout);
817
+ }
818
+ if (this._scrollToItem) {
819
+ this._resetScrollToItemTimeout = window.setTimeout(() => {
820
+ this._scrollToItem = null;
821
+ if (this._isScrolling) {
822
+ this._isScrolling.resolve();
823
+ this._isScrolling = null;
824
+ }
825
+ }, MAXIMUM_TIME_REMAINING);
826
+ }
827
+ }
828
+ }
829
+ /**
830
+ * Default widget size
831
+ */
832
+ WindowedList.DEFAULT_WIDGET_SIZE = 50;
833
+ /**
834
+ * Customized layout for windowed list container.
835
+ */
836
+ export class WindowedLayout extends PanelLayout {
837
+ /**
838
+ * Constructor
839
+ */
840
+ constructor() {
841
+ super({ fitPolicy: 'set-no-constraint' });
842
+ }
843
+ /**
844
+ * Specialized parent type definition
845
+ */
846
+ get parent() {
847
+ return super.parent;
848
+ }
849
+ set parent(value) {
850
+ super.parent = value;
851
+ }
852
+ /**
853
+ * Attach a widget to the parent's DOM node.
854
+ *
855
+ * @param index - The current index of the widget in the layout.
856
+ *
857
+ * @param widget - The widget to attach to the parent.
858
+ *
859
+ * #### Notes
860
+ * This method is called automatically by the panel layout at the
861
+ * appropriate time. It should not be called directly by user code.
862
+ *
863
+ * The default implementation adds the widgets's node to the parent's
864
+ * node at the proper location, and sends the appropriate attach
865
+ * messages to the widget if the parent is attached to the DOM.
866
+ *
867
+ * Subclasses may reimplement this method to control how the widget's
868
+ * node is added to the parent's node.
869
+ */
870
+ attachWidget(index, widget) {
871
+ // Look up the next sibling reference node.
872
+ let ref = this.parent.viewportNode.children[index];
873
+ // Send a `'before-attach'` message if the parent is attached.
874
+ if (this.parent.isAttached) {
875
+ MessageLoop.sendMessage(widget, Widget.Msg.BeforeAttach);
876
+ }
877
+ // Insert the widget's node before the sibling.
878
+ this.parent.viewportNode.insertBefore(widget.node, ref);
879
+ // Send an `'after-attach'` message if the parent is attached.
880
+ if (this.parent.isAttached) {
881
+ MessageLoop.sendMessage(widget, Widget.Msg.AfterAttach);
882
+ }
883
+ }
884
+ /**
885
+ * Detach a widget from the parent's DOM node.
886
+ *
887
+ * @param index - The previous index of the widget in the layout.
888
+ *
889
+ * @param widget - The widget to detach from the parent.
890
+ *
891
+ * #### Notes
892
+ * This method is called automatically by the panel layout at the
893
+ * appropriate time. It should not be called directly by user code.
894
+ *
895
+ * The default implementation removes the widget's node from the
896
+ * parent's node, and sends the appropriate detach messages to the
897
+ * widget if the parent is attached to the DOM.
898
+ *
899
+ * Subclasses may reimplement this method to control how the widget's
900
+ * node is removed from the parent's node.
901
+ */
902
+ detachWidget(index, widget) {
903
+ // Send a `'before-detach'` message if the parent is attached.
904
+ if (this.parent.isAttached) {
905
+ MessageLoop.sendMessage(widget, Widget.Msg.BeforeDetach);
906
+ }
907
+ // Remove the widget's node from the parent.
908
+ this.parent.viewportNode.removeChild(widget.node);
909
+ // Send an `'after-detach'` message if the parent is attached.
910
+ if (this.parent.isAttached) {
911
+ MessageLoop.sendMessage(widget, Widget.Msg.AfterDetach);
912
+ }
913
+ }
914
+ /**
915
+ * Move a widget in the parent's DOM node.
916
+ *
917
+ * @param fromIndex - The previous index of the widget in the layout.
918
+ *
919
+ * @param toIndex - The current index of the widget in the layout.
920
+ *
921
+ * @param widget - The widget to move in the parent.
922
+ *
923
+ * #### Notes
924
+ * This method is called automatically by the panel layout at the
925
+ * appropriate time. It should not be called directly by user code.
926
+ *
927
+ * The default implementation moves the widget's node to the proper
928
+ * location in the parent's node and sends the appropriate attach and
929
+ * detach messages to the widget if the parent is attached to the DOM.
930
+ *
931
+ * Subclasses may reimplement this method to control how the widget's
932
+ * node is moved in the parent's node.
933
+ */
934
+ moveWidget(fromIndex, toIndex, widget) {
935
+ // Optimize move without de-/attaching as motion appends with parent attached
936
+ // Case fromIndex === toIndex, already checked in PanelLayout.insertWidget
937
+ // Look up the next sibling reference node.
938
+ let ref = this.parent.viewportNode.children[toIndex];
939
+ if (fromIndex < toIndex) {
940
+ ref.insertAdjacentElement('afterend', widget.node);
941
+ }
942
+ else {
943
+ ref.insertAdjacentElement('beforebegin', widget.node);
944
+ }
945
+ }
946
+ /**
947
+ * A message handler invoked on an `'update-request'` message.
948
+ *
949
+ * #### Notes
950
+ * This is a reimplementation of the base class method,
951
+ * and is a no-op.
952
+ */
953
+ onUpdateRequest(msg) {
954
+ // This is a no-op.
955
+ }
956
+ }
957
+ //# sourceMappingURL=windowedlist.js.map