UNPKG

6.31 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9exports.default = createBrowserApp;
10
11var _history = require('history');
12
13var _react = require('react');
14
15var _react2 = _interopRequireDefault(_react);
16
17var _core = require('@react-navigation/core');
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
24
25function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
26
27var queryString = require('query-string');
28
29var history = (0, _history.createBrowserHistory)();
30
31var getPathAndParamsFromLocation = function getPathAndParamsFromLocation(location) {
32 var path = location.pathname.substr(1);
33 var params = queryString.parse(location.search);
34 return { path: path, params: params };
35};
36
37var matchPathAndParams = function matchPathAndParams(a, b) {
38 if (a.path !== b.path) {
39 return false;
40 }
41 if (queryString.stringify(a.params) !== queryString.stringify(b.params)) {
42 return false;
43 }
44 return true;
45};
46
47var currentPathAndParams = getPathAndParamsFromLocation(history.location);
48
49function createBrowserApp(App) {
50 var setHistoryListener = function setHistoryListener(dispatch) {
51 history.listen(function (location) {
52 var pathAndParams = getPathAndParamsFromLocation(location);
53 if (matchPathAndParams(pathAndParams, currentPathAndParams)) {
54 return;
55 }
56 currentPathAndParams = pathAndParams;
57 var action = App.router.getActionForPathAndParams(pathAndParams.path, pathAndParams.params);
58 dispatch(action);
59 });
60 };
61
62 var initAction = App.router.getActionForPathAndParams(currentPathAndParams.path, currentPathAndParams.params) || _core.NavigationActions.init();
63
64 var WebApp = function (_React$Component) {
65 _inherits(WebApp, _React$Component);
66
67 function WebApp() {
68 var _ref;
69
70 var _temp, _this, _ret;
71
72 _classCallCheck(this, WebApp);
73
74 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
75 args[_key] = arguments[_key];
76 }
77
78 return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = WebApp.__proto__ || Object.getPrototypeOf(WebApp)).call.apply(_ref, [this].concat(args))), _this), _this.state = { nav: App.router.getStateForAction(initAction) }, _this._title = document.title, _this._actionEventSubscribers = new Set(), _this._dispatch = function (action) {
79 var lastState = _this.state.nav;
80 var newState = App.router.getStateForAction(action, lastState);
81 var dispatchEvents = function dispatchEvents() {
82 return _this._actionEventSubscribers.forEach(function (subscriber) {
83 return subscriber({
84 type: 'action',
85 action: action,
86 state: newState,
87 lastState: lastState
88 });
89 });
90 };
91 if (newState && newState !== lastState) {
92 _this.setState({ nav: newState }, dispatchEvents);
93 var pathAndParams = App.router.getPathAndParamsForState && App.router.getPathAndParamsForState(newState);
94 if (pathAndParams && !matchPathAndParams(pathAndParams, currentPathAndParams)) {
95 currentPathAndParams = pathAndParams;
96 history.push('/' + pathAndParams.path + '?' + queryString.stringify(pathAndParams.params));
97 }
98 } else {
99 dispatchEvents();
100 }
101 }, _temp), _possibleConstructorReturn(_this, _ret);
102 }
103
104 _createClass(WebApp, [{
105 key: 'componentDidMount',
106 value: function componentDidMount() {
107 var _this2 = this;
108
109 setHistoryListener(this._dispatch);
110 this._actionEventSubscribers.forEach(function (subscriber) {
111 return subscriber({
112 type: 'action',
113 action: initAction,
114 state: _this2.state.nav,
115 lastState: null
116 });
117 });
118 }
119 }, {
120 key: 'componentDidUpdate',
121 value: function componentDidUpdate() {
122 var _navigation = this._navigation,
123 state = _navigation.state,
124 getChildNavigation = _navigation.getChildNavigation;
125
126 var childKey = state.routes[state.index].key;
127 var activeNav = this._navigation.getChildNavigation(childKey);
128 var opts = App.router.getScreenOptions(activeNav);
129 this._title = opts.title || opts.headerTitle;
130 document.title = this._title;
131 }
132 }, {
133 key: 'render',
134 value: function render() {
135 var _this3 = this;
136
137 this._navigation = (0, _core.getNavigation)(App.router, this.state.nav, this._dispatch, this._actionEventSubscribers, function () {
138 return _this3.props.screenProps;
139 }, function () {
140 return _this3._navigation;
141 });
142 return _react2.default.createElement(App, { navigation: this._navigation });
143 }
144 }]);
145
146 return WebApp;
147 }(_react2.default.Component);
148
149 return WebApp;
150}
\No newline at end of file