@grafana/ui
Version:
Grafana Components Library
112 lines (107 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var lodash = require('lodash');
var React = require('react');
var rxjs = require('rxjs');
var operators = require('rxjs/operators');
var data = require('@grafana/data');
;
const EventBusPlugin = ({ config, eventBus, frame }) => {
const frameRef = React.useRef(frame);
frameRef.current = frame;
React.useLayoutEffect(() => {
let u = null;
const payload = {
point: {
time: null
},
data: frameRef.current
};
config.addHook("init", (_u) => {
u = _u;
});
let closestSeriesIdx = null;
config.addHook("setSeries", (u2, seriesIdx) => {
closestSeriesIdx = seriesIdx;
});
config.addHook("setLegend", () => {
var _a;
let viaSync = u.cursor.event == null;
if (!viaSync) {
let dataIdx = u.cursor.idxs.find((v) => v != null);
if (dataIdx == null) {
throttledClear();
} else {
let rowIdx = dataIdx;
let colIdx = closestSeriesIdx;
let xData = (_a = u.data[0]) != null ? _a : u.data[1][0];
payload.point.time = xData[rowIdx];
payload.rowIndex = rowIdx != null ? rowIdx : void 0;
payload.columnIndex = colIdx != null ? colIdx : void 0;
payload.data = frameRef.current;
let top = u.cursor.top;
payload.point.panelRelY = top === 0 ? 1e-3 : top > 0 ? top / u.rect.height : 1;
throttledHover();
}
}
});
function handleCursorUpdate(evt) {
var _a, _b;
const time = (_b = (_a = evt.payload) == null ? void 0 : _a.point) == null ? void 0 : _b.time;
if (time) {
const left = u.valToPos(time, "x");
u.setCursor({
left,
top: u.rect.height / 2
});
}
}
const subscription = new rxjs.Subscription();
const hoverEvent = new data.DataHoverEvent(payload).setTags(["uplot"]);
const clearEvent = new data.DataHoverClearEvent().setTags(["uplot"]);
let throttledHover = lodash.throttle(() => {
eventBus.publish(hoverEvent);
}, 100);
let throttledClear = lodash.throttle(() => {
eventBus.publish(clearEvent);
}, 100);
subscription.add(
eventBus.getStream(data.DataHoverEvent).subscribe({
next: (evt) => {
var _a;
if (eventBus === evt.origin || ((_a = evt.tags) == null ? void 0 : _a.has("uplot"))) {
return;
}
handleCursorUpdate(evt);
}
})
);
subscription.add(
eventBus.getStream(data.LegacyGraphHoverEvent).subscribe({
next: (evt) => handleCursorUpdate(evt)
})
);
subscription.add(
eventBus.getStream(data.DataHoverClearEvent).pipe(operators.throttleTime(50)).subscribe({
next: (evt) => {
var _a;
if (eventBus === evt.origin || ((_a = evt.tags) == null ? void 0 : _a.has("uplot"))) {
return;
}
if (!u.cursor._lock) {
u.setCursor({
left: -10,
top: -10
});
}
}
})
);
return () => {
subscription.unsubscribe();
};
}, [config]);
return null;
};
exports.EventBusPlugin = EventBusPlugin;
//# sourceMappingURL=EventBusPlugin.cjs.map