@randy.tarampi/jsx
Version:
Some common JSX components for www.randytarampi.ca
79 lines (69 loc) • 3.7 kB
JavaScript
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; }
import { LOCATION_CHANGE } from "connected-react-router/immutable";
import { fromJS, List, Map } from "immutable";
import { matchRoutes } from "react-router-config";
import { createSelector } from "reselect";
import { SET_CONTROL_STATE, SET_ROUTES, SWIPEABLE_CHANGE_INDEX, SWIPEABLE_TAB_CHANGE_INDEX } from "../actions";
var initialState = Map({
routes: List(),
swipeable: Map({
index: null,
indexLatest: null,
meta: null
}),
controls: Map()
});
export 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 LOCATION_CHANGE:
{
var location = action.payload.location || action.payload;
return state.setIn(["swipeable", "index"], getIndexForRoute(state, location.pathname));
}
case SWIPEABLE_CHANGE_INDEX:
case SWIPEABLE_TAB_CHANGE_INDEX:
return state.set("swipeable", fromJS(action.payload));
case SET_ROUTES:
return state.set("routes", List(action.payload));
case SET_CONTROL_STATE:
{
var _action$payload = action.payload,
{
id
} = _action$payload,
updatedControlState = _objectWithoutProperties(_action$payload, ["id"]);
var existingControlState = getControlStateForId(state, id) || Map();
return state.setIn(["controls", id], existingControlState.mergeDeep(fromJS(updatedControlState)));
}
default:
return state;
}
};
export default uiReducer;
export var getRoutes = state => state.get("routes");
export var getIndexedRoutes = createSelector(getRoutes, routes => routes.filter(route => !!route.tab));
export var getSwipeable = state => state.get("swipeable");
export var getSwipeableIndex = createSelector(getSwipeable, swipeableState => swipeableState ? swipeableState.get("index") : null);
export var getRouteForIndex = (state, index) => {
var indexedRoutes = getIndexedRoutes(state);
var foundRoute = indexedRoutes && indexedRoutes.get(index);
return foundRoute || null;
};
export var getIndexForRoute = (state, pathname) => {
var indexedRoutes = getIndexedRoutes(state);
var matchedRoutes = matchRoutes(indexedRoutes, pathname);
var bestMatchedRoute = matchedRoutes[matchedRoutes.length - 1];
var routeForIndexSearch = bestMatchedRoute && bestMatchedRoute.route;
var bestRouteIndex;
do {
if (routeForIndexSearch) {
bestRouteIndex = indexedRoutes.findIndex(indexedRoute => indexedRoute.path === routeForIndexSearch.path);
routeForIndexSearch = routeForIndexSearch.parent;
}
} while (bestRouteIndex === -1 && routeForIndexSearch);
return Number.isFinite(bestRouteIndex) && bestRouteIndex !== -1 ? bestRouteIndex : null;
};
export var getControlStateForId = (state, id) => state.getIn(["controls", id]);