UNPKG

5.14 kBJavaScriptView Raw
1import { pathOr, equals, path, clone } from 'ramda';
2import { Injectable, NgModule } from '@angular/core';
3import { Router, RoutesRecognized, NavigationEnd, NavigationCancel, RouterModule } from '@angular/router';
4
5/**
6 * @fileoverview added by tsickle
7 * @suppress {checkTypes} checked by tsc
8 */
9const ROUTE_PENDING = "ROUTE_PENDING";
10const ROUTE_CANCEL = "ROUTE_CANCEL";
11const ROUTE_UPDATE = "ROUTE_UPDATE";
12
13/**
14 * @fileoverview added by tsickle
15 * @suppress {checkTypes} checked by tsc
16 */
17const rebuildStateTree = (rootState, key) => {
18 if (!rootState) {
19 return {};
20 }
21 if (rootState.firstChild) {
22 return Object.assign({}, rootState[key], rebuildStateTree(rootState.firstChild, key));
23 }
24 return Object.assign({}, rootState[key]);
25};
26const getPath = (url = "") => {
27 return url.split(/[?#]/)[0];
28};
29const getFragment = (url = "") => {
30 const /** @type {?} */ segments = pathOr(url, ["url"], url).split("/").pop().split(/[#]/);
31 return segments.length > 1 ? segments[1] : "";
32};
33const getData = (rootState) => {
34 if (!rootState) {
35 return null;
36 }
37 if (rootState.firstChild) {
38 return getData(rootState.firstChild);
39 }
40 return clone(rootState.data);
41};
42const routesAreEqual = (current, next) => {
43 const /** @type {?} */ getProp = (prop, obj) => path([prop], obj);
44 const /** @type {?} */ equalProp = (prop, ...objs) => equals.apply(null, objs.map((obj) => getProp(prop, obj)));
45 return (equalProp("url", current, next) &&
46 equalProp("params", current, next) &&
47 equalProp("query", current, next) &&
48 equalProp("fragment", current, next));
49};
50
51/**
52 * @fileoverview added by tsickle
53 * @suppress {checkTypes} checked by tsc
54 */
55const initRouteChange = (route) => {
56 return {
57 type: ROUTE_PENDING,
58 route: {
59 url: route.urlAfterRedirects || route.url,
60 path: getPath(route.urlAfterRedirects || route.url),
61 params: rebuildStateTree(route.state.root, "params"),
62 query: rebuildStateTree(route.state.root, "queryParams"),
63 fragment: getFragment(route.url),
64 data: getData(route.state.root),
65 },
66 };
67};
68const cancelRouteChange = (route) => {
69 return {
70 type: ROUTE_CANCEL,
71 };
72};
73const endRouteChange = (route) => {
74 return {
75 type: ROUTE_UPDATE,
76 route,
77 };
78};
79
80/**
81 * @fileoverview added by tsickle
82 * @suppress {checkTypes} checked by tsc
83 */
84class ReduxRouter {
85 /**
86 * @param {?} router
87 */
88 constructor(router) {
89 this.router = router;
90 }
91 /**
92 * @param {?} dispatch
93 * @return {?}
94 */
95 initialize(dispatch) {
96 if (!dispatch) {
97 return console.error("No dispatcher provided."); // tslint:disable-line:no-console
98 }
99 this.routerSubscription = this.router.events
100 .subscribe((routeEvent) => {
101 if (routeEvent instanceof RoutesRecognized) {
102 return dispatch(initRouteChange(routeEvent));
103 }
104 if (routeEvent instanceof NavigationCancel) {
105 return dispatch(cancelRouteChange(routeEvent));
106 }
107 if (routeEvent instanceof NavigationEnd) {
108 return dispatch(endRouteChange(routeEvent));
109 }
110 });
111 }
112}
113ReduxRouter.decorators = [
114 { type: Injectable },
115];
116/** @nocollapse */
117ReduxRouter.ctorParameters = () => [
118 { type: Router, },
119];
120
121/**
122 * @fileoverview added by tsickle
123 * @suppress {checkTypes} checked by tsc
124 */
125const REDUX_ROUTER_INITIAL_STATE = {
126 current: null,
127 previous: null,
128 pending: null,
129};
130
131/**
132 * @fileoverview added by tsickle
133 * @suppress {checkTypes} checked by tsc
134 */
135const routerReducer = (state = REDUX_ROUTER_INITIAL_STATE, action) => {
136 if (action.type === ROUTE_PENDING) {
137 if (!routesAreEqual(state.current, action.route)) {
138 return Object.assign({}, state, { pending: clone(action.route) });
139 }
140 }
141 if (action.type === ROUTE_CANCEL) {
142 return Object.assign({}, state, { pending: null });
143 }
144 if (action.type === ROUTE_UPDATE) {
145 return {
146 current: clone(state.pending),
147 previous: clone(state.current),
148 pending: null,
149 };
150 }
151 return state;
152};
153
154/**
155 * @fileoverview added by tsickle
156 * @suppress {checkTypes} checked by tsc
157 */
158class ReduxRouterModule {
159}
160ReduxRouterModule.decorators = [
161 { type: NgModule, args: [{
162 imports: [
163 RouterModule,
164 ],
165 providers: [
166 ReduxRouter,
167 ],
168 },] },
169];
170/** @nocollapse */
171ReduxRouterModule.ctorParameters = () => [];
172
173/**
174 * @fileoverview added by tsickle
175 * @suppress {checkTypes} checked by tsc
176 */
177
178/**
179 * @fileoverview added by tsickle
180 * @suppress {checkTypes} checked by tsc
181 */
182/**
183 * Generated bundle index. Do not edit.
184 */
185
186export { ReduxRouter, routerReducer, ROUTE_PENDING, ROUTE_CANCEL, ROUTE_UPDATE, REDUX_ROUTER_INITIAL_STATE, ReduxRouterModule };
187//# sourceMappingURL=district01-ng-redux-router.js.map