1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.lazyLoadState = exports.registerLazyLoadHook = void 0;
|
4 | var coreservices_1 = require("../common/coreservices");
|
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 | var lazyLoadHook = function (transition) {
|
30 | var router = transition.router;
|
31 | function retryTransition() {
|
32 | if (transition.originalTransition().options().source !== 'url') {
|
33 |
|
34 |
|
35 | var orig = transition.targetState();
|
36 | return router.stateService.target(orig.identifier(), orig.params(), orig.options());
|
37 | }
|
38 |
|
39 |
|
40 | var $url = router.urlService;
|
41 | var result = $url.match($url.parts());
|
42 | var rule = result && result.rule;
|
43 |
|
44 |
|
45 | if (rule && rule.type === 'STATE') {
|
46 | var state = rule.state;
|
47 | var params = result.match;
|
48 | return router.stateService.target(state, params, transition.options());
|
49 | }
|
50 |
|
51 | router.urlService.sync();
|
52 | }
|
53 | var promises = transition
|
54 | .entering()
|
55 | .filter(function (state) { return !!state.$$state().lazyLoad; })
|
56 | .map(function (state) { return lazyLoadState(transition, state); });
|
57 | return coreservices_1.services.$q.all(promises).then(retryTransition);
|
58 | };
|
59 | exports.registerLazyLoadHook = function (transitionService) {
|
60 | return transitionService.onBefore({ entering: function (state) { return !!state.lazyLoad; } }, lazyLoadHook);
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function lazyLoadState(transition, state) {
|
70 | var lazyLoadFn = state.$$state().lazyLoad;
|
71 |
|
72 | var promise = lazyLoadFn['_promise'];
|
73 | if (!promise) {
|
74 | var success = function (result) {
|
75 | delete state.lazyLoad;
|
76 | delete state.$$state().lazyLoad;
|
77 | delete lazyLoadFn['_promise'];
|
78 | return result;
|
79 | };
|
80 | var error = function (err) {
|
81 | delete lazyLoadFn['_promise'];
|
82 | return coreservices_1.services.$q.reject(err);
|
83 | };
|
84 | promise = lazyLoadFn['_promise'] = coreservices_1.services.$q
|
85 | .when(lazyLoadFn(transition, state))
|
86 | .then(updateStateRegistry)
|
87 | .then(success, error);
|
88 | }
|
89 |
|
90 | function updateStateRegistry(result) {
|
91 | if (result && Array.isArray(result.states)) {
|
92 | result.states.forEach(function (_state) { return transition.router.stateRegistry.register(_state); });
|
93 | }
|
94 | return result;
|
95 | }
|
96 | return promise;
|
97 | }
|
98 | exports.lazyLoadState = lazyLoadState;
|
99 |
|
\ | No newline at end of file |