UNPKG

@nativescript-community/ui-material-tabs

Version:

Material Design Tabs organize content across different screens, data sets, and other interactions.

237 lines 11.8 kB
import { themer } from '@nativescript-community/ui-material-core'; import { TabContentItem } from '@nativescript-community/ui-material-core-tabs/tab-content-item'; import { TabsPosition } from '@nativescript-community/ui-material-core-tabs/tab-navigation/index-common'; import { TabNavigation, updateBackgroundPositions, updateTitleAndIconPositions } from '@nativescript-community/ui-material-core-tabs/tab-navigation/index.ios'; import { TabStrip } from '@nativescript-community/ui-material-core-tabs/tab-strip'; import { TabStripItem } from '@nativescript-community/ui-material-core-tabs/tab-strip-item'; import { IOSHelper, Utils } from '@nativescript/core'; export { TabContentItem, TabStrip, TabStripItem }; const majorVersion = Utils.ios.MajorVersion; var MDCTabBarViewDelegateImpl = /** @class */ (function (_super) { __extends(MDCTabBarViewDelegateImpl, _super); function MDCTabBarViewDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } MDCTabBarViewDelegateImpl.initWithOwner = function (owner) { var delegate = MDCTabBarViewDelegateImpl.new(); delegate._owner = owner; return delegate; }; MDCTabBarViewDelegateImpl.prototype.tabBarViewShouldSelectItem = function (tabBar, item) { var _a; var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get(); if (owner) { var shouldSelectItem = owner.mCanSelectItem; var selectedIndex = owner.tabBarItems.indexOf(item); if (owner.selectedIndex !== selectedIndex) { owner.beginTabTransition(); } var tabStrip = owner.tabStrip; var tabStripItems = tabStrip && tabStrip.items; if (tabStripItems && tabStripItems[selectedIndex]) { tabStripItems[selectedIndex]._emit(TabStripItem.tapEvent); tabStrip.notify({ eventName: TabStrip.itemTapEvent, object: tabStrip, index: selectedIndex }); } return shouldSelectItem; } return false; }; MDCTabBarViewDelegateImpl.prototype.tabBarViewDidSelectItem = function (tabBar, selectedItem) { var _a; var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get(); if (owner) { var tabBarItems = owner.tabBarItems; var selectedIndex = tabBarItems.indexOf(selectedItem); owner.selectedIndex = selectedIndex; } }; MDCTabBarViewDelegateImpl.ObjCProtocols = [MDCTabBarViewDelegate]; return MDCTabBarViewDelegateImpl; }(NSObject)); var UIPageViewControllerImpl = /** @class */ (function (_super) { __extends(UIPageViewControllerImpl, _super); function UIPageViewControllerImpl() { return _super !== null && _super.apply(this, arguments) || this; } UIPageViewControllerImpl.initWithOwner = function (owner) { var handler = UIPageViewControllerImpl.alloc().initWithTransitionStyleNavigationOrientationOptions(UIPageViewControllerTransitionStyle.Scroll, UIPageViewControllerNavigationOrientation.Horizontal, null); handler._owner = owner; return handler; }; UIPageViewControllerImpl.prototype.accessibilityScroll = function (direction) { var _a, _b; if ((_b = (_a = this._owner.get()) === null || _a === void 0 ? void 0 : _a.swipeEnabled) !== null && _b !== void 0 ? _b : true) { return _super.prototype.accessibilityScroll.call(this, direction); } return false; }; UIPageViewControllerImpl.prototype.viewDidLoad = function () { var owner = this._owner.get(); var tabBarItems = owner.tabBarItems; var tabBar = MDCTabBarView.alloc().init(); this.tabBar = tabBar; var colorScheme = themer.getAppColorScheme(); if (tabBarItems && tabBarItems.length) { tabBar.items = NSArray.arrayWithArray(tabBarItems); } tabBar.tabBarDelegate = this.tabBarDelegate = MDCTabBarViewDelegateImpl.initWithOwner(new WeakRef(owner)); if (colorScheme && colorScheme.primaryColor) { tabBar.rippleColor = colorScheme.primaryColor.colorWithAlphaComponent(0.24); tabBar.tintColor = colorScheme.primaryColor; tabBar.selectionIndicatorStrokeColor = colorScheme.primaryColor; owner.setTabBarColor(colorScheme.primaryColor); } else { if (majorVersion <= 12 || !UIColor.labelColor) { tabBar.tintColor = UIColor.blueColor; tabBar.barTintColor = UIColor.whiteColor; tabBar.setTitleColorForState(UIColor.blackColor, UIControlState.Normal); tabBar.setTitleColorForState(UIColor.blackColor, UIControlState.Selected); } else { tabBar.tintColor = UIColor.systemBlueColor; tabBar.barTintColor = UIColor.systemBackgroundColor; tabBar.setTitleColorForState(UIColor.labelColor, UIControlState.Normal); tabBar.setTitleColorForState(UIColor.labelColor, UIControlState.Selected); tabBar.rippleColor = UIColor.clearColor; } } tabBar.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleBottomMargin; this.view.addSubview(tabBar); tabBar.sizeToFit(); }; UIPageViewControllerImpl.prototype.viewDidUnload = function () { if (this.tabBar) { this.tabBar.tabBarDelegate = null; this.tabBar = null; } }; UIPageViewControllerImpl.prototype.viewWillAppear = function (animated) { _super.prototype.viewWillAppear.call(this, animated); var owner = this._owner.get(); if (!owner) { return; } IOSHelper.updateAutoAdjustScrollInsets(this, owner); // Tabs can be reset as a root view. Call loaded here in this scenario. if (!owner.isLoaded) { owner.callLoaded(); } }; // public viewWillLayoutSubviews(): void { // super.viewWillLayoutSubviews(); // this.tabBar.sizeToFit(); // } UIPageViewControllerImpl.prototype.viewDidLayoutSubviews = function () { _super.prototype.viewDidLayoutSubviews.call(this); var owner = this._owner.get(); if (!owner) { return; } var safeAreaInsetsBottom = 0; var safeAreaInsetsTop = 0; if (majorVersion > 10) { safeAreaInsetsBottom = this.view.safeAreaInsets.bottom; safeAreaInsetsTop = this.view.safeAreaInsets.top; } else { safeAreaInsetsTop = this.topLayoutGuide.length; } var conditionalSafeAreaBottom = owner.iosOverflowSafeArea ? safeAreaInsetsBottom : 0; var scrollViewTop = 0; var scrollViewHeight = this.view.bounds.size.height + conditionalSafeAreaBottom; if (owner.tabStrip && this.tabBar) { if (owner.tabStrip.visibility === 'visible') { this.tabBar.hidden = false; } else { this.tabBar.hidden = true; } // failsafe with !this.tabBar.hidden just in some really odd case where hidden is false and collapse is true // which should never happen if (!owner.tabStrip.isCollapsed || !this.tabBar.hidden) { var viewWidth = this.view.bounds.size.width; var viewHeight = this.view.bounds.size.height; var tabBarHeight = this.tabBar.frame.size.height; var tabBarTop = safeAreaInsetsTop; scrollViewTop = tabBarHeight; scrollViewHeight = this.view.bounds.size.height - tabBarHeight + conditionalSafeAreaBottom; var tabsPosition = owner.tabsPosition; if (tabsPosition === TabsPosition.Bottom) { tabBarTop = viewHeight - tabBarHeight - safeAreaInsetsBottom; scrollViewTop = this.view.frame.origin.y; scrollViewHeight = viewHeight - tabBarHeight; } var parent = owner.parent; // Handle Angular scenario where Tabs is in a ProxyViewContainer // It is possible to wrap components in ProxyViewContainers indefinitely while (parent && !parent.nativeViewProtected) { parent = parent.parent; } if (parent && majorVersion > 10) { // TODO: Figure out a better way to handle ViewController nesting/Safe Area nesting tabBarTop = Math.max(tabBarTop, parent.nativeView.safeAreaInsets.top); } this.tabBar.frame = CGRectMake(0, tabBarTop, viewWidth, tabBarHeight); } } else { this.tabBar.hidden = true; } var subViews = this.view.subviews; var scrollView = null; for (var i = 0; i < subViews.count; i++) { var view = subViews.objectAtIndex(i); if (view instanceof UIScrollView && !(view instanceof MDCTabBarView)) { scrollView = view; } } if (scrollView) { // The part of the UIPageViewController that is changing the pages is a UIScrollView // We want to expand it to the size of the UIPageViewController as it is not so by default this.scrollView = scrollView; if (!owner.swipeEnabled) { scrollView.scrollEnabled = false; } else { scrollView.scrollEnabled = true; } scrollView.frame = CGRectMake(0, scrollViewTop, this.view.bounds.size.width, scrollViewHeight); //this.view.bounds; } }; // Mind implementation for other controllers UIPageViewControllerImpl.prototype.traitCollectionDidChange = function (previousTraitCollection) { _super.prototype.traitCollectionDidChange.call(this, previousTraitCollection); if (majorVersion >= 13) { var owner = this._owner.get(); if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { owner.notify({ eventName: IOSHelper.traitCollectionColorAppearanceChangedEvent, object: owner }); } } }; UIPageViewControllerImpl.prototype.viewWillTransitionToSizeWithTransitionCoordinator = function (size, coordinator) { var _this = this; _super.prototype.viewWillTransitionToSizeWithTransitionCoordinator.call(this, size, coordinator); coordinator.animateAlongsideTransitionCompletion(function () { var owner = _this._owner.get(); if (owner && owner.tabStrip && owner.tabStrip.items) { var tabStrip_1 = owner.tabStrip; tabStrip_1.items.forEach(function (tabStripItem) { updateBackgroundPositions(tabStrip_1, tabStripItem, owner.selectedIndex !== tabStripItem.index ? owner.mDefaultItemBackgroundColor : null); var index = tabStripItem.index; var tabBarItemController = owner.viewControllers[index]; updateTitleAndIconPositions(tabStripItem, tabBarItemController.tabBarItem, tabBarItemController); }); } }, null); }; return UIPageViewControllerImpl; }(UIPageViewController)); export class Tabs extends TabNavigation { createViewController() { return UIPageViewControllerImpl.initWithOwner(new WeakRef(this)); } } //# sourceMappingURL=index.ios.js.map