Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- // External
- import React, { PureComponent } from 'react';
- import { connect } from 'react-redux';
- import { createStructuredSelector } from 'reselect';
- import ReduxNestedBindActions from 'redux-nested-bind-actions';
- import Router from '../../../utils/router/';
- import Core from '../../../core';
- import R from 'ramda';
- import Lodash from 'lodash';
- import HoistNonReactStatic from 'hoist-non-react-statics';
- import Utils from '../../../utils';
- // Internal
- ////////////////////////////////////////////////////////////////////////////////
- function wixCallWrapper(fnCheckLastAction, fnSetCurrentScreen, strMethod, fnAction, objParams) {
- if (!fnCheckLastAction({ method: strMethod, passProps: objParams.passProps, screen: objParams.screen })) {
- return;
- }
- fnAction(objParams);
- return fnSetCurrentScreen(objParams.screen);
- }
- const fnWixWrapperCurried = R.curry(wixCallWrapper);
- function wixNavigation(WrappedComponent, strScreenName) {
- class WixNavigation extends PureComponent {
- /**
- * Constructor
- * @param {Object} objProps Component props.
- */
- constructor(objProps) {
- super(objProps);
- this.fnSetOnNavigatorEvent = null;
- const fnSetCurrentScreen = fnWixWrapperCurried(
- (objParams) => this._checkLastAction(objParams),
- this.props.setCurrentScreen
- );
- this.canNavigate = true; //Blocks multiple pushes by navigator
- this.navigator = {
- // ...this.props.navigator,
- Snackbar: Utils.snackbar.init(this.props.navigator),
- showSnackbar: (objParams) => this.props.navigator.showSnackbar(objParams),
- showLightBox: (objParams) => this.props.navigator.showLightBox(objParams),
- dismissLightBox: (objParams) => this.props.navigator.dismissLightBox(objParams),
- setTitle: (objParams) => this.props.navigator.setTitle(objParams),
- setButtons: (objParams) => this.props.navigator.setButtons(objParams),
- popToRoot: (objParams) => this.props.navigator.popToRoot(objParams),
- showModal: (objParams) => this.navigationWrapper((objParams) => this.props.navigator.showModal(objParams), objParams),
- dismissModal: (objParams) => this.props.navigator.dismissModal(objParams),
- pop: (objParams) => this.props.navigator.pop(objParams),
- popTo: (objParams) => this.popTo(objParams),
- toggleNavBar: (objParams) => this.props.navigator.toggleNavBar(objParams),
- setOnNavigatorEvent: (fnCallback) => this.fnSetOnNavigatorEvent = fnCallback,
- toggleDrawer: (objParams) => this.props.navigator.toggleDrawer(objParams),
- push: (objParams) => this.navigationWrapper((objParams) => this.props.navigator.push(objParams), objParams),
- resetTo: (objParams) => this.navigationWrapper((objParams) => this.props.navigator.resetTo(objParams), objParams)
- };
- this._lastAction = { params: undefined, timestamp: 0 };
- }
- /**
- * Funcition the wraps the navigation of the navigator.
- * @param {Function} fnNavigator function use to navigate.
- * @param {Object} objParams params of the screen
- */
- navigationWrapper(fnNavigator, objParams) {
- if (this.canNavigate) {
- this.canNavigate = false;
- fnNavigator(objParams);
- this.props.setCurrentScreen(objParams.screen);
- }
- }
- popTo(objParams) {
- let hasEnded = false;
- const objNavigator = this.props.navigatorX || this.navigator;
- this.props.listCurrentStack.reverse().forEach((strName, intIndex, listStack) => {
- if (hasEnded || strName === objParams.screen) {
- hasEnded = true;
- return;
- }
- return objNavigator.pop({ animated: listStack.get(intIndex + 1) === objParams.screen, ...objParams });
- });
- }
- // SOURCE: https://github.com/wix/react-native-navigation/pull/256
- _checkLastAction(objParams) {
- if (Date.now() - this._lastAction.timestamp < 1000 &&
- Lodash.isEqual(objParams, this._lastAction.objParams) &&
- !objParams.force) {
- return false;
- } else {
- this._lastAction = { objParams, timestamp: Date.now() };
- return true;
- }
- }
- /**
- * This function handles the react native did mount event.
- */
- componentDidMount() {
- if (this.props.navigator) {
- this.props.navigator.setOnNavigatorEvent(this.handlesNavigationEvent.bind(this));
- }
- if (strScreenName === this.props.strCurrentScreen && WrappedComponent.setNavbar) {
- WrappedComponent.setNavbar({ ...this.props, navigatorX: this.props.navigatorX || this.navigator });
- }
- }
- /**
- * This function handles the react native will receive props event.
- */
- componentWillReceiveProps(objNextProps) {
- if (this.props.strCurrentScreen === objNextProps.strCurrentScreen && objNextProps.strCurrentScreen === strScreenName && WrappedComponent.onNavbarUpdate) {
- WrappedComponent.onNavbarUpdate({ ...this.props, navigatorX: this.props.navigatorX || this.navigator }, { ...objNextProps, navigatorX: objNextProps.navigatorX || this.navigator }, false);
- } else if (this.props.strCurrentScreen !== objNextProps.strCurrentScreen && objNextProps.strCurrentScreen === strScreenName) {
- if (WrappedComponent.onNavbarUpdate) {
- WrappedComponent.onNavbarUpdate({ ...this.props, navigatorX: this.props.navigatorX || this.navigator }, { ...objNextProps, navigatorX: objNextProps.navigatorX || this.navigator }, true);
- } else if (WrappedComponent.setNavbar) {
- WrappedComponent.setNavbar({ ...this.props, navigatorX: this.props.navigatorX || this.navigator });
- }
- }
- }
- /**
- * This function handles the react native component will unmount event.
- */
- componentWillUnmount() {
- if (this.props.navigator) {
- this.props.setCurrentScreen();
- }
- }
- handlesNavigationEvent(objEvent) {
- if (objEvent.id === 'didAppear') {
- this.canNavigate = true; // can navigate when the view appears.
- }
- if (this.fnSetOnNavigatorEvent) {
- this.fnSetOnNavigatorEvent(objEvent);
- }
- }
- render() {
- return (
- <WrappedComponent
- { ...this.props }
- navigatorX={ this.props.navigatorX || this.navigator }
- />
- );
- }
- }
- HoistNonReactStatic(WixNavigation, WrappedComponent);
- WixNavigation.navigatorStyle = {
- ...Core.stylesheet.WIX_NAVBAR_STYLE,
- ...WrappedComponent.navigatorStyle || {}
- }
- function mapStateToProps() {
- return createStructuredSelector({
- strCurrentScreen: Router.redux.selectors.getCurrentScreen,
- listCurrentStack: Router.redux.selectors.getCurrentStack
- });
- }
- return connect(
- mapStateToProps(),
- (fnDispatch) => ReduxNestedBindActions(
- Router.redux.actions,
- fnDispatch
- )
- )(WixNavigation);
- }
- ////////////////////////////////////////////////////////////////////////////////
- module.exports = wixNavigation;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement