integrating latest dashboard changes
This commit is contained in:
@@ -22,7 +22,7 @@ import { PLOTLY_LOADER } from './plotly-runtime';
|
||||
</section>
|
||||
}
|
||||
`,
|
||||
styles: `:host { display: block; min-width: 0; } .plot-viewport { min-width: 0; overflow-x: auto; } .plot-viewport:focus-visible { outline: 2px solid var(--issste-text); outline-offset: 2px; } .plot { width: 100%; min-width: var(--plot-min-width, 0px); min-height: 620px; visibility: hidden; } [role=alert] { padding: 1rem; } .notes { padding: 1rem; overflow-wrap: anywhere; } .notes h2 { font-size: 1rem; } .notes p { line-height: 1.5; }`,
|
||||
styles: `:host { display: block; min-width: 0; } .plot-viewport { min-width: 0; overflow-x: auto; } .plot-viewport:focus-visible { outline: 2px solid var(--issste-text); outline-offset: 2px; } .plot { width: 100%; min-width: var(--plot-min-width, 0px); min-height: var(--plot-height, 620px); visibility: hidden; } [role=alert] { padding: 1rem; } .notes { padding: 1rem; overflow-wrap: anywhere; } .notes h2 { font-size: 1rem; } .notes p { line-height: 1.5; }`,
|
||||
})
|
||||
export class PlotlyChart implements AfterViewInit, OnChanges, OnDestroy {
|
||||
@Input() showNotes = true;
|
||||
@@ -31,6 +31,7 @@ export class PlotlyChart implements AfterViewInit, OnChanges, OnDestroy {
|
||||
@ViewChild('host', { static: true }) host!: ElementRef<HTMLElement>;
|
||||
readonly renderError = output<void>();
|
||||
readonly widthChanged = output<number>();
|
||||
readonly plotClick = output<unknown>();
|
||||
readonly status = signal<'loading' | 'ready' | 'error'>('loading');
|
||||
get notes(): string[] {
|
||||
const annotations = this.figure?.layout['annotations'];
|
||||
@@ -56,7 +57,7 @@ export class PlotlyChart implements AfterViewInit, OnChanges, OnDestroy {
|
||||
this.controller = new PlotlyController(this.host.nativeElement, this.loadPlotly, state => {
|
||||
this.status.set(state);
|
||||
if (state === 'error') this.renderError.emit();
|
||||
});
|
||||
}, event => this.plotClick.emit(event));
|
||||
if (typeof ResizeObserver !== 'undefined') {
|
||||
this.observer = new ResizeObserver(this.windowResize);
|
||||
this.observer.observe(this.host.nativeElement);
|
||||
|
||||
@@ -31,6 +31,22 @@ describe('PlotlyController async', () => {
|
||||
expect(host.style.visibility).toBe('visible');
|
||||
expect(state).toHaveBeenLastCalledWith('ready');
|
||||
});
|
||||
it('suscribe click una vez, ignora clicks durante react y retira listener al destruir', async () => {
|
||||
let listener!: (event: unknown) => void;
|
||||
const on = vi.fn((_name, callback) => { listener = callback; });
|
||||
const removeListener = vi.fn(), click = vi.fn();
|
||||
Object.assign(host, { on, removeListener });
|
||||
controller = new PlotlyController(host, () => Promise.resolve(plotly), state, click);
|
||||
controller.setFigure(fig(1)); await settle();
|
||||
listener({ points: [1] }); expect(click).toHaveBeenCalledTimes(1);
|
||||
const pending = deferred(); vi.mocked(plotly.react).mockReturnValueOnce(pending.promise);
|
||||
controller.setFigure(fig(2)); await settle(); listener({ points: [2] });
|
||||
expect(click).toHaveBeenCalledTimes(1);
|
||||
pending.resolve(); await settle(); expect(on).toHaveBeenCalledTimes(1);
|
||||
listener({ points: [3] }); expect(click).toHaveBeenCalledTimes(2);
|
||||
controller.destroy(); expect(removeListener).toHaveBeenCalledWith('plotly_click', listener);
|
||||
listener({ points: [4] }); expect(click).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
it('desactiva el envío externo sin alterar el JSON ni las opciones locales recibidas', async () => {
|
||||
const figure: PlotlyFigure = {
|
||||
...fig(1),
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { PlotlyFigure } from '../../features/indicadores/indicadores.models';
|
||||
import { PlotlyRuntime } from './plotly-runtime';
|
||||
|
||||
interface EventHost extends HTMLElement {
|
||||
on?: (name: string, listener: (event: unknown) => void) => void;
|
||||
removeListener?: (name: string, listener: (event: unknown) => void) => void;
|
||||
}
|
||||
|
||||
// One serialized queue owns the host. A completed stale operation cannot expose
|
||||
// its graph or overwrite a newer react operation. Resize uses the same queue.
|
||||
export class PlotlyController {
|
||||
@@ -12,8 +17,13 @@ export class PlotlyController {
|
||||
private resizePending = false;
|
||||
private plotly?: PlotlyRuntime;
|
||||
private frameCount = 0;
|
||||
private listenerAttached = false;
|
||||
private readonly handleClick = (event: unknown) => {
|
||||
if (!this.destroyed && !this.busy && this.rendered === this.version) this.click?.(event);
|
||||
};
|
||||
constructor(private host: HTMLElement, private load: () => Promise<PlotlyRuntime>,
|
||||
private state: (state: 'loading' | 'ready' | 'error') => void) {}
|
||||
private state: (state: 'loading' | 'ready' | 'error') => void,
|
||||
private click?: (event: unknown) => void) {}
|
||||
|
||||
setFigure(figure: PlotlyFigure) {
|
||||
if (this.destroyed) return;
|
||||
@@ -30,6 +40,8 @@ export class PlotlyController {
|
||||
}
|
||||
destroy() {
|
||||
this.destroyed = true;
|
||||
if (this.listenerAttached) (this.host as EventHost).removeListener?.('plotly_click', this.handleClick);
|
||||
this.listenerAttached = false;
|
||||
this.host.style.visibility = 'hidden';
|
||||
this.plotly?.purge(this.host);
|
||||
}
|
||||
@@ -52,6 +64,10 @@ export class PlotlyController {
|
||||
showSendToCloud: false,
|
||||
modeBarButtonsToRemove: ['sendChartToCloud'],
|
||||
});
|
||||
if (!this.destroyed && !this.listenerAttached && this.click && (this.host as EventHost).on) {
|
||||
(this.host as EventHost).on!('plotly_click', this.handleClick);
|
||||
this.listenerAttached = true;
|
||||
}
|
||||
if (this.destroyed) break;
|
||||
if (version !== this.version) continue;
|
||||
if (figure.frames?.length) {
|
||||
|
||||
Reference in New Issue
Block a user