feat(ui): habilitar apertura automatica UX-06E.D.1.1

This commit is contained in:
Angel Capetillo Zavala
2026-10-13 16:34:28 -06:00
parent 35e1bc33d3
commit 98d0614d80
10 changed files with 530 additions and 47 deletions
@@ -1,5 +1,5 @@
import { DOCUMENT } from '@angular/common';
import { afterNextRender, ChangeDetectorRef, Component, DestroyRef, ElementRef, Injector, inject, computed, signal, viewChild } from '@angular/core';
import { afterEveryRender, afterNextRender, ChangeDetectorRef, Component, DestroyRef, ElementRef, Injector, inject, computed, signal, viewChild, viewChildren } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { FormsModule } from '@angular/forms';
@@ -30,6 +30,132 @@ export class IndicadoresPage {
private readonly element = inject<ElementRef<HTMLElement>>(ElementRef);
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 renderedCards: readonly IndicadorCatalogCard[] = [];
private visibilityObserver?: IntersectionObserver;
private sizeObserver?: ResizeObserver;
private readonly sizeTargets = new Set<Element>();
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() {
const browser = this.document.defaultView;
if (this.visibilityDestroyed || !this.visibilityObserver || !browser || this.frame !== undefined) return;
this.frame = browser.requestAnimationFrame(() => {
this.frame = undefined;
this.evaluateVisibility();
});
}
private clearVisibility() {
this.visibilityObserver?.disconnect();
this.visibilityObserver = undefined;
this.observedHeaders.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;
const cards = this.graphicCards();
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);
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);
changed = true;
}
}
if (typeof ResizeObserver !== 'undefined') {
this.sizeObserver ??= new ResizeObserver(this.viewportChanged);
const targets = [this.document.querySelector('.institutional-header'), this.element.nativeElement.querySelector('.catalog-grid')]
.filter((target): target is Element => target !== null);
for (const target of this.sizeTargets) {
if (!targets.includes(target)) { this.sizeObserver.unobserve(target); this.sizeTargets.delete(target); }
}
for (const target of targets) {
if (!this.sizeTargets.has(target)) { this.sizeTargets.add(target); this.sizeObserver.observe(target); }
}
}
if (changed) this.visibilityChanged();
}
private refreshVisibilityObserver() {
const height = Math.max(0, this.document.querySelector('.institutional-header')?.getBoundingClientRect().bottom ?? 0);
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 });
this.visibilityObserver = observer;
for (const header of this.observedHeaders.keys()) observer.observe(header);
}
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.
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] });
}
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;
}
clearSearch() {
this.changeSearchQuery('');
this.searchInput()?.nativeElement.focus({ preventScroll: true });
@@ -37,6 +163,7 @@ export class IndicadoresPage {
changeSearchQuery(query: string) {
const focused = this.document.activeElement;
this.searchQuery.set(query);
this.confirmation = undefined;
if (focused instanceof HTMLElement && this.element.nativeElement.contains(focused)
&& focused.closest('app-indicador-catalog-card')) {
// Moving a retained view can blur its control. Restore only that connected control,
@@ -86,10 +213,25 @@ export class IndicadoresPage {
tema: Tema = 'claro';
cardIdentity(item: Indicador) { return indicatorIdentity(this.dominio, item.id); }
constructor() {
afterEveryRender(() => this.syncVisibility());
const browser = this.document.defaultView;
if (typeof IntersectionObserver !== 'undefined') {
browser?.addEventListener('scroll', this.viewportChanged, { passive: true });
browser?.addEventListener('resize', this.viewportChanged);
}
this.destroyRef.onDestroy(() => {
this.visibilityDestroyed = true;
this.clearVisibility();
this.sizeObserver?.disconnect();
this.sizeTargets.clear();
browser?.removeEventListener('scroll', this.viewportChanged);
browser?.removeEventListener('resize', this.viewportChanged);
});
const initialDomain: unknown = this.route?.snapshot.data['dominio'];
if (isDominio(initialDomain)) this.dominio = initialDomain;
this.tema = this.memory.theme(this.dominio);
this.catalogRequests.pipe(startWith(undefined), switchMap(() => {
this.clearVisibility();
this.catalogLoading.set(true);
this.catalogError.set('');
this.catalog.set([]);