UNPKG

3.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PathNode = void 0;
4var common_1 = require("../common/common");
5var hof_1 = require("../common/hof");
6var param_1 = require("../params/param");
7/**
8 * A node in a [[TreeChanges]] path
9 *
10 * For a [[TreeChanges]] path, this class holds the stateful information for a single node in the path.
11 * Each PathNode corresponds to a state being entered, exited, or retained.
12 * The stateful information includes parameter values and resolve data.
13 */
14var PathNode = /** @class */ (function () {
15 function PathNode(stateOrNode) {
16 if (stateOrNode instanceof PathNode) {
17 var node = stateOrNode;
18 this.state = node.state;
19 this.paramSchema = node.paramSchema.slice();
20 this.paramValues = common_1.extend({}, node.paramValues);
21 this.resolvables = node.resolvables.slice();
22 this.views = node.views && node.views.slice();
23 }
24 else {
25 var state = stateOrNode;
26 this.state = state;
27 this.paramSchema = state.parameters({ inherit: false });
28 this.paramValues = {};
29 this.resolvables = state.resolvables.map(function (res) { return res.clone(); });
30 }
31 }
32 PathNode.prototype.clone = function () {
33 return new PathNode(this);
34 };
35 /** Sets [[paramValues]] for the node, from the values of an object hash */
36 PathNode.prototype.applyRawParams = function (params) {
37 var getParamVal = function (paramDef) { return [paramDef.id, paramDef.value(params[paramDef.id])]; };
38 this.paramValues = this.paramSchema.reduce(function (memo, pDef) { return common_1.applyPairs(memo, getParamVal(pDef)); }, {});
39 return this;
40 };
41 /** Gets a specific [[Param]] metadata that belongs to the node */
42 PathNode.prototype.parameter = function (name) {
43 return common_1.find(this.paramSchema, hof_1.propEq('id', name));
44 };
45 /**
46 * @returns true if the state and parameter values for another PathNode are
47 * equal to the state and param values for this PathNode
48 */
49 PathNode.prototype.equals = function (node, paramsFn) {
50 var diff = this.diff(node, paramsFn);
51 return diff && diff.length === 0;
52 };
53 /**
54 * Finds Params with different parameter values on another PathNode.
55 *
56 * Given another node (of the same state), finds the parameter values which differ.
57 * Returns the [[Param]] (schema objects) whose parameter values differ.
58 *
59 * Given another node for a different state, returns `false`
60 *
61 * @param node The node to compare to
62 * @param paramsFn A function that returns which parameters should be compared.
63 * @returns The [[Param]]s which differ, or null if the two nodes are for different states
64 */
65 PathNode.prototype.diff = function (node, paramsFn) {
66 if (this.state !== node.state)
67 return false;
68 var params = paramsFn ? paramsFn(this) : this.paramSchema;
69 return param_1.Param.changed(params, this.paramValues, node.paramValues);
70 };
71 /**
72 * Returns a clone of the PathNode
73 * @deprecated use instance method `node.clone()`
74 */
75 PathNode.clone = function (node) { return node.clone(); };
76 return PathNode;
77}());
78exports.PathNode = PathNode;
79//# sourceMappingURL=pathNode.js.map
\No newline at end of file