@opendata-ai/openchart-vanilla 2.0.0

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 (44) hide show
  1. package/dist/index.d.ts +327 -0
  2. package/dist/index.js +4745 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/simulation-worker.js +1196 -0
  5. package/package.json +58 -0
  6. package/src/__test-fixtures__/dom.ts +42 -0
  7. package/src/__test-fixtures__/specs.ts +187 -0
  8. package/src/__tests__/edit-events.test.ts +747 -0
  9. package/src/__tests__/events.test.ts +336 -0
  10. package/src/__tests__/export.test.ts +150 -0
  11. package/src/__tests__/mount.test.ts +219 -0
  12. package/src/__tests__/svg-renderer.test.ts +609 -0
  13. package/src/__tests__/table-mount.test.ts +484 -0
  14. package/src/__tests__/tooltip.test.ts +201 -0
  15. package/src/export.ts +105 -0
  16. package/src/graph/__tests__/canvas-renderer.test.ts +704 -0
  17. package/src/graph/__tests__/graph-mount.test.ts +213 -0
  18. package/src/graph/__tests__/interaction.test.ts +205 -0
  19. package/src/graph/__tests__/keyboard.test.ts +653 -0
  20. package/src/graph/__tests__/search.test.ts +88 -0
  21. package/src/graph/__tests__/simulation.test.ts +233 -0
  22. package/src/graph/__tests__/spatial-index.test.ts +142 -0
  23. package/src/graph/__tests__/zoom.test.ts +195 -0
  24. package/src/graph/canvas-renderer.ts +660 -0
  25. package/src/graph/interaction.ts +359 -0
  26. package/src/graph/keyboard.ts +208 -0
  27. package/src/graph/search.ts +50 -0
  28. package/src/graph/simulation-worker-url.ts +30 -0
  29. package/src/graph/simulation-worker.ts +265 -0
  30. package/src/graph/simulation.ts +350 -0
  31. package/src/graph/spatial-index.ts +121 -0
  32. package/src/graph/types.ts +44 -0
  33. package/src/graph/worker-protocol.ts +67 -0
  34. package/src/graph/zoom.ts +104 -0
  35. package/src/graph-mount.ts +675 -0
  36. package/src/index.ts +56 -0
  37. package/src/mount.ts +1639 -0
  38. package/src/renderers/table-cells.ts +444 -0
  39. package/src/resize-observer.ts +46 -0
  40. package/src/svg-renderer.ts +914 -0
  41. package/src/table-keyboard.ts +266 -0
  42. package/src/table-mount.ts +532 -0
  43. package/src/table-renderer.ts +350 -0
  44. package/src/tooltip.ts +120 -0
@@ -0,0 +1,213 @@
1
+ import type { GraphSpec } from '@opendata-ai/openchart-core';
2
+ import { afterEach, describe, expect, it, vi } from 'vitest';
3
+ import { createGraph } from '../../graph-mount';
4
+
5
+ // ---------------------------------------------------------------------------
6
+ // Test data
7
+ // ---------------------------------------------------------------------------
8
+
9
+ const basicSpec: GraphSpec = {
10
+ type: 'graph',
11
+ nodes: [
12
+ { id: 'a', label: 'Node A' },
13
+ { id: 'b', label: 'Node B' },
14
+ { id: 'c', label: 'Node C' },
15
+ ],
16
+ edges: [
17
+ { source: 'a', target: 'b' },
18
+ { source: 'b', target: 'c' },
19
+ ],
20
+ chrome: {
21
+ title: 'Test Graph',
22
+ subtitle: 'A simple test graph',
23
+ },
24
+ };
25
+
26
+ const communitySpec: GraphSpec = {
27
+ type: 'graph',
28
+ nodes: [
29
+ { id: 'a', label: 'Node A', group: 'x' },
30
+ { id: 'b', label: 'Node B', group: 'x' },
31
+ { id: 'c', label: 'Node C', group: 'y' },
32
+ { id: 'd', label: 'Node D', group: 'y' },
33
+ ],
34
+ edges: [
35
+ { source: 'a', target: 'b' },
36
+ { source: 'c', target: 'd' },
37
+ { source: 'a', target: 'c' },
38
+ ],
39
+ layout: {
40
+ clustering: { field: 'group' },
41
+ },
42
+ chrome: {
43
+ title: 'Community Graph',
44
+ },
45
+ };
46
+
47
+ // ---------------------------------------------------------------------------
48
+ // Helpers
49
+ // ---------------------------------------------------------------------------
50
+
51
+ function makeContainer(): HTMLElement {
52
+ const el = document.createElement('div');
53
+ // happy-dom doesn't auto-size elements; we need to provide dimensions
54
+ // for getBoundingClientRect to return useful values
55
+ Object.defineProperty(el, 'getBoundingClientRect', {
56
+ value: () => ({
57
+ width: 800,
58
+ height: 600,
59
+ top: 0,
60
+ left: 0,
61
+ bottom: 600,
62
+ right: 800,
63
+ x: 0,
64
+ y: 0,
65
+ toJSON: () => {},
66
+ }),
67
+ });
68
+ document.body.appendChild(el);
69
+ return el;
70
+ }
71
+
72
+ // ---------------------------------------------------------------------------
73
+ // Cleanup
74
+ // ---------------------------------------------------------------------------
75
+
76
+ let container: HTMLElement;
77
+
78
+ afterEach(() => {
79
+ if (container?.parentNode) {
80
+ container.parentNode.removeChild(container);
81
+ }
82
+ });
83
+
84
+ // ---------------------------------------------------------------------------
85
+ // Tests
86
+ // ---------------------------------------------------------------------------
87
+
88
+ describe('createGraph', () => {
89
+ it('creates expected DOM structure (wrapper, canvas, chrome, legend)', () => {
90
+ container = makeContainer();
91
+ const graph = createGraph(container, basicSpec);
92
+
93
+ // Wrapper
94
+ const wrapper = container.querySelector('.viz-graph-wrapper');
95
+ expect(wrapper).not.toBeNull();
96
+
97
+ // Canvas
98
+ const canvas = container.querySelector('.viz-graph-canvas');
99
+ expect(canvas).not.toBeNull();
100
+ expect(canvas?.tagName.toLowerCase()).toBe('canvas');
101
+
102
+ // Chrome
103
+ const chrome = container.querySelector('.viz-graph-chrome');
104
+ expect(chrome).not.toBeNull();
105
+
106
+ // Title
107
+ const title = container.querySelector('.viz-title');
108
+ expect(title).not.toBeNull();
109
+ expect(title?.textContent).toBe('Test Graph');
110
+
111
+ // Subtitle
112
+ const subtitle = container.querySelector('.viz-subtitle');
113
+ expect(subtitle).not.toBeNull();
114
+ expect(subtitle?.textContent).toBe('A simple test graph');
115
+
116
+ // Legend exists (even if hidden for non-community graphs)
117
+ const legend = container.querySelector('.viz-graph-legend');
118
+ expect(legend).not.toBeNull();
119
+
120
+ graph.destroy();
121
+ });
122
+
123
+ it('destroy cleans up DOM and does not error on subsequent calls', () => {
124
+ container = makeContainer();
125
+ const graph = createGraph(container, basicSpec);
126
+
127
+ expect(container.querySelector('.viz-graph-wrapper')).not.toBeNull();
128
+
129
+ graph.destroy();
130
+
131
+ expect(container.querySelector('.viz-graph-wrapper')).toBeNull();
132
+ expect(container.querySelector('.viz-graph-canvas')).toBeNull();
133
+
134
+ // Calling destroy again should not throw
135
+ expect(() => graph.destroy()).not.toThrow();
136
+
137
+ // Calling methods after destroy should not throw
138
+ expect(() => graph.update(basicSpec)).not.toThrow();
139
+ expect(() => graph.search('test')).not.toThrow();
140
+ expect(() => graph.zoomToFit()).not.toThrow();
141
+ expect(() => graph.resize()).not.toThrow();
142
+ expect(graph.getSelectedNodes()).toEqual([]);
143
+ });
144
+
145
+ it('update re-initializes with new spec', () => {
146
+ container = makeContainer();
147
+ const graph = createGraph(container, basicSpec);
148
+
149
+ const titleBefore = container.querySelector('.viz-title');
150
+ expect(titleBefore?.textContent).toBe('Test Graph');
151
+
152
+ graph.update(communitySpec);
153
+
154
+ const titleAfter = container.querySelector('.viz-title');
155
+ expect(titleAfter?.textContent).toBe('Community Graph');
156
+
157
+ graph.destroy();
158
+ });
159
+
160
+ it('shows legend for community graphs', () => {
161
+ container = makeContainer();
162
+ const graph = createGraph(container, communitySpec);
163
+
164
+ const legend = container.querySelector('.viz-graph-legend');
165
+ expect(legend).not.toBeNull();
166
+ // Community graph should have visible legend items
167
+ const items = container.querySelectorAll('.viz-graph-legend-item');
168
+ expect(items.length).toBeGreaterThan(0);
169
+
170
+ graph.destroy();
171
+ });
172
+
173
+ it('search and clearSearch update without errors', () => {
174
+ container = makeContainer();
175
+ const graph = createGraph(container, basicSpec);
176
+
177
+ expect(() => graph.search('Node')).not.toThrow();
178
+ expect(() => graph.clearSearch()).not.toThrow();
179
+
180
+ graph.destroy();
181
+ });
182
+
183
+ it('selectNode and getSelectedNodes work', () => {
184
+ container = makeContainer();
185
+ const graph = createGraph(container, basicSpec);
186
+
187
+ graph.selectNode('a');
188
+ expect(graph.getSelectedNodes()).toEqual(['a']);
189
+
190
+ graph.destroy();
191
+ });
192
+
193
+ it('applies viz-dark class in dark mode', () => {
194
+ container = makeContainer();
195
+ const graph = createGraph(container, basicSpec, { darkMode: 'force' });
196
+
197
+ expect(container.classList.contains('viz-dark')).toBe(true);
198
+
199
+ graph.destroy();
200
+ expect(container.classList.contains('viz-dark')).toBe(false);
201
+ });
202
+
203
+ it('onSelectionChange callback fires on selectNode', () => {
204
+ container = makeContainer();
205
+ const onSelectionChange = vi.fn();
206
+ const graph = createGraph(container, basicSpec, { onSelectionChange });
207
+
208
+ graph.selectNode('b');
209
+ expect(onSelectionChange).toHaveBeenCalledWith(['b']);
210
+
211
+ graph.destroy();
212
+ });
213
+ });
@@ -0,0 +1,205 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
+ import type { InteractionCallbacks } from '../interaction';
3
+ import { GraphInteractionManager } from '../interaction';
4
+ import { SpatialIndex } from '../spatial-index';
5
+ import type { PositionedNode } from '../types';
6
+ import { ZoomTransform } from '../zoom';
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // Helpers
10
+ // ---------------------------------------------------------------------------
11
+
12
+ function makeNode(id: string, x: number, y: number, radius = 10): PositionedNode {
13
+ return {
14
+ id,
15
+ x,
16
+ y,
17
+ radius,
18
+ fill: '#3b82f6',
19
+ stroke: '#2563eb',
20
+ strokeWidth: 1,
21
+ label: id,
22
+ labelPriority: 0.5,
23
+ community: undefined,
24
+ data: {},
25
+ };
26
+ }
27
+
28
+ function createMockCanvas(): HTMLCanvasElement {
29
+ const canvas = document.createElement('canvas');
30
+ canvas.width = 800;
31
+ canvas.height = 600;
32
+ // Mock getBoundingClientRect for coordinate conversion
33
+ canvas.getBoundingClientRect = () => ({
34
+ left: 0,
35
+ top: 0,
36
+ right: 800,
37
+ bottom: 600,
38
+ width: 800,
39
+ height: 600,
40
+ x: 0,
41
+ y: 0,
42
+ toJSON: () => ({}),
43
+ });
44
+ return canvas;
45
+ }
46
+
47
+ function createCallbacks(): InteractionCallbacks & {
48
+ transformChanges: ZoomTransform[];
49
+ hoverChanges: (string | null)[];
50
+ selectionChanges: string[][];
51
+ dragStarts: string[];
52
+ drags: Array<{ nodeId: string; x: number; y: number }>;
53
+ dragEnds: string[];
54
+ doubleClicks: string[];
55
+ } {
56
+ const state = {
57
+ transformChanges: [] as ZoomTransform[],
58
+ hoverChanges: [] as (string | null)[],
59
+ selectionChanges: [] as string[][],
60
+ dragStarts: [] as string[],
61
+ drags: [] as Array<{ nodeId: string; x: number; y: number }>,
62
+ dragEnds: [] as string[],
63
+ doubleClicks: [] as string[],
64
+ onTransformChange(t: ZoomTransform) {
65
+ state.transformChanges.push(t);
66
+ },
67
+ onHoverChange(nodeId: string | null) {
68
+ state.hoverChanges.push(nodeId);
69
+ },
70
+ onSelectionChange(nodeIds: string[]) {
71
+ state.selectionChanges.push(nodeIds);
72
+ },
73
+ onNodeDragStart(nodeId: string) {
74
+ state.dragStarts.push(nodeId);
75
+ },
76
+ onNodeDrag(nodeId: string, x: number, y: number) {
77
+ state.drags.push({ nodeId, x, y });
78
+ },
79
+ onNodeDragEnd(nodeId: string) {
80
+ state.dragEnds.push(nodeId);
81
+ },
82
+ onDoubleClick(nodeId: string) {
83
+ state.doubleClicks.push(nodeId);
84
+ },
85
+ };
86
+ return state;
87
+ }
88
+
89
+ // ---------------------------------------------------------------------------
90
+ // Tests
91
+ // ---------------------------------------------------------------------------
92
+
93
+ describe('GraphInteractionManager', () => {
94
+ let canvas: HTMLCanvasElement;
95
+ let spatialIndex: SpatialIndex;
96
+ let callbacks: ReturnType<typeof createCallbacks>;
97
+ let manager: GraphInteractionManager;
98
+
99
+ beforeEach(() => {
100
+ canvas = createMockCanvas();
101
+ spatialIndex = new SpatialIndex();
102
+ callbacks = createCallbacks();
103
+
104
+ const nodes = [
105
+ makeNode('center', 400, 300),
106
+ makeNode('left', 100, 300),
107
+ makeNode('right', 700, 300),
108
+ ];
109
+ spatialIndex.rebuild(nodes);
110
+
111
+ manager = new GraphInteractionManager(canvas, spatialIndex, callbacks);
112
+ });
113
+
114
+ describe('zoom', () => {
115
+ it('updates transform on wheel', () => {
116
+ const wheelEvent = new WheelEvent('wheel', {
117
+ deltaY: -100,
118
+ clientX: 400,
119
+ clientY: 300,
120
+ });
121
+ canvas.dispatchEvent(wheelEvent);
122
+
123
+ expect(callbacks.transformChanges.length).toBe(1);
124
+ const t = callbacks.transformChanges[0];
125
+ expect(t.k).toBeGreaterThan(1);
126
+ });
127
+
128
+ it('zoom in increases scale', () => {
129
+ // Negative deltaY = zoom in
130
+ canvas.dispatchEvent(new WheelEvent('wheel', { deltaY: -200, clientX: 400, clientY: 300 }));
131
+ const t = callbacks.transformChanges[0];
132
+ expect(t.k).toBeGreaterThan(1);
133
+ });
134
+
135
+ it('zoom out decreases scale', () => {
136
+ // Positive deltaY = zoom out
137
+ canvas.dispatchEvent(new WheelEvent('wheel', { deltaY: 200, clientX: 400, clientY: 300 }));
138
+ const t = callbacks.transformChanges[0];
139
+ expect(t.k).toBeLessThan(1);
140
+ });
141
+ });
142
+
143
+ describe('hover', () => {
144
+ it('fires hover change on mousemove over a node', () => {
145
+ // Move over node at (400, 300) with identity transform
146
+ canvas.dispatchEvent(new MouseEvent('mousemove', { clientX: 400, clientY: 300 }));
147
+
148
+ expect(callbacks.hoverChanges.length).toBeGreaterThan(0);
149
+ expect(callbacks.hoverChanges[callbacks.hoverChanges.length - 1]).toBe('center');
150
+ });
151
+
152
+ it('fires null hover on mousemove over background', () => {
153
+ canvas.dispatchEvent(new MouseEvent('mousemove', { clientX: 0, clientY: 0 }));
154
+
155
+ expect(callbacks.hoverChanges.length).toBeGreaterThan(0);
156
+ expect(callbacks.hoverChanges[callbacks.hoverChanges.length - 1]).toBeNull();
157
+ });
158
+ });
159
+
160
+ describe('selection', () => {
161
+ it('selects a node on click', () => {
162
+ // Mousedown on node
163
+ canvas.dispatchEvent(new MouseEvent('mousedown', { clientX: 400, clientY: 300 }));
164
+ // Mouseup without moving (click)
165
+ canvas.dispatchEvent(new MouseEvent('mouseup', { clientX: 400, clientY: 300 }));
166
+
167
+ expect(callbacks.selectionChanges.length).toBe(1);
168
+ expect(callbacks.selectionChanges[0]).toEqual(['center']);
169
+ });
170
+
171
+ it('clears selection on background click', () => {
172
+ // First select a node
173
+ canvas.dispatchEvent(new MouseEvent('mousedown', { clientX: 400, clientY: 300 }));
174
+ canvas.dispatchEvent(new MouseEvent('mouseup', { clientX: 400, clientY: 300 }));
175
+
176
+ // Then click background
177
+ canvas.dispatchEvent(new MouseEvent('mousedown', { clientX: 0, clientY: 0 }));
178
+ canvas.dispatchEvent(new MouseEvent('mouseup', { clientX: 0, clientY: 0 }));
179
+
180
+ expect(callbacks.selectionChanges.length).toBe(2);
181
+ expect(callbacks.selectionChanges[1]).toEqual([]);
182
+ });
183
+ });
184
+
185
+ describe('transform', () => {
186
+ it('setTransform and getTransform work', () => {
187
+ const t = new ZoomTransform(10, 20, 3);
188
+ manager.setTransform(t);
189
+ const got = manager.getTransform();
190
+ expect(got.x).toBe(10);
191
+ expect(got.y).toBe(20);
192
+ expect(got.k).toBe(3);
193
+ });
194
+ });
195
+
196
+ describe('cleanup', () => {
197
+ it('destroy removes event listeners (no errors on subsequent events)', () => {
198
+ manager.destroy();
199
+
200
+ // Should not throw and should not trigger callbacks
201
+ canvas.dispatchEvent(new WheelEvent('wheel', { deltaY: -100, clientX: 400, clientY: 300 }));
202
+ expect(callbacks.transformChanges.length).toBe(0);
203
+ });
204
+ });
205
+ });