1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PathUtils = void 0;
|
4 | var common_1 = require("../common/common");
|
5 | var hof_1 = require("../common/hof");
|
6 | var targetState_1 = require("../state/targetState");
|
7 | var pathNode_1 = require("./pathNode");
|
8 |
|
9 |
|
10 |
|
11 | var PathUtils = (function () {
|
12 | function PathUtils() {
|
13 | }
|
14 |
|
15 | PathUtils.makeTargetState = function (registry, path) {
|
16 | var state = common_1.tail(path).state;
|
17 | return new targetState_1.TargetState(registry, state, path.map(hof_1.prop('paramValues')).reduce(common_1.mergeR, {}), {});
|
18 | };
|
19 | PathUtils.buildPath = function (targetState) {
|
20 | var toParams = targetState.params();
|
21 | return targetState.$state().path.map(function (state) { return new pathNode_1.PathNode(state).applyRawParams(toParams); });
|
22 | };
|
23 |
|
24 | PathUtils.buildToPath = function (fromPath, targetState) {
|
25 | var toPath = PathUtils.buildPath(targetState);
|
26 | if (targetState.options().inherit) {
|
27 | return PathUtils.inheritParams(fromPath, toPath, Object.keys(targetState.params()));
|
28 | }
|
29 | return toPath;
|
30 | };
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 | PathUtils.applyViewConfigs = function ($view, path, states) {
|
37 |
|
38 | path
|
39 | .filter(function (node) { return common_1.inArray(states, node.state); })
|
40 | .forEach(function (node) {
|
41 | var viewDecls = common_1.values(node.state.views || {});
|
42 | var subPath = PathUtils.subPath(path, function (n) { return n === node; });
|
43 | var viewConfigs = viewDecls.map(function (view) { return $view.createViewConfig(subPath, view); });
|
44 | node.views = viewConfigs.reduce(common_1.unnestR, []);
|
45 | });
|
46 | };
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | PathUtils.inheritParams = function (fromPath, toPath, toKeys) {
|
59 | if (toKeys === void 0) { toKeys = []; }
|
60 | function nodeParamVals(path, state) {
|
61 | var node = common_1.find(path, hof_1.propEq('state', state));
|
62 | return common_1.extend({}, node && node.paramValues);
|
63 | }
|
64 | var noInherit = fromPath
|
65 | .map(function (node) { return node.paramSchema; })
|
66 | .reduce(common_1.unnestR, [])
|
67 | .filter(function (param) { return !param.inherit; })
|
68 | .map(hof_1.prop('id'));
|
69 | |
70 |
|
71 |
|
72 |
|
73 | function makeInheritedParamsNode(toNode) {
|
74 |
|
75 | var toParamVals = common_1.extend({}, toNode && toNode.paramValues);
|
76 |
|
77 | var incomingParamVals = common_1.pick(toParamVals, toKeys);
|
78 | toParamVals = common_1.omit(toParamVals, toKeys);
|
79 | var fromParamVals = common_1.omit(nodeParamVals(fromPath, toNode.state) || {}, noInherit);
|
80 |
|
81 | var ownParamVals = common_1.extend(toParamVals, fromParamVals, incomingParamVals);
|
82 | return new pathNode_1.PathNode(toNode.state).applyRawParams(ownParamVals);
|
83 | }
|
84 |
|
85 | return toPath.map(makeInheritedParamsNode);
|
86 | };
|
87 | |
88 |
|
89 |
|
90 | PathUtils.treeChanges = function (fromPath, toPath, reloadState) {
|
91 | var max = Math.min(fromPath.length, toPath.length);
|
92 | var keep = 0;
|
93 | var nodesMatch = function (node1, node2) { return node1.equals(node2, PathUtils.nonDynamicParams); };
|
94 | while (keep < max && fromPath[keep].state !== reloadState && nodesMatch(fromPath[keep], toPath[keep])) {
|
95 | keep++;
|
96 | }
|
97 |
|
98 | function applyToParams(retainedNode, idx) {
|
99 | var cloned = retainedNode.clone();
|
100 | cloned.paramValues = toPath[idx].paramValues;
|
101 | return cloned;
|
102 | }
|
103 | var from, retained, exiting, entering, to;
|
104 | from = fromPath;
|
105 | retained = from.slice(0, keep);
|
106 | exiting = from.slice(keep);
|
107 |
|
108 | var retainedWithToParams = retained.map(applyToParams);
|
109 | entering = toPath.slice(keep);
|
110 | to = retainedWithToParams.concat(entering);
|
111 | return { from: from, to: to, retained: retained, retainedWithToParams: retainedWithToParams, exiting: exiting, entering: entering };
|
112 | };
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | PathUtils.matching = function (pathA, pathB, paramsFn) {
|
129 | var done = false;
|
130 | var tuples = common_1.arrayTuples(pathA, pathB);
|
131 | return tuples.reduce(function (matching, _a) {
|
132 | var nodeA = _a[0], nodeB = _a[1];
|
133 | done = done || !nodeA.equals(nodeB, paramsFn);
|
134 | return done ? matching : matching.concat(nodeA);
|
135 | }, []);
|
136 | };
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | PathUtils.equals = function (pathA, pathB, paramsFn) {
|
146 | return pathA.length === pathB.length && PathUtils.matching(pathA, pathB, paramsFn).length === pathA.length;
|
147 | };
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | PathUtils.subPath = function (path, predicate) {
|
159 | var node = common_1.find(path, predicate);
|
160 | var elementIdx = path.indexOf(node);
|
161 | return elementIdx === -1 ? undefined : path.slice(0, elementIdx + 1);
|
162 | };
|
163 | PathUtils.nonDynamicParams = function (node) {
|
164 | return node.state.parameters({ inherit: false }).filter(function (param) { return !param.dynamic; });
|
165 | };
|
166 |
|
167 | PathUtils.paramValues = function (path) { return path.reduce(function (acc, node) { return common_1.extend(acc, node.paramValues); }, {}); };
|
168 | return PathUtils;
|
169 | }());
|
170 | exports.PathUtils = PathUtils;
|
171 |
|
\ | No newline at end of file |