1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.HookBuilder = void 0;
|
4 | var common_1 = require("../common/common");
|
5 | var predicates_1 = require("../common/predicates");
|
6 | var interface_1 = require("./interface");
|
7 | var transitionHook_1 = require("./transitionHook");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var HookBuilder = (function () {
|
22 | function HookBuilder(transition) {
|
23 | this.transition = transition;
|
24 | }
|
25 | HookBuilder.prototype.buildHooksForPhase = function (phase) {
|
26 | var _this = this;
|
27 | var $transitions = this.transition.router.transitionService;
|
28 | return $transitions._pluginapi
|
29 | ._getEvents(phase)
|
30 | .map(function (type) { return _this.buildHooks(type); })
|
31 | .reduce(common_1.unnestR, [])
|
32 | .filter(common_1.identity);
|
33 | };
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | HookBuilder.prototype.buildHooks = function (hookType) {
|
44 | var transition = this.transition;
|
45 | var treeChanges = transition.treeChanges();
|
46 |
|
47 | var matchingHooks = this.getMatchingHooks(hookType, treeChanges, transition);
|
48 | if (!matchingHooks)
|
49 | return [];
|
50 | var baseHookOptions = {
|
51 | transition: transition,
|
52 | current: transition.options().current,
|
53 | };
|
54 | var makeTransitionHooks = function (hook) {
|
55 |
|
56 | var matches = hook.matches(treeChanges, transition);
|
57 |
|
58 | var matchingNodes = matches[hookType.criteriaMatchPath.name];
|
59 |
|
60 | return matchingNodes.map(function (node) {
|
61 | var _options = common_1.extend({
|
62 | bind: hook.bind,
|
63 | traceData: { hookType: hookType.name, context: node },
|
64 | }, baseHookOptions);
|
65 | var state = hookType.criteriaMatchPath.scope === interface_1.TransitionHookScope.STATE ? node.state.self : null;
|
66 | var transitionHook = new transitionHook_1.TransitionHook(transition, state, hook, _options);
|
67 | return { hook: hook, node: node, transitionHook: transitionHook };
|
68 | });
|
69 | };
|
70 | return matchingHooks
|
71 | .map(makeTransitionHooks)
|
72 | .reduce(common_1.unnestR, [])
|
73 | .sort(tupleSort(hookType.reverseSort))
|
74 | .map(function (tuple) { return tuple.transitionHook; });
|
75 | };
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | HookBuilder.prototype.getMatchingHooks = function (hookType, treeChanges, transition) {
|
88 | var isCreate = hookType.hookPhase === interface_1.TransitionHookPhase.CREATE;
|
89 |
|
90 | var $transitions = this.transition.router.transitionService;
|
91 | var registries = isCreate ? [$transitions] : [this.transition, $transitions];
|
92 | return registries
|
93 | .map(function (reg) { return reg.getHooks(hookType.name); })
|
94 | .filter(common_1.assertPredicate(predicates_1.isArray, "broken event named: " + hookType.name))
|
95 | .reduce(common_1.unnestR, [])
|
96 | .filter(function (hook) { return hook.matches(treeChanges, transition); });
|
97 | };
|
98 | return HookBuilder;
|
99 | }());
|
100 | exports.HookBuilder = HookBuilder;
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function tupleSort(reverseDepthSort) {
|
111 | if (reverseDepthSort === void 0) { reverseDepthSort = false; }
|
112 | return function nodeDepthThenPriority(l, r) {
|
113 | var factor = reverseDepthSort ? -1 : 1;
|
114 | var depthDelta = (l.node.state.path.length - r.node.state.path.length) * factor;
|
115 | return depthDelta !== 0 ? depthDelta : r.hook.priority - l.hook.priority;
|
116 | };
|
117 | }
|
118 |
|
\ | No newline at end of file |