-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdsr.ts
211 lines (178 loc) · 7.15 KB
/
dsr.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {
StateObject,
StateDeclaration,
Param,
UIRouter,
RawParams,
StateOrName,
TargetState,
Transition,
UIRouterPlugin,
TransitionService,
StateService,
} from '@uirouter/core';
import { DSRDataStore, StateObjectDataStore } from './DSRDataStore';
import { _DSRConfig, DSRConfigObj, DSRFunction, DSRProp, ParamPredicate, RecordedDSR } from './interface';
class DSRPlugin implements UIRouterPlugin {
name = 'deep-state-redirect';
dataStore: DSRDataStore;
$transitions: TransitionService;
$state: StateService;
hookDeregFns = [];
constructor($uiRouter: UIRouter, options: { dataStore: DSRDataStore }) {
this.$transitions = $uiRouter.transitionService;
this.$state = $uiRouter.stateService;
this.dataStore = (options && options.dataStore) || new StateObjectDataStore();
this.dataStore.init($uiRouter);
this.hookDeregFns.push(
this.$transitions.onRetain(
{ retained: (state) => !!this.getDsrProp(state.self) },
this.recordDeepState.bind(this)
)
);
this.hookDeregFns.push(
this.$transitions.onEnter({ entering: (state) => !!this.getDsrProp(state.self) }, this.recordDeepState.bind(this))
);
this.hookDeregFns.push(
this.$transitions.onBefore({ to: (state) => !!this.getDsrProp(state.self) }, this.deepStateRedirect.bind(this))
);
}
dispose(_router: UIRouter): void {
this.hookDeregFns.forEach((fn) => fn());
}
/**
* Resets deep state redirect
*
* A deep state is recorded for each DSR state.
* This function resets recorded deep state redirect(s) to the initial value.
*
* If called with no parameters, the redirects for all states are reset.
*
* If called with a `state` parameter, the redirect for that state is reset.
*
* If called with `state` and `params` parameters, the redirect for that state and set of parameter values is reset.
*
* @param state (optional) the redirect for this state will be reset
* @param params (optional) the redirect for the state and parameters will be reset
*/
reset(state?: StateOrName, params?: RawParams): void {
const { $state } = this;
if (!state) {
$state.get().forEach((_state) => this.dataStore.set(_state, undefined));
} else if (!params) {
this.dataStore.set(state, undefined);
} else {
const currentDSRS = this.dataStore.get(state);
const $$state = $state.get(state).$$state();
this.dataStore.set(state, currentDSRS.filter(this.paramsEqual($$state, params, undefined, true)));
}
}
/**
* Returns the recorded redirect
*
* Returns the recorded redirect for a given DSR `state` (and optionally `params`).
*
* @param state the DSR state
* @param params (optional) the parameter values
*
* @returns the recorded redirect `TargetState`
*/
getRedirect(state: StateOrName, params?: RawParams): TargetState {
return this.getDeepStateRedirect(state, params);
}
private getDsrProp(state: StateDeclaration): DSRProp {
return state.deepStateRedirect || state.dsr;
}
public getConfig(state: StateDeclaration): _DSRConfig {
const { $state } = this;
const dsrProp: DSRProp = this.getDsrProp(state);
if (typeof dsrProp === 'undefined') return;
let params: ParamPredicate;
let defaultTarget: TargetState = typeof dsrProp === 'string' ? $state.target(dsrProp) : undefined;
let fn: DSRFunction = typeof dsrProp === 'function' ? dsrProp : undefined;
if (typeof dsrProp === 'object') {
fn = dsrProp.fn;
if (typeof dsrProp.default === 'object') {
defaultTarget = $state.target(dsrProp.default.state, dsrProp.default.params, dsrProp.default.options);
} else if (typeof dsrProp.default === 'string') {
defaultTarget = $state.target(dsrProp.default);
}
const paramsProp = (dsrProp as DSRConfigObj).params;
if (paramsProp === true) {
params = () => true;
} else if (Array.isArray(paramsProp)) {
params = (param: Param) => paramsProp.indexOf(param.id) !== -1;
}
}
fn = fn || (((transition: Transition, target: TargetState) => target) as DSRFunction);
return { default: defaultTarget, params, fn };
}
public paramsEqual(
state: StateObject,
transParams: RawParams,
paramPredicate: ParamPredicate = () => true,
negate = false
): (redirect: RecordedDSR) => boolean {
const schema = state.parameters({ inherit: true }).filter(paramPredicate);
return (redirect: RecordedDSR) => {
const equals = Param.equals(schema, redirect.triggerParams, transParams);
return negate ? !equals : equals;
};
}
private recordDeepState(transition: Transition, state: StateDeclaration): void {
const { $state } = this;
const hasParamsConfig = !!this.getConfig(state).params;
const _state: StateObject = state.$$state();
transition.promise.then(() => {
const transTo = transition.to();
const triggerParams = transition.params();
const target: TargetState = $state.target(transTo, triggerParams);
const targetStateName = target.name();
const targetParams = target.params();
const recordedDSR: RecordedDSR = { triggerParams, targetStateName, targetParams };
if (hasParamsConfig) {
const currentDSRS: RecordedDSR[] = this.dataStore.get(_state);
const predicate = this.paramsEqual(transTo.$$state(), triggerParams, this.getConfig(state).params, true);
const updatedDSRS = currentDSRS.filter(predicate).concat(recordedDSR);
this.dataStore.set(_state, updatedDSRS);
} else {
this.dataStore.set(_state, [recordedDSR]);
}
});
}
private deepStateRedirect(transition: Transition): TargetState | undefined {
const opts = transition.options();
if (opts['ignoreDsr'] || (opts.custom && opts.custom.ignoreDsr)) return;
const config: _DSRConfig = this.getConfig(transition.to());
let redirect: TargetState = this.getDeepStateRedirect(transition.to(), transition.params());
redirect = config.fn(transition, redirect);
if (redirect && redirect.state() === transition.to()) return;
return redirect;
}
private getTargetState(dsr: RecordedDSR): TargetState {
return this.$state.target(dsr.targetStateName, dsr.targetParams);
}
private getDeepStateRedirect(stateOrName: StateOrName, params: RawParams): TargetState {
const { $state } = this;
const _state = $state.get(stateOrName);
const state = _state && _state.$$state();
const config: _DSRConfig = this.getConfig(_state);
const currentDSRS = this.dataStore.get(stateOrName);
let recordedDSR: RecordedDSR;
if (config.params) {
const predicate = this.paramsEqual(state, params, config.params, false);
const match = currentDSRS.find(predicate);
recordedDSR = match && match;
} else {
recordedDSR = currentDSRS[0] && currentDSRS[0];
}
let dsrTarget = recordedDSR ? this.getTargetState(recordedDSR) : config.default;
if (dsrTarget) {
// merge original params with deep state redirect params
const targetParams = Object.assign({}, params, dsrTarget.params());
dsrTarget = $state.target(dsrTarget.state(), targetParams, dsrTarget.options());
}
return dsrTarget;
}
}
export { DSRPlugin };