1 | import { pathOr, equals, path, clone } from 'ramda';
|
2 | import { Injectable, NgModule } from '@angular/core';
|
3 | import { Router, RoutesRecognized, NavigationEnd, NavigationCancel, RouterModule } from '@angular/router';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const ROUTE_PENDING = "ROUTE_PENDING";
|
10 | const ROUTE_CANCEL = "ROUTE_CANCEL";
|
11 | const ROUTE_UPDATE = "ROUTE_UPDATE";
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const 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 | };
|
26 | const getPath = (url = "") => {
|
27 | return url.split(/[?#]/)[0];
|
28 | };
|
29 | const getFragment = (url = "") => {
|
30 | const segments = pathOr(url, ["url"], url).split("/").pop().split(/[#]/);
|
31 | return segments.length > 1 ? segments[1] : "";
|
32 | };
|
33 | const 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 | };
|
42 | const routesAreEqual = (current, next) => {
|
43 | const getProp = (prop, obj) => path([prop], obj);
|
44 | const 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 |
|
53 |
|
54 |
|
55 | const 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 | };
|
68 | const cancelRouteChange = (route) => {
|
69 | return {
|
70 | type: ROUTE_CANCEL,
|
71 | };
|
72 | };
|
73 | const endRouteChange = (route) => {
|
74 | return {
|
75 | type: ROUTE_UPDATE,
|
76 | route,
|
77 | };
|
78 | };
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | class ReduxRouter {
|
85 | |
86 |
|
87 |
|
88 | constructor(router) {
|
89 | this.router = router;
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 | initialize(dispatch) {
|
96 | if (!dispatch) {
|
97 | return console.error("No dispatcher provided.");
|
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 | }
|
113 | ReduxRouter.decorators = [
|
114 | { type: Injectable },
|
115 | ];
|
116 |
|
117 | ReduxRouter.ctorParameters = () => [
|
118 | { type: Router, },
|
119 | ];
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | const REDUX_ROUTER_INITIAL_STATE = {
|
126 | current: null,
|
127 | previous: null,
|
128 | pending: null,
|
129 | };
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | const 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 |
|
156 |
|
157 |
|
158 | class ReduxRouterModule {
|
159 | }
|
160 | ReduxRouterModule.decorators = [
|
161 | { type: NgModule, args: [{
|
162 | imports: [
|
163 | RouterModule,
|
164 | ],
|
165 | providers: [
|
166 | ReduxRouter,
|
167 | ],
|
168 | },] },
|
169 | ];
|
170 |
|
171 | ReduxRouterModule.ctorParameters = () => [];
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | export { ReduxRouter, routerReducer, ROUTE_PENDING, ROUTE_CANCEL, ROUTE_UPDATE, REDUX_ROUTER_INITIAL_STATE, ReduxRouterModule };
|
187 |
|