@adaptabletools/adaptable-cjs
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
57 lines (56 loc) • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useExternalChartState = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const AdaptableContext_1 = require("../AdaptableContext");
// need to trigger other show-chart buttons to close/hide
const buttonChangeSubscribers = [];
const useExternalChartState = (chartDefinition) => {
const adaptable = (0, AdaptableContext_1.useAdaptable)();
// needs to update when ever a chart is:
// - opened, hidden
const [isOpen, setIsOpen] = react_1.default.useState(() => {
return (chartDefinition &&
adaptable.api.chartingApi.internalApi.isExternalChartOpened(chartDefinition));
});
const liveChartState = adaptable.api.chartingApi.internalApi.isExternalChartOpened(chartDefinition);
if (liveChartState !== isOpen) {
setIsOpen(liveChartState);
}
const updateIsOpen = react_1.default.useCallback(() => {
setIsOpen(chartDefinition &&
adaptable.api.chartingApi.internalApi.isExternalChartOpened(chartDefinition));
}, [chartDefinition]);
react_1.default.useEffect(() => {
const subscriber = () => {
updateIsOpen();
};
buttonChangeSubscribers.push(subscriber);
// clear own subscriptions
return () => {
const index = buttonChangeSubscribers.indexOf(subscriber);
if (index > -1) {
buttonChangeSubscribers.splice(index, 1);
}
};
}, [updateIsOpen]);
return {
showChart: (container) => {
adaptable.api.chartingApi.internalApi.onShowExternalChart(chartDefinition, container);
// The small delay allows the library to render the chart before the button is updated
requestAnimationFrame(() => {
buttonChangeSubscribers.forEach((subscriber) => subscriber());
});
},
hideChart: () => {
adaptable.api.chartingApi.internalApi.onHideExternalChart(chartDefinition);
// The small delay allows the library to destroy the chart before the button is updated
requestAnimationFrame(() => {
buttonChangeSubscribers.forEach((subscriber) => subscriber());
});
},
isOpen,
};
};
exports.useExternalChartState = useExternalChartState;