- Fix dashboard draggin: introduced sophistaced drag handle - Fix disappearing of charts when dragged inside a dashboard widget
This commit is contained in:
@@ -59,6 +59,8 @@ function applyThemeOverlay(options: any): any {
|
||||
export class ApexChart extends LitElement {
|
||||
private chart?: ApexCharts;
|
||||
private lastOptions?: any;
|
||||
private lastOptionsJson?: string;
|
||||
private destroyTimer?: ReturnType<typeof setTimeout>;
|
||||
|
||||
private readonly onThemeChange = () => {
|
||||
if (!this.chart || !this.lastOptions) return;
|
||||
@@ -71,7 +73,14 @@ export class ApexChart extends LitElement {
|
||||
|
||||
connectedCallback() {
|
||||
super.connectedCallback();
|
||||
clearTimeout(this.destroyTimer);
|
||||
this.destroyTimer = undefined;
|
||||
window.addEventListener('dialect-theme-change', this.onThemeChange);
|
||||
// Back after a real teardown (Flow detach, cached view): rebuild, since
|
||||
// the server only pushes options on setData(), not on re-attach.
|
||||
if (!this.chart && this.lastOptionsJson) {
|
||||
void this.renderChart(this.lastOptionsJson);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
@@ -80,6 +89,7 @@ export class ApexChart extends LitElement {
|
||||
|
||||
async renderChart(optionsJson: string) {
|
||||
await this.updateComplete;
|
||||
this.lastOptionsJson = optionsJson;
|
||||
const options = JSON.parse(optionsJson);
|
||||
|
||||
options.chart = {
|
||||
@@ -106,6 +116,15 @@ export class ApexChart extends LitElement {
|
||||
disconnectedCallback() {
|
||||
super.disconnectedCallback();
|
||||
window.removeEventListener('dialect-theme-change', this.onThemeChange);
|
||||
this.chart?.destroy();
|
||||
// gridstack's _sortDom() re-appends item elements after every
|
||||
// move/resize, which disconnects and immediately reconnects this
|
||||
// element within the same task — destroying the chart synchronously
|
||||
// would blank it with nothing to trigger a re-render. Defer past the
|
||||
// current task and bail out if we came back.
|
||||
this.destroyTimer = setTimeout(() => {
|
||||
if (this.isConnected) return;
|
||||
this.chart?.destroy();
|
||||
this.chart = undefined;
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user