Advertisement
Guest User

Untitled

a guest
Jan 9th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3.  
  4. // External
  5. import React, { PureComponent } from 'react';
  6. import { connect } from 'react-redux';
  7. import { createStructuredSelector } from 'reselect';
  8. import ReduxNestedBindActions from 'redux-nested-bind-actions';
  9. import Router from '../../../utils/router/';
  10. import Core from '../../../core';
  11. import R from 'ramda';
  12. import Lodash from 'lodash';
  13. import HoistNonReactStatic from 'hoist-non-react-statics';
  14. import Utils from '../../../utils';
  15.  
  16.  
  17. // Internal
  18.  
  19.  
  20. ////////////////////////////////////////////////////////////////////////////////
  21.  
  22.  
  23. function wixCallWrapper(fnCheckLastAction, fnSetCurrentScreen, strMethod, fnAction, objParams) {
  24.   if (!fnCheckLastAction({ method: strMethod, passProps: objParams.passProps, screen: objParams.screen })) {
  25.     return;
  26.   }
  27.  
  28.   fnAction(objParams);
  29.  
  30.   return fnSetCurrentScreen(objParams.screen);
  31. }
  32.  
  33.  
  34. const fnWixWrapperCurried = R.curry(wixCallWrapper);
  35.  
  36.  
  37. function wixNavigation(WrappedComponent, strScreenName) {
  38.   class WixNavigation extends PureComponent {
  39.     /**
  40.      * Constructor
  41.      * @param  {Object} objProps Component props.
  42.      */
  43.     constructor(objProps) {
  44.       super(objProps);
  45.  
  46.       this.fnSetOnNavigatorEvent = null;
  47.  
  48.       const fnSetCurrentScreen = fnWixWrapperCurried(
  49.         (objParams) => this._checkLastAction(objParams),
  50.         this.props.setCurrentScreen
  51.       );
  52.       this.canNavigate = true; //Blocks multiple pushes by navigator
  53.       this.navigator = {
  54.         // ...this.props.navigator,
  55.         Snackbar: Utils.snackbar.init(this.props.navigator),
  56.         showSnackbar: (objParams) => this.props.navigator.showSnackbar(objParams),
  57.         showLightBox: (objParams) => this.props.navigator.showLightBox(objParams),
  58.         dismissLightBox: (objParams) => this.props.navigator.dismissLightBox(objParams),
  59.         setTitle: (objParams) => this.props.navigator.setTitle(objParams),
  60.         setButtons: (objParams) => this.props.navigator.setButtons(objParams),
  61.         popToRoot: (objParams) => this.props.navigator.popToRoot(objParams),
  62.         showModal: (objParams) => this.navigationWrapper((objParams) => this.props.navigator.showModal(objParams), objParams),
  63.         dismissModal: (objParams) => this.props.navigator.dismissModal(objParams),
  64.         pop: (objParams) => this.props.navigator.pop(objParams),
  65.         popTo: (objParams) => this.popTo(objParams),
  66.         toggleNavBar: (objParams) => this.props.navigator.toggleNavBar(objParams),
  67.         setOnNavigatorEvent: (fnCallback) => this.fnSetOnNavigatorEvent = fnCallback,
  68.         toggleDrawer: (objParams) => this.props.navigator.toggleDrawer(objParams),
  69.         push: (objParams) => this.navigationWrapper((objParams) => this.props.navigator.push(objParams), objParams),
  70.         resetTo: (objParams) => this.navigationWrapper((objParams) => this.props.navigator.resetTo(objParams), objParams)
  71.       };
  72.  
  73.       this._lastAction = { params: undefined, timestamp: 0 };
  74.     }
  75.  
  76.     /**
  77.      * Funcition the wraps the navigation of the navigator.
  78.      * @param {Function} fnNavigator function use to navigate.
  79.      * @param {Object} objParams params of the screen
  80.      */
  81.     navigationWrapper(fnNavigator, objParams) {
  82.       if (this.canNavigate) {
  83.         this.canNavigate = false;
  84.         fnNavigator(objParams);
  85.         this.props.setCurrentScreen(objParams.screen);
  86.       }
  87.     }
  88.  
  89.     popTo(objParams) {
  90.       let hasEnded = false;
  91.       const objNavigator = this.props.navigatorX || this.navigator;
  92.       this.props.listCurrentStack.reverse().forEach((strName, intIndex, listStack) => {
  93.         if (hasEnded || strName === objParams.screen) {
  94.           hasEnded = true;
  95.           return;
  96.         }
  97.  
  98.         return objNavigator.pop({ animated: listStack.get(intIndex + 1) === objParams.screen, ...objParams });
  99.       });
  100.     }
  101.  
  102.  
  103.     // SOURCE: https://github.com/wix/react-native-navigation/pull/256
  104.     _checkLastAction(objParams) {
  105.       if (Date.now() - this._lastAction.timestamp < 1000 &&
  106.         Lodash.isEqual(objParams, this._lastAction.objParams) &&
  107.         !objParams.force) {
  108.         return false;
  109.       } else {
  110.         this._lastAction = { objParams, timestamp: Date.now() };
  111.         return true;
  112.       }
  113.     }
  114.  
  115.  
  116.     /**
  117.      * This function handles the react native did mount event.
  118.      */
  119.     componentDidMount() {
  120.       if (this.props.navigator) {
  121.         this.props.navigator.setOnNavigatorEvent(this.handlesNavigationEvent.bind(this));
  122.       }
  123.  
  124.  
  125.       if (strScreenName === this.props.strCurrentScreen && WrappedComponent.setNavbar) {
  126.         WrappedComponent.setNavbar({ ...this.props, navigatorX: this.props.navigatorX || this.navigator });
  127.       }
  128.     }
  129.  
  130.  
  131.     /**
  132.      * This function handles the react native will receive props event.
  133.      */
  134.     componentWillReceiveProps(objNextProps) {
  135.       if (this.props.strCurrentScreen === objNextProps.strCurrentScreen && objNextProps.strCurrentScreen === strScreenName && WrappedComponent.onNavbarUpdate) {
  136.         WrappedComponent.onNavbarUpdate({ ...this.props, navigatorX: this.props.navigatorX || this.navigator }, { ...objNextProps, navigatorX: objNextProps.navigatorX || this.navigator }, false);
  137.       } else if (this.props.strCurrentScreen !== objNextProps.strCurrentScreen && objNextProps.strCurrentScreen === strScreenName) {
  138.         if (WrappedComponent.onNavbarUpdate) {
  139.           WrappedComponent.onNavbarUpdate({ ...this.props, navigatorX: this.props.navigatorX || this.navigator }, { ...objNextProps, navigatorX: objNextProps.navigatorX || this.navigator }, true);
  140.         } else if (WrappedComponent.setNavbar) {
  141.           WrappedComponent.setNavbar({ ...this.props, navigatorX: this.props.navigatorX || this.navigator });
  142.         }
  143.       }
  144.     }
  145.  
  146.     /**
  147.      * This function handles the react native component will unmount event.
  148.      */
  149.     componentWillUnmount() {
  150.       if (this.props.navigator) {
  151.         this.props.setCurrentScreen();
  152.       }
  153.     }
  154.  
  155.  
  156.     handlesNavigationEvent(objEvent) {
  157.       if (objEvent.id === 'didAppear') {
  158.         this.canNavigate = true; // can navigate when the view appears.
  159.       }
  160.       if (this.fnSetOnNavigatorEvent) {
  161.         this.fnSetOnNavigatorEvent(objEvent);
  162.       }
  163.     }
  164.  
  165.  
  166.     render() {
  167.       return (
  168.         <WrappedComponent
  169.           { ...this.props }
  170.           navigatorX={ this.props.navigatorX || this.navigator }
  171.         />
  172.       );
  173.     }
  174.   }
  175.  
  176.  
  177.   HoistNonReactStatic(WixNavigation, WrappedComponent);
  178.  
  179.  
  180.   WixNavigation.navigatorStyle = {
  181.     ...Core.stylesheet.WIX_NAVBAR_STYLE,
  182.     ...WrappedComponent.navigatorStyle || {}
  183.   }
  184.  
  185.  
  186.   function mapStateToProps() {
  187.     return createStructuredSelector({
  188.       strCurrentScreen: Router.redux.selectors.getCurrentScreen,
  189.       listCurrentStack: Router.redux.selectors.getCurrentStack
  190.     });
  191.   }
  192.  
  193.  
  194.   return connect(
  195.     mapStateToProps(),
  196.     (fnDispatch) => ReduxNestedBindActions(
  197.       Router.redux.actions,
  198.       fnDispatch
  199.     )
  200.   )(WixNavigation);
  201. }
  202.  
  203.  
  204. ////////////////////////////////////////////////////////////////////////////////
  205.  
  206.  
  207.  
  208.  
  209. module.exports = wixNavigation;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement