UNPKG

@randy.tarampi/jsx

Version:

Some common JSX components for www.randytarampi.ca

124 lines (95 loc) 4.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getControlStateForId = exports.getIndexForRoute = exports.getRouteForIndex = exports.getSwipeableIndex = exports.getSwipeable = exports.getIndexedRoutes = exports.getRoutes = exports["default"] = exports.uiReducer = void 0; var _immutable = require("connected-react-router/immutable"); var _immutable2 = require("immutable"); var _reactRouterConfig = require("react-router-config"); var _reselect = require("reselect"); var _actions = require("../actions"); function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } var initialState = (0, _immutable2.Map)({ routes: (0, _immutable2.List)(), swipeable: (0, _immutable2.Map)({ index: null, indexLatest: null, meta: null }), controls: (0, _immutable2.Map)() }); var uiReducer = function uiReducer() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; var action = arguments.length > 1 ? arguments[1] : undefined; switch (action.type) { case _immutable.LOCATION_CHANGE: { var location = action.payload.location || action.payload; return state.setIn(["swipeable", "index"], getIndexForRoute(state, location.pathname)); } case _actions.SWIPEABLE_CHANGE_INDEX: case _actions.SWIPEABLE_TAB_CHANGE_INDEX: return state.set("swipeable", (0, _immutable2.fromJS)(action.payload)); case _actions.SET_ROUTES: return state.set("routes", (0, _immutable2.List)(action.payload)); case _actions.SET_CONTROL_STATE: { var _action$payload = action.payload, id = _action$payload.id, updatedControlState = _objectWithoutProperties(_action$payload, ["id"]); var existingControlState = getControlStateForId(state, id) || (0, _immutable2.Map)(); return state.setIn(["controls", id], existingControlState.mergeDeep((0, _immutable2.fromJS)(updatedControlState))); } default: return state; } }; exports.uiReducer = uiReducer; var _default = uiReducer; exports["default"] = _default; var getRoutes = function getRoutes(state) { return state.get("routes"); }; exports.getRoutes = getRoutes; var getIndexedRoutes = (0, _reselect.createSelector)(getRoutes, function (routes) { return routes.filter(function (route) { return !!route.tab; }); }); exports.getIndexedRoutes = getIndexedRoutes; var getSwipeable = function getSwipeable(state) { return state.get("swipeable"); }; exports.getSwipeable = getSwipeable; var getSwipeableIndex = (0, _reselect.createSelector)(getSwipeable, function (swipeableState) { return swipeableState ? swipeableState.get("index") : null; }); exports.getSwipeableIndex = getSwipeableIndex; var getRouteForIndex = function getRouteForIndex(state, index) { var indexedRoutes = getIndexedRoutes(state); var foundRoute = indexedRoutes && indexedRoutes.get(index); return foundRoute || null; }; exports.getRouteForIndex = getRouteForIndex; var getIndexForRoute = function getIndexForRoute(state, pathname) { var indexedRoutes = getIndexedRoutes(state); var matchedRoutes = (0, _reactRouterConfig.matchRoutes)(indexedRoutes, pathname); var bestMatchedRoute = matchedRoutes[matchedRoutes.length - 1]; var routeForIndexSearch = bestMatchedRoute && bestMatchedRoute.route; var bestRouteIndex; do { if (routeForIndexSearch) { bestRouteIndex = indexedRoutes.findIndex(function (indexedRoute) { return indexedRoute.path === routeForIndexSearch.path; }); routeForIndexSearch = routeForIndexSearch.parent; } } while (bestRouteIndex === -1 && routeForIndexSearch); return Number.isFinite(bestRouteIndex) && bestRouteIndex !== -1 ? bestRouteIndex : null; }; exports.getIndexForRoute = getIndexForRoute; var getControlStateForId = function getControlStateForId(state, id) { return state.getIn(["controls", id]); }; exports.getControlStateForId = getControlStateForId;