feat(ui): mejorar periodos KPI y presentacion del dashboard

This commit is contained in:
Angel Capetillo Zavala
2026-10-14 11:46:17 -06:00
parent 9fcc65d84f
commit 69afcc7fdb
26 changed files with 778 additions and 604 deletions
@@ -20,6 +20,7 @@ import { KPI_BY_DOMAIN } from './indicadores-kpi';
import { IndicadorKpiPanel } from './indicador-kpi-panel';
import { IndicadorKpiCard } from './indicador-kpi-card';
import { indicatorIdentity } from './indicadores-identities';
import { emptyAvailability, IndicadoresPeriodos } from './indicadores-periodos';
import { httpMessage } from './indicadores-http';
@Component({
@@ -34,7 +35,7 @@ export class IndicadoresPage {
private readonly injector = inject(Injector);
private readonly searchInput = viewChild<ElementRef<HTMLInputElement>>('searchInput');
private readonly graphicCards = viewChildren(IndicadorCatalogCard);
private readonly observedHeaders = new Map<Element, IndicadorCatalogCard>();
private readonly observedRegions = new Map<Element, IndicadorCatalogCard>();
private renderedCards: readonly IndicadorCatalogCard[] = [];
private visibilityObserver?: IntersectionObserver;
private sizeObserver?: ResizeObserver;
@@ -42,11 +43,9 @@ export class IndicadoresPage {
private frame?: number;
private stickyHeight = -1;
private visibilityDestroyed = false;
private confirmation?: { cards: IndicadorCatalogCard[]; trigger: IndicadorCatalogCard; top: number; left: number; width: number; height: number; viewportWidth: number; viewportHeight: number };
private readonly viewportChanged = () => this.visibilityChanged();
visibilityChanged() {
this.confirmation = undefined;
this.queueVisibility();
}
private queueVisibility() {
@@ -60,31 +59,34 @@ export class IndicadoresPage {
private clearVisibility() {
this.visibilityObserver?.disconnect();
this.visibilityObserver = undefined;
this.observedHeaders.clear();
this.observedRegions.clear();
this.renderedCards = [];
this.confirmation = undefined;
if (this.frame !== undefined) this.document.defaultView?.cancelAnimationFrame(this.frame);
this.frame = undefined;
}
private syncVisibility() {
if (this.visibilityDestroyed || typeof IntersectionObserver === 'undefined' || !this.document.defaultView) return;
if (this.visibilityDestroyed || !this.document.defaultView) return;
const cards = this.graphicCards();
if (typeof IntersectionObserver === 'undefined') {
for (const card of cards) card.setVisible(true);
return;
}
const orderChanged = cards.length !== this.renderedCards.length || cards.some((card, i) => card !== this.renderedCards[i]);
this.renderedCards = [...cards];
this.refreshVisibilityObserver();
let changed = orderChanged;
for (const [header, card] of this.observedHeaders) {
if (!cards.includes(card) || !card.awaitingVisibility) {
this.visibilityObserver?.unobserve(header);
this.observedHeaders.delete(header);
for (const [region, card] of this.observedRegions) {
if (!cards.includes(card) || card.visibilityRegion?.nativeElement !== region) {
this.visibilityObserver?.unobserve(region);
this.observedRegions.delete(region);
changed = true;
}
}
for (const card of cards) {
const header = card.visibilityHeader.nativeElement;
if (card.awaitingVisibility && !this.observedHeaders.has(header)) {
this.observedHeaders.set(header, card);
this.visibilityObserver?.observe(header);
const region = card.visibilityRegion?.nativeElement;
if (region && !this.observedRegions.has(region)) {
this.observedRegions.set(region, card);
this.visibilityObserver?.observe(region);
changed = true;
}
}
@@ -106,58 +108,29 @@ export class IndicadoresPage {
if (this.visibilityObserver && Math.abs(height - this.stickyHeight) < 1) return;
this.stickyHeight = height;
this.visibilityObserver?.disconnect();
this.confirmation = undefined;
const observer = new IntersectionObserver(entries => {
// Callbacks from a replaced observer or retired target cannot authorize activation.
if (this.visibilityDestroyed || this.visibilityObserver !== observer) return;
if (entries.some(entry => this.observedHeaders.has(entry.target))) this.queueVisibility();
}, { root: null, rootMargin: `-${height}px 0px 0px 0px`, threshold: 0.25 });
if (entries.some(entry => this.observedRegions.has(entry.target))) this.queueVisibility();
}, { root: null, rootMargin: `-${height}px 0px 200px 0px`, threshold: 0 });
this.visibilityObserver = observer;
for (const header of this.observedHeaders.keys()) observer.observe(header);
for (const region of this.observedRegions.keys()) observer.observe(region);
}
private evaluateVisibility() {
const browser = this.document.defaultView;
if (!browser || this.visibilityDestroyed || !this.visibilityObserver) return;
this.refreshVisibilityObserver();
const width = browser.innerWidth, height = browser.innerHeight;
const rows: { top: number; cards: IndicadorCatalogCard[] }[] = [];
// Read the current DOM geometry; never retain index pairs or observer entry rectangles.
// Reread connected graphic regions after scroll/reorder; old observer rectangles
// cannot activate a card that has since moved outside the current viewport.
for (const card of this.renderedCards) {
if (card.dominio !== this.dominio || !card.host.nativeElement.isConnected) continue;
const top = card.host.nativeElement.getBoundingClientRect().top;
const row = rows.find(row => row.cards.length < 2 && Math.abs(row.top - top) <= 1);
if (row) row.cards.push(card); else rows.push({ top, cards: [card] });
const region = card.visibilityRegion?.nativeElement;
if (!region || card.dominio !== this.dominio || !region.isConnected) continue;
const rect = region.getBoundingClientRect();
card.setVisible(rect.width > 0 && rect.height > 0 && rect.right > 0
&& rect.left < width && rect.bottom > this.stickyHeight && rect.top < height + 200);
}
rows.sort((a, b) => a.top - b.top);
for (const row of rows) {
const trigger = row.cards.find(card => {
if (!card.awaitingVisibility || !this.observedHeaders.has(card.visibilityHeader.nativeElement)) return false;
const rect = card.visibilityHeader.nativeElement.getBoundingClientRect();
const visibleWidth = Math.max(0, Math.min(rect.right, width) - Math.max(rect.left, 0));
const visibleHeight = Math.max(0, Math.min(rect.bottom, height) - Math.max(rect.top, this.stickyHeight));
return rect.width > 0 && rect.height > 0 && visibleWidth * visibleHeight / (rect.width * rect.height) >= 0.25;
});
if (!trigger) continue;
const rect = trigger.visibilityHeader.nativeElement.getBoundingClientRect();
const previous = this.confirmation;
const stable = previous && previous.trigger === trigger && previous.cards.length === row.cards.length
&& row.cards.every((card, i) => card === previous.cards[i])
&& Math.abs(previous.top - rect.top) <= 1 && Math.abs(previous.left - rect.left) <= 1
&& Math.abs(previous.width - rect.width) <= 1 && Math.abs(previous.height - rect.height) <= 1
&& previous.viewportWidth === width && previous.viewportHeight === height;
if (!stable) {
this.confirmation = { cards: row.cards, trigger, top: rect.top, left: rect.left, width: rect.width, height: rect.height, viewportWidth: width, viewportHeight: height };
this.queueVisibility();
return;
}
this.confirmation = undefined;
for (const card of row.cards) card.activateVisible();
this.changeDetector.markForCheck();
// Wait for Angular's height change before considering any lower row.
afterNextRender(() => this.queueVisibility(), { injector: this.injector });
return;
}
this.confirmation = undefined;
this.changeDetector.markForCheck();
}
clearSearch() {
this.changeSearchQuery('');
@@ -166,13 +139,11 @@ export class IndicadoresPage {
changeSearchQuery(query: string) {
this.preserveCardFocus();
this.searchQuery.set(query);
this.confirmation = undefined;
}
isPinned(item: Indicador) { return this.memory.isPinned(this.dominio, item.id); }
togglePinned(item: Indicador) {
this.preserveCardFocus();
this.memory.togglePinned(this.dominio, item.id);
this.confirmation = undefined;
}
private preserveCardFocus() {
const focused = this.document.activeElement;
@@ -189,6 +160,9 @@ export class IndicadoresPage {
}
private readonly changeDetector = inject(ChangeDetectorRef);
private readonly service = inject(IndicadoresService);
private readonly periodService = inject(IndicadoresPeriodos);
private readonly availabilityRequests = new Subject<void>();
readonly availability = signal(emptyAvailability());
private readonly memory = inject(IndicadoresMemory);
private readonly destroyRef = inject(DestroyRef);
private readonly route = inject(ActivatedRoute, { optional: true });
@@ -229,8 +203,10 @@ export class IndicadoresPage {
tema: Tema = 'claro';
filters?: GlobalFilterState;
readonly filterTypes = computed(() => domainTypes(this.catalog()));
retryAvailability() { this.availabilityRequests.next(); }
filtersEdited() { if (this.filters) this.memory.saveGlobal(this.dominio, this.filters); }
applyFilters() {
this.evaluateVisibility();
if (!this.filters || !applyGlobal(this.filters, this.filterTypes())) return;
this.filtersEdited();
this.changeDetector.markForCheck();
@@ -255,11 +231,16 @@ export class IndicadoresPage {
const initialDomain: unknown = this.route?.snapshot.data['dominio'];
if (isDominio(initialDomain)) this.dominio = initialDomain;
this.tema = this.memory.theme(this.dominio);
this.availabilityRequests.pipe(switchMap(() => {
this.availability.set(emptyAvailability());
return this.periodService.dominio(this.dominio, this.catalog());
}), takeUntilDestroyed(this.destroyRef)).subscribe(state => this.availability.set(state));
this.catalogRequests.pipe(startWith(undefined), switchMap(() => {
this.clearVisibility();
this.catalogLoading.set(true);
this.catalogError.set('');
this.catalog.set([]);
this.availabilityRequests.next();
return this.service.catalogo(this.dominio).pipe(map(items => ({ items, error: '' })),
catchError(error => of({ items: [] as Indicador[], error: httpMessage(error, true) })));
}), takeUntilDestroyed(this.destroyRef)).subscribe(({ items, error }) => {
@@ -268,6 +249,7 @@ export class IndicadoresPage {
this.catalog.set(items);
this.filtersEdited();
this.catalogError.set(error);
this.availabilityRequests.next();
});
}
changeDominio(dominio: Dominio) {
@@ -282,6 +264,7 @@ export class IndicadoresPage {
retryCatalog() { this.catalogRequests.next(); }
changeTema(tema: Tema) {
if ((tema !== 'claro' && tema !== 'oscuro') || tema === this.tema) return;
this.evaluateVisibility();
this.tema = tema;
this.memory.rememberTheme(this.dominio, tema);
this.changeDetector.markForCheck();