@statewalker/fsm-charts 0.2.4 → 0.2.6

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.
Files changed (155) hide show
  1. package/README.md +206 -1
  2. package/dist/StateChart-cXyVRcs-.d.ts +94 -0
  3. package/dist/StateChart-cXyVRcs-.d.ts.map +1 -0
  4. package/dist/StateChartIndex-BK2315qj.d.ts +142 -0
  5. package/dist/StateChartIndex-BK2315qj.d.ts.map +1 -0
  6. package/dist/StateChartIndex-BOIrc_nq.js +355 -0
  7. package/dist/StateChartIndex-BOIrc_nq.js.map +1 -0
  8. package/dist/buildStatechartsPanel-CxQlIwTh.js +494 -0
  9. package/dist/buildStatechartsPanel-CxQlIwTh.js.map +1 -0
  10. package/dist/buildStatechartsPanel-hCtZbdiF.d.ts +79 -0
  11. package/dist/buildStatechartsPanel-hCtZbdiF.d.ts.map +1 -0
  12. package/dist/defineProperty-C0rC37pP.js +39 -0
  13. package/dist/description-panels-DI2MNlbK.js +184 -0
  14. package/dist/description-panels-DI2MNlbK.js.map +1 -0
  15. package/dist/getLabelDimensions-BY0q7j3D.d.ts +79 -0
  16. package/dist/getLabelDimensions-BY0q7j3D.d.ts.map +1 -0
  17. package/dist/getPadding-Cw0e7G2v.js +25 -0
  18. package/dist/getPadding-Cw0e7G2v.js.map +1 -0
  19. package/dist/index-config.d.ts +3 -12
  20. package/dist/index-config.js +2 -1
  21. package/dist/index-dom.d.ts +2 -49
  22. package/dist/index-dom.js +2 -1
  23. package/dist/index-html.d.ts +3 -2
  24. package/dist/index-html.js +2 -2
  25. package/dist/index-layout.d.ts +3 -77
  26. package/dist/index-layout.js +2 -2
  27. package/dist/index-runtime.d.ts +3 -141
  28. package/dist/index-runtime.js +2 -1
  29. package/dist/index.d.ts +11 -10
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +6 -6
  32. package/dist/layout-Dp671N6q.js +2465 -0
  33. package/dist/layout-Dp671N6q.js.map +1 -0
  34. package/dist/newTreeBuilder-CvtLDjGj.d.ts +54 -0
  35. package/dist/newTreeBuilder-CvtLDjGj.d.ts.map +1 -0
  36. package/dist/toFsmStateConfig-DlJkyZni.js +135 -0
  37. package/dist/toFsmStateConfig-DlJkyZni.js.map +1 -0
  38. package/dist/toFsmStateConfig-DmOlTRjv.d.ts +15 -0
  39. package/dist/toFsmStateConfig-DmOlTRjv.d.ts.map +1 -0
  40. package/package.json +54 -28
  41. package/src/config/concatTransitionsToString.ts +4 -4
  42. package/src/config/index.ts +4 -4
  43. package/src/config/serializeProcessConfig.ts +3 -3
  44. package/src/config/splitTransitionString.ts +2 -4
  45. package/src/config/toFsmStateConfig.ts +5 -12
  46. package/src/dagre/acyclic.js +9 -10
  47. package/src/dagre/add-border-segments.js +11 -7
  48. package/src/dagre/coordinate-system.js +12 -12
  49. package/src/dagre/data/list.js +2 -2
  50. package/src/dagre/debug.d.ts +1 -1
  51. package/src/dagre/debug.js +11 -11
  52. package/src/dagre/greedy-fas.js +17 -21
  53. package/src/dagre/index.d.ts +4 -4
  54. package/src/dagre/index.js +4 -4
  55. package/src/dagre/layout.js +107 -96
  56. package/src/dagre/nesting-graph.js +13 -19
  57. package/src/dagre/normalize.js +7 -7
  58. package/src/dagre/order/add-subgraph-constraints.js +2 -2
  59. package/src/dagre/order/barycenter.d.ts +1 -1
  60. package/src/dagre/order/barycenter.js +4 -4
  61. package/src/dagre/order/build-layer-graph.d.ts +1 -1
  62. package/src/dagre/order/build-layer-graph.js +7 -9
  63. package/src/dagre/order/cross-count.js +14 -17
  64. package/src/dagre/order/index.js +19 -17
  65. package/src/dagre/order/init-order.js +5 -15
  66. package/src/dagre/order/resolve-conflicts.js +10 -14
  67. package/src/dagre/order/sort-subgraph.d.ts +1 -1
  68. package/src/dagre/order/sort-subgraph.js +13 -14
  69. package/src/dagre/order/sort.d.ts +1 -1
  70. package/src/dagre/order/sort.js +6 -10
  71. package/src/dagre/parent-dummy-chains.js +10 -4
  72. package/src/dagre/position/bk.d.ts +3 -3
  73. package/src/dagre/position/bk.js +84 -65
  74. package/src/dagre/position/index.js +7 -11
  75. package/src/dagre/rank/feasible-tree.d.ts +1 -1
  76. package/src/dagre/rank/feasible-tree.js +6 -6
  77. package/src/dagre/rank/index.js +6 -6
  78. package/src/dagre/rank/network-simplex.d.ts +0 -1
  79. package/src/dagre/rank/network-simplex.js +20 -25
  80. package/src/dagre/rank/util.js +2 -6
  81. package/src/dagre/util.d.ts +12 -6
  82. package/src/dagre/util.js +31 -37
  83. package/src/dom/buildSections.ts +15 -19
  84. package/src/dom/description-panels.ts +5 -7
  85. package/src/dom/getStatesDescriptions.ts +4 -10
  86. package/src/full/index.ts +1 -1
  87. package/src/full/toFsmStateCharts.ts +11 -11
  88. package/src/graphlib/alg/components.js +2 -2
  89. package/src/graphlib/alg/dfs.js +5 -5
  90. package/src/graphlib/alg/dijkstra-all.js +4 -4
  91. package/src/graphlib/alg/dijkstra.js +9 -12
  92. package/src/graphlib/alg/find-cycles.js +7 -5
  93. package/src/graphlib/alg/floyd-warshall.js +8 -11
  94. package/src/graphlib/alg/index.d.ts +11 -11
  95. package/src/graphlib/alg/index.js +11 -11
  96. package/src/graphlib/alg/is-acyclic.js +1 -1
  97. package/src/graphlib/alg/postorder.js +2 -2
  98. package/src/graphlib/alg/preorder.js +2 -2
  99. package/src/graphlib/alg/prim.d.ts +1 -1
  100. package/src/graphlib/alg/prim.js +5 -5
  101. package/src/graphlib/alg/tarjan.js +3 -3
  102. package/src/graphlib/alg/topsort.js +1 -1
  103. package/src/graphlib/data/priority-queue.js +8 -10
  104. package/src/graphlib/graph.js +53 -52
  105. package/src/graphlib/index.d.ts +2 -2
  106. package/src/graphlib/index.js +2 -2
  107. package/src/graphlib/json.d.ts +1 -1
  108. package/src/graphlib/json.js +6 -6
  109. package/src/html/buildStatechartCss.ts +2 -2
  110. package/src/html/buildStatechartStyles.ts +5 -15
  111. package/src/html/buildStatechartSvg.ts +8 -20
  112. package/src/html/buildStatechartsPanel.ts +3 -5
  113. package/src/html/index.ts +4 -4
  114. package/src/index-config.ts +2 -2
  115. package/src/index-dom.ts +1 -1
  116. package/src/index-html.ts +2 -2
  117. package/src/index-layout.ts +2 -2
  118. package/src/index-runtime.ts +2 -2
  119. package/src/index.ts +8 -8
  120. package/src/layout/buildCharts.ts +6 -7
  121. package/src/layout/buildFlatCharts.ts +17 -21
  122. package/src/layout/getGraphParamsProvider.ts +7 -19
  123. package/src/layout/getLabelDimensions.ts +5 -5
  124. package/src/layout/index.ts +5 -4
  125. package/src/lodash-es/index.ts +2 -2
  126. package/src/lodash-es/types.ts +37 -35
  127. package/src/runtime/RuntimeStatechartApi.ts +13 -46
  128. package/src/runtime/StateChartIndex.ts +7 -24
  129. package/src/runtime/index.ts +2 -2
  130. package/src/types/FsmStateConfig.ts +1 -1
  131. package/src/types/GraphParamsProvider.ts +3 -6
  132. package/src/types/Position.ts +1 -1
  133. package/src/types/StateChart.ts +2 -2
  134. package/src/types/StateGraphEdge.ts +2 -2
  135. package/src/types/StateGraphNode.ts +2 -2
  136. package/src/types/TransitionsGraph.ts +4 -4
  137. package/src/types/index.ts +11 -11
  138. package/src/utils/BasisCurve.ts +6 -15
  139. package/src/utils/domToText.ts +3 -7
  140. package/src/utils/getPadding.ts +2 -3
  141. package/src/utils/newId.ts +2 -1
  142. package/src/utils/newRegistry.ts +13 -13
  143. package/src/utils/serializeAttrs.ts +6 -14
  144. package/src/utils/serializeCss.ts +3 -7
  145. package/src/utils/serializeStyle.ts +2 -5
  146. package/src/utils/toKebabCase.ts +1 -4
  147. package/CHANGELOG.md +0 -1
  148. package/dist/StateChart-BYBjTmC2.d.ts +0 -82
  149. package/dist/chunk-36ZLXJVD.js +0 -606
  150. package/dist/chunk-AZBRQNTU.js +0 -2739
  151. package/dist/chunk-FIQDLBWD.js +0 -9
  152. package/dist/chunk-SVKVQY7B.js +0 -133
  153. package/dist/chunk-TZHCBZVP.js +0 -422
  154. package/dist/chunk-VILLJJJD.js +0 -231
  155. package/dist/index-html-DgSCNHkR.d.ts +0 -75
@@ -1,9 +0,0 @@
1
- // src/utils/getPadding.ts
2
- function getPadding(padding) {
3
- if (typeof padding === "number") return [padding, padding, padding, padding];
4
- if (padding.length === 2)
5
- return [padding[0], padding[1], padding[0], padding[1]];
6
- return padding;
7
- }
8
-
9
- export { getPadding };
@@ -1,133 +0,0 @@
1
- // src/config/concatTransitionsToString.ts
2
- function concatTransitionsToString(transitions) {
3
- let list = [];
4
- let str = "";
5
- let prev;
6
- for (const [from, event, to] of transitions) {
7
- if (prev !== from) {
8
- if (str) list.push(str);
9
- str = from;
10
- }
11
- str += ` -${escape(event)}-> ${escape(to)}`;
12
- prev = to;
13
- }
14
- if (str) list.push(str);
15
- return list;
16
- function escape(str2) {
17
- return str2;
18
- }
19
- }
20
-
21
- // src/config/serializeProcessConfig.ts
22
- function serializeProcessConfig(config) {
23
- if (!config) return "";
24
- let lines = [];
25
- visit(config, lines);
26
- return lines.join("\n");
27
- function visit(config2, lines2, shift = "") {
28
- lines2.push(`${shift}{`);
29
- const { key, states, transitions, ...params } = config2;
30
- lines2.push(`${shift} "key": ${JSON.stringify(key)}`);
31
- if (transitions && transitions.length > 0) {
32
- lines2[lines2.length - 1] += ",";
33
- lines2.push(`${shift} "transitions": [`);
34
- for (let i = 0; i < transitions.length; i++) {
35
- if (i > 0) {
36
- lines2[lines2.length - 1] += ",";
37
- }
38
- lines2.push(`${shift} ${JSON.stringify(transitions[i])}`);
39
- }
40
- lines2.push(`${shift} ]`);
41
- }
42
- if (states && states.length > 0) {
43
- lines2[lines2.length - 1] += ",";
44
- lines2.push(`${shift} "states": [`);
45
- for (let i = 0; i < states.length; i++) {
46
- if (i > 0) {
47
- lines2[lines2.length - 1] += ",";
48
- }
49
- visit(states[i], lines2, shift + " ");
50
- }
51
- lines2.push(`${shift} ]`);
52
- }
53
- if (params && Object.keys(params).length > 0) {
54
- lines2[lines2.length - 1] += ",";
55
- const str = JSON.stringify(params, null, 2);
56
- const paramsLines = str.split("\n");
57
- paramsLines.shift();
58
- paramsLines.pop();
59
- lines2.push(...paramsLines.map((line) => shift + line));
60
- }
61
- lines2.push(`${shift}}`);
62
- }
63
- }
64
-
65
- // src/config/splitTransitionString.ts
66
- function splitTransitionString(str) {
67
- let list = [];
68
- let pos = 0;
69
- const matches = Array.from(
70
- str.matchAll(/-\s*("[^"]*?"|'[^']*?'|[^-]*?)\s*->/g)
71
- );
72
- for (const match of matches) {
73
- list.push(str.substring(pos, match.index));
74
- list.push(match[1]);
75
- pos = match.index + match[0].length;
76
- }
77
- list.push(str.substring(pos));
78
- list = list.map((str2) => str2.trim());
79
- const transitions = [];
80
- for (let i = 0; i < list.length - 2; ) {
81
- const from = list[i++] || "";
82
- const event = list[i++] || "";
83
- const to = list[i] || "";
84
- transitions.push([from, event, to]);
85
- }
86
- return transitions;
87
- }
88
-
89
- // src/config/toFsmStateConfig.ts
90
- function toFsmStateConfig(json) {
91
- if (!json || typeof json !== "object") return;
92
- const { key, transitions, states, ...options } = json;
93
- if (typeof key !== "string") return;
94
- const stateTransitions = toFsmTransitions(transitions);
95
- const substates = toArray(states).map(toFsmStateConfig).filter((v) => !!v);
96
- const state = {
97
- key,
98
- transitions: stateTransitions
99
- };
100
- if (substates && substates.length > 0) state.states = substates;
101
- return Object.assign(state, options);
102
- function toFsmTransitions(json2) {
103
- const transitions2 = [];
104
- const index = {};
105
- const array = toArray(json2);
106
- for (let line of array) {
107
- const lineTransitions = typeof line === "string" ? splitTransitionString(line) : [toArray(line)];
108
- for (const transition of lineTransitions.map(validateTransition)) {
109
- if (!transition) continue;
110
- const key2 = transition.join("|");
111
- if (key2 in index) continue;
112
- index[key2] = true;
113
- transitions2.push(transition);
114
- }
115
- }
116
- return transitions2;
117
- }
118
- function validateTransition(value) {
119
- const array = toArray(value);
120
- if (array.length !== 3) return;
121
- const [from, event, to] = array;
122
- if (typeof from !== "string" || typeof event !== "string" || typeof to !== "string")
123
- return;
124
- return [from, event, to];
125
- }
126
- function toArray(value) {
127
- if (value === void 0) return [];
128
- if (Array.isArray(value)) return value;
129
- return [value];
130
- }
131
- }
132
-
133
- export { concatTransitionsToString, serializeProcessConfig, splitTransitionString, toFsmStateConfig };
@@ -1,422 +0,0 @@
1
- // src/runtime/RuntimeStatechartApi.ts
2
- var RuntimeStatechartApi = class {
3
- constructor({
4
- element,
5
- statechart
6
- }) {
7
- this._listenersCounter = 0;
8
- this._transitionsClickListeners = {};
9
- this._statesClickListeners = {};
10
- this._registry = {};
11
- this.element = element;
12
- this.statechart = statechart;
13
- this._bindListeners();
14
- }
15
- close() {
16
- Object.values(this._registry).forEach((r) => r && r());
17
- this._registry = {};
18
- this._transitionsClickListeners = {};
19
- this._statesClickListeners = {};
20
- }
21
- onStateClick(listener) {
22
- return this._addListener(this._statesClickListeners, listener);
23
- }
24
- onTransitionClick(listener) {
25
- return this._addListener(this._transitionsClickListeners, listener);
26
- }
27
- openStatePanels() {
28
- this._setStatePanelStatus(`details[data-state-id]`, true);
29
- }
30
- closeStatePanels() {
31
- this._setStatePanelStatus(`details[data-state-id]`, false);
32
- }
33
- isStatePanelOpen(stateId) {
34
- const elm = this.element.querySelector(
35
- `details[data-state-id="${stateId}"]`
36
- );
37
- return elm && !!elm.open;
38
- }
39
- toggleState(...stateIds) {
40
- const lastId = stateIds[stateIds.length - 1];
41
- const selected = this.isStateSelected(lastId);
42
- this.deselectStates();
43
- this.closeStatePanels();
44
- this.activateStates(...stateIds);
45
- if (selected) {
46
- this.deselectState(lastId);
47
- this.closeStatePanel(lastId);
48
- }
49
- return !selected;
50
- }
51
- activateStates(...stateIds) {
52
- for (let i = 0; i < stateIds.length; i++) {
53
- const id = stateIds[i];
54
- this.openStatePanel(id);
55
- this.selectState(id);
56
- }
57
- }
58
- focusStates(...stateIds) {
59
- for (let i = 0; i < stateIds.length; i++) {
60
- const stateId = stateIds[i];
61
- for (const tagName of ["details", "g"]) {
62
- const elm = this.element.querySelector(
63
- `${tagName}[data-state-id="${stateId}"]`
64
- );
65
- elm?.scrollIntoView();
66
- }
67
- }
68
- }
69
- setStateDescription(stateId, description) {
70
- const descriptionContainer = this.element.querySelector(
71
- `details[data-state-id="${stateId}"] .state-details__description`
72
- );
73
- if (descriptionContainer) {
74
- descriptionContainer.innerHTML = "";
75
- descriptionContainer.append(description ?? "");
76
- }
77
- }
78
- closeStatePanel(stateId) {
79
- this._setStatePanelStatus(`details[data-state-id="${stateId}"]`, false);
80
- }
81
- openStatePanel(stateId) {
82
- this._setStatePanelStatus(`details[data-state-id="${stateId}"]`, true);
83
- }
84
- deselectTransitions(modifier = "selected") {
85
- this._removeTransitionsModifier("[data-transition-id]", modifier);
86
- }
87
- deselectTransition(id, modifier = "selected") {
88
- this._removeTransitionsModifier(`[data-transition-id="${id}"]`, modifier);
89
- }
90
- selectTransition(id, modifier = "selected") {
91
- this._addTransitionsModifier(`[data-transition-id="${id}"]`, modifier);
92
- }
93
- toggleTransition(id, modifier = "selected") {
94
- const selected = this.isTransitionSelected(id, modifier);
95
- this.deselectTransitions();
96
- if (!selected) {
97
- this.selectTransition(id, modifier);
98
- }
99
- }
100
- isTransitionSelected(id, modifier = "selected") {
101
- const elm = this.element.querySelector(`[data-transition-id="${id}"]`);
102
- const stateModifier = `transition--${modifier}`;
103
- return !!elm?.classList.contains(stateModifier);
104
- }
105
- deselectStates(modifier = "selected") {
106
- this._removeStateModifier("[data-state-id]", modifier);
107
- }
108
- isStateSelected(id, modifier = "selected") {
109
- const elm = this.element.querySelector(`[data-state-id="${id}"]`);
110
- const stateModifier = `state--${modifier}`;
111
- return !!elm?.classList.contains(stateModifier);
112
- }
113
- deselectState(id, modifier = "selected") {
114
- this._removeStateModifier(`[data-state-id="${id}"]`, modifier);
115
- }
116
- selectState(id, modifier = "selected") {
117
- this._addStateModifier(`[data-state-id="${id}"]`, modifier);
118
- }
119
- // Internal methods
120
- _addListener(index, listener) {
121
- const id = this._listenersCounter++;
122
- index[id] = listener;
123
- return () => {
124
- delete index[id];
125
- };
126
- }
127
- _bindListeners() {
128
- const bindListeners = (index, dataField, eventType) => {
129
- function newEventListener(index2, elm) {
130
- return (ev) => {
131
- ev.preventDefault();
132
- ev.stopPropagation();
133
- let id = elm.getAttribute(dataField);
134
- if (!id) {
135
- id = elm.closest(`[${dataField}]`)?.getAttribute(dataField);
136
- }
137
- if (id) {
138
- for (const listener of Object.values(index2)) {
139
- listener(id);
140
- }
141
- }
142
- };
143
- }
144
- [...this.element.querySelectorAll(`[${dataField}]`)].forEach((elm) => {
145
- const eventListener = newEventListener(index, elm);
146
- elm.addEventListener(eventType, eventListener);
147
- this._addListener(
148
- this._registry,
149
- () => elm.removeEventListener(eventType, eventListener)
150
- );
151
- });
152
- };
153
- bindListeners(this._statesClickListeners, "data-state-id", "click");
154
- bindListeners(
155
- this._transitionsClickListeners,
156
- "data-transition-id",
157
- "click"
158
- );
159
- }
160
- _setStatePanelStatus(selector, open) {
161
- [...this.element.querySelectorAll(selector)].forEach((elm) => {
162
- if (open) elm.setAttribute("open", "open");
163
- else elm.removeAttribute("open");
164
- });
165
- }
166
- _addStateModifier(cssSelector, modifier = "selected") {
167
- this._applyStateModifier(
168
- cssSelector,
169
- modifier,
170
- (elm, cls) => elm && elm.classList.add(cls)
171
- );
172
- }
173
- _removeStateModifier(cssSelector, modifier = "selected") {
174
- this._applyStateModifier(
175
- cssSelector,
176
- modifier,
177
- (elm, cls) => elm && elm.classList.remove(cls)
178
- );
179
- }
180
- _applyStateModifier(cssSelector, modifier = "selected", apply) {
181
- const detailsModifier = `state-details--${modifier}`;
182
- const stateLabelsModifier = `state-details__label--${modifier}`;
183
- const stateModifier = `state--${modifier}`;
184
- [...this.element.querySelectorAll(cssSelector)].forEach((elm) => {
185
- if (elm.tagName.toLowerCase() === "details") {
186
- apply(elm, detailsModifier);
187
- const labelElm = elm.querySelector(".state-details__label");
188
- if (labelElm) apply(labelElm, stateLabelsModifier);
189
- } else {
190
- apply(elm, stateModifier);
191
- }
192
- });
193
- }
194
- _addTransitionsModifier(cssSelector, modifier = "selected") {
195
- this._applyTransitionsModifier(
196
- cssSelector,
197
- modifier,
198
- (elm, cls) => elm && elm.classList.add(cls)
199
- );
200
- }
201
- _removeTransitionsModifier(cssSelector, modifier = "selected") {
202
- this._applyTransitionsModifier(
203
- cssSelector,
204
- modifier,
205
- (elm, cls) => elm && elm.classList.remove(cls)
206
- );
207
- }
208
- _applyTransitionsModifier(cssSelector, modifier = "selected", apply) {
209
- const transitionModifier = `transition--${modifier}`;
210
- const transitionLineModifier = `transition__line--${modifier}`;
211
- const transitionBoxModifier = `transition__box--${modifier}`;
212
- const transitionLabelModifier = `transition__label--${modifier}`;
213
- [...this.element.querySelectorAll(cssSelector)].forEach((elm) => {
214
- apply(elm, transitionModifier);
215
- apply(elm.querySelector(".transition__line"), transitionLineModifier);
216
- apply(elm.querySelector(".transition__box"), transitionBoxModifier);
217
- apply(elm.querySelector(".transition__label"), transitionLabelModifier);
218
- });
219
- }
220
- };
221
-
222
- // src/runtime/StateChartIndex.ts
223
- var StateChartIndex = class {
224
- constructor({
225
- statechart,
226
- initialStateKey = "<initial>",
227
- finalStateKey = "<final>"
228
- }) {
229
- this._chartsIndex = {};
230
- this._nodesIndex = {};
231
- this._nodesParentsIndex = {};
232
- this._edgeIndex = {};
233
- this._edgesParentsIndex = {};
234
- this.statechart = statechart;
235
- this.initialStateKey = initialStateKey;
236
- this.finalStateKey = finalStateKey;
237
- this._buildIndexes();
238
- }
239
- /**
240
- * Returns a state node or a chart by the specified identifier.
241
- * @param id the id of a state node or a chart
242
- * @returns a state node or a chart
243
- */
244
- getNodeById(id) {
245
- return this._nodesIndex[id] || this._chartsIndex[id];
246
- }
247
- /**
248
- * Returns a chart by the specified identifier.
249
- * @param id the id of a state node or a chart
250
- * @returns a state node or a chart
251
- */
252
- getChartById(id) {
253
- return this._chartsIndex[id];
254
- }
255
- /**
256
- * Returns an edge by the specified identifier.
257
- * @param id the id of a graph edge
258
- * @returns a graph edge
259
- */
260
- getEdgeById(id) {
261
- return this._edgeIndex[id];
262
- }
263
- /**
264
- * Returns a list of all nodes in this chart.
265
- * @return a list of all nodes
266
- */
267
- getAllNodes() {
268
- return [
269
- .../* @__PURE__ */ new Set([
270
- ...Object.values(this._chartsIndex),
271
- ...Object.values(this._nodesIndex)
272
- ])
273
- ];
274
- }
275
- /**
276
- * Returns a list of all non-empty state nodes in this chart.
277
- * The result excludes nodes for initial and fina states.
278
- * @return a list of all non-empty state nodes
279
- */
280
- getStateNodes() {
281
- return this.getAllNodes().filter(
282
- (node) => node.key !== this.initialStateKey && node.key !== this.finalStateKey
283
- );
284
- }
285
- /**
286
- * Transforms a stack of state keys to the corresponding identifiers.
287
- * If a key in the stack was not found then this method returns
288
- * an empty stack.
289
- * @param stack a stack of state keys
290
- * @returns a stack of state identifiers for the given states
291
- */
292
- getStatesIds(...stack) {
293
- let result = [];
294
- let list = [this.statechart];
295
- for (let i = 0; list?.length > 0 && i < stack.length; i++) {
296
- const key = stack[i];
297
- const node = list.find((node2) => node2.key === key);
298
- if (!node) break;
299
- result.push(node.id);
300
- list = [...node.children || [], ...node.nodes || []];
301
- }
302
- return result.length === stack.length ? result : [];
303
- }
304
- /**
305
- * Returns graph nodes for the specified identifier.
306
- * @param id the id of a node
307
- * @returns a stack of nodes
308
- */
309
- getStackByStateId(id) {
310
- if (!id) return [];
311
- const node = this._chartsIndex[id] || this._nodesIndex[id];
312
- if (!node) return [];
313
- const parentId = this._nodesParentsIndex[id];
314
- if (parentId) {
315
- const parentStack = this.getStackByStateId(parentId);
316
- if (!parentStack.length) return [];
317
- parentStack.push(node);
318
- return parentStack;
319
- } else {
320
- return [node];
321
- }
322
- }
323
- /**
324
- * Returns an edge object corresponding to the transition from the state
325
- * @param stateId identifier of the source state
326
- * @param event an event triggering the transition
327
- * @returns graph edge
328
- */
329
- getTransition(stateId, event) {
330
- const stack = this.getStackByStateId(stateId);
331
- const state = stack.pop();
332
- if (!state || !stack.length) return;
333
- const parent = stack[stack.length - 1];
334
- const t = parent.edges.find(
335
- (t2) => t2.from === state.key && t2.event === event
336
- );
337
- return t;
338
- }
339
- /**
340
- * Returns a list of edge objects corresponding to all transitions from the state
341
- * @param stateId identifier of the source state
342
- * @returns a list of graph edges
343
- */
344
- getTransitions(stateId) {
345
- const stack = this.getStackByStateId(stateId);
346
- const state = stack.pop();
347
- if (!state || !stack.length) return [];
348
- const parent = stack[stack.length - 1];
349
- return parent.edges.filter((t) => t.from === state.key);
350
- }
351
- /**
352
- * Returns a graph node corresponding to the specified state id.
353
- * @param stateId identifier of the state
354
- * @returns graph node
355
- * @see getStateChart
356
- */
357
- getStateNode(stateId) {
358
- return stateId ? this._nodesIndex[stateId] : void 0;
359
- }
360
- /**
361
- * Returns a chart corresponding to the specified state id.
362
- * @param stateId identifier of the state
363
- * @returns graph node
364
- * @see getStateNode
365
- */
366
- getStateChart(stateId) {
367
- return stateId ? this._chartsIndex[stateId] : void 0;
368
- }
369
- /**
370
- * Returns ids of the previous and next state for the specified transition
371
- * @param transition a graph edge instance defining transition
372
- * @returns an array with two identifiers - [sourceStateId, targetStateId] - the source state id and target state id
373
- */
374
- getTransitionStateIds(transition) {
375
- const result = [
376
- void 0,
377
- void 0
378
- ];
379
- if (!transition) return result;
380
- const parentId = this._edgesParentsIndex[transition.id];
381
- if (!parentId) return result;
382
- const parent = this._chartsIndex[parentId];
383
- if (!parent) return result;
384
- parent.nodes.forEach((node) => {
385
- if (node.key === transition.from) {
386
- result[0] = node.id;
387
- }
388
- if (node.key === transition.to) {
389
- result[1] = node.id;
390
- }
391
- });
392
- return result;
393
- }
394
- // ----------------------------------------------------------
395
- // Internal methods
396
- _buildIndexes() {
397
- const addToIndex = (chart) => {
398
- const chartId = chart.id;
399
- this._chartsIndex[chartId] = chart;
400
- if (!(chartId in this._nodesIndex)) {
401
- this._nodesIndex[chartId] = chart;
402
- }
403
- chart.nodes.forEach((node) => {
404
- const nodeId = node.id;
405
- this._nodesIndex[nodeId] = node;
406
- this._nodesParentsIndex[nodeId] = chartId;
407
- });
408
- chart.edges.forEach((edge) => {
409
- const edgeId = edge.id;
410
- this._edgeIndex[edgeId] = edge;
411
- this._edgesParentsIndex[edgeId] = chartId;
412
- });
413
- (chart.children || []).forEach((child) => {
414
- this._nodesParentsIndex[child.id] = chartId;
415
- addToIndex(child);
416
- });
417
- };
418
- addToIndex(this.statechart);
419
- }
420
- };
421
-
422
- export { RuntimeStatechartApi, StateChartIndex };