diff --git a/lib/attachHistoryModifiers.js b/lib/attachHistoryModifiers.js old mode 100644 new mode 100755 index c91b148..666cad9 --- a/lib/attachHistoryModifiers.js +++ b/lib/attachHistoryModifiers.js @@ -5,10 +5,10 @@ Object.defineProperty(exports,"__esModule",{value:true});exports.default= -attachHistoryModifiers;var _urlParse=require('url-parse');var _urlParse2=_interopRequireDefault(_urlParse);var _navigation=require('./navigation');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};} // HACK global.__BUNDLE_START_TIME__ is only present in React Native -var __WEB__=!global.__BUNDLE_START_TIME__&&window.location.pathname;function attachHistoryModifiers(){var _ref=arguments.length<=0||arguments[0]===undefined?{}:arguments[0];var BackAndroid=_ref.BackAndroid;var Linking=_ref.Linking;return function(createStore){return function(reducer,preloadedState,enhancer){ -var store=createStore(reducer,preloadedState,enhancer);var -dispatch=store.dispatch;var getState=store.getState; +attachHistoryModifiers;var _urlParse=require('url-parse');var _urlParse2=_interopRequireDefault(_urlParse);var _navigation=require('./navigation');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}var __WEB__=!global.__BUNDLE_START_TIME__&&window.location.pathname;function attachHistoryModifiers(){var _ref=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{},BackAndroid=_ref.BackAndroid,Linking=_ref.Linking; +return function(createStore){return function(reducer,preloadedState,enhancer){ +var store=createStore(reducer,preloadedState,enhancer);var +dispatch=store.dispatch,getState=store.getState; if(__WEB__){ window.onpopstate=function(_ref2){var state=_ref2.state; @@ -18,22 +18,24 @@ indexOf(state.index); var lastIndex=getState().navigation.index; if(newIndex<=lastIndex)dispatch((0,_navigation.back)(true)); -if(newIndex>lastIndex)dispatch((0,_navigation.forward)(true));};} - - +if(newIndex>lastIndex)dispatch((0,_navigation.forward)(true)); +}; +} if(BackAndroid){ -BackAndroid.addEventListener('hardwareBackPress',function(){var +BackAndroid.addEventListener('hardwareBackPress',function(){var index=store.getState().navigation.index; if(index===0)return false; dispatch((0,_navigation.back)()); -return true;});} - - +return true; +}); +} if(Linking){ Linking.addEventListener('url',function(_ref3){var url=_ref3.url;var _ref4= -new _urlParse2.default(url);var pathname=_ref4.pathname;var query=_ref4.query;var hash=_ref4.hash; -dispatch((0,_navigation.replaceState)(0,0,''+pathname+query+hash));});} - - - -return store;};};}module.exports=exports['default']; \ No newline at end of file +new _urlParse2.default(url),pathname=_ref4.pathname,query=_ref4.query,hash=_ref4.hash; +dispatch((0,_navigation.replaceState)(0,0,''+pathname+query+hash)); +}); +} + +return store; +};}; +}module.exports=exports['default']; \ No newline at end of file diff --git a/lib/createOrchestrator.js b/lib/createOrchestrator.js old mode 100644 new mode 100755 index d97229e..c37b5e0 --- a/lib/createOrchestrator.js +++ b/lib/createOrchestrator.js @@ -1,26 +1,27 @@ -Object.defineProperty(exports,"__esModule",{value:true});var _extends=Object.assign||function(target){for(var i=1;i at the top '+'of your app.');(0,_invariant2.default)(context.store.getState().navigation,'Couldn\'t find the navigation reducer on the store. '+'Make sure you have react-stack-nav\'s reducer on '+'your root reducer.');return _this2;}_createClass(Orchestrator,[{key:'getChildContext',value:function getChildContext(){return {lastOrchestratorId:this._orchestratorId,orchestratorPath:this._orchestratorPath};}},{key:'componentWillMount',value:function componentWillMount(){var lastOrchestratorId=this.context.lastOrchestratorId;this._orchestratorId=lastOrchestratorId!==undefined?lastOrchestratorId+1:0;this._orchestratorPath=this.context.orchestratorPath?[].concat(_toConsumableArray(this.context.orchestratorPath),[fragment]):[];this._updateUrlStack();this._unsubscribeFromStore=this.context.store.subscribe(this._updateUrlStack);}},{key:'componentWillUnmount',value:function componentWillUnmount(){this._unsubscribeFromStore();}},{key:'render',value:function render() +_this2.setState({ +urlStack:makeStackFromPathname(state.navigation.history[index].url)}); + +};(0,_invariant2.default)(context.store,'Couldn\'t find the store on the context. '+'Make sure you have a redux at the top '+'of your app.');(0,_invariant2.default)(context.store.getState().navigation,'Couldn\'t find the navigation reducer on the store. '+'Make sure you have react-stack-nav\'s reducer on '+'your root reducer.');return _this2;}_createClass(Orchestrator,[{key:'getChildContext',value:function getChildContext(){return{lastOrchestratorId:this._orchestratorId,orchestratorPath:this._orchestratorPath};}},{key:'componentWillMount',value:function componentWillMount(){var lastOrchestratorId=this.context.lastOrchestratorId;this._orchestratorId=lastOrchestratorId!==undefined?lastOrchestratorId+1:0;this._orchestratorPath=this.context.orchestratorPath?[].concat(_toConsumableArray(this.context.orchestratorPath),[fragment]):[];this._updateUrlStack();this._unsubscribeFromStore=this.context.store.subscribe(this._updateUrlStack);}},{key:'componentWillUnmount',value:function componentWillUnmount(){this._unsubscribeFromStore();}},{key:'render',value:function render() @@ -92,12 +93,12 @@ urlStack:makeStackFromPathname(state.navigation.history[index].url)});};(0,_inva { -return ( +return( _react2.default.createElement(ComposedComponent,_extends({}, this.props,{ -routeFragment:this._routeFragment,__source:{fileName:_jsxFileName,lineNumber:96}})));}},{key:'_routeFragment',get:function get(){var _this3=this;var urlMatchesOrchestratorPath=this._orchestratorPath.reduce(function(prev,curr,i){if(!prev)return false;if(!_this3.state.urlStack[i])return false; // Handle regex orchestrators -if(_this3._orchestratorPath[i] instanceof RegExp){return _this3.state.urlStack[i].match(_this3._orchestratorPath[i]);} // Handle string orchestrators -return _this3.state.urlStack[i]===_this3._orchestratorPath[i];},true);if(!urlMatchesOrchestratorPath)return undefined;return this.state.urlStack[this._orchestratorId]||'';}}]);return Orchestrator;}(_react.Component); +routeFragment:this._routeFragment,__source:{fileName:_jsxFileName,lineNumber:97}}))); + +}},{key:'_routeFragment',get:function get(){var _this3=this;var urlMatchesOrchestratorPath=this._orchestratorPath.reduce(function(prev,curr,i){if(!prev)return false;if(!_this3.state.urlStack[i])return false;if(_this3._orchestratorPath[i]instanceof RegExp){return _this3.state.urlStack[i].match(_this3._orchestratorPath[i]);}return _this3.state.urlStack[i]===_this3._orchestratorPath[i];},true);if(!urlMatchesOrchestratorPath)return undefined;return this.state.urlStack[this._orchestratorId]||'';}}]);return Orchestrator;}(_react.Component); Orchestrator.displayName='Orchestrator('+( @@ -105,15 +106,16 @@ _react.Component.displayName||_react.Component.name||'Component')+')'; Orchestrator.contextTypes=_extends({}, Orchestrator.contextTypes,{ -store:_react.PropTypes.object, -lastOrchestratorId:_react.PropTypes.number, -orchestratorPath:_react.PropTypes.array}); +store:_propTypes2.default.object, +lastOrchestratorId:_propTypes2.default.number, +orchestratorPath:_propTypes2.default.array}); Orchestrator.childContextTypes=_extends({}, Orchestrator.childContextTypes,{ -lastOrchestratorId:_react.PropTypes.number, -orchestratorPath:_react.PropTypes.array}); +lastOrchestratorId:_propTypes2.default.number, +orchestratorPath:_propTypes2.default.array}); -return Orchestrator;};};module.exports=exports['default']; \ No newline at end of file +return Orchestrator; +};};module.exports=exports['default']; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js old mode 100644 new mode 100755 index 4a1ff8c..066224e --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,4 @@ Object.defineProperty(exports,"__esModule",{value:true});var _attachHistoryModifiers=require('./attachHistoryModifiers');Object.defineProperty(exports,'attachHistoryModifiers',{enumerable:true,get:function get(){return _interopRequireDefault(_attachHistoryModifiers).default;}});var _createOrchestrator=require('./createOrchestrator');Object.defineProperty(exports,'createOrchestrator',{enumerable:true,get:function get(){return _interopRequireDefault(_createOrchestrator). default;}});var _navigation=require('./navigation'); -Object.keys(_navigation).forEach(function(key){if(key==="default")return;Object.defineProperty(exports,key,{enumerable:true,get:function get(){return _navigation[key];}});});Object.defineProperty(exports,'navigation',{enumerable:true,get:function get(){return _interopRequireDefault(_navigation). +Object.keys(_navigation).forEach(function(key){if(key==="default"||key==="__esModule")return;Object.defineProperty(exports,key,{enumerable:true,get:function get(){return _navigation[key];}});});Object.defineProperty(exports,'navigation',{enumerable:true,get:function get(){return _interopRequireDefault(_navigation). default;}});function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};} \ No newline at end of file diff --git a/lib/navigation.js b/lib/navigation.js old mode 100644 new mode 100755 index 3d8d1b7..64aaf97 --- a/lib/navigation.js +++ b/lib/navigation.js @@ -1,43 +1,43 @@ -Object.defineProperty(exports,"__esModule",{value:true});var _extends=Object.assign||function(target){for(var i=1;i0&&arguments[0]!==undefined?arguments[0]:initialState;var action=arguments[1]; switch(action.type){ -case 'HISTORY_PUSH_STATE':{var _action$payload= -action.payload;var stateObj=_action$payload.stateObj;var title=_action$payload.title;var url=_action$payload.url; +case'HISTORY_PUSH_STATE':{var _action$payload= +action.payload,stateObj=_action$payload.stateObj,title=_action$payload.title,url=_action$payload.url; if(url===state.history[state.index].url)return state; var stateObjWithIndex=_extends({},stateObj,{index:state.index+1}); if(__WEB__)window.history.pushState(stateObjWithIndex,title,url.length?url:'/'); -return { +return{ index:state.index+1, history:state.history. slice(0,state.index+1). -concat([{stateObj:stateObjWithIndex,title:title,url:url}])};} +concat([{stateObj:stateObjWithIndex,title:title,url:url}])}; - -case 'HISTORY_REPLACE_STATE':{var _action$payload2= -action.payload;var _stateObj=_action$payload2.stateObj;var _title=_action$payload2.title;var _url=_action$payload2.url; +} +case'HISTORY_REPLACE_STATE':{var _action$payload2= +action.payload,_stateObj=_action$payload2.stateObj,_title=_action$payload2.title,_url=_action$payload2.url; if(_url===state.history[state.index].url)return state; var _stateObjWithIndex=_extends({},_stateObj,{index:state.index}); if(__WEB__)window.history.replaceState(_stateObjWithIndex,_title,_url.length?_url:'/'); -return { +return{ index:state.index, history:state.history. slice(0,state.index). -concat([{stateObj:_stateObjWithIndex,title:_title,url:_url}])};} - +concat([{stateObj:_stateObjWithIndex,title:_title,url:_url}])}; -case 'HISTORY_REPLACE_TOP':{var _action$payload3= -action.payload;var _stateObj2=_action$payload3.stateObj;var _title2=_action$payload3.title;var _url2=_action$payload3.url; +} +case'HISTORY_REPLACE_TOP':{var _action$payload3= +action.payload,_stateObj2=_action$payload3.stateObj,_title2=_action$payload3.title,_url2=_action$payload3.url; var _stateObjWithIndex2=_extends({},_stateObj2,{index:state.index}); var newUrl=state.history[state.index].url+'/'+_url2; if(__WEB__)window.history.replaceState(_stateObjWithIndex2,_title2,newUrl); -return { +return{ index:state.index, history:state.history. slice(0,state.index). -concat([{stateObj:_stateObjWithIndex2,title:_title2,url:newUrl}])};} - +concat([{stateObj:_stateObjWithIndex2,title:_title2,url:newUrl}])}; -case 'HISTORY_PUSH_TOP':{var _action$payload4= -action.payload;var _stateObj3=_action$payload4.stateObj;var _title3=_action$payload4.title;var _url3=_action$payload4.url; +} +case'HISTORY_PUSH_TOP':{var _action$payload4= +action.payload,_stateObj3=_action$payload4.stateObj,_title3=_action$payload4.title,_url3=_action$payload4.url; var _stateObjWithIndex3=_extends({},_stateObj3,{index:state.index+1}); var _newUrl=state.history[state.index].url+'/'+_url3; if(__WEB__)window.history.pushState(_stateObjWithIndex3,_title3,_newUrl); -return { +return{ index:state.index+1, history:state.history. slice(0,state.index+1). -concat([{stateObj:_stateObjWithIndex3,title:_title3,url:_newUrl}])};} +concat([{stateObj:_stateObjWithIndex3,title:_title3,url:_newUrl}])}; - -case 'HISTORY_BACK': +} +case'HISTORY_BACK': if(state.index===0)return state; return _extends({},state,{index:state.index-1}); -case 'HISTORY_FORWARD': +case'HISTORY_FORWARD': if(state.index===state.history.length-1)return state; return _extends({},state,{index:state.index+1}); -case 'HISTORY_GO':{ +case'HISTORY_GO':{ var targetIndex=state.index+action.payload.numberOfEntries; if(!state.history[targetIndex]){ -console.warn('Tried to `go()` to a non-existant index!'); // eslint-disable-line no-console, max-len -return state;} - -return _extends({},state,{index:targetIndex});} +console.warn('Tried to `go()` to a non-existant index!'); +return state; +} +return _extends({},state,{index:targetIndex}); +} +default:return state;} -default:return state;}}; \ No newline at end of file +}; \ No newline at end of file diff --git a/package.json b/package.json index 18c7328..1f36cd3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-stack-nav", - "version": "0.1.0", + "version": "0.2.0", "description": "Simple navigation for React.", "main": "lib/index.js", "scripts": { @@ -37,8 +37,8 @@ "eslint-plugin-react": "^6.2.0" }, "peerDependencies": { - "react": ">=15.3.2", - "redux": ">=3.0.0" + "react": ">=16.3.0", + "redux": ">=3.7.2" }, "dependencies": { "url-parse": "^1.1.7" diff --git a/src/createOrchestrator.js b/src/createOrchestrator.js index a093ce2..3d36c3d 100644 --- a/src/createOrchestrator.js +++ b/src/createOrchestrator.js @@ -1,4 +1,5 @@ -import React, { Component, PropTypes } from 'react' +import React, { Component } from 'react' +import PropTypes from 'prop-types' import invariant from 'invariant' function makeStackFromPathname(pathname) {