Skip to content

Commit 6f3b396

Browse files
committed
Clean up size and position computations for graph inpector modal
1 parent 808ce3b commit 6f3b396

File tree

1 file changed

+15
-49
lines changed

1 file changed

+15
-49
lines changed

src/components/graphs/RecordingGraph.svelte

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
LinearScale,
1010
PointElement,
1111
} from 'chart.js';
12-
import { graphInspectorState } from '../3d-inspector/View3DUtility';
1312
import RecordingInspector from '../3d-inspector/RecordingInspector.svelte';
14-
import { element } from 'svelte/internal';
1513
1614
export let data: { x: number[]; y: number[]; z: number[] };
1715
@@ -33,22 +31,17 @@
3331
}
3432
}
3533
36-
const lengthOfGraphsInPixels = 134;
37-
const ratio = (1 / data.x.length) * lengthOfGraphsInPixels;
38-
// let dataPointInFocusIndex = 0; // 10 to 144
39-
// let inspectorActive = false;
40-
// let inspectedDataPoint = writable({ x: 0, y: 0, z: 0 });
41-
let componentElement: HTMLDivElement;
42-
const inspectorMarginInPixels = 5;
34+
let htmlElement: HTMLDivElement;
35+
const inspectorMarginPx = 5;
4336
4437
4538
function recomputeModalParams(){
46-
const rect = componentElement.getBoundingClientRect()
39+
const rect = htmlElement.getBoundingClientRect()
4740
modalSize = generateSizeOfInspector(rect)
4841
modalPosition = generatePositionOfInspector(rect, modalSize)
4942
}
5043
51-
/**
44+
/**
5245
* Positions in regards to size of modal following these rules:
5346
* - It prefers center-aligning with element in focus (The recording). Moves into view if circle
5447
* escapes the left or right side.
@@ -57,28 +50,19 @@
5750
*/
5851
function generatePositionOfInspector(rect: DOMRect, size: number): {x: number, y: number} {
5952
const rectCenterX = (rect.left + rect.right) / 2;
60-
let x = 0;
61-
let y = 0;
62-
63-
// Calculate x value
64-
if (rectCenterX < size / 2) {
65-
// What the hell is this if structure
66-
} else if (window.innerWidth - rectCenterX < size / 2)
67-
x = window.innerWidth - size;
68-
else x = rectCenterX - size / 2;
69-
70-
// Calculate y value
71-
if (window.innerHeight - rect.bottom - 15 < size) {
72-
y = rect.top - inspectorMarginInPixels - size;
73-
} else {
74-
y = rect.bottom + inspectorMarginInPixels;
75-
}
53+
let x = rectCenterX - size / 2;
54+
55+
x = Math.max(0, x)
56+
x = Math.min(x, window.innerWidth - size)
57+
58+
const showAboveGraph = window.innerHeight - rect.bottom - 15 < size
59+
const y = showAboveGraph ? rect.top - inspectorMarginPx - size : rect.bottom + inspectorMarginPx;
7660
7761
return { x, y };
7862
}
7963
8064
function generateSizeOfInspector(rect: DOMRect): number {
81-
return (window.innerHeight - rect.height) / 2 - inspectorMarginInPixels;
65+
return (window.innerHeight - rect.height) / 2 - inspectorMarginPx;
8266
}
8367
8468
function getConfig(): ChartConfiguration<keyof ChartTypeRegistry, {x: number, y: number}[], string> {
@@ -163,7 +147,8 @@
163147
}
164148
verticalLineX = args.event.x ?? NaN
165149
if (args.event.native != null){
166-
if (isNaN(hoverIndex)){
150+
// only recompute modal params if modal is closed (i.e. this event opens the modal)
151+
if (isNaN(hoverIndex)){
167152
recomputeModalParams()
168153
}
169154
hoverIndex = chart.getElementsAtEventForMode(args.event.native, 'nearest', {}, true)[0].index
@@ -191,24 +176,6 @@
191176
};
192177
}
193178
194-
// function computeInspectorPosition
195-
196-
// function openInspector(): void {
197-
198-
// }
199-
200-
// function closeInspector() {
201-
202-
// }
203-
// $ : {
204-
// graphInspectorState.update(s => {
205-
// s.isOpen = !isNaN(hoverIndex)
206-
// s.dataPoint = {x: data.x[hoverIndex], y: data.y[hoverIndex], z: data.z[hoverIndex]}
207-
// return s
208-
// })
209-
// }
210-
211-
212179
let canvas: HTMLCanvasElement;
213180
onMount(() => {
214181
Chart.register([LinearScale, LineController, PointElement, LineElement]);
@@ -221,11 +188,10 @@
221188
}
222189
});
223190
224-
225191
</script>
226192

227193
<div
228-
bind:this={componentElement}
194+
bind:this={htmlElement}
229195
class="h-full w-full relative"
230196
>
231197
<div class="z-1 h-full w-full absolute">

0 commit comments

Comments
 (0)