1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.makeEvent = exports.RegisteredHook = exports.matchState = void 0;
|
4 | var common_1 = require("../common");
|
5 | var interface_1 = require("./interface");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function matchState(state, criterion, transition) {
|
20 | var toMatch = common_1.isString(criterion) ? [criterion] : criterion;
|
21 | function matchGlobs(_state) {
|
22 | var globStrings = toMatch;
|
23 | for (var i = 0; i < globStrings.length; i++) {
|
24 | var glob = new common_1.Glob(globStrings[i]);
|
25 | if ((glob && glob.matches(_state.name)) || (!glob && globStrings[i] === _state.name)) {
|
26 | return true;
|
27 | }
|
28 | }
|
29 | return false;
|
30 | }
|
31 | var matchFn = (common_1.isFunction(toMatch) ? toMatch : matchGlobs);
|
32 | return !!matchFn(state, transition);
|
33 | }
|
34 | exports.matchState = matchState;
|
35 |
|
36 |
|
37 |
|
38 | var RegisteredHook = (function () {
|
39 | function RegisteredHook(tranSvc, eventType, callback, matchCriteria, removeHookFromRegistry, options) {
|
40 | if (options === void 0) { options = {}; }
|
41 | this.tranSvc = tranSvc;
|
42 | this.eventType = eventType;
|
43 | this.callback = callback;
|
44 | this.matchCriteria = matchCriteria;
|
45 | this.removeHookFromRegistry = removeHookFromRegistry;
|
46 | this.invokeCount = 0;
|
47 | this._deregistered = false;
|
48 | this.priority = options.priority || 0;
|
49 | this.bind = options.bind || null;
|
50 | this.invokeLimit = options.invokeLimit;
|
51 | }
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | RegisteredHook.prototype._matchingNodes = function (nodes, criterion, transition) {
|
68 | if (criterion === true)
|
69 | return nodes;
|
70 | var matching = nodes.filter(function (node) { return matchState(node.state, criterion, transition); });
|
71 | return matching.length ? matching : null;
|
72 | };
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | RegisteredHook.prototype._getDefaultMatchCriteria = function () {
|
88 | return common_1.mapObj(this.tranSvc._pluginapi._getPathTypes(), function () { return true; });
|
89 | };
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | RegisteredHook.prototype._getMatchingNodes = function (treeChanges, transition) {
|
106 | var _this = this;
|
107 | var criteria = common_1.extend(this._getDefaultMatchCriteria(), this.matchCriteria);
|
108 | var paths = common_1.values(this.tranSvc._pluginapi._getPathTypes());
|
109 | return paths.reduce(function (mn, pathtype) {
|
110 |
|
111 |
|
112 | var isStateHook = pathtype.scope === interface_1.TransitionHookScope.STATE;
|
113 | var path = treeChanges[pathtype.name] || [];
|
114 | var nodes = isStateHook ? path : [common_1.tail(path)];
|
115 | mn[pathtype.name] = _this._matchingNodes(nodes, criteria[pathtype.name], transition);
|
116 | return mn;
|
117 | }, {});
|
118 | };
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | RegisteredHook.prototype.matches = function (treeChanges, transition) {
|
126 | var matches = this._getMatchingNodes(treeChanges, transition);
|
127 |
|
128 | var allMatched = common_1.values(matches).every(common_1.identity);
|
129 | return allMatched ? matches : null;
|
130 | };
|
131 | RegisteredHook.prototype.deregister = function () {
|
132 | this.removeHookFromRegistry(this);
|
133 | this._deregistered = true;
|
134 | };
|
135 | return RegisteredHook;
|
136 | }());
|
137 | exports.RegisteredHook = RegisteredHook;
|
138 |
|
139 | function makeEvent(registry, transitionService, eventType) {
|
140 |
|
141 | var _registeredHooks = (registry._registeredHooks = registry._registeredHooks || {});
|
142 | var hooks = (_registeredHooks[eventType.name] = []);
|
143 | var removeHookFn = common_1.removeFrom(hooks);
|
144 |
|
145 | registry[eventType.name] = hookRegistrationFn;
|
146 | function hookRegistrationFn(matchObject, callback, options) {
|
147 | if (options === void 0) { options = {}; }
|
148 | var registeredHook = new RegisteredHook(transitionService, eventType, callback, matchObject, removeHookFn, options);
|
149 | hooks.push(registeredHook);
|
150 | return registeredHook.deregister.bind(registeredHook);
|
151 | }
|
152 | return hookRegistrationFn;
|
153 | }
|
154 | exports.makeEvent = makeEvent;
|
155 |
|
\ | No newline at end of file |