-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdraw.js
More file actions
264 lines (244 loc) · 10.3 KB
/
draw.js
File metadata and controls
264 lines (244 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
/* global JSROOT */
import { h } from '/js/src/index.js';
import { generateDrawingOptionString, isObjectOfTypeChecker } from './../../../library/qcObject/utils.js';
import checkersPanel from './checkersPanel.js';
import { keyedTimerDebouncer, pointerId } from '../utils.js';
import { failureToDrawPanel } from './failureToDrawPanel.js';
/**
* Renders a QCObject as a virtual DOM node using JSROOT.
* Depending on the state of the requested object, this function handles:
* - `NotAsked`: returns `null`.
* - `Loading`: returns a loading placeholder.
* - `Failure`: returns an error box with the error message.
* - `Success`: draws the object using `drawObject`.
* @param {RemoteData} remoteData - the RemoteData object containing {qcObject, info, timestamps}
* @param {object} options - optional options of presentation
* @param {string[]} drawingOptions - optional drawing options to be used
* @param {(Error) => void} failFn - optional function to execute upon drawing failure
* @returns {vnode} output virtual-dom, a single div with JSROOT attached to it
*/
export const draw = (remoteData = {}, options = {}, drawingOptions = [], failFn = () => {}) =>
remoteData?.match({
NotAsked: () => null,
Loading: () => h('.flex-column.items-center.justify-center', [h('.animate-slow-appearance', 'Loading')]),
Failure: (error) => failureToDrawPanel(error),
Success: (data) => drawObject(data, options, drawingOptions, failFn),
});
/**
* Draws a QC Object depending on its type:
* - uses JSROOT for standard ROOT objects in which JSROOT is then used to insert an SVG with the respective plot
* - builds a checkers panel for QC unique checkers
* @param {JSON} object - {qcObject, info, timestamps}
* @param {object} options - optional options of presentation
* @param {string[]} drawingOptions - optional drawing options to be used
* @param {(Error) => void} failFn - optional function to execute upon drawing failure
* @returns {vnode} output virtual-dom, a single div with JSROOT attached to it
*/
export const drawObject = (object, options = {}, drawingOptions = [], failFn = () => {}) => {
const { qcObject, etag } = object;
const { root, rootError } = qcObject;
if (isObjectOfTypeChecker(root)) {
return checkersPanel(root);
} else if (rootError) {
return failureToDrawPanel(rootError);
}
drawingOptions = Array.from(new Set(drawingOptions));
const defaultOptions = {
width: '100%', // CSS size
height: '100%', // CSS size
className: '', // Any CSS class
};
options = { ...defaultOptions, ...options };
const attributes = {
key: etag, // Completely re-create this div if the chart is not the same at all
id: etag,
class: options.className,
style: {
height: options.height,
width: options.width,
},
oncreate: (vnode) => {
// Setup resize function
vnode.dom.onresize = () => {
redrawOnSizeUpdate(vnode.dom, root, drawingOptions, failFn);
};
// Resize on window size change
window.addEventListener('resize', vnode.dom.onresize);
drawOnCreate(vnode.dom, root, drawingOptions, failFn);
},
onupdate: (vnode) => {
const isRedrawn = redrawOnDataUpdate(vnode.dom, root, drawingOptions);
if (!isRedrawn) {
redrawOnSizeUpdate(vnode.dom, root, drawingOptions, failFn);
}
},
onremove: (vnode) => {
// Remove JSROOT binding to avoid memory leak
if (JSROOT.cleanup) {
JSROOT.cleanup(vnode.dom);
}
// Stop listening for window size change
window.removeEventListener('resize', vnode.dom.onresize);
},
};
// On success, JSROOT will erase all DOM inside div and put its own
return h('.relative.jsroot-container', attributes);
};
/**
* Inserts SVG into div element by using JSROOT.draw method
* Applies specific drawing options to ensure correct plotting
* @param {HTMLElement} dom - the div containing jsroot plot
* @param {object} root - root object in JSON representation
* @param {string[]} drawingOptions - list of options to be used for drawing object
* @param {(Error) => void} failFn - function to execute upon drawing failure
* @throws {EvalError} If CSP disallows 'unsafe-eval'.
* This is typically called when the drawing is incomplete or malformed.
* @returns {undefined}
*/
const drawOnCreate = async (dom, root, drawingOptions, failFn) => {
const finalDrawingOptions = generateDrawingOptionString(root, drawingOptions);
JSROOT.draw(dom, root, finalDrawingOptions).then((painter) => {
if (painter === null) {
if (typeof failFn === 'function') {
failFn(new Error('null painter in JSROOT'));
}
}
}).catch((error) => {
if (typeof failFn === 'function') {
failFn(error);
}
});
dom.dataset.fingerprintRedraw = fingerprintResize(dom.clientWidth, dom.clientHeight);
dom.dataset.fingerprintData = fingerprintData(root, drawingOptions);
};
/**
* Debounced resize handler for JSROOT graphs.
*
* Behavior:
* - Resizes are debounced by 200 ms to avoid excessive redraws during rapid events.
* - After debounce, a 50 ms interval checks whether the element's size has fully stabilized
* (important because CSS transitions, flexbox, and layout effects can continue to adjust size
* after resize events stop).
* - Only once the size is stable is the graph redrawn.
*
* Keying:
* - Debouncing is keyed by the DOM element, allowing multiple graphs to update independently.
*
* onFirstCall logic:
* - Runs immediately the first time a specific DOM element triggers this debouncer.
* - Ensures an instant initial redraw without waiting for the debounce delay or stabilization interval.
* - Subsequent resizes for the same element follow the normal debounce + stabilization flow.
* @param {Model} model - Root model of the application
* @param {HTMLElement} dom - Element containing the JSROOT plot
* @param {TabObject} tabObject - Object describing the graph to redraw inside `dom`
* @param {(Error) => void} failFn - Function to execute upon drawing failure
* @returns {undefined}
*/
const redrawOnSizeUpdate = keyedTimerDebouncer(
(_, dom) => dom,
(dom, root, drawingOptions, failFn) => {
let previousFingerprint = dom.dataset.fingerprintResize;
const intervalId = setInterval(() => {
try {
const currentFingerprint = fingerprintResize(dom.clientWidth, dom.clientHeight);
// Check for playing animation/transition
if (previousFingerprint !== currentFingerprint) {
previousFingerprint = currentFingerprint;
return;
}
// Size stable across intervals (safe to redraw)
if (dom.dataset.fingerprintResize !== currentFingerprint) {
redraw(dom, root, drawingOptions, failFn);
}
clearInterval(intervalId);
// eslint-disable-next-line no-unused-vars
} catch (_) {
// stop monitoring on error
clearInterval(intervalId);
}
}, 50);
},
200,
(dom, root, drawingOptions, failFn) => {
const resizeFingerprint = fingerprintResize(dom.clientWidth, dom.clientHeight);
if (dom.dataset.fingerprintResize !== resizeFingerprint) {
redraw(dom, root, drawingOptions, failFn);
}
},
);
/**
* Vnode update hook.
* Apply a JSROOT redraw when view goes from one data state to another
* State is stored DOM dataset of element
* @param {HTMLElement} dom - Target element containing the JSROOT graph.
* @param {object} root - JSROOT-compatible data object to be rendered.
* @param {string[]} drawingOptions - Initial or user-provided drawing options.
* @param {(Error) => void} failFn - Function to execute upon drawing failure
* @returns {boolean} whether the JSROOT plot was redrawn
*/
const redrawOnDataUpdate = (dom, root, drawingOptions, failFn) => {
const dataFingerprint = fingerprintData(root, drawingOptions);
if (dom.dataset.fingerprintData !== dataFingerprint) {
redraw(dom, root, drawingOptions, failFn);
return true;
}
return false;
};
/**
* Performs a JSROOT redraw using the final resolved drawing options.
* @param {HTMLElement} dom - Target element containing the JSROOT graph.
* @param {object} root - JSROOT-compatible data object to be rendered.
* @param {string[]} drawingOptions - Initial or user-provided drawing options.
* @param {(Error) => void} failFn - Function to execute upon drawing failure
* @returns {undefined}
*/
const redraw = (dom, root, drawingOptions, failFn) => {
// A bug exists in JSROOT where the cursor gets stuck on `wait` when redrawing multiple objects simultaneously.
// We save the current cursor state here and revert back to it after redrawing is complete.
const currentCursor = document.body.style.cursor;
const finalDrawingOptions = generateDrawingOptionString(root, drawingOptions);
try {
JSROOT.redraw(dom, root, finalDrawingOptions);
} catch (error) {
if (typeof failFn === 'function') {
failFn(error);
}
}
document.body.style.cursor = currentCursor;
};
/**
* Generates a resize fingerprint.
* When it changes, JSROOT should resize canvas
* @param {number} width - the width of the element
* @param {number} height - the height of the element
* @returns {string} - the resize fingerprint
*/
const fingerprintResize = (width, height) =>
`${width}:${height}`;
/**
* Generates a data fingerprint.
* When it changes, JSROOT should redraw canvas
* - object data could be replaced on data refresh
* - tabObject.options change requires redraw
* @param {object} root - root object in JSON representation
* @param {string[]} drawingOptions - list of options to be used for drawing object
* @returns {string} - id of the redraw
*/
const fingerprintData = (root, drawingOptions) => {
const finalDrawingOptions = generateDrawingOptionString(root, drawingOptions);
const rootPointerId = pointerId(root);
return `${rootPointerId}:${finalDrawingOptions}`;
};