/
stateParams.ts
38 lines (33 loc) · 1.31 KB
/
stateParams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { extend, ancestors, Obj } from '../common/common';
import { StateObject } from '../state/stateObject';
export class StateParams {
[key: string]: any;
constructor(params: Obj = {}) {
extend(this, params);
}
/**
* Merges a set of parameters with all parameters inherited between the common parents of the
* current state and a given destination state.
*
* @param {Object} newParams The set of parameters which will be composited with inherited params.
* @param {Object} $current Internal definition of object representing the current state.
* @param {Object} $to Internal definition of object representing state to transition to.
*/
$inherit(newParams: Obj, $current: StateObject, $to: StateObject) {
let parentParams: string[];
const parents = ancestors($current, $to),
inherited: Obj = {},
inheritList: string[] = [];
for (const i in parents) {
if (!parents[i] || !parents[i].params) continue;
parentParams = Object.keys(parents[i].params);
if (!parentParams.length) continue;
for (const j in parentParams) {
if (inheritList.indexOf(parentParams[j]) >= 0) continue;
inheritList.push(parentParams[j]);
inherited[parentParams[j]] = this[parentParams[j]];
}
}
return extend({}, inherited, newParams);
}
}