@statewalker/fsm-charts 0.1.0 → 0.1.2

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.
@@ -0,0 +1,377 @@
1
+ import { __publicField } from './chunk-XEO3YXBM.js';
2
+
3
+ // src/runtime/RuntimeStatechartApi.ts
4
+ var RuntimeStatechartApi = class {
5
+ constructor({
6
+ element,
7
+ statechart
8
+ }) {
9
+ __publicField(this, "element");
10
+ __publicField(this, "statechart");
11
+ __publicField(this, "_listenersCounter", 0);
12
+ __publicField(this, "_transitionsClickListeners", {});
13
+ __publicField(this, "_statesClickListeners", {});
14
+ __publicField(this, "_registry", {});
15
+ this.element = element;
16
+ this.statechart = statechart;
17
+ this._bindListeners();
18
+ }
19
+ close() {
20
+ Object.values(this._registry).forEach((r) => r && r());
21
+ this._registry = {};
22
+ this._transitionsClickListeners = {};
23
+ this._statesClickListeners = {};
24
+ }
25
+ onStateClick(listener) {
26
+ return this._addListener(this._statesClickListeners, listener);
27
+ }
28
+ onTransitionClick(listener) {
29
+ return this._addListener(this._transitionsClickListeners, listener);
30
+ }
31
+ openStatePenals() {
32
+ this._setStatePanelStatus(`details[data-state-id]`, true);
33
+ }
34
+ closeStatePenals() {
35
+ this._setStatePanelStatus(`details[data-state-id]`, false);
36
+ }
37
+ isStatePanelOpen(stateId) {
38
+ const elm = this.element.querySelector(
39
+ `details[data-state-id="${stateId}"]`
40
+ );
41
+ return elm && !!elm.open;
42
+ }
43
+ toggleState(...stateIds) {
44
+ const lastId = stateIds[stateIds.length - 1];
45
+ const selected = this.isStateSelected(lastId);
46
+ this.deselectStates();
47
+ this.closeStatePenals();
48
+ this.activateStates(...stateIds);
49
+ if (selected) {
50
+ this.deselectState(lastId);
51
+ this.closeStatePanel(lastId);
52
+ }
53
+ return !selected;
54
+ }
55
+ activateStates(...stateIds) {
56
+ for (let i = 0; i < stateIds.length; i++) {
57
+ const id = stateIds[i];
58
+ this.openStatePanel(id);
59
+ this.selectState(id);
60
+ }
61
+ }
62
+ // toggleStatePanel(stateId) {
63
+ // const elm = this.element.querySelector(
64
+ // `details[data-state-id="${stateId}"]`
65
+ // );
66
+ // if (elm) {
67
+ // elm.open = !elm.open;
68
+ // }
69
+ // }
70
+ closeStatePanel(stateId) {
71
+ this._setStatePanelStatus(`details[data-state-id="${stateId}"]`, false);
72
+ }
73
+ openStatePanel(stateId) {
74
+ this._setStatePanelStatus(`details[data-state-id="${stateId}"]`, true);
75
+ }
76
+ deselectTransitions(modifier = "selected") {
77
+ this._removeTransitionsModifier("[data-transition-id]", modifier);
78
+ }
79
+ deselectTransition(id, modifier = "selected") {
80
+ this._removeTransitionsModifier(`[data-transition-id="${id}"]`, modifier);
81
+ }
82
+ selectTransition(id, modifier = "selected") {
83
+ this._addTransitionsModifier(`[data-transition-id="${id}"]`, modifier);
84
+ }
85
+ toggleTransition(id, modifier = "selected") {
86
+ const selected = this.isTransitionSelected(id, modifier);
87
+ this.deselectTransitions();
88
+ if (!selected) {
89
+ this.selectTransition(id, modifier);
90
+ }
91
+ }
92
+ isTransitionSelected(id, modifier = "selected") {
93
+ const elm = this.element.querySelector(`[data-transition-id="${id}"]`);
94
+ const stateModifier = `transition--${modifier}`;
95
+ return !!elm?.classList.contains(stateModifier);
96
+ }
97
+ deselectStates(modifier = "selected") {
98
+ this._removeStateModifier("[data-state-id]", modifier);
99
+ }
100
+ isStateSelected(id, modifier = "selected") {
101
+ const elm = this.element.querySelector(`[data-state-id="${id}"]`);
102
+ const stateModifier = `state--${modifier}`;
103
+ return !!elm?.classList.contains(stateModifier);
104
+ }
105
+ deselectState(id, modifier = "selected") {
106
+ this._removeStateModifier(`[data-state-id="${id}"]`, modifier);
107
+ }
108
+ selectState(id, modifier = "selected") {
109
+ this._addStateModifier(`[data-state-id="${id}"]`, modifier);
110
+ }
111
+ // Internal methods
112
+ _addListener(index, listener) {
113
+ const id = this._listenersCounter++;
114
+ index[id] = listener;
115
+ return () => {
116
+ delete index[id];
117
+ };
118
+ }
119
+ _bindListeners() {
120
+ const bindListeners = (index, dataField, eventType) => {
121
+ function newEventListener(index2, elm) {
122
+ return (ev) => {
123
+ ev.preventDefault();
124
+ ev.stopPropagation();
125
+ let id = elm.getAttribute(dataField);
126
+ if (!id) {
127
+ id = elm.closest(`[${dataField}]`)?.getAttribute(dataField);
128
+ }
129
+ if (id) {
130
+ for (const listener of Object.values(index2)) {
131
+ listener(id);
132
+ }
133
+ }
134
+ };
135
+ }
136
+ [...this.element.querySelectorAll(`[${dataField}]`)].forEach((elm) => {
137
+ const eventListener = newEventListener(index, elm);
138
+ elm.addEventListener(eventType, eventListener);
139
+ this._addListener(
140
+ this._registry,
141
+ () => elm.removeEventListener(eventType, eventListener)
142
+ );
143
+ });
144
+ };
145
+ bindListeners(this._statesClickListeners, "data-state-id", "click");
146
+ bindListeners(
147
+ this._transitionsClickListeners,
148
+ "data-transition-id",
149
+ "click"
150
+ );
151
+ }
152
+ _setStatePanelStatus(selector, open) {
153
+ [...this.element.querySelectorAll(selector)].forEach((elm) => {
154
+ if (open)
155
+ elm.setAttribute("open", "open");
156
+ else
157
+ elm.removeAttribute("open");
158
+ });
159
+ }
160
+ _addStateModifier(cssSelector, modifier = "selected") {
161
+ this._applyStateModifier(
162
+ cssSelector,
163
+ modifier,
164
+ (elm, cls) => elm && elm.classList.add(cls)
165
+ );
166
+ }
167
+ _removeStateModifier(cssSelector, modifier = "selected") {
168
+ this._applyStateModifier(
169
+ cssSelector,
170
+ modifier,
171
+ (elm, cls) => elm && elm.classList.remove(cls)
172
+ );
173
+ }
174
+ _applyStateModifier(cssSelector, modifier = "selected", apply) {
175
+ const detailsModifier = `state-details--${modifier}`;
176
+ const stateLabelsModifier = `state-details__label--${modifier}`;
177
+ const stateModifier = `state--${modifier}`;
178
+ [...this.element.querySelectorAll(cssSelector)].forEach((elm) => {
179
+ if (elm.tagName.toLowerCase() === "details") {
180
+ apply(elm, detailsModifier);
181
+ const labelElm = elm.querySelector(".state-details__label");
182
+ if (labelElm)
183
+ apply(labelElm, stateLabelsModifier);
184
+ } else {
185
+ apply(elm, stateModifier);
186
+ }
187
+ });
188
+ }
189
+ _addTransitionsModifier(cssSelector, modifier = "selected") {
190
+ this._applyTransitionsModifier(
191
+ cssSelector,
192
+ modifier,
193
+ (elm, cls) => elm && elm.classList.add(cls)
194
+ );
195
+ }
196
+ _removeTransitionsModifier(cssSelector, modifier = "selected") {
197
+ this._applyTransitionsModifier(
198
+ cssSelector,
199
+ modifier,
200
+ (elm, cls) => elm && elm.classList.remove(cls)
201
+ );
202
+ }
203
+ _applyTransitionsModifier(cssSelector, modifier = "selected", apply) {
204
+ const transitionModifier = `transition--${modifier}`;
205
+ const transitionLineModifier = `transition__line--${modifier}`;
206
+ const transitionBoxModifier = `transition__box--${modifier}`;
207
+ const transitionLabelModifier = `transition__label--${modifier}`;
208
+ [...this.element.querySelectorAll(cssSelector)].forEach((elm) => {
209
+ apply(elm, transitionModifier);
210
+ apply(elm.querySelector(".transition__line"), transitionLineModifier);
211
+ apply(elm.querySelector(".transition__box"), transitionBoxModifier);
212
+ apply(elm.querySelector(".transition__label"), transitionLabelModifier);
213
+ });
214
+ }
215
+ };
216
+
217
+ // src/runtime/StateChartIndex.ts
218
+ var StateChartIndex = class {
219
+ constructor({ statechart }) {
220
+ __publicField(this, "statechart");
221
+ __publicField(this, "_chartsIndex", {});
222
+ __publicField(this, "_nodesIndex", {});
223
+ __publicField(this, "_nodesParentsIndex", {});
224
+ __publicField(this, "_edgeIndex", {});
225
+ __publicField(this, "_edgesParentsIndex", {});
226
+ this.statechart = statechart;
227
+ this._buildIndexes();
228
+ }
229
+ /**
230
+ * Transforms a stack of state keys to the corresponding identifiers.
231
+ * If a key in the stack was not found then this method returns
232
+ * an empty stack.
233
+ * @param stack a stack of state keys
234
+ * @returns a stack of state identifiers for the given states
235
+ */
236
+ getStatesIds(...stack) {
237
+ let result = [];
238
+ let list = [this.statechart];
239
+ for (let i = 0; list?.length > 0 && i < stack.length; i++) {
240
+ const key = stack[i];
241
+ const node = list.find((node2) => node2.key === key);
242
+ if (!node)
243
+ break;
244
+ result.push(node.id);
245
+ list = node.children;
246
+ if (!list?.length)
247
+ list = node.nodes;
248
+ }
249
+ return result.length === stack.length ? result : [];
250
+ }
251
+ /**
252
+ * Returns graph nodes for the specified identifier.
253
+ * @param id the id of a node
254
+ * @returns a stack of nodes
255
+ */
256
+ getStackByStateId(id) {
257
+ if (!id)
258
+ return [];
259
+ const node = this._chartsIndex[id] || this._nodesIndex[id];
260
+ if (!node)
261
+ return [];
262
+ const parentId = this._nodesParentsIndex[id];
263
+ if (parentId) {
264
+ const parentStack = this.getStackByStateId(parentId);
265
+ if (!parentStack.length)
266
+ return [];
267
+ parentStack.push(node);
268
+ return parentStack;
269
+ } else {
270
+ return [node];
271
+ }
272
+ }
273
+ /**
274
+ * Returns an edge object corresponding to the transition from the state
275
+ * @param stateId identifier of the source state
276
+ * @param event an event triggering the transition
277
+ * @returns graph edge
278
+ */
279
+ getTransition(stateId, event) {
280
+ const stack = this.getStackByStateId(stateId);
281
+ const state = stack.pop();
282
+ if (!state || !stack.length)
283
+ return;
284
+ const parent = stack[stack.length - 1];
285
+ const t = parent.edges.find(
286
+ (t2) => t2.from === state.key && t2.event === event
287
+ );
288
+ return t;
289
+ }
290
+ /**
291
+ * Returns a list of edge objects corresponding to all transitions from the state
292
+ * @param stateId identifier of the source state
293
+ * @returns a list of graph edges
294
+ */
295
+ getTransitions(stateId) {
296
+ const stack = this.getStackByStateId(stateId);
297
+ const state = stack.pop();
298
+ if (!state || !stack.length)
299
+ return [];
300
+ const parent = stack[stack.length - 1];
301
+ return parent.edges.filter((t) => t.from === state.key);
302
+ }
303
+ /**
304
+ * Returns a graph node corresponding to the specified state id.
305
+ * @param stateId identifier of the state
306
+ * @returns graph node
307
+ * @see getStateChart
308
+ */
309
+ getStateNode(stateId) {
310
+ return stateId ? this._nodesIndex[stateId] : void 0;
311
+ }
312
+ /**
313
+ * Returns a chart corresponding to the specified state id.
314
+ * @param stateId identifier of the state
315
+ * @returns graph node
316
+ * @see getStateNode
317
+ */
318
+ getStateChart(stateId) {
319
+ return stateId ? this._chartsIndex[stateId] : void 0;
320
+ }
321
+ /**
322
+ * Returns ids of the previous and next state for the specified transition
323
+ * @param transition a graph edge instance defining transition
324
+ * @returns an array with two identifiers - [sourceStateId, targetStateId] - the source state id and target state id
325
+ */
326
+ getTransitionStateIds(transition) {
327
+ const result = [
328
+ void 0,
329
+ void 0
330
+ ];
331
+ if (!transition)
332
+ return result;
333
+ const parentId = this._edgesParentsIndex[transition.id];
334
+ if (!parentId)
335
+ return result;
336
+ const parent = this._chartsIndex[parentId];
337
+ if (!parent)
338
+ return result;
339
+ parent.nodes.forEach((node) => {
340
+ if (node.key === transition.from) {
341
+ result[0] = node.id;
342
+ }
343
+ if (node.key === transition.to) {
344
+ result[1] = node.id;
345
+ }
346
+ });
347
+ return result;
348
+ }
349
+ // ----------------------------------------------------------
350
+ // Internal methods
351
+ _buildIndexes() {
352
+ const addToIndex = (chart) => {
353
+ const chartId = chart.id;
354
+ this._chartsIndex[chartId] = chart;
355
+ if (!(chartId in this._nodesIndex)) {
356
+ this._nodesIndex[chartId] = chart;
357
+ }
358
+ chart.nodes.forEach((node) => {
359
+ const nodeId = node.id;
360
+ this._nodesIndex[nodeId] = node;
361
+ this._nodesParentsIndex[nodeId] = chartId;
362
+ });
363
+ chart.edges.forEach((edge) => {
364
+ const edgeId = edge.id;
365
+ this._edgeIndex[edgeId] = edge;
366
+ this._edgesParentsIndex[edgeId] = chartId;
367
+ });
368
+ (chart.children || []).forEach((child) => {
369
+ this._nodesParentsIndex[child.id] = chartId;
370
+ addToIndex(child);
371
+ });
372
+ };
373
+ addToIndex(this.statechart);
374
+ }
375
+ };
376
+
377
+ export { RuntimeStatechartApi, StateChartIndex };
@@ -1,6 +1,48 @@
1
1
  import { S as StateChart, c as StateGraphNode, b as StateGraphEdge } from './StateChart-PdtJDEPy.js';
2
2
  export { D as Dimensions, F as FsmStateConfig, G as GraphParamsProvider, P as Padding, a as Position, T as Transition, d as TransitionsGraph } from './StateChart-PdtJDEPy.js';
3
3
 
4
+ type RuntimeListener = (id: string) => void;
5
+ declare class RuntimeStatechartApi {
6
+ element: HTMLElement;
7
+ statechart: StateChart;
8
+ _listenersCounter: number;
9
+ _transitionsClickListeners: Record<number, RuntimeListener>;
10
+ _statesClickListeners: Record<number, RuntimeListener>;
11
+ _registry: Record<number, () => void>;
12
+ constructor({ element, statechart, }: {
13
+ element: HTMLElement;
14
+ statechart: StateChart;
15
+ });
16
+ close(): void;
17
+ onStateClick(listener: RuntimeListener): () => void;
18
+ onTransitionClick(listener: RuntimeListener): () => void;
19
+ openStatePenals(): void;
20
+ closeStatePenals(): void;
21
+ isStatePanelOpen(stateId: string): boolean;
22
+ toggleState(...stateIds: string[]): boolean;
23
+ activateStates(...stateIds: string[]): void;
24
+ closeStatePanel(stateId: string): void;
25
+ openStatePanel(stateId: string): void;
26
+ deselectTransitions(modifier?: string): void;
27
+ deselectTransition(id: string, modifier?: string): void;
28
+ selectTransition(id: string, modifier?: string): void;
29
+ toggleTransition(id: string, modifier?: string): void;
30
+ isTransitionSelected(id: string, modifier?: string): boolean;
31
+ deselectStates(modifier?: string): void;
32
+ isStateSelected(id: string, modifier?: string): boolean;
33
+ deselectState(id: string, modifier?: string): void;
34
+ selectState(id: string, modifier?: string): void;
35
+ _addListener<T = Function>(index: Record<string, T>, listener: T): () => void;
36
+ _bindListeners(): void;
37
+ _setStatePanelStatus(selector: string, open: boolean): void;
38
+ _addStateModifier(cssSelector: string, modifier?: string): void;
39
+ _removeStateModifier(cssSelector: string, modifier?: string): void;
40
+ _applyStateModifier(cssSelector: string, modifier: string | undefined, apply: (elm: Element | null, className: string) => void): void;
41
+ _addTransitionsModifier(cssSelector: string, modifier?: string): void;
42
+ _removeTransitionsModifier(cssSelector: string, modifier?: string): void;
43
+ _applyTransitionsModifier(cssSelector: string, modifier: string | undefined, apply: (elm: Element | null, className: string) => void): void;
44
+ }
45
+
4
46
  declare class StateChartIndex {
5
47
  statechart: StateChart;
6
48
  _chartsIndex: Record<string, StateChart>;
@@ -32,6 +74,12 @@ declare class StateChartIndex {
32
74
  * @returns graph edge
33
75
  */
34
76
  getTransition(stateId?: string, event?: string): StateGraphEdge | undefined;
77
+ /**
78
+ * Returns a list of edge objects corresponding to all transitions from the state
79
+ * @param stateId identifier of the source state
80
+ * @returns a list of graph edges
81
+ */
82
+ getTransitions(stateId?: string): StateGraphEdge[];
35
83
  /**
36
84
  * Returns a graph node corresponding to the specified state id.
37
85
  * @param stateId identifier of the state
@@ -55,4 +103,4 @@ declare class StateChartIndex {
55
103
  _buildIndexes(): void;
56
104
  }
57
105
 
58
- export { StateChart, StateChartIndex, StateGraphEdge, StateGraphNode };
106
+ export { type RuntimeListener, RuntimeStatechartApi, StateChart, StateChartIndex, StateGraphEdge, StateGraphNode };
@@ -1,2 +1,2 @@
1
- export { StateChartIndex } from './chunk-XPUWN37Q.js';
1
+ export { RuntimeStatechartApi, StateChartIndex } from './chunk-UBYXED35.js';
2
2
  import './chunk-XEO3YXBM.js';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { D as Dimensions, F as FsmStateConfig, G as GraphParamsProvider, P as Padding, a as Position, S as StateChart, b as StateGraphEdge, c as StateGraphNode, T as Transition, d as TransitionsGraph } from './StateChart-PdtJDEPy.js';
2
2
  export { buildCharts, buildFlatCharts, getGraphParamsProvider, getLabelDimensions } from './index-layout.js';
3
3
  export { C as CssTree, b as buildStatechartCss, c as buildStatechartStyles, d as buildStatechartSvg, a as buildStatechartsPanel, s as serializeCss } from './index-html-CbDZzk2n.js';
4
- export { StateChartIndex } from './index-runtime.js';
4
+ export { RuntimeListener, RuntimeStatechartApi, StateChartIndex } from './index-runtime.js';
5
5
 
6
6
  declare function toKebabCase(str: string): string;
7
7
 
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { buildCharts, buildFlatCharts, getGraphParamsProvider, getLabelDimensions } from './chunk-ZSGS37QH.js';
2
2
  export { buildStatechartCss, buildStatechartStyles, buildStatechartSvg, buildStatechartsPanel, serializeCss, toKebabCase } from './chunk-YYNW6Q2T.js';
3
3
  import './chunk-NXWOFQMO.js';
4
- export { StateChartIndex } from './chunk-XPUWN37Q.js';
4
+ export { RuntimeStatechartApi, StateChartIndex } from './chunk-UBYXED35.js';
5
5
  import './chunk-XEO3YXBM.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statewalker/fsm-charts",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "HFSM Implementation",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/statewalker/statewalker-fsm-charts",
@@ -0,0 +1,250 @@
1
+ import type { StateChart } from "../types/index.ts";
2
+
3
+ export type RuntimeListener = (id: string) => void;
4
+ export class RuntimeStatechartApi {
5
+ element: HTMLElement;
6
+ statechart: StateChart;
7
+ _listenersCounter: number = 0;
8
+ _transitionsClickListeners: Record<number, RuntimeListener> = {};
9
+ _statesClickListeners: Record<number, RuntimeListener> = {};
10
+ _registry: Record<number, () => void> = {};
11
+
12
+ constructor({
13
+ element,
14
+ statechart,
15
+ }: {
16
+ element: HTMLElement;
17
+ statechart: StateChart;
18
+ }) {
19
+ this.element = element;
20
+ this.statechart = statechart;
21
+ this._bindListeners();
22
+ }
23
+
24
+ close() {
25
+ Object.values(this._registry).forEach((r) => r && r());
26
+ this._registry = {};
27
+ this._transitionsClickListeners = {};
28
+ this._statesClickListeners = {};
29
+ }
30
+
31
+ onStateClick(listener: RuntimeListener) {
32
+ return this._addListener(this._statesClickListeners, listener);
33
+ }
34
+ onTransitionClick(listener: RuntimeListener) {
35
+ return this._addListener(this._transitionsClickListeners, listener);
36
+ }
37
+
38
+ openStatePenals() {
39
+ this._setStatePanelStatus(`details[data-state-id]`, true);
40
+ }
41
+ closeStatePenals() {
42
+ this._setStatePanelStatus(`details[data-state-id]`, false);
43
+ }
44
+ isStatePanelOpen(stateId: string) {
45
+ const elm = this.element.querySelector(
46
+ `details[data-state-id="${stateId}"]`
47
+ ) as HTMLDetailsElement;
48
+ return elm && !!elm.open;
49
+ }
50
+
51
+ toggleState(...stateIds: string[]) {
52
+ const lastId = stateIds[stateIds.length - 1];
53
+ const selected = this.isStateSelected(lastId);
54
+ this.deselectStates();
55
+ this.closeStatePenals();
56
+ this.activateStates(...stateIds);
57
+ if (selected) {
58
+ this.deselectState(lastId);
59
+ this.closeStatePanel(lastId);
60
+ }
61
+ return !selected;
62
+ }
63
+
64
+ activateStates(...stateIds: string[]) {
65
+ for (let i = 0; i < stateIds.length; i++) {
66
+ const id = stateIds[i];
67
+ this.openStatePanel(id);
68
+ this.selectState(id);
69
+ }
70
+ }
71
+
72
+ // toggleStatePanel(stateId) {
73
+ // const elm = this.element.querySelector(
74
+ // `details[data-state-id="${stateId}"]`
75
+ // );
76
+ // if (elm) {
77
+ // elm.open = !elm.open;
78
+ // }
79
+ // }
80
+
81
+ closeStatePanel(stateId: string) {
82
+ this._setStatePanelStatus(`details[data-state-id="${stateId}"]`, false);
83
+ }
84
+ openStatePanel(stateId: string) {
85
+ this._setStatePanelStatus(`details[data-state-id="${stateId}"]`, true);
86
+ }
87
+
88
+ deselectTransitions(modifier = "selected") {
89
+ this._removeTransitionsModifier("[data-transition-id]", modifier);
90
+ }
91
+ deselectTransition(id: string, modifier: string = "selected") {
92
+ this._removeTransitionsModifier(`[data-transition-id="${id}"]`, modifier);
93
+ }
94
+ selectTransition(id: string, modifier: string = "selected") {
95
+ this._addTransitionsModifier(`[data-transition-id="${id}"]`, modifier);
96
+ }
97
+ toggleTransition(id: string, modifier: string = "selected") {
98
+ const selected = this.isTransitionSelected(id, modifier);
99
+ this.deselectTransitions();
100
+ if (!selected) {
101
+ this.selectTransition(id, modifier);
102
+ }
103
+ // if (!selected) this.deselectTransition(id, modifier);
104
+ // else this.selectTransition(id, modifier);
105
+ }
106
+ isTransitionSelected(id: string, modifier: string = "selected") {
107
+ const elm = this.element.querySelector(`[data-transition-id="${id}"]`);
108
+ const stateModifier = `transition--${modifier}`;
109
+ return !!elm?.classList.contains(stateModifier);
110
+ }
111
+
112
+ deselectStates(modifier: string = "selected") {
113
+ this._removeStateModifier("[data-state-id]", modifier);
114
+ }
115
+ isStateSelected(id: string, modifier: string = "selected") {
116
+ const elm = this.element.querySelector(`[data-state-id="${id}"]`);
117
+ const stateModifier = `state--${modifier}`;
118
+ return !!elm?.classList.contains(stateModifier);
119
+ }
120
+ deselectState(id: string, modifier: string = "selected") {
121
+ this._removeStateModifier(`[data-state-id="${id}"]`, modifier);
122
+ }
123
+ selectState(id: string, modifier: string = "selected") {
124
+ this._addStateModifier(`[data-state-id="${id}"]`, modifier);
125
+ }
126
+
127
+ // Internal methods
128
+
129
+ _addListener<T = Function>(index: Record<string, T>, listener: T) {
130
+ const id = this._listenersCounter++;
131
+ index[id] = listener;
132
+ return () => {
133
+ delete index[id];
134
+ };
135
+ }
136
+
137
+ _bindListeners() {
138
+ const bindListeners = (
139
+ index: Record<string, RuntimeListener>,
140
+ dataField: string,
141
+ eventType: string
142
+ ) => {
143
+ function newEventListener(
144
+ index: Record<string, RuntimeListener>,
145
+ elm: Element
146
+ ) {
147
+ return (ev: Event) => {
148
+ ev.preventDefault();
149
+ ev.stopPropagation();
150
+ let id: string | null | undefined = elm.getAttribute(dataField);
151
+ if (!id) {
152
+ id = elm.closest(`[${dataField}]`)?.getAttribute(dataField);
153
+ }
154
+ if (id) {
155
+ for (const listener of Object.values(index)) {
156
+ listener(id);
157
+ }
158
+ }
159
+ };
160
+ }
161
+ [...this.element.querySelectorAll(`[${dataField}]`)].forEach((elm) => {
162
+ const eventListener = newEventListener(index, elm);
163
+ elm.addEventListener(eventType, eventListener);
164
+ this._addListener(this._registry, () =>
165
+ elm.removeEventListener(eventType, eventListener)
166
+ );
167
+ });
168
+ };
169
+ bindListeners(this._statesClickListeners, "data-state-id", "click");
170
+ bindListeners(
171
+ this._transitionsClickListeners,
172
+ "data-transition-id",
173
+ "click"
174
+ );
175
+ }
176
+
177
+ _setStatePanelStatus(selector: string, open: boolean) {
178
+ [...this.element.querySelectorAll(selector)].forEach((elm) => {
179
+ if (open) elm.setAttribute("open", "open");
180
+ else elm.removeAttribute("open");
181
+ });
182
+ }
183
+
184
+ _addStateModifier(cssSelector: string, modifier: string = "selected") {
185
+ this._applyStateModifier(
186
+ cssSelector,
187
+ modifier,
188
+ (elm, cls) => elm && elm.classList.add(cls)
189
+ );
190
+ }
191
+ _removeStateModifier(cssSelector: string, modifier: string = "selected") {
192
+ this._applyStateModifier(
193
+ cssSelector,
194
+ modifier,
195
+ (elm, cls) => elm && elm.classList.remove(cls)
196
+ );
197
+ }
198
+ _applyStateModifier(
199
+ cssSelector: string,
200
+ modifier: string = "selected",
201
+ apply: (elm: Element | null, className: string) => void
202
+ ) {
203
+ const detailsModifier = `state-details--${modifier}`;
204
+ const stateLabelsModifier = `state-details__label--${modifier}`;
205
+ const stateModifier = `state--${modifier}`;
206
+ [...this.element.querySelectorAll(cssSelector)].forEach((elm) => {
207
+ if (elm.tagName.toLowerCase() === "details") {
208
+ apply(elm, detailsModifier);
209
+ const labelElm = elm.querySelector(".state-details__label");
210
+ if (labelElm) apply(labelElm, stateLabelsModifier);
211
+ } else {
212
+ apply(elm, stateModifier);
213
+ }
214
+ });
215
+ }
216
+
217
+ _addTransitionsModifier(cssSelector: string, modifier: string = "selected") {
218
+ this._applyTransitionsModifier(
219
+ cssSelector,
220
+ modifier,
221
+ (elm, cls) => elm && elm.classList.add(cls)
222
+ );
223
+ }
224
+ _removeTransitionsModifier(
225
+ cssSelector: string,
226
+ modifier: string = "selected"
227
+ ) {
228
+ this._applyTransitionsModifier(
229
+ cssSelector,
230
+ modifier,
231
+ (elm, cls) => elm && elm.classList.remove(cls)
232
+ );
233
+ }
234
+ _applyTransitionsModifier(
235
+ cssSelector: string,
236
+ modifier: string = "selected",
237
+ apply: (elm: Element | null, className: string) => void
238
+ ) {
239
+ const transitionModifier = `transition--${modifier}`;
240
+ const transitionLineModifier = `transition__line--${modifier}`;
241
+ const transitionBoxModifier = `transition__box--${modifier}`;
242
+ const transitionLabelModifier = `transition__label--${modifier}`;
243
+ [...this.element.querySelectorAll(cssSelector)].forEach((elm) => {
244
+ apply(elm, transitionModifier);
245
+ apply(elm.querySelector(".transition__line"), transitionLineModifier);
246
+ apply(elm.querySelector(".transition__box"), transitionBoxModifier);
247
+ apply(elm.querySelector(".transition__label"), transitionLabelModifier);
248
+ });
249
+ }
250
+ }
@@ -75,6 +75,19 @@ export class StateChartIndex {
75
75
  return t;
76
76
  }
77
77
 
78
+ /**
79
+ * Returns a list of edge objects corresponding to all transitions from the state
80
+ * @param stateId identifier of the source state
81
+ * @returns a list of graph edges
82
+ */
83
+ getTransitions(stateId?: string): StateGraphEdge[] {
84
+ const stack = this.getStackByStateId(stateId);
85
+ const state = stack.pop();
86
+ if (!state || !stack.length) return [];
87
+ const parent = stack[stack.length - 1];
88
+ return parent.edges.filter((t: StateGraphEdge) => t.from === state.key);
89
+ }
90
+
78
91
  /**
79
92
  * Returns a graph node corresponding to the specified state id.
80
93
  * @param stateId identifier of the state
@@ -1 +1,2 @@
1
+ export * from "./RuntimeStatechartApi.ts";
1
2
  export * from "./StateChartIndex.ts";
@@ -1,150 +0,0 @@
1
- import { __publicField } from './chunk-XEO3YXBM.js';
2
-
3
- // src/runtime/StateChartIndex.ts
4
- var StateChartIndex = class {
5
- constructor({ statechart }) {
6
- __publicField(this, "statechart");
7
- __publicField(this, "_chartsIndex", {});
8
- __publicField(this, "_nodesIndex", {});
9
- __publicField(this, "_nodesParentsIndex", {});
10
- __publicField(this, "_edgeIndex", {});
11
- __publicField(this, "_edgesParentsIndex", {});
12
- this.statechart = statechart;
13
- this._buildIndexes();
14
- }
15
- /**
16
- * Transforms a stack of state keys to the corresponding identifiers.
17
- * If a key in the stack was not found then this method returns
18
- * an empty stack.
19
- * @param stack a stack of state keys
20
- * @returns a stack of state identifiers for the given states
21
- */
22
- getStatesIds(...stack) {
23
- let result = [];
24
- let list = [this.statechart];
25
- for (let i = 0; list?.length > 0 && i < stack.length; i++) {
26
- const key = stack[i];
27
- const node = list.find((node2) => node2.key === key);
28
- if (!node)
29
- break;
30
- result.push(node.id);
31
- list = node.children;
32
- if (!list?.length)
33
- list = node.nodes;
34
- }
35
- return result.length === stack.length ? result : [];
36
- }
37
- /**
38
- * Returns graph nodes for the specified identifier.
39
- * @param id the id of a node
40
- * @returns a stack of nodes
41
- */
42
- getStackByStateId(id) {
43
- if (!id)
44
- return [];
45
- const node = this._chartsIndex[id] || this._nodesIndex[id];
46
- if (!node)
47
- return [];
48
- const parentId = this._nodesParentsIndex[id];
49
- if (parentId) {
50
- const parentStack = this.getStackByStateId(parentId);
51
- if (!parentStack.length)
52
- return [];
53
- parentStack.push(node);
54
- return parentStack;
55
- } else {
56
- return [node];
57
- }
58
- }
59
- /**
60
- * Returns an edge object corresponding to the transition from the state
61
- * @param stateId identifier of the source state
62
- * @param event an event triggering the transition
63
- * @returns graph edge
64
- */
65
- getTransition(stateId, event) {
66
- const stack = this.getStackByStateId(stateId);
67
- const state = stack.pop();
68
- if (!state || !stack.length)
69
- return;
70
- const parent = stack[stack.length - 1];
71
- const t = parent.edges.find(
72
- (t2) => t2.from === state.key && t2.event === event
73
- );
74
- return t;
75
- }
76
- /**
77
- * Returns a graph node corresponding to the specified state id.
78
- * @param stateId identifier of the state
79
- * @returns graph node
80
- * @see getStateChart
81
- */
82
- getStateNode(stateId) {
83
- return stateId ? this._nodesIndex[stateId] : void 0;
84
- }
85
- /**
86
- * Returns a chart corresponding to the specified state id.
87
- * @param stateId identifier of the state
88
- * @returns graph node
89
- * @see getStateNode
90
- */
91
- getStateChart(stateId) {
92
- return stateId ? this._chartsIndex[stateId] : void 0;
93
- }
94
- /**
95
- * Returns ids of the previous and next state for the specified transition
96
- * @param transition a graph edge instance defining transition
97
- * @returns an array with two identifiers - [sourceStateId, targetStateId] - the source state id and target state id
98
- */
99
- getTransitionStateIds(transition) {
100
- const result = [
101
- void 0,
102
- void 0
103
- ];
104
- if (!transition)
105
- return result;
106
- const parentId = this._edgesParentsIndex[transition.id];
107
- if (!parentId)
108
- return result;
109
- const parent = this._chartsIndex[parentId];
110
- if (!parent)
111
- return result;
112
- parent.nodes.forEach((node) => {
113
- if (node.key === transition.from) {
114
- result[0] = node.id;
115
- }
116
- if (node.key === transition.to) {
117
- result[1] = node.id;
118
- }
119
- });
120
- return result;
121
- }
122
- // ----------------------------------------------------------
123
- // Internal methods
124
- _buildIndexes() {
125
- const addToIndex = (chart) => {
126
- const chartId = chart.id;
127
- this._chartsIndex[chartId] = chart;
128
- if (!(chartId in this._nodesIndex)) {
129
- this._nodesIndex[chartId] = chart;
130
- }
131
- chart.nodes.forEach((node) => {
132
- const nodeId = node.id;
133
- this._nodesIndex[nodeId] = node;
134
- this._nodesParentsIndex[nodeId] = chartId;
135
- });
136
- chart.edges.forEach((edge) => {
137
- const edgeId = edge.id;
138
- this._edgeIndex[edgeId] = edge;
139
- this._edgesParentsIndex[edgeId] = chartId;
140
- });
141
- (chart.children || []).forEach((child) => {
142
- this._nodesParentsIndex[child.id] = chartId;
143
- addToIndex(child);
144
- });
145
- };
146
- addToIndex(this.statechart);
147
- }
148
- };
149
-
150
- export { StateChartIndex };