one
Version:
One is a React framework for web, iOS, and Android apps.
143 lines (140 loc) • 4.61 kB
JavaScript
import { getReactNavigationRouteName } from "../getReactNavigationConfig.native.js";
function findRouteNodeFromState(state, rootNode) {
var _state_index;
if (!state || !state.routes || !rootNode) {
return null;
}
var currentRoute = state.routes[(_state_index = state.index) !== null && _state_index !== void 0 ? _state_index : state.routes.length - 1];
if (!currentRoute) {
return null;
}
var matchingNode = findNodeByRouteName(rootNode, currentRoute.name);
if (!matchingNode) {
return null;
}
if (currentRoute.state && currentRoute.state.routes) {
var nestedResult = findRouteNodeFromState(currentRoute.state, matchingNode);
if (nestedResult) {
return nestedResult;
}
}
return matchingNode;
}
function findNodeByRouteName(node, routeName) {
if (getReactNavigationRouteName(node) === routeName) {
return node;
}
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
try {
for (var _iterator = node.children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var child = _step.value;
var found = findNodeByRouteName(child, routeName);
if (found) {
return found;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return null;
}
function extractParamsFromState(state) {
var _state_index;
if (!state || !state.routes) {
return {};
}
var params = {};
var route = state.routes[(_state_index = state.index) !== null && _state_index !== void 0 ? _state_index : state.routes.length - 1];
if (route === null || route === void 0 ? void 0 : route.params) {
Object.assign(params, route.params);
}
if (route === null || route === void 0 ? void 0 : route.state) {
Object.assign(params, extractParamsFromState(route.state));
}
return params;
}
function extractSearchFromHref(href) {
var searchIndex = href.indexOf("?");
if (searchIndex === -1) {
return {};
}
var searchString = href.slice(searchIndex + 1);
var params = {};
var searchParams = new URLSearchParams(searchString);
searchParams.forEach(function(value, key) {
var existing = params[key];
if (existing === void 0) {
params[key] = value;
} else if (Array.isArray(existing)) {
existing.push(value);
} else {
params[key] = [existing, value];
}
});
return params;
}
function extractPathnameFromHref(href) {
var searchIndex = href.indexOf("?");
var hashIndex = href.indexOf("#");
var endIndex = href.length;
if (searchIndex !== -1) endIndex = Math.min(endIndex, searchIndex);
if (hashIndex !== -1) endIndex = Math.min(endIndex, hashIndex);
return href.slice(0, endIndex);
}
function findAllRouteNodesFromState(state, rootNode) {
if (!state || !state.routes || !rootNode) {
return [];
}
var nodes = [];
function collectNodes(currentState, parentNode) {
var _currentState_index;
var _currentRoute_params;
if (!currentState || !currentState.routes || !parentNode) {
return;
}
var currentRoute = currentState.routes[(_currentState_index = currentState.index) !== null && _currentState_index !== void 0 ? _currentState_index : currentState.routes.length - 1];
if (!currentRoute) {
return;
}
var matchingNode = findNodeByRouteName(parentNode, currentRoute.name);
if (!matchingNode) {
if (process.env.NODE_ENV === "development") {
console.log("[one] findAllRouteNodesFromState: could not find node for", currentRoute.name, "in", parentNode.route);
}
return;
}
nodes.push(matchingNode);
if (currentRoute.state && currentRoute.state.routes) {
collectNodes(currentRoute.state, matchingNode);
} else if ((_currentRoute_params = currentRoute.params) === null || _currentRoute_params === void 0 ? void 0 : _currentRoute_params.screen) {
var childRouteName = currentRoute.params.screen;
var childNode = matchingNode.children.find(function(c) {
return c.route === childRouteName;
});
if (childNode) {
nodes.push(childNode);
if (currentRoute.params.params && currentRoute.params.params.screen) {
collectNodes({ routes: [{
name: childRouteName,
params: currentRoute.params.params
}] }, childNode);
}
}
}
}
collectNodes(state, rootNode);
return nodes;
}
export { extractParamsFromState, extractPathnameFromHref, extractSearchFromHref, findAllRouteNodesFromState, findRouteNodeFromState };
//# sourceMappingURL=findRouteNode.native.js.map