mobx-route 1.2.3 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +10 -737
- package/index.cjs.map +1 -1
- package/index.d.ts +34 -0
- package/index.js +10 -738
- package/index.js.map +1 -1
- package/package.json +9 -1
- package/react.cjs +5 -2
- package/react.cjs.map +1 -1
- package/react.d.ts +4 -0
- package/react.js +6 -3
- package/react.js.map +1 -1
- package/view-model.cjs +58 -43
- package/view-model.cjs.map +1 -1
- package/view-model.d.ts +383 -21
- package/view-model.js +59 -44
- package/view-model.js.map +1 -1
- package/virtual-route-CFGwOGHi.cjs +832 -0
- package/virtual-route-CFGwOGHi.cjs.map +1 -0
- package/virtual-route-fZzKp_7K.js +833 -0
- package/virtual-route-fZzKp_7K.js.map +1 -0
package/index.cjs
CHANGED
|
@@ -1,745 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const mobxLocationHistory = require("mobx-location-history");
|
|
4
|
-
const
|
|
5
|
-
const mobx = require("mobx");
|
|
6
|
-
const pathToRegexp = require("path-to-regexp");
|
|
7
|
-
const mobx$1 = require("yummies/mobx");
|
|
8
|
-
const common = require("yummies/common");
|
|
9
|
-
const routeConfig = complex.createGlobalDynamicConfig(
|
|
10
|
-
(update, current) => {
|
|
11
|
-
let history;
|
|
12
|
-
let queryParams;
|
|
13
|
-
if (update?.history) {
|
|
14
|
-
history = update.history;
|
|
15
|
-
queryParams = update.queryParams;
|
|
16
|
-
if (current?.history && mobxLocationHistory.isObservableHistory(current.history)) {
|
|
17
|
-
current.history.destroy();
|
|
18
|
-
}
|
|
19
|
-
} else if (current?.history) {
|
|
20
|
-
history = current.history;
|
|
21
|
-
queryParams = update?.queryParams ?? current.queryParams;
|
|
22
|
-
} else {
|
|
23
|
-
history = mobxLocationHistory.createBrowserHistory();
|
|
24
|
-
}
|
|
25
|
-
queryParams ??= mobxLocationHistory.createQueryParams({ history });
|
|
26
|
-
return {
|
|
27
|
-
...update,
|
|
28
|
-
history,
|
|
29
|
-
queryParams
|
|
30
|
-
};
|
|
31
|
-
},
|
|
32
|
-
/* @__PURE__ */ Symbol.for("MOBX_ROUTE_CONFIG")
|
|
33
|
-
);
|
|
34
|
-
const annotations$3 = [
|
|
35
|
-
[
|
|
36
|
-
mobx.computed,
|
|
37
|
-
"isPathMatched",
|
|
38
|
-
"isOpened",
|
|
39
|
-
"isOpening",
|
|
40
|
-
"path",
|
|
41
|
-
"absolutePath",
|
|
42
|
-
"hasOpenedChildren",
|
|
43
|
-
"isAbleToMergeQuery",
|
|
44
|
-
"baseUrl"
|
|
45
|
-
],
|
|
46
|
-
[mobx.computed.struct, "parsedPathData", "params"],
|
|
47
|
-
[mobx.observable, "children"],
|
|
48
|
-
[mobx.observable.ref, "parent", "status"]
|
|
49
|
-
];
|
|
50
|
-
class Route {
|
|
51
|
-
constructor(pathDeclaration, config = {}) {
|
|
52
|
-
this.config = config;
|
|
53
|
-
this.history = config.history ?? routeConfig.get().history;
|
|
54
|
-
this.query = config.queryParams ?? routeConfig.get().queryParams;
|
|
55
|
-
this.pathDeclaration = pathDeclaration;
|
|
56
|
-
this.isIndex = !!this.config.index;
|
|
57
|
-
this.isHash = !!this.config.hash;
|
|
58
|
-
this.meta = this.config.meta;
|
|
59
|
-
this.status = "unknown";
|
|
60
|
-
this.parent = config.parent ?? null;
|
|
61
|
-
mobx$1.applyObservable(this, annotations$3);
|
|
62
|
-
if (this.config.abortSignal?.aborted) {
|
|
63
|
-
this.isDestroyed = true;
|
|
64
|
-
} else {
|
|
65
|
-
this.disposer = mobx.reaction(() => this.isPathMatched, this.checkPathMatch, {
|
|
66
|
-
fireImmediately: true
|
|
67
|
-
});
|
|
68
|
-
this.config.abortSignal?.addEventListener("abort", () => this.destroy(), {
|
|
69
|
-
once: true
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
config;
|
|
74
|
-
isDestroyed;
|
|
75
|
-
disposer;
|
|
76
|
-
history;
|
|
77
|
-
/**
|
|
78
|
-
* Parent route.
|
|
79
|
-
*
|
|
80
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#parent)
|
|
81
|
-
*/
|
|
82
|
-
parent;
|
|
83
|
-
query;
|
|
84
|
-
_tokenData;
|
|
85
|
-
_matcher;
|
|
86
|
-
_compiler;
|
|
87
|
-
ignoreOpenByPathMatch = false;
|
|
88
|
-
status;
|
|
89
|
-
meta;
|
|
90
|
-
/**
|
|
91
|
-
* Route path pattern declaration.
|
|
92
|
-
*
|
|
93
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#pathdeclaration)
|
|
94
|
-
*/
|
|
95
|
-
pathDeclaration;
|
|
96
|
-
/**
|
|
97
|
-
* Indicates if this route is an index route. Index routes activate when parent route path matches exactly.
|
|
98
|
-
*
|
|
99
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isindex)
|
|
100
|
-
*/
|
|
101
|
-
isIndex;
|
|
102
|
-
/**
|
|
103
|
-
* Indicates if this route is an hash route.
|
|
104
|
-
*
|
|
105
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#ishash)
|
|
106
|
-
*/
|
|
107
|
-
isHash;
|
|
108
|
-
/**
|
|
109
|
-
* Array of child routes.
|
|
110
|
-
*
|
|
111
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#children)
|
|
112
|
-
*/
|
|
113
|
-
children = [];
|
|
114
|
-
get baseUrl() {
|
|
115
|
-
const baseUrl = this.config.baseUrl ?? routeConfig.get().baseUrl;
|
|
116
|
-
return baseUrl?.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Checks whether current route matches provided path.
|
|
120
|
-
*
|
|
121
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#matchpath)
|
|
122
|
-
*/
|
|
123
|
-
matchPath(path) {
|
|
124
|
-
let pathnameToCheck;
|
|
125
|
-
if (path != null) {
|
|
126
|
-
pathnameToCheck = path;
|
|
127
|
-
} else if (this.isHash) {
|
|
128
|
-
pathnameToCheck = this.history.location.hash.slice(1);
|
|
129
|
-
} else {
|
|
130
|
-
pathnameToCheck = this.history.location.pathname;
|
|
131
|
-
}
|
|
132
|
-
if (this.baseUrl) {
|
|
133
|
-
if (!pathnameToCheck.startsWith(this.baseUrl)) {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
pathnameToCheck = pathnameToCheck.replace(this.baseUrl, "");
|
|
137
|
-
}
|
|
138
|
-
if ((this.pathDeclaration === "" || this.pathDeclaration === "/") && (pathnameToCheck === "/" || pathnameToCheck === "")) {
|
|
139
|
-
return { params: {}, path: pathnameToCheck };
|
|
140
|
-
}
|
|
141
|
-
this._matcher ??= pathToRegexp.match(this.tokenData, {
|
|
142
|
-
end: this.config.exact ?? false,
|
|
143
|
-
...this.config.matchOptions
|
|
144
|
-
});
|
|
145
|
-
const parsed = this._matcher(pathnameToCheck);
|
|
146
|
-
if (parsed === false) {
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
return parsed;
|
|
150
|
-
}
|
|
151
|
-
get parsedPathData() {
|
|
152
|
-
return this.matchPath();
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isopening)
|
|
156
|
-
*
|
|
157
|
-
* Also true in the short gap after history already matches this route but
|
|
158
|
-
* `confirmOpening` has not started yet (e.g. browser Back) — otherwise every
|
|
159
|
-
* route looks closed for one tick and RouteViewGroup unmounts the page.
|
|
160
|
-
*/
|
|
161
|
-
get isOpening() {
|
|
162
|
-
if (this.status === "opening") {
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
if (this.isDestroyed || !this.isPathMatched || this.params === null || this.status === "open-confirmed" || this.status === "open-rejected") {
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
return this.status === "closed" || this.status === "unknown";
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Matched path segment for current URL.
|
|
172
|
-
*
|
|
173
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#path)
|
|
174
|
-
*/
|
|
175
|
-
get path() {
|
|
176
|
-
return this.parsedPathData?.path ?? null;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Matched path segment for current URL with base URL.
|
|
180
|
-
*
|
|
181
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#absolutepath)
|
|
182
|
-
*/
|
|
183
|
-
get absolutePath() {
|
|
184
|
-
const path = this.path;
|
|
185
|
-
if (path === null) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
return `${this.baseUrl || ""}${path}`;
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Current parsed path parameters.
|
|
192
|
-
*
|
|
193
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#params)
|
|
194
|
-
*/
|
|
195
|
-
get params() {
|
|
196
|
-
if (!this.parsedPathData?.params) {
|
|
197
|
-
return null;
|
|
198
|
-
}
|
|
199
|
-
let params = this.parsedPathData?.params ?? null;
|
|
200
|
-
if (this.config.params) {
|
|
201
|
-
const result = this.config.params(
|
|
202
|
-
this.parsedPathData.params,
|
|
203
|
-
this.config.meta
|
|
204
|
-
);
|
|
205
|
-
if (result) {
|
|
206
|
-
params = result;
|
|
207
|
-
} else {
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return params;
|
|
212
|
-
}
|
|
213
|
-
get isPathMatched() {
|
|
214
|
-
return this.parsedPathData !== null;
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Defines the "open" state for this route.
|
|
218
|
-
*
|
|
219
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isopened)
|
|
220
|
-
*/
|
|
221
|
-
get isOpened() {
|
|
222
|
-
if (this.isDestroyed || !this.isPathMatched || this.params === null || this.status !== "open-confirmed") {
|
|
223
|
-
return false;
|
|
224
|
-
}
|
|
225
|
-
return (
|
|
226
|
-
// this.parsedPathData is defined because this.params !== null
|
|
227
|
-
!this.config.checkOpened || this.config.checkOpened(this.parsedPathData)
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Allows to create child route based on this route with merging this route path and extending path.
|
|
232
|
-
*
|
|
233
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#extend)
|
|
234
|
-
*/
|
|
235
|
-
extend(pathDeclaration, config) {
|
|
236
|
-
const { index, params, exact, ...configFromCurrentRoute } = this.config;
|
|
237
|
-
const extendedChild = new Route(`${this.pathDeclaration}${pathDeclaration}`, {
|
|
238
|
-
...configFromCurrentRoute,
|
|
239
|
-
...config,
|
|
240
|
-
parent: this
|
|
241
|
-
});
|
|
242
|
-
this.addChildren(extendedChild);
|
|
243
|
-
return extendedChild;
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Manually add child routes.
|
|
247
|
-
*
|
|
248
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#addchildren)
|
|
249
|
-
*/
|
|
250
|
-
addChildren(...routes) {
|
|
251
|
-
this.children.push(...routes);
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Remove specified routes from children.
|
|
255
|
-
*
|
|
256
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#removechildren)
|
|
257
|
-
*/
|
|
258
|
-
removeChildren(...routes) {
|
|
259
|
-
this.children = this.children.filter((child) => !routes.includes(child));
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#hasopenedchildren)
|
|
263
|
-
*/
|
|
264
|
-
get hasOpenedChildren() {
|
|
265
|
-
return this.children.some(
|
|
266
|
-
(child) => child.isOpened || child.hasOpenedChildren
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
processParams(params) {
|
|
270
|
-
if (params == null) return void 0;
|
|
271
|
-
return Object.entries(params).reduce((acc, [key, value]) => {
|
|
272
|
-
if (value != null) {
|
|
273
|
-
acc[key] = Array.isArray(value) ? value.map(String) : String(value);
|
|
274
|
-
}
|
|
275
|
-
return acc;
|
|
276
|
-
}, {});
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Generates full route URL.
|
|
280
|
-
*
|
|
281
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#createurl)
|
|
282
|
-
*/
|
|
283
|
-
createUrl(...args) {
|
|
284
|
-
const params = args[0];
|
|
285
|
-
const rawQuery = args[1];
|
|
286
|
-
const mergeQueryOrOutputParams = args[2] ?? this.isAbleToMergeQuery;
|
|
287
|
-
const outputParams = typeof mergeQueryOrOutputParams === "boolean" ? { mergeQuery: mergeQueryOrOutputParams } : mergeQueryOrOutputParams;
|
|
288
|
-
const query = outputParams?.mergeQuery ? { ...this.query.data, ...rawQuery } : rawQuery ?? {};
|
|
289
|
-
this._compiler ??= pathToRegexp.compile(this.tokenData);
|
|
290
|
-
const defaultUrlCreateParams = {
|
|
291
|
-
baseUrl: this.baseUrl,
|
|
292
|
-
params,
|
|
293
|
-
query
|
|
294
|
-
};
|
|
295
|
-
const urlCreateParams = this.config.createUrl?.(defaultUrlCreateParams, this.query.data) ?? routeConfig.get().createUrl?.(defaultUrlCreateParams, this.query.data) ?? defaultUrlCreateParams;
|
|
296
|
-
let path;
|
|
297
|
-
try {
|
|
298
|
-
path = this._compiler(this.processParams(urlCreateParams.params));
|
|
299
|
-
} catch (e) {
|
|
300
|
-
if (process.env.NODE_ENV !== "production") {
|
|
301
|
-
console.error(
|
|
302
|
-
'Error #1: Route path compilation failed\nThe path pattern could not be built into a URL (often missing or invalid params for a `:param` segment). Using fallbackPath or "/".\nSee docs: https://js2me.github.io/mobx-route/errors/1',
|
|
303
|
-
e
|
|
304
|
-
);
|
|
305
|
-
} else {
|
|
306
|
-
console.error("minified error #1;see mobx-route docs", e);
|
|
307
|
-
}
|
|
308
|
-
path = this.config.fallbackPath ?? routeConfig.get().fallbackPath ?? "/";
|
|
309
|
-
}
|
|
310
|
-
const url = `${urlCreateParams.baseUrl || ""}${this.isHash ? "#" : ""}${path}`;
|
|
311
|
-
if (outputParams?.omitQuery) {
|
|
312
|
-
return url;
|
|
313
|
-
}
|
|
314
|
-
return `${url}${mobxLocationHistory.buildSearchString(urlCreateParams.query)}`;
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Navigates to this route.
|
|
318
|
-
*
|
|
319
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#open)
|
|
320
|
-
*/
|
|
321
|
-
async open(...args) {
|
|
322
|
-
const {
|
|
323
|
-
replace,
|
|
324
|
-
state: rawState,
|
|
325
|
-
query: rawQuery,
|
|
326
|
-
mergeQuery: rawMergeQuery
|
|
327
|
-
} = typeof args[1] === "boolean" || args.length > 2 ? { replace: args[1], query: args[2] } : args[1] ?? {};
|
|
328
|
-
let url;
|
|
329
|
-
let params;
|
|
330
|
-
const mergeQuery = rawMergeQuery ?? this.isAbleToMergeQuery;
|
|
331
|
-
const query = mergeQuery ? { ...this.query.data, ...rawQuery } : rawQuery;
|
|
332
|
-
if (typeof args[0] === "string") {
|
|
333
|
-
url = args[0];
|
|
334
|
-
} else {
|
|
335
|
-
params = args[0];
|
|
336
|
-
url = this.createUrl(args[0], query);
|
|
337
|
-
}
|
|
338
|
-
const state = rawState ?? null;
|
|
339
|
-
const trx = {
|
|
340
|
-
url,
|
|
341
|
-
params,
|
|
342
|
-
replace,
|
|
343
|
-
state,
|
|
344
|
-
query
|
|
345
|
-
};
|
|
346
|
-
this.ignoreOpenByPathMatch = true;
|
|
347
|
-
const isConfirmed = await this.confirmOpening(trx);
|
|
348
|
-
if (isConfirmed !== true) {
|
|
349
|
-
this.ignoreOpenByPathMatch = false;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
get tokenData() {
|
|
353
|
-
if (!this._tokenData) {
|
|
354
|
-
this._tokenData = pathToRegexp.parse(this.pathDeclaration, this.config.parseOptions);
|
|
355
|
-
}
|
|
356
|
-
return this._tokenData;
|
|
357
|
-
}
|
|
358
|
-
async confirmOpening(trx) {
|
|
359
|
-
mobx.runInAction(() => {
|
|
360
|
-
this.status = "opening";
|
|
361
|
-
});
|
|
362
|
-
let skipHistoryUpdate = !!trx.preferSkipHistoryUpdate;
|
|
363
|
-
if (skipHistoryUpdate) {
|
|
364
|
-
this.ignoreOpenByPathMatch = false;
|
|
365
|
-
}
|
|
366
|
-
if (this.config.beforeOpen) {
|
|
367
|
-
const feedback = await this.config.beforeOpen(trx);
|
|
368
|
-
if (feedback === false) {
|
|
369
|
-
mobx.runInAction(() => {
|
|
370
|
-
this.status = "open-rejected";
|
|
371
|
-
});
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
if (typeof feedback === "object") {
|
|
375
|
-
skipHistoryUpdate = false;
|
|
376
|
-
Object.assign(trx, feedback);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
if (this.isDestroyed) {
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
if (!skipHistoryUpdate) {
|
|
383
|
-
if (trx.replace) {
|
|
384
|
-
this.history.replace(trx.url, trx.state);
|
|
385
|
-
} else {
|
|
386
|
-
this.history.push(trx.url, trx.state);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
if (this.isPathMatched) {
|
|
390
|
-
mobx.runInAction(() => {
|
|
391
|
-
this.status = "open-confirmed";
|
|
392
|
-
});
|
|
393
|
-
this.config.afterOpen?.(this.parsedPathData, this);
|
|
394
|
-
}
|
|
395
|
-
return true;
|
|
396
|
-
}
|
|
397
|
-
confirmClosing() {
|
|
398
|
-
mobx.runInAction(() => {
|
|
399
|
-
this.status = "closed";
|
|
400
|
-
});
|
|
401
|
-
return true;
|
|
402
|
-
}
|
|
403
|
-
firstPathMatchingRun = true;
|
|
404
|
-
checkPathMatch = async (isPathMathched) => {
|
|
405
|
-
if (this.firstPathMatchingRun) {
|
|
406
|
-
this.firstPathMatchingRun = false;
|
|
407
|
-
if (!isPathMathched) {
|
|
408
|
-
return;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
if (isPathMathched) {
|
|
412
|
-
if (this.ignoreOpenByPathMatch) {
|
|
413
|
-
this.ignoreOpenByPathMatch = false;
|
|
414
|
-
if (this.status === "opening" && this.parsedPathData) {
|
|
415
|
-
mobx.runInAction(() => {
|
|
416
|
-
this.status = "open-confirmed";
|
|
417
|
-
});
|
|
418
|
-
this.config.afterOpen?.(this.parsedPathData, this);
|
|
419
|
-
}
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
if (this.status !== "opening" && this.status !== "open-confirmed") {
|
|
423
|
-
mobx.runInAction(() => {
|
|
424
|
-
this.status = "opening";
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
const trx = {
|
|
428
|
-
url: this.parsedPathData.path,
|
|
429
|
-
params: this.parsedPathData.params,
|
|
430
|
-
state: this.history.location.state,
|
|
431
|
-
query: this.query.data,
|
|
432
|
-
preferSkipHistoryUpdate: true
|
|
433
|
-
};
|
|
434
|
-
await this.confirmOpening(trx);
|
|
435
|
-
} else {
|
|
436
|
-
this.ignoreOpenByPathMatch = false;
|
|
437
|
-
const isConfirmed = this.confirmClosing();
|
|
438
|
-
if (isConfirmed) {
|
|
439
|
-
this.config.afterClose?.();
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
get isAbleToMergeQuery() {
|
|
444
|
-
return this.config.mergeQuery ?? routeConfig.get().mergeQuery;
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Destroys route subscriptions and reactions.
|
|
448
|
-
*
|
|
449
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#destroy)
|
|
450
|
-
*/
|
|
451
|
-
destroy() {
|
|
452
|
-
this.isDestroyed = true;
|
|
453
|
-
this.disposer?.();
|
|
454
|
-
this.disposer = void 0;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
const createRoute = (path, config) => new Route(path, config);
|
|
458
|
-
const annotations$2 = [
|
|
459
|
-
[mobx.computed, "isOpened", "indexRoute", "canNavigate"],
|
|
460
|
-
[mobx.observable.shallow, "routes"]
|
|
461
|
-
];
|
|
462
|
-
class RouteGroup {
|
|
463
|
-
constructor(routes, _indexRoute) {
|
|
464
|
-
this._indexRoute = _indexRoute;
|
|
465
|
-
this.routes = routes;
|
|
466
|
-
mobx$1.applyObservable(this, annotations$2);
|
|
467
|
-
}
|
|
468
|
-
_indexRoute;
|
|
469
|
-
routes;
|
|
470
|
-
/**
|
|
471
|
-
* Returns true if at least one route in the group is open.
|
|
472
|
-
*
|
|
473
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/groupRoutes.html#isopened)
|
|
474
|
-
*/
|
|
475
|
-
get isOpened() {
|
|
476
|
-
const routes = Object.values(this.routes);
|
|
477
|
-
return routes.some(
|
|
478
|
-
(route) => route.isOpened || "hasOpenedChildren" in route && route.hasOpenedChildren
|
|
479
|
-
);
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* First found index route.
|
|
483
|
-
*
|
|
484
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/groupRoutes.html#indexroute)
|
|
485
|
-
*/
|
|
486
|
-
get indexRoute() {
|
|
487
|
-
return this._indexRoute ?? Object.values(this.routes).find(
|
|
488
|
-
(route) => "isIndex" in route && route.isIndex
|
|
489
|
-
);
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* `true` if `open()` has a target to navigate to —
|
|
493
|
-
* either an own index route or a nested `RouteGroup` that itself can navigate.
|
|
494
|
-
*
|
|
495
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/groupRoutes.html#cannavigate)
|
|
496
|
-
*/
|
|
497
|
-
get canNavigate() {
|
|
498
|
-
if (this.indexRoute) return true;
|
|
499
|
-
for (const routeName in this.routes) {
|
|
500
|
-
const route = this.routes[routeName];
|
|
501
|
-
if (route instanceof RouteGroup && route.canNavigate) {
|
|
502
|
-
return true;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
return false;
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* Main navigation method for the group.
|
|
509
|
-
*
|
|
510
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/groupRoutes.html#open)
|
|
511
|
-
*/
|
|
512
|
-
open(...args) {
|
|
513
|
-
if (this.indexRoute && "open" in this.indexRoute) {
|
|
514
|
-
this.indexRoute.open(...args);
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
for (const routeName in this.routes) {
|
|
518
|
-
const route = this.routes[routeName];
|
|
519
|
-
if (route instanceof RouteGroup && route.canNavigate) {
|
|
520
|
-
route.open(...args);
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
if (process.env.NODE_ENV !== "production") {
|
|
525
|
-
console.warn(
|
|
526
|
-
"Warning #1: RouteGroup.open() cannot navigate\nThis group has no index route (`index: true` or `groupRoutes(routes, indexRoute)`) and no navigable nested RouteGroup, so open() does nothing.\nSee docs: https://js2me.github.io/mobx-route/warnings/1"
|
|
527
|
-
);
|
|
528
|
-
} else {
|
|
529
|
-
console.warn("minified warning #1;see mobx-route docs");
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
const groupRoutes = (routes, indexRoute) => new RouteGroup(routes, indexRoute);
|
|
534
|
-
const annotations$1 = [
|
|
535
|
-
[mobx.computed.struct, "location"]
|
|
536
|
-
];
|
|
537
|
-
class Router {
|
|
538
|
-
routes;
|
|
539
|
-
history;
|
|
540
|
-
query;
|
|
541
|
-
constructor(config) {
|
|
542
|
-
this.routes = config.routes;
|
|
543
|
-
this.history = config.history ?? routeConfig.get().history;
|
|
544
|
-
this.query = config.queryParams ?? routeConfig.get().queryParams;
|
|
545
|
-
mobx$1.applyObservable(this, annotations$1);
|
|
546
|
-
}
|
|
547
|
-
get location() {
|
|
548
|
-
return this.history.location;
|
|
549
|
-
}
|
|
550
|
-
navigate(url, options) {
|
|
551
|
-
const query = options?.mergeQuery ?? routeConfig.get().mergeQuery ? { ...this.query.data, ...options?.query } : { ...options?.query };
|
|
552
|
-
const searchString = mobxLocationHistory.buildSearchString(query);
|
|
553
|
-
const navigationUrl = `${url}${searchString}`;
|
|
554
|
-
if (options?.replace) {
|
|
555
|
-
this.history.replace(navigationUrl, options?.state);
|
|
556
|
-
} else {
|
|
557
|
-
this.history.push(navigationUrl, options?.state);
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
const createRouter = (config) => new Router(config);
|
|
4
|
+
const virtualRoute = require("./virtual-route-CFGwOGHi.cjs");
|
|
562
5
|
const isRouteEntity = (route) => route && "isOpened" in route;
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
this.query = config.queryParams ?? routeConfig.get().queryParams;
|
|
573
|
-
this.params = common.callFunction(config.initialParams, this) ?? null;
|
|
574
|
-
this.openChecker = config.checkOpened;
|
|
575
|
-
this.skipAutoOpenClose = false;
|
|
576
|
-
this.isOuterOpened = this.openChecker?.(this);
|
|
577
|
-
this.status = this.isOuterOpened ? "opened" : "unknown";
|
|
578
|
-
mobx$1.applyObservable(this, annotations);
|
|
579
|
-
if (this.config.abortSignal?.aborted) {
|
|
580
|
-
this.isDestroyed = true;
|
|
581
|
-
} else {
|
|
582
|
-
this.disposer = mobx.reaction(
|
|
583
|
-
() => this.openChecker?.(this),
|
|
584
|
-
mobx.action((isOuterOpened) => {
|
|
585
|
-
this.isOuterOpened = isOuterOpened;
|
|
586
|
-
if (this.skipAutoOpenClose || this.status === "closing" || this.status === "opening") {
|
|
587
|
-
return;
|
|
588
|
-
}
|
|
589
|
-
if (this.isOuterOpened) {
|
|
590
|
-
if (this.status === "opened") {
|
|
591
|
-
return;
|
|
592
|
-
}
|
|
593
|
-
void this.confirmOpening({
|
|
594
|
-
params: this.params ?? null,
|
|
595
|
-
...this.config.getAutoOpenParams?.(this)
|
|
596
|
-
});
|
|
597
|
-
} else {
|
|
598
|
-
if (this.status === "closed" || this.status === "unknown") {
|
|
599
|
-
return;
|
|
600
|
-
}
|
|
601
|
-
void this.confirmClosing();
|
|
602
|
-
}
|
|
603
|
-
}),
|
|
604
|
-
{ signal: this.config.abortSignal, fireImmediately: true }
|
|
605
|
-
);
|
|
606
|
-
}
|
|
607
|
-
if (this.status === "opened") {
|
|
608
|
-
this.config.afterOpen?.(this.params, this);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
config;
|
|
612
|
-
isDestroyed;
|
|
613
|
-
disposer;
|
|
614
|
-
query;
|
|
615
|
-
params;
|
|
616
|
-
status;
|
|
617
|
-
openChecker;
|
|
618
|
-
trx;
|
|
619
|
-
skipAutoOpenClose;
|
|
620
|
-
/**
|
|
621
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isouteropened)
|
|
622
|
-
*/
|
|
623
|
-
isOuterOpened;
|
|
624
|
-
/**
|
|
625
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isopened)
|
|
626
|
-
*/
|
|
627
|
-
get isOpened() {
|
|
628
|
-
return !this.isDestroyed && this.status === "opened" && this.isOuterOpened !== false;
|
|
629
|
-
}
|
|
630
|
-
/**
|
|
631
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isopening)
|
|
632
|
-
*/
|
|
633
|
-
get isOpening() {
|
|
634
|
-
return this.status === "opening";
|
|
635
|
-
}
|
|
636
|
-
/**
|
|
637
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isclosing)
|
|
638
|
-
*/
|
|
639
|
-
get isClosing() {
|
|
640
|
-
return this.status === "closing";
|
|
641
|
-
}
|
|
642
|
-
/**
|
|
643
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#setopenchecker)
|
|
644
|
-
*/
|
|
645
|
-
setOpenChecker(openChecker) {
|
|
646
|
-
this.openChecker = openChecker;
|
|
647
|
-
}
|
|
648
|
-
async open(...args) {
|
|
649
|
-
const params = args[0] ?? null;
|
|
650
|
-
const extra = args[1];
|
|
651
|
-
this.skipAutoOpenClose = true;
|
|
652
|
-
this.trx = {
|
|
653
|
-
params,
|
|
654
|
-
extra,
|
|
655
|
-
manual: true
|
|
656
|
-
};
|
|
657
|
-
await this.confirmOpening(this.trx);
|
|
658
|
-
this.skipAutoOpenClose = false;
|
|
659
|
-
}
|
|
660
|
-
/**
|
|
661
|
-
* [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#close)
|
|
662
|
-
*/
|
|
663
|
-
async close() {
|
|
664
|
-
this.skipAutoOpenClose = true;
|
|
665
|
-
const result = await this.confirmClosing();
|
|
666
|
-
this.skipAutoOpenClose = false;
|
|
667
|
-
return result;
|
|
668
|
-
}
|
|
669
|
-
async confirmOpening(trx) {
|
|
670
|
-
mobx.runInAction(() => {
|
|
671
|
-
this.trx = void 0;
|
|
672
|
-
this.status = "opening";
|
|
673
|
-
});
|
|
674
|
-
if (await this.config.beforeOpen?.(trx.params, this) === false) {
|
|
675
|
-
mobx.runInAction(() => {
|
|
676
|
-
this.status = "open-rejected";
|
|
677
|
-
this.trx = void 0;
|
|
678
|
-
});
|
|
679
|
-
return;
|
|
680
|
-
}
|
|
681
|
-
if (await this.config.open?.(trx.params, this) === false) {
|
|
682
|
-
mobx.runInAction(() => {
|
|
683
|
-
this.status = "open-rejected";
|
|
684
|
-
this.trx = void 0;
|
|
685
|
-
});
|
|
686
|
-
return;
|
|
687
|
-
}
|
|
688
|
-
mobx.runInAction(() => {
|
|
689
|
-
if (trx.extra?.query) {
|
|
690
|
-
this.query.update(trx.extra.query, trx.extra.replace);
|
|
691
|
-
}
|
|
692
|
-
this.trx = void 0;
|
|
693
|
-
this.params = trx.params;
|
|
694
|
-
this.status = "opened";
|
|
695
|
-
this.config.afterOpen?.(this.params, this);
|
|
696
|
-
});
|
|
697
|
-
return true;
|
|
698
|
-
}
|
|
699
|
-
async confirmClosing() {
|
|
700
|
-
if (this.status === "closed") {
|
|
701
|
-
return true;
|
|
702
|
-
}
|
|
703
|
-
const lastStatus = this.status;
|
|
704
|
-
mobx.runInAction(() => {
|
|
705
|
-
this.status = "closing";
|
|
706
|
-
});
|
|
707
|
-
if (await this.config.beforeClose?.() === false) {
|
|
708
|
-
mobx.runInAction(() => {
|
|
709
|
-
this.status = lastStatus;
|
|
710
|
-
});
|
|
711
|
-
return;
|
|
712
|
-
}
|
|
713
|
-
if (this.config.close?.(this) === false) {
|
|
714
|
-
mobx.runInAction(() => {
|
|
715
|
-
this.status = lastStatus;
|
|
716
|
-
});
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
719
|
-
mobx.runInAction(() => {
|
|
720
|
-
this.status = "closed";
|
|
721
|
-
this.params = null;
|
|
722
|
-
});
|
|
723
|
-
this.config.afterClose?.();
|
|
724
|
-
return true;
|
|
725
|
-
}
|
|
726
|
-
destroy() {
|
|
727
|
-
this.isDestroyed = true;
|
|
728
|
-
this.status = "unknown";
|
|
729
|
-
this.disposer?.();
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
const createVirtualRoute = (config) => new VirtualRoute(config);
|
|
733
|
-
exports.Route = Route;
|
|
734
|
-
exports.RouteGroup = RouteGroup;
|
|
735
|
-
exports.Router = Router;
|
|
736
|
-
exports.VirtualRoute = VirtualRoute;
|
|
737
|
-
exports.createRoute = createRoute;
|
|
738
|
-
exports.createRouter = createRouter;
|
|
739
|
-
exports.createVirtualRoute = createVirtualRoute;
|
|
740
|
-
exports.groupRoutes = groupRoutes;
|
|
6
|
+
exports.Route = virtualRoute.Route;
|
|
7
|
+
exports.RouteGroup = virtualRoute.RouteGroup;
|
|
8
|
+
exports.Router = virtualRoute.Router;
|
|
9
|
+
exports.VirtualRoute = virtualRoute.VirtualRoute;
|
|
10
|
+
exports.createRoute = virtualRoute.createRoute;
|
|
11
|
+
exports.createRouter = virtualRoute.createRouter;
|
|
12
|
+
exports.createVirtualRoute = virtualRoute.createVirtualRoute;
|
|
13
|
+
exports.groupRoutes = virtualRoute.groupRoutes;
|
|
14
|
+
exports.routeConfig = virtualRoute.routeConfig;
|
|
741
15
|
exports.isRouteEntity = isRouteEntity;
|
|
742
|
-
exports.routeConfig = routeConfig;
|
|
743
16
|
Object.keys(mobxLocationHistory).forEach((k) => {
|
|
744
17
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
745
18
|
enumerable: true,
|