@triniwiz/nativescript-pager
Version:
A Carousel/Pager plugin for NativeScript
1,109 lines • 44.2 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Color, ObservableArray, profile, ProxyViewContainer, StackLayout, Utils, View, } from "@nativescript/core";
import * as common from "./common";
import { autoplayDelayProperty, autoPlayProperty, disableSwipeProperty, Indicator, indicatorColorProperty, indicatorProperty, indicatorSelectedColorProperty, ITEMDISPOSING, ITEMLOADING, itemsProperty, itemTemplatesProperty, LOADMOREITEMS, orientationProperty, PagerBase, selectedIndexProperty, showIndicatorProperty, } from "./common";
export * from "./common";
export { Transformer } from "./common";
function notifyForItemAtIndex(owner, nativeView, view, eventName, index) {
let args = {
eventName: eventName,
object: owner,
index: index,
view: view,
ios: nativeView,
android: undefined,
};
owner.notify(args);
return args;
}
const main_queue = dispatch_get_current_queue();
const PFLAG_FORCE_LAYOUT = 1;
export class Pager extends PagerBase {
constructor() {
super();
this.lastEvent = 0;
this._disableSwipe = false;
this._disableAnimation = false;
this._preparingCell = false;
this._isDirty = false;
this._isRefreshing = false;
this._isInit = false;
this._observableArrayHandler = (args) => {
if (!this.pager) {
return;
}
if (this.indicatorView &&
this._observableArrayInstance &&
this._observableArrayInstance.length) {
this.indicatorView.numberOfPages = this._observableArrayInstance.length;
}
const collectionView = this.pager;
if (collectionView) {
// @ts-ignore
PagerUtils.updateCollection({
action: args.action,
index: args.index,
addedCount: args.addedCount,
removedCount: args.removed ? args.removed.length : 0
}, collectionView);
}
};
this._map = new Map();
this._childrenViews = new Map();
}
get pager() {
return this._pager;
}
get indicatorView() {
return this._indicatorView;
}
createNativeView() {
const nativeView = UIView.new();
this._layout = UICollectionViewFlowLinearLayoutImpl.initWithOwner(new WeakRef(this));
this._layout.scrollDirection =
1 /* UICollectionViewScrollDirection.Horizontal */;
this._layout.minimumInteritemSpacing = 0;
this._pager = UICollectionView.alloc().initWithFrameCollectionViewLayout(CGRectZero, this._layout);
this.pager.showsHorizontalScrollIndicator = false;
this.pager.showsVerticalScrollIndicator = false;
this.pager.decelerationRate = UIScrollViewDecelerationRateFast;
nativeView.addSubview(this.pager);
return nativeView;
}
initNativeView() {
super.initNativeView();
const nativeView = this.pager;
nativeView.registerClassForCellWithReuseIdentifier(PagerCell.class(), this._defaultTemplate.key);
nativeView.backgroundColor = UIColor.clearColor;
nativeView.autoresizesSubviews = false;
nativeView.autoresizingMask = 0 /* UIViewAutoresizing.None */;
nativeView.dataSource = this._dataSource = UICollectionViewDataSourceImpl.initWithOwner(new WeakRef(this));
nativeView.scrollEnabled = !(String(this.disableSwipe) === "true");
if (this.orientation === "vertical") {
this._layout.scrollDirection =
0 /* UICollectionViewScrollDirection.Vertical */;
nativeView.alwaysBounceVertical = true;
nativeView.alwaysBounceHorizontal = false;
}
else {
this._layout.scrollDirection =
1 /* UICollectionViewScrollDirection.Horizontal */;
nativeView.alwaysBounceHorizontal = true;
nativeView.alwaysBounceVertical = false;
}
this._setIndicator(this.indicator);
this._delegate = UICollectionDelegateImpl.initWithOwner(new WeakRef(this));
this._setNativeClipToBounds();
this._initAutoPlay(this.autoPlay);
}
_getRealWidthHeight() {
let height = 0;
let width = 0;
width =
(Utils.layout.toDeviceIndependentPixels(this._effectiveItemWidth) -
(this.perPage * 2 * this._getSpacing() +
this._getPeaking() * 2)) /
this.perPage;
height =
(Utils.layout.toDeviceIndependentPixels(this._effectiveItemHeight) -
(this.perPage * 2 * this._getSpacing() +
this._getPeaking() * 2)) /
this.perPage;
return { height, width };
}
_nextIndex() {
if (this.circularMode) {
// TODO
return 0;
}
else {
let next = this.selectedIndex + 1;
if (next > this.lastIndex) {
return 0;
}
return next;
}
}
_initAutoPlay(value) {
if (!this.items || this.items.length === 0) {
return;
}
if (!value) {
if (this._autoPlayInterval) {
clearInterval(this._autoPlayInterval);
this._autoPlayInterval = undefined;
}
}
else {
if (this.isLayoutValid && !this._autoPlayInterval) {
this._autoPlayInterval = setInterval(() => {
this.selectedIndex = this._nextIndex();
}, this.autoPlayDelay);
}
}
}
getPosition(index) {
let position = index;
if (this.circularMode) {
if (position === 0) {
position = this.lastDummy;
}
else if (position === this.firstDummy) {
position = 0;
}
else {
position = position - 1;
}
}
return position;
}
get itemCount() {
return this._childrenCount
? this._childrenCount + (this.circularMode ? 2 : 0)
: 0;
}
get lastIndex() {
if (this.items && this.items.length === 0) {
return 0;
}
return this.circularMode ? this.itemCount - 3 : this.itemCount - 1;
}
get firstDummy() {
const count = this.itemCount;
if (count === 0) {
return 0;
}
return this.itemCount - 1;
}
get lastDummy() {
return this.lastIndex;
}
// @ts-ignore
get ios() {
return this.nativeView;
}
_setIndicator(value) {
if (this._indicatorView) {
this._indicatorView.removeFromSuperview();
}
switch (value) {
case Indicator.None:
this._indicatorView = CHIPageControlAji.new();
break;
case Indicator.Worm:
this._indicatorView = CHIPageControlAleppo.new();
break;
case Indicator.Fill:
this._indicatorView = CHIPageControlChimayo.new();
break;
case Indicator.Swap:
this._indicatorView = CHIPageControlPuya.new();
break;
case Indicator.THIN_WORM:
this._indicatorView = CHIPageControlJalapeno.new();
break;
case Indicator.Flat:
this._indicatorView = CHIPageControlJaloro.new();
break;
default:
break;
}
this._indicatorView.tintColor = UIColor.whiteColor;
this._indicatorView.currentPageTintColor = UIColor.whiteColor;
}
get _childrenCount() {
return this.items
? this.items.length
: this._childrenViews
? this._childrenViews.size
: 0;
}
itemTemplateUpdated(oldData, newData) { }
_setNativeClipToBounds() {
this.pager.clipsToBounds = true;
}
[orientationProperty.setNative](value) {
if (value === "horizontal") {
this._layout.scrollDirection =
1 /* UICollectionViewScrollDirection.Horizontal */;
}
else {
this._layout.scrollDirection =
0 /* UICollectionViewScrollDirection.Vertical */;
}
}
eachChildView(callback) {
this._map.forEach((view, key) => {
callback(view);
});
}
_updateScrollPosition() {
const view = this.pager;
const size = this.orientation === "vertical"
? view.contentSize.height
: view.contentSize.width;
if (!view || size === 0) {
return;
}
this._scrollToIndexAnimated(this.selectedIndex, false);
}
[selectedIndexProperty.setNative](value) {
if (this.isLoaded) {
this.scrollToIndexAnimated(value, !this.disableAnimation);
}
}
[itemTemplatesProperty.getDefault]() {
return null;
}
[itemTemplatesProperty.setNative](value) {
this._itemTemplatesInternal = new Array(this._defaultTemplate);
if (value) {
for (let i = 0, length = value.length; i < length; i++) {
this.pager.registerClassForCellWithReuseIdentifier(PagerCell.class(), value[i].key);
}
this._itemTemplatesInternal = this._itemTemplatesInternal.concat(value);
}
}
[itemsProperty.setNative](value) {
if (this.indicatorView && value && value.length) {
this.indicatorView.numberOfPages = value.length;
}
// remove old instance
if (this._observableArrayInstance) {
this._observableArrayInstance.off(ObservableArray.changeEvent, this._observableArrayHandler);
this._observableArrayInstance = null;
}
if (value instanceof ObservableArray) {
this._observableArrayInstance = value;
this._observableArrayInstance.on(ObservableArray.changeEvent, this._observableArrayHandler);
}
else {
this.refresh();
}
if (!value) {
this._isInit = false;
}
selectedIndexProperty.coerce(this);
}
[autoPlayProperty.setNative](value) {
this._initAutoPlay(value);
}
[autoplayDelayProperty.setNative](value) {
if (this._autoPlayInterval) {
clearInterval(this._autoPlayInterval);
this._autoPlayInterval = undefined;
this._initAutoPlay(this.autoPlay);
}
}
[showIndicatorProperty.setNative](value) {
if (!this.indicatorView) {
this._setIndicator(this.indicatorView);
}
if (!this.nativeView) {
return;
}
this.indicatorView.center = CGPointMake(this.nativeView.center.x, this.nativeView.bounds.size.height -
this.indicatorView.intrinsicContentSize.height);
const hasParent = this.indicatorView.superview;
if (value) {
if (!hasParent) {
this.nativeView.addSubview(this.indicatorView);
this.nativeView.bringSubviewToFront(this.indicatorView);
}
}
else {
if (hasParent) {
this.indicatorView.removeFromSuperview();
}
}
}
_onItemsChanged(oldValue, newValue) { }
_scrollToIndexAnimated(index, animate) {
if (!this.pager)
return;
const contentSize = this.pager.contentSize;
const size = this.orientation === "vertical"
? contentSize.height
: contentSize.width;
if (size === 0) {
return;
}
if (this._childrenCount === 0) {
return;
}
let maxMinIndex = -1;
const max = this._childrenCount - 1;
if (index < 0) {
maxMinIndex = 0;
}
else if (index > max) {
maxMinIndex = max;
}
else {
maxMinIndex = index;
}
if (maxMinIndex === -1) {
maxMinIndex = 0;
}
dispatch_async(main_queue, () => {
this.pager.scrollToItemAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(maxMinIndex, 0), this.orientation === "vertical"
? 2 /* UICollectionViewScrollPosition.CenteredVertically */
: 16 /* UICollectionViewScrollPosition.CenteredHorizontally */, !!animate);
selectedIndexProperty.nativeValueChange(this, maxMinIndex);
});
}
scrollToIndexAnimated(index, animate) {
this._scrollToIndexAnimated(index, animate);
}
_reset() {
if (!this.pager) {
return;
}
this.pager.reloadData();
this.pager.collectionViewLayout.invalidateLayout();
this._updateScrollPosition();
}
_refresh() {
if (!this.pager) {
return;
}
if (this.items instanceof ObservableArray) {
this.pager.performBatchUpdatesCompletion(() => {
this._reset();
}, null);
}
else {
this._reset();
}
}
refresh() {
dispatch_async(main_queue, () => {
this._refresh();
});
}
onLoaded() {
super.onLoaded();
if (this.showIndicator && this.indicatorView) {
this.nativeView.addSubview(this.indicatorView);
this.nativeView.bringSubviewToFront(this.indicatorView);
}
if (!this._isDirty) {
this.refresh();
this._isDirty = true;
}
this.pager.delegate = this._delegate;
if (!this.items && this._childrenCount > 0) {
selectedIndexProperty.coerce(this);
this._updateScrollPosition();
}
}
onUnloaded() {
if (this.pager) {
this.pager.delegate = null;
}
super.onUnloaded();
}
disposeNativeView() {
this._delegate = null;
this._dataSource = null;
this._layout = null;
if (this._observableArrayInstance) {
this._observableArrayInstance.off(ObservableArray.changeEvent, this._observableArrayHandler);
this._observableArrayInstance = null;
}
super.disposeNativeView();
}
[indicatorProperty.setNative](value) {
this._setIndicator(value);
}
[indicatorColorProperty.setNative](value) {
if (this.indicatorView) {
if (value instanceof Color) {
this.indicatorView.tintColor = value.ios;
}
else if (Utils.isString(value)) {
this.indicatorView.tintColor = new Color(value).ios;
}
}
}
[indicatorSelectedColorProperty.setNative](value) {
if (this.indicatorView) {
if (value instanceof Color) {
this.indicatorView.currentPageTintColor = value.ios;
}
else if (Utils.isString(value)) {
this.indicatorView.currentPageTintColor = new Color(value).ios;
}
}
}
[disableSwipeProperty.setNative](value) {
if (this.pager) {
this.pager.scrollEnabled = !(String(value) === "true");
}
this._disableSwipe = String(value) === "true";
}
get disableAnimation() {
return this._disableAnimation;
}
set disableAnimation(value) {
this._disableAnimation = value;
}
_removeContainer(cell, indexPath) {
let view = cell.view;
let args = {
eventName: ITEMDISPOSING,
object: this,
index: indexPath.row,
android: undefined,
ios: cell,
view: view,
};
this.notify(args);
view = args.view;
if (view && view.parent) {
// This is to clear the StackLayout that is used to wrap ProxyViewContainer instances.
if (!(view.parent instanceof Pager)) {
this._removeView(view.parent);
}
view.parent._removeView(view);
}
this._map.delete(cell);
}
measure(widthMeasureSpec, heightMeasureSpec) {
const changed = this._setCurrentMeasureSpecs(widthMeasureSpec, heightMeasureSpec);
super.measure(widthMeasureSpec, heightMeasureSpec);
// @ts-ignore
let forceLayout = (this._privateFlags & PFLAG_FORCE_LAYOUT) === PFLAG_FORCE_LAYOUT;
if (changed) {
dispatch_async(main_queue, () => {
if (!this.pager) {
return;
}
if (changed) {
this._updateScrollPosition();
}
this._initAutoPlay(this.autoPlay);
});
}
}
onMeasure(widthMeasureSpec, heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this._map.forEach((childView, pagerCell) => {
View.measureChild(this, childView, childView._currentWidthMeasureSpec, childView._currentHeightMeasureSpec);
});
}
onLayout(left, top, right, bottom) {
super.onLayout(left, top, right, bottom);
this.pager.frame = this.nativeView.bounds;
if (this.indicatorView && this.indicatorView.intrinsicContentSize) {
this.indicatorView.center = CGPointMake(this.nativeView.center.x, this.nativeView.bounds.size.height -
this.indicatorView.intrinsicContentSize.height);
}
const size = this._getSize();
this._map.forEach((childView, pagerCell) => {
const width = Utils.layout.toDevicePixels(size.width);
const height = Utils.layout.toDevicePixels(size.height);
View.layoutChild(this, childView, 0, 0, width, height);
});
}
requestLayout() {
// When preparing cell don't call super - no need to invalidate our measure when cell desiredSize is changed.
if (!this._preparingCell) {
super.requestLayout();
}
}
_prepareCell(cell, indexPath) {
try {
this._preparingCell = true;
let view = cell.view;
const template = this._getItemTemplate(indexPath.row);
if (!view) {
view = template.createView();
if (!view && this._itemViewLoader !== undefined) {
view = this._itemViewLoader(this._getItemTemplateKey(indexPath.row));
}
}
const bindingContext = this._getDataItem(indexPath.row);
let args = {
eventName: ITEMLOADING,
object: this,
index: indexPath.row,
android: undefined,
ios: cell,
view: view,
bindingContext,
};
this.notify(args);
view = args.view || this._getDefaultItemContent(indexPath.row);
// Proxy containers should not get treated as layouts.
// Wrap them in a real layout as well.
if (view instanceof ProxyViewContainer) {
let sp = new StackLayout();
sp.addChild(view);
view = sp;
}
// If cell is reused it have old content - remove it first.
if (!cell.view) {
cell.owner = new WeakRef(view);
}
else if (cell.view !== view) {
this._map.delete(cell);
this._removeContainer(cell, indexPath);
cell.view.ios.removeFromSuperview();
cell.owner = new WeakRef(view);
}
if (view) {
view.bindingContext = bindingContext;
}
this._map.set(cell, view);
if (view && !view.parent) {
this._addView(view);
cell.contentView.addSubview(view.ios);
}
this._layoutCell(view, indexPath);
}
finally {
this._preparingCell = false;
}
}
_layoutCell(cellView, index) {
if (cellView) {
const size = this._getSize();
let width = Utils.layout.toDevicePixels(size.width);
let height = Utils.layout.toDevicePixels(size.height);
const widthMeasureSpec = Utils.layout.makeMeasureSpec(width, Utils.layout.EXACTLY);
const heightMeasureSpec = Utils.layout.makeMeasureSpec(height, Utils.layout.EXACTLY);
const measured = View.measureChild(this, cellView, widthMeasureSpec, heightMeasureSpec);
}
}
_addChildFromBuilder(name, value) {
if (value instanceof common.PagerItem) {
if (!this._childrenViews) {
this._childrenViews = new Map();
}
const count = this._childrenViews.size;
const keys = Array.from(this._childrenViews.keys());
if (count === 0) {
this._childrenViews.set(this._childrenCount, value);
}
else {
for (let i = 0; i < count; i++) {
const key = keys[i];
const view = this._childrenViews.get(key);
if (i === keys.length - 1 && value !== view) {
this._childrenViews.set(this._childrenCount, value);
}
}
}
}
}
get horizontalOffset() {
return this.pager ? this.pager.contentOffset.x : 0;
}
get verticalOffset() {
return this.pager ? this.pager.contentOffset.y : 0;
}
_getSpacing() {
return Utils.layout.toDeviceIndependentPixels(this.convertToSize(this.spacing));
}
_getPeaking() {
return Utils.layout.toDeviceIndependentPixels(this.convertToSize(this.peaking));
}
_getSize(w = 0, h = 0) {
let width = 0;
let height = 0;
if (width === 0) {
width = Utils.layout.toDeviceIndependentPixels(this._effectiveItemWidth);
}
if (height === 0) {
height = Utils.layout.toDeviceIndependentPixels(this._effectiveItemHeight);
}
if (this.orientation === "vertical") {
height =
(height - (this._getSpacing() * 2 + this._getPeaking() * 2)) /
this.perPage;
}
else {
width =
(width - (this._getSpacing() * 2 + this._getPeaking() * 2)) /
this.perPage;
}
if (Number.isNaN(width)) {
width = 0;
}
if (Number.isNaN(height)) {
height = 0;
}
return { width, height };
}
}
__decorate([
profile,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], Pager.prototype, "onLoaded", null);
var PagerCell = /** @class */ (function (_super) {
__extends(PagerCell, _super);
function PagerCell() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(PagerCell.prototype, "view", {
get: function () {
return this.owner ? this.owner.get() : null;
},
enumerable: true,
configurable: true
});
PagerCell.initWithEmptyBackground = function () {
var cell = PagerCell.new();
// Clear background by default - this will make cells transparent
cell.backgroundColor = null;
return cell;
};
PagerCell.prototype.willMoveToSuperview = function (newSuperview) {
var parent = (this.view ? this.view.parent : null);
// When inside Pager and there is no newSuperview this cell is
// removed from native visual tree so we remove it from our tree too.
if (parent && !newSuperview) {
parent._removeContainer(this, this.index);
}
};
return PagerCell;
}(UICollectionViewCell));
var UICollectionDelegateImpl = /** @class */ (function (_super) {
__extends(UICollectionDelegateImpl, _super);
function UICollectionDelegateImpl() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._indexOfCellBeforeDragging = 0;
return _this;
}
UICollectionDelegateImpl_1 = UICollectionDelegateImpl;
UICollectionDelegateImpl.initWithOwner = function (owner) {
var delegate = UICollectionDelegateImpl_1.alloc().init();
delegate._owner = owner;
delegate._startingScrollingOffset = CGPointMake(0, 0);
return delegate;
};
UICollectionDelegateImpl.prototype.collectionViewLayoutInsetForSectionAtIndex = function (collectionView, collectionViewLayout, section) {
var owner = this._owner ? this._owner.get() : null;
if (owner) {
var inset = owner._getSpacing() + owner._getPeaking();
if (owner.orientation === "vertical") {
return new UIEdgeInsets({
bottom: inset,
left: 0,
right: 0,
top: inset,
});
}
return new UIEdgeInsets({
bottom: 0,
left: inset,
right: inset,
top: 0,
});
}
return new UIEdgeInsets({ bottom: 0, left: 0, right: 0, top: 0 });
};
UICollectionDelegateImpl.prototype.collectionViewLayoutSizeForItemAtIndexPath = function (collectionView, collectionViewLayout, indexPath) {
var owner = this._owner && this._owner.get();
if (!owner)
return CGSizeZero;
var size = owner._getSize();
return CGSizeMake(size.width, size.height);
};
UICollectionDelegateImpl.prototype.collectionViewWillDisplayCellForItemAtIndexPath = function (collectionView, cell, indexPath) {
var owner = this._owner && this._owner.get();
if (owner) {
if (!owner._isInit) {
owner._updateScrollPosition();
owner._isInit = true;
}
if (owner.items &&
indexPath.row === owner.lastIndex - owner.loadMoreCount) {
owner.notify({
eventName: LOADMOREITEMS,
object: owner,
});
}
}
if (cell.preservesSuperviewLayoutMargins) {
cell.preservesSuperviewLayoutMargins = false;
}
if (cell.layoutMargins) {
cell.layoutMargins = UIEdgeInsetsZero;
}
};
UICollectionDelegateImpl.prototype.collectionViewLayoutMinimumLineSpacingForSectionAtIndex = function (collectionView, collectionViewLayout, section) {
var owner = this._owner ? this._owner.get() : null;
if (!owner)
return 0;
return owner._getSpacing();
};
UICollectionDelegateImpl.prototype.scrollViewWillBeginDragging = function (scrollView) {
this._startingScrollingOffset = scrollView.contentOffset;
var owner = this._owner && this._owner.get();
if (owner) {
if (owner.lastEvent === 0) {
owner.notify({
eventName: Pager.swipeStartEvent,
object: owner,
});
owner.lastEvent = 1;
}
}
};
UICollectionDelegateImpl.prototype.scrollViewDidEndScrollingAnimation = function (scrollView) {
var owner = this._owner ? this._owner.get() : null;
if (owner) {
owner.notify({
eventName: Pager.swipeEvent,
object: owner,
});
}
};
UICollectionDelegateImpl.prototype.scrollViewDidScroll = function (scrollView) {
var owner = this._owner.get();
if (owner) {
var width = void 0;
var offset = void 0;
var size = owner._getRealWidthHeight();
var total = void 0;
var percent = void 0;
if (owner.orientation === "vertical") {
width = size.height;
offset = scrollView.contentOffset.y;
total =
scrollView.contentSize.height -
scrollView.bounds.size.height;
}
else {
width = size.width;
offset = scrollView.contentOffset.x;
total =
scrollView.contentSize.width - scrollView.bounds.size.width;
}
percent = offset / total;
var progress = percent * (owner.itemCount - 1);
if (owner.indicatorView &&
owner.indicatorView.setWithProgressAnimated &&
!Number.isNaN(progress)) {
owner.indicatorView.progress = progress;
}
var index = parseInt(progress.toFixed(0), 10);
if (owner.selectedIndex !== index && !Number.isNaN(index)) {
// selectedIndexProperty.nativeValueChange(owner, index);
}
owner.notify({
object: owner,
eventName: Pager.scrollEvent,
scrollX: owner.horizontalOffset,
scrollY: owner.verticalOffset,
});
if (owner.lastEvent === 1) {
owner.notify({
eventName: Pager.swipeOverEvent,
object: owner,
});
owner.lastEvent = 1;
}
}
};
UICollectionDelegateImpl.prototype.scrollViewDidEndDraggingWillDecelerate = function (scrollView, decelerate) {
if (!decelerate) {
// (scrollView as any).scrollToItemAtIndexPathAtScrollPositionAnimated(
// NSIndexPath.indexPathForRowInSection(this._getIndex(scrollView), 0), UICollectionViewScrollPosition.CenteredHorizontally, true
// );
}
};
UICollectionDelegateImpl.prototype._getIndex = function (scrollView) {
var index = 0;
var owner = this._owner && this._owner.get();
if (owner) {
var offset = void 0;
var itemSize = owner._getRealWidthHeight();
var size = void 0;
if (owner.orientation === "vertical") {
offset = scrollView.contentOffset.y;
size = itemSize.height;
}
else {
offset = scrollView.contentOffset.x;
size = itemSize.width;
}
index = parseInt(Number(offset / size).toFixed(0), 10);
}
return index;
};
UICollectionDelegateImpl.prototype.scrollViewWillEndDraggingWithVelocityTargetContentOffset = function (scrollView, velocity, targetContentOffset) {
var owner = this._owner ? this._owner.get() : null;
if (!owner)
return;
if (owner.lastEvent === 1) {
owner.notify({
eventName: Pager.swipeEndEvent,
object: owner,
});
owner.lastEvent = 0;
}
};
UICollectionDelegateImpl.prototype._getPointFromAttrOffset = function (attribute, target) {
var owner = this._owner && this._owner.get();
if (owner) {
var x = owner.orientation === "vertical"
? target.x
: attribute.frame.origin.x -
(owner._getSpacing() + owner._getPeaking());
var y = owner.orientation === "vertical"
? attribute.frame.origin.y -
(owner._getSpacing() + owner._getPeaking())
: target.y;
return CGPointMake(x, y);
}
return CGPointZero;
};
var UICollectionDelegateImpl_1;
UICollectionDelegateImpl = UICollectionDelegateImpl_1 = __decorate([
ObjCClass(UICollectionViewDelegate, UICollectionViewDelegateFlowLayout)
], UICollectionDelegateImpl);
return UICollectionDelegateImpl;
}(NSObject));
var UICollectionViewDataSourceImpl = /** @class */ (function (_super) {
__extends(UICollectionViewDataSourceImpl, _super);
function UICollectionViewDataSourceImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
UICollectionViewDataSourceImpl_1 = UICollectionViewDataSourceImpl;
UICollectionViewDataSourceImpl.initWithOwner = function (owner) {
var delegate = UICollectionViewDataSourceImpl_1.alloc().init();
delegate._owner = owner;
return delegate;
};
UICollectionViewDataSourceImpl.prototype.collectionViewCellForItemAtIndexPath = function (collectionView, indexPath) {
var owner = this._owner ? this._owner.get() : null;
var cell;
var count = 0;
if (owner) {
count = owner._childrenCount;
if (owner.circularMode) {
count = owner.itemCount;
switch (indexPath.row) {
case 0:
indexPath = NSIndexPath.indexPathForRowInSection(owner.lastDummy, 0);
break;
case owner.firstDummy:
indexPath = NSIndexPath.indexPathForRowInSection(0, 0);
break;
default:
indexPath = NSIndexPath.indexPathForRowInSection(indexPath.row - 1, 0);
break;
}
}
}
if (owner && !owner.items && count > 0) {
owner._preparingCell = true;
var size = owner._getSize();
collectionView.registerClassForCellWithReuseIdentifier(PagerCell.class(), "static-".concat(indexPath.row));
cell =
collectionView.dequeueReusableCellWithReuseIdentifierForIndexPath("static-".concat(indexPath.row), indexPath) || PagerCell.initWithEmptyBackground();
cell.index = indexPath;
var view = owner._childrenViews.get(indexPath.row);
if (view instanceof ProxyViewContainer) {
var sp = new StackLayout();
sp.addChild(view);
view = sp;
}
// If cell is reused it has old content - remove it first.
if (!cell.view) {
cell.owner = new WeakRef(view);
}
else if (cell.view !== view) {
/*
if (!(view.parent instanceof Pager)) {
owner._removeView(view.parent);
}
view.parent._removeView(view);
*/
cell.view.ios.removeFromSuperview();
cell.owner = new WeakRef(view);
}
if (view && !view.parent) {
owner._addView(view);
cell.contentView.addSubview(view.ios);
}
else if (view && view.ios) {
cell.contentView.addSubview(view.ios);
}
owner._layoutCell(view, indexPath);
var width = Utils.layout.toDevicePixels(size.width);
var height = Utils.layout.toDevicePixels(size.height);
if (view && view.isLayoutRequired) {
View.layoutChild(owner, view, 0, 0, width, height);
}
owner._preparingCell = false;
return cell;
}
var template = owner && owner._getItemTemplate(indexPath.row);
cell =
collectionView.dequeueReusableCellWithReuseIdentifierForIndexPath(template.key, indexPath) || PagerCell.initWithEmptyBackground();
cell.index = indexPath;
if (owner) {
var size = owner._getSize();
owner._prepareCell(cell, indexPath);
var cellView = cell.view;
if (cellView && cellView.isLayoutRequired) {
View.layoutChild(owner, cellView, 0, 0, Utils.layout.toDevicePixels(size.width), Utils.layout.toDevicePixels(size.height));
}
}
return cell;
};
UICollectionViewDataSourceImpl.prototype.collectionViewNumberOfItemsInSection = function (collectionView, section) {
var owner = this._owner ? this._owner.get() : null;
if (!owner)
return 0;
return owner.circularMode ? owner.itemCount : owner._childrenCount;
};
UICollectionViewDataSourceImpl.prototype.numberOfSectionsInCollectionView = function (collectionView) {
return 1;
};
var UICollectionViewDataSourceImpl_1;
UICollectionViewDataSourceImpl = UICollectionViewDataSourceImpl_1 = __decorate([
ObjCClass(UICollectionViewDataSource)
], UICollectionViewDataSourceImpl);
return UICollectionViewDataSourceImpl;
}(NSObject));
var UICollectionViewFlowLinearLayoutImpl = /** @class */ (function (_super) {
__extends(UICollectionViewFlowLinearLayoutImpl, _super);
function UICollectionViewFlowLinearLayoutImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
UICollectionViewFlowLinearLayoutImpl.initWithOwner = function (owner) {
var layout = UICollectionViewFlowLinearLayoutImpl.new();
layout._owner = owner;
layout._curl = CATransition.animation();
return layout;
};
UICollectionViewFlowLinearLayoutImpl.prototype.layoutAttributesForElementsInRect = function (rect) {
var owner = this._owner ? this._owner.get() : null;
var originalLayoutAttribute = _super.prototype.layoutAttributesForElementsInRect.call(this, rect);
var visibleLayoutAttributes = [];
if (owner) {
if (owner.transformers &&
owner.transformers.indexOf("scale") > -1) {
var count = originalLayoutAttribute.count;
for (var i = 0; i < count; i++) {
var attributes = originalLayoutAttribute.objectAtIndex(i);
visibleLayoutAttributes[i] = attributes;
var frame = attributes.frame;
var width = attributes.frame.size.width * 0.75;
var height = attributes.frame.size.height * 0.75;
attributes.frame.size.width = width;
attributes.frame.size.height = height;
var spacing = owner.convertToSize(owner.spacing);
var distance = Math.abs(this.collectionView.contentOffset.x +
this.collectionView.contentInset.left +
spacing -
frame.origin.x);
var scale = Math.min(Math.max(1 -
distance /
this.collectionView.bounds.size.width, 0.75), 1);
attributes.transform = CGAffineTransformScale(attributes.transform, 1, scale);
}
}
else {
return originalLayoutAttribute;
}
}
return visibleLayoutAttributes;
};
UICollectionViewFlowLinearLayoutImpl.prototype.shouldInvalidateLayoutForBoundsChange = function (newBounds) {
return true;
};
UICollectionViewFlowLinearLayoutImpl.prototype.initialLayoutAttributesForAppearingItemAtIndexPath = function (itemIndexPath) {
var attrs = _super.prototype.initialLayoutAttributesForAppearingItemAtIndexPath.call(this, itemIndexPath);
if (attrs) {
attrs.alpha = 1;
}
return attrs;
};
UICollectionViewFlowLinearLayoutImpl.prototype.finalLayoutAttributesForDisappearingItemAtIndexPath = function (itemIndexPath) {
var attrs = _super.prototype.finalLayoutAttributesForDisappearingItemAtIndexPath.call(this, itemIndexPath);
if (attrs) {
attrs.alpha = 1;
}
return attrs;
};
UICollectionViewFlowLinearLayoutImpl.prototype.targetContentOffsetForProposedContentOffsetWithScrollingVelocity = function (proposedContentOffset, velocity) {
var owner = this._owner ? this._owner.get() : null;
if (!this.collectionView || !owner) {
return _super.prototype.targetContentOffsetForProposedContentOffsetWithScrollingVelocity.call(this, proposedContentOffset, velocity);
}
var size = owner._getRealWidthHeight();
if (this.scrollDirection === UICollectionViewScrollDirection.Horizontal) {
// Page width used for estimating and calculating paging.
var pageWidth = size.width + this.minimumInteritemSpacing;
// Make an estimation of the current page position.
var approximatePage = this.collectionView.contentOffset.x / pageWidth;
// Determine the current page based on velocity.
var currentPage = velocity.x == 0
? Math.round(approximatePage)
: velocity.x < 0.0
? Math.floor(approximatePage)
: Math.ceil(approximatePage);
currentPage = owner.selectedIndex;
var flickedPages = 0;
if (velocity.x >= 0.5) {
flickedPages = 1;
}
else if (velocity.x <= -0.5) {
flickedPages = -1;
}
// currentPage = owner.selectedIndex;
var newPageIndex = currentPage + flickedPages;
selectedIndexProperty.nativeValueChange(owner, Math.max(newPageIndex, 0));
// Calculate newHorizontalOffset.
var newHorizontalOffset = newPageIndex * pageWidth -
this.collectionView.contentInset.left;
var cell = this.collectionView.cellForItemAtIndexPath(NSIndexPath.indexPathForRowInSection(newPageIndex, 0));
if (newPageIndex > owner.itemCount || !cell) {
return _super.prototype.targetContentOffsetForProposedContentOffsetWithScrollingVelocity.call(this, proposedContentOffset, velocity);
}
newHorizontalOffset = cell.frame.origin.x - (owner._getPeaking() + owner._getSpacing());
return CGPointMake(newHorizontalOffset, proposedContentOffset.y);
}
else {
// Page height used for estimating and calculating paging.
var pageHeight = size.height + this.minimumLineSpacing;
// Make an estimation of the current page position.
var approximatePage = this.collectionView.contentOffset.y / pageHeight;
// Determine the current page based on velocity.
var currentPage = velocity.y == 0
? Math.round(approximatePage)
: velocity.y < 0.0
? Math.floor(approximatePage)
: Math.ceil(approximatePage);
// Create custom flickVelocity.
var flickVelocity = velocity.y * 0.3;
// Check how many pages the user flicked, if <= 1 then flickedPages should return 0.
var flickedPages = Math.abs(Math.round(flickVelocity)) <= 1
? 0
: Math.round(flickVelocity);
var newPageIndex = currentPage + flickedPages;
selectedIndexProperty.nativeValueChange(owner, Math.max(newPageIndex, 0));
var newVerticalOffset = newPageIndex * pageHeight -
this.collectionView.contentInset.top;
return CGPointMake(proposedContentOffset.x, newVerticalOffset);
}
};
return UICollectionViewFlowLinearLayoutImpl;
}(UICollectionViewFlowLayout));
//# sourceMappingURL=index.ios.js.map