@nativescript-community/ui-collectionview 4.0.47 → 4.0.48

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.
@@ -1,1004 +0,0 @@
1
- import { ChangeType, ContentView, Observable, Property, ProxyViewContainer, Trace, Utils, View, paddingBottomProperty, paddingLeftProperty, paddingRightProperty, paddingTopProperty, profile } from '@nativescript/core';
2
- import { layout } from '@nativescript/core/utils/utils';
3
- import { reorderLongPressEnabledProperty, reorderingEnabledProperty, reverseLayoutProperty, scrollBarIndicatorVisibleProperty } from './collectionview';
4
- import { CLog, CLogTypes, CollectionViewBase, ListViewViewTypes, isBounceEnabledProperty, isScrollEnabledProperty, itemTemplatesProperty, orientationProperty } from './collectionview-common';
5
- export * from './collectionview-common';
6
- const infinity = layout.makeMeasureSpec(0, layout.UNSPECIFIED);
7
- export var ContentInsetAdjustmentBehavior;
8
- (function (ContentInsetAdjustmentBehavior) {
9
- ContentInsetAdjustmentBehavior[ContentInsetAdjustmentBehavior["Always"] = 3] = "Always";
10
- ContentInsetAdjustmentBehavior[ContentInsetAdjustmentBehavior["Automatic"] = 0] = "Automatic";
11
- ContentInsetAdjustmentBehavior[ContentInsetAdjustmentBehavior["Never"] = 2] = "Never";
12
- ContentInsetAdjustmentBehavior[ContentInsetAdjustmentBehavior["ScrollableAxes"] = 1] = "ScrollableAxes";
13
- })(ContentInsetAdjustmentBehavior || (ContentInsetAdjustmentBehavior = {}));
14
- function parseContentInsetAdjustmentBehavior(value) {
15
- if (typeof value === 'string') {
16
- switch (value) {
17
- case 'always':
18
- return ContentInsetAdjustmentBehavior.Always;
19
- case 'never':
20
- return ContentInsetAdjustmentBehavior.Never;
21
- case 'scrollableAxes':
22
- return ContentInsetAdjustmentBehavior.ScrollableAxes;
23
- default:
24
- case 'automatic':
25
- return ContentInsetAdjustmentBehavior.Automatic;
26
- }
27
- }
28
- else {
29
- return value;
30
- }
31
- }
32
- export const contentInsetAdjustmentBehaviorProperty = new Property({
33
- name: 'contentInsetAdjustmentBehavior',
34
- valueConverter: parseContentInsetAdjustmentBehavior,
35
- defaultValue: ContentInsetAdjustmentBehavior.Automatic
36
- });
37
- export class CollectionView extends CollectionViewBase {
38
- constructor() {
39
- super();
40
- this._preparingCell = false;
41
- this.reorderStartingRow = -1;
42
- this.reorderEndingRow = -1;
43
- this.manualDragging = false;
44
- this.scrollEnabledBeforeDragging = true;
45
- this.needsScrollStartEvent = false;
46
- this.isScrolling = false;
47
- this._map = new Map();
48
- this._sizes = new Array();
49
- }
50
- createNativeView() {
51
- let layout;
52
- if (CollectionViewBase.layoutStyles[this.layoutStyle]) {
53
- layout = this._layout = CollectionViewBase.layoutStyles[this.layoutStyle].createLayout(this);
54
- }
55
- else {
56
- layout = this._layout = UICollectionViewFlowLayout.alloc().init();
57
- }
58
- if (layout instanceof UICollectionViewFlowLayout) {
59
- layout.minimumLineSpacing = 0;
60
- layout.minimumInteritemSpacing = 0;
61
- }
62
- const view = UICollectionView.alloc().initWithFrameCollectionViewLayout(CGRectMake(0, 0, 0, 0), layout);
63
- view.backgroundColor = UIColor.clearColor;
64
- this._itemTemplatesInternal.forEach((t) => {
65
- view.registerClassForCellWithReuseIdentifier(CollectionViewCell.class(), t.key.toLowerCase());
66
- });
67
- view.autoresizesSubviews = false;
68
- view.autoresizingMask = 0;
69
- this.lastContentOffset = view.contentOffset;
70
- return view;
71
- }
72
- onTemplateAdded(t) {
73
- super.onTemplateAdded(t);
74
- if (this.nativeViewProtected) {
75
- this.nativeViewProtected.registerClassForCellWithReuseIdentifier(CollectionViewCell.class(), t.key.toLowerCase());
76
- }
77
- }
78
- initNativeView() {
79
- super.initNativeView();
80
- const nativeView = this.nativeView;
81
- this._dataSource = CollectionViewDataSource.initWithOwner(this);
82
- nativeView.dataSource = this._dataSource;
83
- const layoutStyle = CollectionViewBase.layoutStyles[this.layoutStyle];
84
- if (layoutStyle && layoutStyle.createDelegate) {
85
- this._delegate = layoutStyle.createDelegate();
86
- }
87
- else {
88
- this._delegate = UICollectionViewDelegateImpl.initWithOwner(this);
89
- }
90
- this._delegate._owner = new WeakRef(this);
91
- this._measureCellMap = new Map();
92
- this.nativeView.delegate = this._delegate;
93
- this._setNativeClipToBounds();
94
- }
95
- disposeNativeView() {
96
- if (Trace.isEnabled()) {
97
- CLog(CLogTypes.log, 'disposeNativeView');
98
- }
99
- const nativeView = this.nativeView;
100
- nativeView.delegate = null;
101
- this._delegate = null;
102
- nativeView.dataSource = null;
103
- this._dataSource = null;
104
- this._layout = null;
105
- this.reorderLongPressHandler = null;
106
- this.reorderLongPressGesture = null;
107
- this.clearRealizedCells();
108
- super.disposeNativeView();
109
- }
110
- get _childrenCount() {
111
- return this._map.size;
112
- }
113
- eachChild(callback) {
114
- this._map.forEach((view) => {
115
- if (view.parent instanceof CollectionView) {
116
- callback(view);
117
- }
118
- else {
119
- if (view.parent) {
120
- callback(view.parent);
121
- }
122
- }
123
- });
124
- }
125
- getViewForItemAtIndex(index) {
126
- let result;
127
- if (this.nativeViewProtected) {
128
- const cell = this.nativeViewProtected.cellForItemAtIndexPath(NSIndexPath.indexPathForRowInSection(index, 0));
129
- return cell === null || cell === void 0 ? void 0 : cell.view;
130
- }
131
- return result;
132
- }
133
- startDragging(index, pointer) {
134
- if (this.reorderEnabled && this.nativeViewProtected) {
135
- this.manualDragging = true;
136
- this.draggingStartDelta = null;
137
- if (pointer) {
138
- const view = this.getViewForItemAtIndex(index);
139
- if (view) {
140
- const size = view.nativeViewProtected.bounds.size;
141
- const point = pointer.ios.locationInView(view.nativeViewProtected);
142
- this.draggingStartDelta = [point.x - size.width / 2, point.y - size.height / 2];
143
- }
144
- }
145
- this.nativeViewProtected.beginInteractiveMovementForItemAtIndexPath(NSIndexPath.indexPathForRowInSection(index, 0));
146
- this.scrollEnabledBeforeDragging = this.isScrollEnabled;
147
- this.nativeViewProtected.scrollEnabled = false;
148
- }
149
- }
150
- onReorderingTouch(event) {
151
- if (!this.manualDragging) {
152
- return;
153
- }
154
- const collectionView = this.nativeViewProtected;
155
- const pointer = event.getActivePointers()[0];
156
- switch (event.action) {
157
- case 'move':
158
- let x = pointer.getX();
159
- let y = pointer.getY();
160
- if (this.draggingStartDelta) {
161
- x -= this.draggingStartDelta[0];
162
- y -= this.draggingStartDelta[1];
163
- }
164
- collectionView.updateInteractiveMovementTargetPosition(CGPointMake(x, y));
165
- break;
166
- case 'up':
167
- this.manualDragging = false;
168
- collectionView && collectionView.endInteractiveMovement();
169
- this.nativeViewProtected.scrollEnabled = this.scrollEnabledBeforeDragging;
170
- this.handleReorderEnd();
171
- break;
172
- case 'cancel':
173
- this.manualDragging = false;
174
- collectionView && collectionView.cancelInteractiveMovement();
175
- this.nativeViewProtected.scrollEnabled = this.scrollEnabledBeforeDragging;
176
- this.handleReorderEnd();
177
- break;
178
- }
179
- }
180
- handleReorderEnd() {
181
- if (!this.reorderEndingRow) {
182
- this.reorderEndingRow = this.reorderStartingRow;
183
- }
184
- const item = this.getItemAtIndex(this.reorderStartingRow);
185
- this._callItemReorderedEvent(this.reorderStartingRow, this.reorderEndingRow, item);
186
- this.reorderEndingRow = -1;
187
- this.reorderEndingRow = -1;
188
- }
189
- onReorderLongPress(gesture) {
190
- const collectionView = this.nativeViewProtected;
191
- if (!collectionView) {
192
- return;
193
- }
194
- switch (gesture.state) {
195
- case 1:
196
- const selectedIndexPath = collectionView.indexPathForItemAtPoint(gesture.locationInView(collectionView));
197
- collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath);
198
- break;
199
- case 2:
200
- collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(collectionView));
201
- break;
202
- case 3:
203
- collectionView.endInteractiveMovement();
204
- this.handleReorderEnd();
205
- break;
206
- default:
207
- collectionView.cancelInteractiveMovement();
208
- this.handleReorderEnd();
209
- break;
210
- }
211
- }
212
- [contentInsetAdjustmentBehaviorProperty.setNative](value) {
213
- this.nativeViewProtected.contentInsetAdjustmentBehavior = value;
214
- }
215
- [paddingTopProperty.setNative](value) {
216
- this._setPadding({ top: layout.toDeviceIndependentPixels(this.effectivePaddingTop) });
217
- }
218
- [paddingRightProperty.setNative](value) {
219
- this._setPadding({ right: layout.toDeviceIndependentPixels(this.effectivePaddingRight) });
220
- }
221
- [paddingBottomProperty.setNative](value) {
222
- this._setPadding({ bottom: layout.toDeviceIndependentPixels(this.effectivePaddingBottom) });
223
- }
224
- [paddingLeftProperty.setNative](value) {
225
- this._setPadding({ left: layout.toDeviceIndependentPixels(this.effectivePaddingLeft) });
226
- }
227
- [orientationProperty.setNative](value) {
228
- const layout = this._layout;
229
- if (layout instanceof UICollectionViewFlowLayout) {
230
- if (value === 'horizontal') {
231
- layout.scrollDirection = 1;
232
- }
233
- else {
234
- layout.scrollDirection = 0;
235
- }
236
- }
237
- this.updateScrollBarVisibility(this.scrollBarIndicatorVisible);
238
- }
239
- [isScrollEnabledProperty.setNative](value) {
240
- this.nativeViewProtected.scrollEnabled = value;
241
- this.scrollEnabledBeforeDragging = value;
242
- }
243
- [isBounceEnabledProperty.setNative](value) {
244
- this.nativeViewProtected.bounces = value;
245
- }
246
- [itemTemplatesProperty.getDefault]() {
247
- return null;
248
- }
249
- [reverseLayoutProperty.setNative](value) {
250
- this.nativeViewProtected.transform = value ? CGAffineTransformMakeRotation(-Math.PI) : null;
251
- }
252
- [reorderLongPressEnabledProperty.setNative](value) {
253
- if (value) {
254
- if (!this.reorderLongPressGesture) {
255
- this.reorderLongPressHandler = ReorderLongPressImpl.initWithOwner(new WeakRef(this));
256
- this.reorderLongPressGesture = UILongPressGestureRecognizer.alloc().initWithTargetAction(this.reorderLongPressHandler, 'longPress');
257
- this.nativeViewProtected.addGestureRecognizer(this.reorderLongPressGesture);
258
- }
259
- else {
260
- this.reorderLongPressGesture.enabled = true;
261
- }
262
- }
263
- else {
264
- if (this.reorderLongPressGesture) {
265
- this.reorderLongPressGesture.enabled = false;
266
- }
267
- }
268
- }
269
- [reorderingEnabledProperty.setNative](value) {
270
- if (value) {
271
- this.on('touch', this.onReorderingTouch, this);
272
- }
273
- else {
274
- this.off('touch', this.onReorderingTouch, this);
275
- }
276
- }
277
- [scrollBarIndicatorVisibleProperty.getDefault]() {
278
- return true;
279
- }
280
- [scrollBarIndicatorVisibleProperty.setNative](value) {
281
- this.updateScrollBarVisibility(value);
282
- }
283
- updateScrollBarVisibility(value) {
284
- if (!this.nativeViewProtected) {
285
- return;
286
- }
287
- if (this.orientation === 'horizontal') {
288
- this.nativeViewProtected.showsHorizontalScrollIndicator = value;
289
- }
290
- else {
291
- this.nativeViewProtected.showsVerticalScrollIndicator = value;
292
- }
293
- }
294
- eachChildView(callback) {
295
- this._map.forEach((view, key) => {
296
- callback(view);
297
- });
298
- }
299
- onLayout(left, top, right, bottom) {
300
- super.onLayout(left, top, right, bottom);
301
- const p = CollectionViewBase.plugins[this.layoutStyle];
302
- if (p && p.onLayout) {
303
- p.onLayout(this, left, top, right, bottom);
304
- }
305
- this.plugins.forEach((k) => {
306
- const p = CollectionViewBase.plugins[k];
307
- p.onLayout && p.onLayout(this, left, top, right, bottom);
308
- });
309
- const layoutView = this.nativeViewProtected.collectionViewLayout;
310
- if (!layoutView) {
311
- return;
312
- }
313
- if ((layoutView instanceof UICollectionViewFlowLayout && this._effectiveColWidth) || this._effectiveRowHeight) {
314
- layoutView.estimatedItemSize = layoutView.itemSize = CGSizeMake(layout.toDeviceIndependentPixels(this._effectiveColWidth), layout.toDeviceIndependentPixels(this._effectiveRowHeight));
315
- }
316
- layoutView.invalidateLayout();
317
- const layoutKey = this._innerWidth + '_' + this._innerHeight;
318
- if (this._lastLayoutKey !== layoutKey) {
319
- this.refresh();
320
- }
321
- }
322
- isHorizontal() {
323
- return this.orientation === 'horizontal';
324
- }
325
- onSourceCollectionChanged(event) {
326
- const view = this.nativeViewProtected;
327
- if (!view || this._dataUpdatesSuspended) {
328
- return;
329
- }
330
- if (Trace.isEnabled()) {
331
- CLog(CLogTypes.log, 'onItemsChanged', ChangeType.Update, event.action, event.index, event.addedCount, event.removed && event.removed.length);
332
- }
333
- this.clearCellSize();
334
- switch (event.action) {
335
- case ChangeType.Delete: {
336
- const indexes = NSMutableArray.new();
337
- for (let index = 0; index < event.addedCount; index++) {
338
- indexes.addObject(NSIndexPath.indexPathForRowInSection(event.index + index, 0));
339
- }
340
- this.unbindUnusedCells(event.removed);
341
- if (Trace.isEnabled()) {
342
- CLog(CLogTypes.info, 'deleteItemsAtIndexPaths', indexes.count);
343
- }
344
- view.performBatchUpdatesCompletion(() => {
345
- view.deleteItemsAtIndexPaths(indexes);
346
- }, null);
347
- return;
348
- }
349
- case ChangeType.Update: {
350
- const indexes = NSMutableArray.new();
351
- indexes.addObject(NSIndexPath.indexPathForRowInSection(event.index, 0));
352
- if (Trace.isEnabled()) {
353
- CLog(CLogTypes.info, 'reloadItemsAtIndexPaths', event.index, indexes.count);
354
- }
355
- UIView.performWithoutAnimation(() => {
356
- view.performBatchUpdatesCompletion(() => {
357
- view.reloadItemsAtIndexPaths(indexes);
358
- }, null);
359
- });
360
- return;
361
- }
362
- case ChangeType.Add: {
363
- const indexes = NSMutableArray.new();
364
- for (let index = 0; index < event.addedCount; index++) {
365
- indexes.addObject(NSIndexPath.indexPathForRowInSection(event.index + index, 0));
366
- }
367
- if (Trace.isEnabled()) {
368
- CLog(CLogTypes.info, 'insertItemsAtIndexPaths', indexes.count);
369
- }
370
- view.performBatchUpdatesCompletion(() => {
371
- view.insertItemsAtIndexPaths(indexes);
372
- }, null);
373
- return;
374
- }
375
- case ChangeType.Splice: {
376
- view.performBatchUpdatesCompletion(() => {
377
- const added = event.addedCount;
378
- const removed = (event.removed && event.removed.length) || 0;
379
- if (added > 0 && added === removed) {
380
- const indexes = NSMutableArray.new();
381
- for (let index = 0; index < added; index++) {
382
- indexes.addObject(NSIndexPath.indexPathForRowInSection(event.index + index, 0));
383
- }
384
- view.reloadItemsAtIndexPaths(indexes);
385
- }
386
- else {
387
- if (event.addedCount > 0) {
388
- const indexes = NSMutableArray.alloc().init();
389
- for (let index = 0; index < event.addedCount; index++) {
390
- indexes.addObject(NSIndexPath.indexPathForItemInSection(event.index + index, 0));
391
- }
392
- view.insertItemsAtIndexPaths(indexes);
393
- }
394
- if (event.removed && event.removed.length > 0) {
395
- const indexes = NSMutableArray.new();
396
- for (let index = 0; index < event.removed.length; index++) {
397
- indexes.addObject(NSIndexPath.indexPathForItemInSection(event.index + index, 0));
398
- }
399
- this.unbindUnusedCells(event.removed);
400
- if (Trace.isEnabled()) {
401
- CLog(CLogTypes.info, 'deleteItemsAtIndexPaths', indexes.count);
402
- }
403
- view.performBatchUpdatesCompletion(() => {
404
- view.deleteItemsAtIndexPaths(indexes);
405
- }, null);
406
- }
407
- }
408
- }, null);
409
- return;
410
- }
411
- }
412
- this.refresh();
413
- }
414
- onItemTemplatesChanged(oldValue, newValue) {
415
- super.onItemTemplatesChanged(oldValue, newValue);
416
- if (!this.nativeViewProtected) {
417
- return;
418
- }
419
- const view = this.nativeViewProtected;
420
- this._itemTemplatesInternal.forEach((t) => {
421
- view.registerClassForCellWithReuseIdentifier(CollectionViewCell.class(), t.key.toLowerCase());
422
- });
423
- }
424
- unbindUnusedCells(removedDataItems) {
425
- this._map.forEach((view, nativeView, map) => {
426
- if (!view || !view.bindingContext) {
427
- return;
428
- }
429
- if (removedDataItems.indexOf(view.bindingContext) !== -1) {
430
- view.bindingContext = undefined;
431
- }
432
- }, this);
433
- }
434
- refreshVisibleItems() {
435
- const view = this.nativeViewProtected;
436
- if (!view) {
437
- return;
438
- }
439
- const visibles = view.indexPathsForVisibleItems;
440
- UIView.performWithoutAnimation(() => {
441
- view.performBatchUpdatesCompletion(() => {
442
- view.reloadItemsAtIndexPaths(visibles);
443
- }, null);
444
- });
445
- }
446
- isItemAtIndexVisible(itemIndex) {
447
- const view = this.nativeViewProtected;
448
- if (!view) {
449
- return false;
450
- }
451
- const indexes = Array.from(view.indexPathsForVisibleItems);
452
- return indexes.some((visIndex) => visIndex.row === itemIndex);
453
- }
454
- refresh() {
455
- if (!this.isLoaded || !this.nativeView) {
456
- this._isDataDirty = true;
457
- return;
458
- }
459
- this._isDataDirty = false;
460
- this._lastLayoutKey = this._innerWidth + '_' + this._innerHeight;
461
- if (Trace.isEnabled()) {
462
- CLog(CLogTypes.info, 'refresh');
463
- }
464
- this.clearCellSize();
465
- this._map.forEach((view, nativeView, map) => {
466
- if (!(view.bindingContext instanceof Observable)) {
467
- view.bindingContext = null;
468
- }
469
- });
470
- this.nativeViewProtected.reloadData();
471
- const args = {
472
- eventName: CollectionViewBase.dataPopulatedEvent,
473
- object: this
474
- };
475
- this.notify(args);
476
- }
477
- get scrollOffset() {
478
- const view = this.nativeViewProtected;
479
- return (this.isHorizontal() ? view === null || view === void 0 ? void 0 : view.contentOffset.x : view === null || view === void 0 ? void 0 : view.contentOffset.y) || 0;
480
- }
481
- get verticalOffsetX() {
482
- var _a;
483
- return ((_a = this.nativeViewProtected) === null || _a === void 0 ? void 0 : _a.contentOffset.x) || 0;
484
- }
485
- get verticalOffsetY() {
486
- var _a;
487
- return ((_a = this.nativeViewProtected) === null || _a === void 0 ? void 0 : _a.contentOffset.y) || 0;
488
- }
489
- scrollToIndex(index, animated = true) {
490
- this.nativeViewProtected.scrollToItemAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), this.orientation === 'vertical' ? 1 : 8, animated);
491
- }
492
- requestLayout() {
493
- if (!this._preparingCell) {
494
- super.requestLayout();
495
- }
496
- }
497
- measure(widthMeasureSpec, heightMeasureSpec) {
498
- const changed = this._setCurrentMeasureSpecs(widthMeasureSpec, heightMeasureSpec);
499
- super.measure(widthMeasureSpec, heightMeasureSpec);
500
- if (changed && this.nativeView) {
501
- this.nativeView.reloadData();
502
- }
503
- }
504
- _setNativeClipToBounds() {
505
- this.nativeView.clipsToBounds = true;
506
- }
507
- notifyForItemAtIndex(listView, cell, view, eventName, indexPath, bindingContext) {
508
- const args = { eventName, object: listView, index: indexPath.row, view, ios: cell, bindingContext };
509
- listView.notify(args);
510
- return args;
511
- }
512
- _getItemTemplateType(indexPath) {
513
- const selector = this._itemTemplateSelector;
514
- let type = this._defaultTemplate.key;
515
- if (selector) {
516
- type = selector(this.getItemAtIndex(indexPath.item), indexPath.item, this.items);
517
- }
518
- return type.toLowerCase();
519
- }
520
- getItemTemplateContent(index, templateType) {
521
- return this.getViewForViewType(ListViewViewTypes.ItemView, templateType);
522
- }
523
- _prepareCell(cell, indexPath, templateType, addToMap = true) {
524
- let cellSize;
525
- try {
526
- this._preparingCell = true;
527
- let view = cell.view;
528
- const index = indexPath.row;
529
- let needsLayout = false;
530
- if (!view) {
531
- needsLayout = true;
532
- view = this.getItemTemplateContent(index, templateType);
533
- }
534
- const oldBindingContext = view && view.bindingContext;
535
- const bindingContext = this._prepareItem(view, index);
536
- needsLayout = needsLayout || oldBindingContext !== bindingContext;
537
- if (Trace.isEnabled()) {
538
- CLog(CLogTypes.log, '_prepareCell', index, !!cell.view, !!view, cell.view !== view, needsLayout);
539
- }
540
- const args = this.notifyForItemAtIndex(this, cell, view, CollectionViewBase.itemLoadingEvent, indexPath, bindingContext);
541
- view = args.view;
542
- if (view instanceof ProxyViewContainer) {
543
- const sp = new ContentView();
544
- sp.content = view;
545
- view = sp;
546
- }
547
- if (!cell.view) {
548
- cell.owner = new WeakRef(view);
549
- }
550
- else if (cell.view !== view) {
551
- this._removeContainer(cell);
552
- cell.view.nativeViewProtected.removeFromSuperview();
553
- cell.owner = new WeakRef(view);
554
- }
555
- if (addToMap) {
556
- this._map.set(cell, view);
557
- }
558
- if (view && !view.parent) {
559
- this._addView(view);
560
- const innerView = NSCellView.new();
561
- innerView.view = new WeakRef(view);
562
- innerView.addSubview(view.nativeViewProtected);
563
- cell.contentView.addSubview(innerView);
564
- }
565
- cellSize = this.measureCell(cell, view, indexPath);
566
- if (Trace.isEnabled()) {
567
- CLog(CLogTypes.log, '_prepareCell done', index, cellSize);
568
- }
569
- }
570
- finally {
571
- this._preparingCell = false;
572
- }
573
- return cellSize;
574
- }
575
- getCellSize(index) {
576
- let result = this._sizes[index];
577
- if (!result) {
578
- let width = this._effectiveColWidth;
579
- let height = this._effectiveRowHeight;
580
- if (this.spanSize) {
581
- const dataItem = this.getItemAtIndex(index);
582
- const spanSize = this.spanSize(dataItem, index);
583
- const horizontal = this.isHorizontal();
584
- if (horizontal) {
585
- height *= spanSize;
586
- }
587
- else {
588
- width *= spanSize;
589
- }
590
- }
591
- if (width && height) {
592
- result = [width, height];
593
- }
594
- else if (height && this.orientation === 'vertical') {
595
- result = [this.getMeasuredWidth(), height];
596
- }
597
- else if (width && this.orientation === 'horizontal') {
598
- result = [width, this.getMeasuredHeight()];
599
- }
600
- }
601
- return result;
602
- }
603
- storeCellSize(index, value) {
604
- this._sizes[index] = value;
605
- }
606
- clearCellSize() {
607
- this._sizes = new Array();
608
- }
609
- measureCell(cell, cellView, index) {
610
- if (cellView) {
611
- let width = this._effectiveColWidth;
612
- let height = this._effectiveRowHeight;
613
- const horizontal = this.isHorizontal();
614
- if (this.spanSize) {
615
- const position = index.row;
616
- const dataItem = this.getItemAtIndex(position);
617
- const spanSize = this.spanSize(dataItem, position);
618
- if (horizontal) {
619
- height *= spanSize;
620
- }
621
- else {
622
- width *= spanSize;
623
- }
624
- }
625
- const widthMeasureSpec = width ? layout.makeMeasureSpec(width, layout.EXACTLY) : horizontal ? infinity : layout.makeMeasureSpec(this._innerWidth, layout.UNSPECIFIED);
626
- const heightMeasureSpec = height ? layout.makeMeasureSpec(height, layout.EXACTLY) : horizontal ? layout.makeMeasureSpec(this._innerHeight, layout.UNSPECIFIED) : infinity;
627
- if (Trace.isEnabled()) {
628
- CLog(CLogTypes.log, 'measureCell', width, height, widthMeasureSpec, heightMeasureSpec);
629
- }
630
- const measuredSize = View.measureChild(this, cellView, widthMeasureSpec, heightMeasureSpec);
631
- const result = [measuredSize.measuredWidth, measuredSize.measuredHeight];
632
- this.storeCellSize(index.row, result);
633
- return result;
634
- }
635
- return undefined;
636
- }
637
- layoutCell(index, cell, cellView) {
638
- const cellSize = this.getCellSize(index);
639
- cellView['iosIgnoreSafeArea'] = true;
640
- View.layoutChild(this, cellView, 0, 0, cellSize[0], cellSize[1]);
641
- if (Trace.isEnabled()) {
642
- CLog(CLogTypes.log, 'layoutCell', index, cellView.getMeasuredWidth(), cellView.getMeasuredHeight());
643
- }
644
- }
645
- clearRealizedCells() {
646
- const that = new WeakRef(this);
647
- this._map.forEach(function (value, key) {
648
- that.get()._removeContainer(key);
649
- that.get()._clearCellViews(key);
650
- }, that);
651
- this._map.clear();
652
- }
653
- _clearCellViews(cell) {
654
- if (cell && cell.view) {
655
- if (cell.view.nativeViewProtected) {
656
- cell.view.nativeViewProtected.removeFromSuperview();
657
- }
658
- cell.owner = undefined;
659
- }
660
- }
661
- _removeContainer(cell) {
662
- const view = cell.view;
663
- if (!(view.parent instanceof CollectionView)) {
664
- this._removeView(view.parent);
665
- }
666
- const preparing = this._preparingCell;
667
- this._preparingCell = true;
668
- view.parent._removeView(view);
669
- this._preparingCell = preparing;
670
- this._map.delete(cell);
671
- }
672
- _setPadding(newPadding) {
673
- const layout = this._layout;
674
- const padding = {
675
- top: layout['sectionInset'].top,
676
- right: layout['sectionInset'].right,
677
- bottom: layout['sectionInset'].bottom,
678
- left: layout['sectionInset'].left
679
- };
680
- const newValue = Object.assign(padding, newPadding);
681
- layout['sectionInset'] = newValue;
682
- }
683
- numberOfSectionsInCollectionView(collectionView) {
684
- return 1;
685
- }
686
- collectionViewNumberOfItemsInSection(collectionView, section) {
687
- var _a;
688
- return ((_a = this.items) === null || _a === void 0 ? void 0 : _a.length) || 0;
689
- }
690
- collectionViewCellForItemAtIndexPath(collectionView, indexPath) {
691
- const templateType = this._getItemTemplateType(indexPath);
692
- let cell = collectionView.dequeueReusableCellWithReuseIdentifierForIndexPath(templateType, indexPath);
693
- if (!cell) {
694
- cell = CollectionViewCell.new();
695
- }
696
- if (Trace.isEnabled()) {
697
- CLog(CLogTypes.log, 'collectionViewCellForItemAtIndexPath', indexPath.row, templateType, !!cell.view, cell);
698
- }
699
- this._prepareCell(cell, indexPath, templateType);
700
- const cellView = cell.view;
701
- if (cellView['isLayoutRequired']) {
702
- this.layoutCell(indexPath.row, cell, cellView);
703
- }
704
- return cell;
705
- }
706
- collectionViewWillDisplayCellForItemAtIndexPath(collectionView, cell, indexPath) {
707
- if (this.reverseLayout) {
708
- cell.transform = CGAffineTransformMakeRotation(-Math.PI);
709
- }
710
- if (this.items) {
711
- const loadMoreItemIndex = this.items.length - this.loadMoreThreshold;
712
- if (indexPath.row === loadMoreItemIndex && this.hasListeners(CollectionViewBase.loadMoreItemsEvent)) {
713
- this.notify({
714
- eventName: CollectionViewBase.loadMoreItemsEvent,
715
- object: this
716
- });
717
- }
718
- }
719
- if (cell.preservesSuperviewLayoutMargins) {
720
- cell.preservesSuperviewLayoutMargins = false;
721
- }
722
- if (cell.layoutMargins) {
723
- cell.layoutMargins = UIEdgeInsetsZero;
724
- }
725
- }
726
- collectionViewDidSelectItemAtIndexPath(collectionView, indexPath) {
727
- const cell = collectionView.cellForItemAtIndexPath(indexPath);
728
- const position = indexPath.row;
729
- this.notify({
730
- eventName: CollectionViewBase.itemTapEvent,
731
- object: this,
732
- item: this.getItemAtIndex(position),
733
- index: position,
734
- view: cell.view
735
- });
736
- cell.highlighted = false;
737
- return indexPath;
738
- }
739
- collectionViewLayoutSizeForItemAtIndexPath(collectionView, collectionViewLayout, indexPath) {
740
- const row = indexPath.row;
741
- let measuredSize = this.getCellSize(row);
742
- if (!measuredSize) {
743
- if (Trace.isEnabled()) {
744
- CLog(CLogTypes.log, 'collectionViewLayoutSizeForItemAtIndexPath', row);
745
- }
746
- const templateType = this._getItemTemplateType(indexPath);
747
- if (templateType) {
748
- const measureData = this._measureCellMap.get(templateType);
749
- let cell = measureData && measureData.cell;
750
- if (!cell) {
751
- cell = CollectionViewCell.new();
752
- }
753
- else if (!cell.view) {
754
- cell.owner = new WeakRef(measureData.view);
755
- }
756
- measuredSize = this._prepareCell(cell, indexPath, templateType, false);
757
- this._measureCellMap.set(templateType, { cell, view: cell.view });
758
- }
759
- }
760
- if (Trace.isEnabled()) {
761
- CLog(CLogTypes.log, 'collectionViewLayoutSizeForItemAtIndexPath', row, measuredSize);
762
- }
763
- if (measuredSize) {
764
- return CGSizeMake(layout.toDeviceIndependentPixels(measuredSize[0]), layout.toDeviceIndependentPixels(measuredSize[1]));
765
- }
766
- return CGSizeZero;
767
- }
768
- computeScrollEventData(scrollView, eventName, dx, dy) {
769
- const horizontal = this.isHorizontal();
770
- const safeAreaInsetsTop = this.iosIgnoreSafeArea ? 0 : scrollView.safeAreaInsets.top;
771
- const offset = horizontal ? scrollView.contentOffset.x : scrollView.contentOffset.y + safeAreaInsetsTop;
772
- const size = horizontal ? scrollView.contentSize.width - scrollView.bounds.size.width : scrollView.contentSize.height - scrollView.bounds.size.height + safeAreaInsetsTop;
773
- return {
774
- object: this,
775
- eventName,
776
- scrollOffset: offset,
777
- scrollOffsetPercentage: offset / size,
778
- dx,
779
- dy: dy + safeAreaInsetsTop
780
- };
781
- }
782
- scrollViewWillBeginDragging(scrollView) {
783
- this.lastContentOffset = scrollView.contentOffset;
784
- this.needsScrollStartEvent = true;
785
- this.isScrolling = true;
786
- }
787
- scrollViewDidScroll(scrollView) {
788
- const contentOffset = scrollView.contentOffset;
789
- const dx = contentOffset.x - this.lastContentOffset.x;
790
- const dy = contentOffset.y - this.lastContentOffset.y;
791
- this.lastContentOffset = scrollView.contentOffset;
792
- if (this.needsScrollStartEvent) {
793
- this.needsScrollStartEvent = false;
794
- if (this.hasListeners(CollectionViewBase.scrollStartEvent)) {
795
- this.notify(this.computeScrollEventData(scrollView, CollectionViewBase.scrollStartEvent, dx, dy));
796
- }
797
- }
798
- this.notify(this.computeScrollEventData(scrollView, CollectionViewBase.scrollEvent, dx, dy));
799
- }
800
- stopScrolling(scrollView) {
801
- if (this.isScrolling) {
802
- this.isScrolling = false;
803
- this.notify(this.computeScrollEventData(scrollView, CollectionViewBase.scrollEndEvent));
804
- }
805
- }
806
- scrollViewDidEndDecelerating(scrollView) {
807
- this.stopScrolling(scrollView);
808
- }
809
- scrollViewWillEndDraggingWithVelocityTargetContentOffset(scrollView, velocity, targetContentOffset) {
810
- this.stopScrolling(scrollView);
811
- }
812
- scrollViewDidEndDraggingWillDecelerate(scrollView, decelerate) {
813
- this.stopScrolling(scrollView);
814
- }
815
- scrollViewDidEndScrollingAnimation(scrollView) {
816
- this.stopScrolling(scrollView);
817
- }
818
- }
819
- __decorate([
820
- profile
821
- ], CollectionView.prototype, "refresh", null);
822
- contentInsetAdjustmentBehaviorProperty.register(CollectionView);
823
- var NSCellView = /** @class */ (function (_super) {
824
- __extends(NSCellView, _super);
825
- function NSCellView() {
826
- return _super !== null && _super.apply(this, arguments) || this;
827
- }
828
- NSCellView.prototype.layoutSubviews = function () {
829
- _super.prototype.layoutSubviews.call(this);
830
- var view = this.view && this.view.get();
831
- if (!view) {
832
- return;
833
- }
834
- this.frame = this.superview.bounds;
835
- var size = this.bounds.size;
836
- View.layoutChild(null, view, 0, 0, Utils.layout.toDevicePixels(size.width), Utils.layout.toDevicePixels(size.height));
837
- };
838
- return NSCellView;
839
- }(UIView));
840
- var CollectionViewCell = /** @class */ (function (_super) {
841
- __extends(CollectionViewCell, _super);
842
- function CollectionViewCell() {
843
- return _super !== null && _super.apply(this, arguments) || this;
844
- }
845
- Object.defineProperty(CollectionViewCell.prototype, "view", {
846
- get: function () {
847
- return this.owner ? this.owner.get() : null;
848
- },
849
- enumerable: true,
850
- configurable: true
851
- });
852
- return CollectionViewCell;
853
- }(UICollectionViewCell));
854
- var CollectionViewDataSource = /** @class */ (function (_super) {
855
- __extends(CollectionViewDataSource, _super);
856
- function CollectionViewDataSource() {
857
- return _super !== null && _super.apply(this, arguments) || this;
858
- }
859
- CollectionViewDataSource.initWithOwner = function (owner) {
860
- var delegate = CollectionViewDataSource.new();
861
- delegate._owner = new WeakRef(owner);
862
- return delegate;
863
- };
864
- CollectionViewDataSource.prototype.numberOfSectionsInCollectionView = function (collectionView) {
865
- var owner = this._owner.get();
866
- if (owner) {
867
- return owner.numberOfSectionsInCollectionView(collectionView);
868
- }
869
- return 0;
870
- };
871
- CollectionViewDataSource.prototype.collectionViewNumberOfItemsInSection = function (collectionView, section) {
872
- var owner = this._owner.get();
873
- if (owner) {
874
- return owner.collectionViewNumberOfItemsInSection(collectionView, section);
875
- }
876
- return 0;
877
- };
878
- CollectionViewDataSource.prototype.collectionViewCellForItemAtIndexPath = function (collectionView, indexPath) {
879
- var owner = this._owner.get();
880
- if (owner) {
881
- return owner.collectionViewCellForItemAtIndexPath(collectionView, indexPath);
882
- }
883
- return null;
884
- };
885
- CollectionViewDataSource.prototype.collectionViewMoveItemAtIndexPathToIndexPath = function (collectionView, sourceIndexPath, destinationIndexPath) {
886
- var owner = this._owner.get();
887
- if (owner) {
888
- owner.reorderStartingRow = sourceIndexPath.row;
889
- owner.reorderEndingRow = destinationIndexPath.row;
890
- owner._reorderItemInSource(sourceIndexPath.row, destinationIndexPath.row, false);
891
- }
892
- };
893
- CollectionViewDataSource.prototype.collectionViewTargetIndexPathForMoveFromItemAtIndexPathToProposedIndexPath = function (collectionView, originalIndexPath, proposedIndexPath) {
894
- var owner = this._owner.get();
895
- if (owner) {
896
- owner.reorderEndingRow = proposedIndexPath.row;
897
- }
898
- return proposedIndexPath;
899
- };
900
- CollectionViewDataSource.prototype.collectionViewCanMoveItemAtIndexPath = function (collectionView, indexPath) {
901
- var owner = this._owner.get();
902
- if (owner) {
903
- var result = owner.shouldMoveItemAtIndex(indexPath.row);
904
- if (result) {
905
- owner.reorderStartingRow = indexPath.row;
906
- }
907
- return result;
908
- }
909
- return false;
910
- };
911
- CollectionViewDataSource.ObjCProtocols = [UICollectionViewDataSource];
912
- return CollectionViewDataSource;
913
- }(NSObject));
914
- var UICollectionViewDelegateImpl = /** @class */ (function (_super) {
915
- __extends(UICollectionViewDelegateImpl, _super);
916
- function UICollectionViewDelegateImpl() {
917
- return _super !== null && _super.apply(this, arguments) || this;
918
- }
919
- UICollectionViewDelegateImpl.initWithOwner = function (owner) {
920
- var delegate = UICollectionViewDelegateImpl.new();
921
- delegate._owner = new WeakRef(owner);
922
- return delegate;
923
- };
924
- UICollectionViewDelegateImpl.prototype.collectionViewWillDisplayCellForItemAtIndexPath = function (collectionView, cell, indexPath) {
925
- var owner = this._owner.get();
926
- if (owner) {
927
- owner.collectionViewWillDisplayCellForItemAtIndexPath(collectionView, cell, indexPath);
928
- }
929
- };
930
- UICollectionViewDelegateImpl.prototype.collectionViewDidSelectItemAtIndexPath = function (collectionView, indexPath) {
931
- var owner = this._owner.get();
932
- if (owner) {
933
- return owner.collectionViewDidSelectItemAtIndexPath(collectionView, indexPath);
934
- }
935
- return indexPath;
936
- };
937
- UICollectionViewDelegateImpl.prototype.collectionViewLayoutSizeForItemAtIndexPath = function (collectionView, collectionViewLayout, indexPath) {
938
- var owner = this._owner.get();
939
- if (owner) {
940
- return owner.collectionViewLayoutSizeForItemAtIndexPath(collectionView, collectionViewLayout, indexPath);
941
- }
942
- return CGSizeZero;
943
- };
944
- UICollectionViewDelegateImpl.prototype.scrollViewDidScroll = function (scrollView) {
945
- var owner = this._owner.get();
946
- if (owner) {
947
- owner.scrollViewDidScroll(scrollView);
948
- }
949
- };
950
- UICollectionViewDelegateImpl.prototype.scrollViewWillBeginDragging = function (scrollView) {
951
- var owner = this._owner.get();
952
- if (owner) {
953
- owner.scrollViewWillBeginDragging(scrollView);
954
- }
955
- };
956
- UICollectionViewDelegateImpl.prototype.scrollViewDidEndDecelerating = function (scrollView) {
957
- var owner = this._owner.get();
958
- if (owner) {
959
- owner.scrollViewDidEndDecelerating(scrollView);
960
- }
961
- };
962
- UICollectionViewDelegateImpl.prototype.scrollViewWillEndDraggingWithVelocityTargetContentOffset = function (scrollView, velocity, targetContentOffset) {
963
- var owner = this._owner.get();
964
- if (owner) {
965
- owner.scrollViewWillEndDraggingWithVelocityTargetContentOffset(scrollView, velocity, targetContentOffset);
966
- }
967
- };
968
- UICollectionViewDelegateImpl.prototype.scrollViewDidEndDraggingWillDecelerate = function (scrollView, decelerate) {
969
- var owner = this._owner.get();
970
- if (owner) {
971
- owner.scrollViewDidEndDraggingWillDecelerate(scrollView, decelerate);
972
- }
973
- };
974
- UICollectionViewDelegateImpl.prototype.scrollViewDidEndScrollingAnimation = function (scrollView) {
975
- var owner = this._owner.get();
976
- if (owner) {
977
- owner.scrollViewDidEndScrollingAnimation(scrollView);
978
- }
979
- };
980
- UICollectionViewDelegateImpl.ObjCProtocols = [UICollectionViewDelegate, UICollectionViewDelegateFlowLayout];
981
- return UICollectionViewDelegateImpl;
982
- }(NSObject));
983
- var ReorderLongPressImpl = /** @class */ (function (_super) {
984
- __extends(ReorderLongPressImpl, _super);
985
- function ReorderLongPressImpl() {
986
- return _super !== null && _super.apply(this, arguments) || this;
987
- }
988
- ReorderLongPressImpl.initWithOwner = function (owner) {
989
- var handler = ReorderLongPressImpl.new();
990
- handler._owner = owner;
991
- return handler;
992
- };
993
- ReorderLongPressImpl.prototype.longPress = function (recognizer) {
994
- var owner = this._owner && this._owner.get();
995
- if (owner) {
996
- owner.onReorderLongPress(recognizer);
997
- }
998
- };
999
- ReorderLongPressImpl.ObjCExposedMethods = {
1000
- longPress: { returns: interop.types.void, params: [interop.types.id] }
1001
- };
1002
- return ReorderLongPressImpl;
1003
- }(NSObject));
1004
- //# sourceMappingURL=collectionview.ios.js.map