|
1 | 1 | /** @module params */ /** for typedoc */ |
2 | | -import {forEach, ancestors, extend, copy, pick, omit} from "../common/common"; |
| 2 | +import {extend, ancestors} from "../common/common"; |
3 | 3 |
|
4 | 4 | export class StateParams { |
5 | 5 | constructor(params: Object = {}) { |
6 | 6 | extend(this, params); |
7 | 7 | } |
8 | 8 |
|
9 | | - $digest() {} |
10 | | - $inherit(newParams, $current, $to) {} |
11 | | - $set(params, url) {} |
12 | | - $sync() {} |
13 | | - $off() {} |
14 | | - $raw() {} |
15 | | - $localize(state, params) {} |
16 | | - $observe(key: string, fn: Function) {} |
17 | | -} |
18 | | - |
19 | | - |
20 | | - export function stateParamsFactory() { |
21 | | - let observers = {}, current = {}; |
22 | | - |
23 | | - function unhook(key, func) { |
24 | | - return () => { |
25 | | - forEach(key.split(" "), k => observers[k].splice(observers[k].indexOf(func), 1)); |
26 | | - }; |
27 | | - } |
28 | | - |
29 | | - function observeChange(key, val?: any) { |
30 | | - if (!observers[key] || !observers[key].length) return; |
31 | | - forEach(observers[key], func => func(val)); |
32 | | - } |
33 | | - |
34 | | - |
35 | | - StateParams.prototype.$digest = function() { |
36 | | - forEach(this, (val, key) => { |
37 | | - if (val === current[key] || !this.hasOwnProperty(key)) return; |
38 | | - current[key] = val; |
39 | | - observeChange(key, val); |
40 | | - }); |
41 | | - }; |
42 | | - |
43 | | - /** |
44 | | - * Merges a set of parameters with all parameters inherited between the common parents of the |
45 | | - * current state and a given destination state. |
46 | | - * |
47 | | - * @param {Object} newParams The set of parameters which will be composited with inherited params. |
48 | | - * @param {Object} $current Internal definition of object representing the current state. |
49 | | - * @param {Object} $to Internal definition of object representing state to transition to. |
50 | | - */ |
51 | | - StateParams.prototype.$inherit = function(newParams, $current, $to) { |
52 | | - let parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = []; |
53 | | - |
54 | | - for (let i in parents) { |
55 | | - if (!parents[i] || !parents[i].params) continue; |
56 | | - parentParams = Object.keys(parents[i].params); |
57 | | - if (!parentParams.length) continue; |
58 | | - |
59 | | - for (let j in parentParams) { |
60 | | - if (inheritList.indexOf(parentParams[j]) >= 0) continue; |
61 | | - inheritList.push(parentParams[j]); |
62 | | - inherited[parentParams[j]] = this[parentParams[j]]; |
63 | | - } |
64 | | - } |
65 | | - return extend({}, inherited, newParams); |
66 | | - }; |
67 | | - |
68 | | - StateParams.prototype.$set = function(params, url) { |
69 | | - let hasChanged = false, abort = false; |
70 | | - |
71 | | - if (url) { |
72 | | - forEach(params, function(val, key) { |
73 | | - if ((url.parameter(key) || {}).dynamic !== true) abort = true; |
74 | | - }); |
| 9 | + /** |
| 10 | + * Merges a set of parameters with all parameters inherited between the common parents of the |
| 11 | + * current state and a given destination state. |
| 12 | + * |
| 13 | + * @param {Object} newParams The set of parameters which will be composited with inherited params. |
| 14 | + * @param {Object} $current Internal definition of object representing the current state. |
| 15 | + * @param {Object} $to Internal definition of object representing state to transition to. |
| 16 | + */ |
| 17 | + $inherit(newParams, $current, $to) { |
| 18 | + let parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = []; |
| 19 | + |
| 20 | + for (let i in parents) { |
| 21 | + if (!parents[i] || !parents[i].params) continue; |
| 22 | + parentParams = Object.keys(parents[i].params); |
| 23 | + if (!parentParams.length) continue; |
| 24 | + |
| 25 | + for (let j in parentParams) { |
| 26 | + if (inheritList.indexOf(parentParams[j]) >= 0) continue; |
| 27 | + inheritList.push(parentParams[j]); |
| 28 | + inherited[parentParams[j]] = this[parentParams[j]]; |
75 | 29 | } |
76 | | - if (abort) return false; |
77 | | - |
78 | | - forEach(params, (val, key) => { |
79 | | - if (val !== this[key]) { |
80 | | - this[key] = val; |
81 | | - observeChange(key); |
82 | | - hasChanged = true; |
83 | | - } |
84 | | - }); |
85 | | - |
86 | | - this.$sync(); |
87 | | - return hasChanged; |
88 | | - }; |
89 | | - |
90 | | - StateParams.prototype.$sync = function() { |
91 | | - copy(this, current); |
92 | | - return this; |
93 | | - }; |
94 | | - |
95 | | - StateParams.prototype.$off = function() { |
96 | | - observers = {}; |
97 | | - return this; |
98 | | - }; |
99 | | - |
100 | | - StateParams.prototype.$raw = function() { |
101 | | - return omit( |
102 | | - this, |
103 | | - Object.keys(this).filter(StateParams.prototype.hasOwnProperty.bind(StateParams.prototype)) |
104 | | - ); |
105 | | - }; |
106 | | - |
107 | | - StateParams.prototype.$localize = function(state, params) { |
108 | | - return new StateParams(pick(params || this, Object.keys(state.params))); |
109 | | - }; |
110 | | - |
111 | | - StateParams.prototype.$observe = function(key: string, fn: Function) { |
112 | | - forEach(key.split(" "), k => (observers[k] || (observers[k] = [])).push(fn)); |
113 | | - return unhook(key, fn); |
114 | | - }; |
| 30 | + } |
| 31 | + return extend({}, inherited, newParams); |
| 32 | + }; |
| 33 | +} |
115 | 34 |
|
116 | | - return new StateParams(); |
117 | | - } |
| 35 | +export function stateParamsFactory() { |
| 36 | + return new StateParams(); |
| 37 | +} |
0 commit comments