Files
WebApp/src/app/features/indicadores/indicador-catalog-card.ts
T
Roberto Treviño Cervantes e6e6fb7210 preliminary release
2026-10-29 12:12:15 -06:00

220 lines
13 KiB
TypeScript

import { ChangeDetectorRef, Component, effect, ElementRef, inject, Input, OnChanges, OnDestroy, output, untracked, ViewChild } from '@angular/core';
import { catchError, of, Subscription } from 'rxjs';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatSelectModule } from '@angular/material/select';
import { MatIconModule } from '@angular/material/icon';
import { Dominio, Indicador, IndicadorMetadatos, Tema } from './indicadores.models';
import { GlobalApplied, IndicadoresMemory } from './indicadores-memory';
import { indicatorIdentity, figureIdentity, identityElementId } from './indicadores-identities';
import { IndicadorFigurePanel } from './indicador-figure-panel';
import { figureDisplayName } from './indicadores-figure-labels';
import { READABLE_VISUAL_PROFILE, FigureVisualProfile } from './indicadores-visual-profile';
import { dualFigures } from './indicadores-dual-figures';
import { IndicadoresSnapshots, snapshotKey } from './indicadores-snapshots';
import { filterCompatibility } from './indicadores-memory';
import { DonutSelection } from './indicadores-donut-drilldown';
import { IndicadoresService } from './indicadores.service';
import { IndicadorEu7Escenario } from './indicador-eu7-escenario';
import { IndicadorTiemposPareados } from './indicador-tiempos-pareados';
import { IndicadorConfirmacionCitologia } from './indicador-confirmacion-citologia';
import { ID_CONFIRMACION } from './indicadores-confirmacion-citologia';
import { parTiempos } from './indicadores-tiempos-pareados';
@Component({
selector: 'app-indicador-catalog-card',
host: { '[class.dual-card]': 'isDual || isEu7 || isConfirmacion || !!tiempos' },
imports: [MatButtonModule, MatCardModule, MatIconModule, MatFormFieldModule, MatSelectModule, IndicadorFigurePanel, IndicadorEu7Escenario, IndicadorTiemposPareados, IndicadorConfirmacionCitologia],
template: `
<mat-card>
<header>
<div class="card-identity">
<h2 [id]="titleId" tabindex="-1">{{ indicador.titulo }}</h2>
</div>
</header>
<button class="pin-toggle" mat-button type="button" [attr.aria-pressed]="pinned"
[attr.aria-label]="(pinned ? 'Desfijar indicador' : 'Fijar indicador') + ', ' + indicador.titulo"
(click)="pinChanged.emit()">
<mat-icon aria-hidden="true">push_pin</mat-icon>{{ pinned ? 'Desfijar indicador' : 'Fijar indicador' }}
</button>
<div class="catalog-content" [id]="contentId" [attr.aria-labelledby]="titleId">
<p class="catalog-metadata">{{ indicador.figuras.length }} figuras publicadas</p>
@if (isEu7) {
<div #visibilityRegion class="eu7-region">
<app-indicador-eu7-escenario [dominio]="dominio" [indicador]="indicador" [tema]="tema" [applied]="applied" [visible]="visible" [metadata]="metadata" (metadataRequested)="loadMetadata()" />
</div>
} @else if (isConfirmacion) {
<div #visibilityRegion class="confirmacion-region">
<app-indicador-confirmacion-citologia [dominio]="dominio" [indicador]="indicador" [tema]="tema" [applied]="applied" [visible]="visible" [metadata]="metadata" (metadataRequested)="loadMetadata()" />
</div>
} @else if (tiempos; as par) {
<div #visibilityRegion class="tiempos-region">
<app-indicador-tiempos-pareados [dominio]="dominio" [par]="par" [indicador]="indicador" [tema]="tema" [applied]="applied" [visible]="visible" [metadata]="metadata" (metadataRequested)="loadMetadata()" />
</div>
} @else if (selectedFigure) {
@if (!pair.length || otherFigures.length) {
<mat-form-field appearance="outline" subscriptSizing="dynamic">
<mat-label>Representación gráfica</mat-label>
<mat-select [value]="selectedFigure" [disabled]="indicador.figuras.length === 1"
[attr.aria-label]="'Representación gráfica de ' + indicador.titulo" (selectionChange)="selectFigure($event.value)">
@if (pair.length) { <mat-option [value]="pair[0]">Dona y barras</mat-option> }
@for (figure of (pair.length ? otherFigures : indicador.figuras); track figure) { <mat-option [value]="figure">{{ figureDisplayName(figure, indicador.figuras) }}</mat-option> }
</mat-select>
</mat-form-field>
}
<div #visibilityRegion class="graphic-region" [class.dual-panels]="isDual">
@for (figure of displayFigures; track panelIdentity(figure)) {
<div class="representation-panel">
@if (isDual) { <h3>{{ figureDisplayName(figure, indicador.figuras) }}</h3> }
<app-indicador-figure-panel [dominio]="dominio" [indicador]="indicador" [metadata]="metadata" [figuraId]="figure" [tema]="tema" [applied]="applied" [independentPeriods]="independentPeriods" [visible]="visible" [visualProfile]="visualProfile" [localSelection]="localSelection" (metadataRequested)="loadMetadata()" (hierarchySelected)="localSelection = $event" />
</div>
}
</div>
} @else { <p role="status">El indicador no tiene figuras disponibles.</p> }
</div>
</mat-card>
`,
styles: `
:host { display: flex; min-width: 0; margin: 0; }
mat-card { flex: 1; min-width: 0; padding: clamp(.75rem, 2vw, 1.5rem); background: var(--issste-surface);
color: var(--issste-text); border: 1px solid var(--issste-border); border-radius: var(--issste-radius); box-shadow: none; }
header { display: grid; grid-template-columns: minmax(0, 1fr); align-items: start; gap: var(--issste-space-3); }
.catalog-content { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.graphic-region { flex: 1; min-width: 0; }
:host(.dual-card) { grid-column: 1 / -1; }
.graphic-region { display: grid; grid-template-columns: minmax(0, 1fr); }
.representation-panel { display: grid; min-width: 0; grid-template-rows: auto minmax(0, 1fr); }
.representation-panel > h3 { margin: 1rem 0 0; overflow-wrap: anywhere; }
app-indicador-figure-panel { grid-row: 2; }
// Nine shared tracks: name, controls, help, title, legend, plot, notes,
// full labels and stratified detail. Content determines their height.
@supports (grid-template-rows: subgrid) {
.graphic-region { grid-auto-rows: auto; grid-template-rows: auto auto auto auto auto minmax(0, 1fr) auto auto auto; }
.representation-panel { grid-row: span 9; grid-template-rows: subgrid; }
app-indicador-figure-panel { grid-row: 2 / span 8; }
.graphic-region:has(app-indicador-figure-card.methodology-expanded) { grid-template-rows: none; align-items: start; }
.graphic-region:has(app-indicador-figure-card.methodology-expanded) .representation-panel { grid-row: auto; grid-template-rows: auto auto; }
.graphic-region:has(app-indicador-figure-card.methodology-expanded) app-indicador-figure-panel { grid-row: auto; grid-template-rows: auto auto auto; }
.graphic-region:has(app-indicador-figure-card.methodology-expanded) app-indicador-figure-card { grid-row: auto; grid-template-rows: auto auto auto auto auto auto; }
}
@media (min-width: 901px) {
.dual-panels { grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: var(--issste-space-4); }
}
.card-identity { min-width: 0; }
h2, p { overflow-wrap: anywhere; }
h2 { margin: 0; font-family: var(--issste-font-heading); font-weight: 700; font-size: 1.5rem; line-height: 1.25; }
.pin-toggle { align-self: start; margin-top: var(--issste-space-1); max-width: 100%; height: auto;
min-width: 48px; min-height: 48px; color: var(--issste-text); }
.pin-toggle mat-icon { flex-shrink: 0; }
button:focus-visible { outline: 2px solid var(--issste-text); outline-offset: 3px; }
mat-form-field { width: 100%; margin-top: var(--issste-space-4); }
.catalog-metadata { margin: var(--issste-space-4) 0 0; padding-top: var(--issste-space-4);
border-top: 1px solid var(--issste-border); color: var(--issste-text-secondary); font: var(--mat-sys-body-small); }
@media (max-width: 550px) { h2 { font-size: 1.25rem; } header { gap: var(--issste-space-2); } }
`,
})
export class IndicadorCatalogCard implements OnChanges, OnDestroy {
localSelection: DonutSelection = {};
private selectionContext = '';
private readonly snapshots = inject(IndicadoresSnapshots);
private numericSubscription?: Subscription;
private numericKey = '';
private readonly indicators = inject(IndicadoresService);
private metadataSubscription?: Subscription;
private metadataKey = '';
metadata?: IndicadorMetadatos;
private activatedKey = '';
constructor() {
effect(() => {
this.snapshots.expanded(); this.snapshots.retries();
if (this.indicador && this.applied) untracked(() => this.updateNumeric());
});
}
get pair() { return dualFigures(this.indicador); }
get isEu7() { return this.dominio === 'cacu' && this.indicador.id === 'cacu_eu7'; }
get isConfirmacion() { return this.dominio === 'cacu' && this.indicador.id === ID_CONFIRMACION; }
// Connected time pairs replace the diagnosis card with one coordinated box plot.
get tiempos() { return parTiempos(this.dominio, this.indicador.id); }
get otherFigures() { return this.indicador.figuras.filter(id => !this.pair.includes(id)); }
get isDual() { return this.pair.includes(this.selectedFigure ?? ''); }
get displayFigures() { return this.isDual ? this.pair : this.selectedFigure ? [this.selectedFigure] : []; }
private updateNumeric() {
const key = snapshotKey(this.dominio, this.indicador, this.applied);
if (key !== this.activatedKey) {
this.numericSubscription?.unsubscribe(); this.numericKey = '';
if (this.visible) this.activatedKey = key;
}
if (this.activatedKey !== key || !this.snapshots.expanded()
|| !filterCompatibility(this.applied, this.indicador.tipos_periodo).compatible) return;
const attempt = JSON.stringify([key, this.snapshots.retries()]);
if (this.numericKey === attempt) return;
this.numericKey = attempt;
this.numericSubscription?.unsubscribe();
this.numericSubscription = this.snapshots.read(this.dominio, this.indicador, this.applied).subscribe();
}
ngOnDestroy() { this.numericSubscription?.unsubscribe(); this.metadataSubscription?.unsubscribe(); }
readonly figureDisplayName = figureDisplayName;
@Input({ required: true }) dominio!: Dominio;
@Input({ required: true }) indicador!: Indicador;
@Input({ required: true }) tema!: Tema;
@Input({ required: true }) applied!: GlobalApplied;
@Input() independentPeriods = false;
@Input() pinned = false;
@Input() visualProfile: FigureVisualProfile = READABLE_VISUAL_PROFILE;
readonly pinChanged = output<void>();
@ViewChild('visibilityRegion') visibilityRegion?: ElementRef<HTMLElement>;
readonly host = inject<ElementRef<HTMLElement>>(ElementRef);
private readonly changeDetector = inject(ChangeDetectorRef);
private readonly memory = inject(IndicadoresMemory);
private identity = '';
visible = false;
titleId = '';
contentId = '';
panelIdentity(figure: string) { return figureIdentity(this.dominio, this.indicador.id, figure); }
selectedFigure?: string;
selectFigure(figure: string) {
if (!this.indicador.figuras.includes(figure) || figure === this.selectedFigure) return;
this.selectedFigure = this.pair.includes(figure) ? this.pair[0] : figure;
this.memory.rememberFigure(this.dominio, this.indicador.id, figure);
this.changeDetector.markForCheck();
}
ngOnChanges() {
const context = JSON.stringify([this.dominio, this.indicador.id, this.indicador.version, this.applied, this.tema]);
if (context !== this.selectionContext) { this.localSelection = {}; this.selectionContext = context; }
const remembered = this.memory.figure(this.dominio, this.indicador.id);
const selected = remembered && this.indicador.figuras.includes(remembered) ? remembered : this.pair[0] ?? this.indicador.figuras[0];
this.selectedFigure = this.pair.includes(selected) ? this.pair[0] : selected;
const identity = indicatorIdentity(this.dominio, this.indicador.id);
if (identity === this.identity) { this.updateNumeric(); return; }
this.identity = identity;
this.metadataSubscription?.unsubscribe();
this.metadata = undefined;
this.metadataKey = '';
const id = identityElementId(identity);
this.titleId = id + '-title';
this.contentId = id + '-content';
this.visible = false;
this.updateNumeric();
}
setVisible(visible: boolean) {
if (this.visible === visible) return;
this.visible = visible;
this.updateNumeric();
this.changeDetector.markForCheck();
}
loadMetadata() {
const key = JSON.stringify([this.dominio, this.indicador.id]);
if (this.metadataKey === key) return;
this.metadataKey = key;
this.metadataSubscription?.unsubscribe();
this.metadataSubscription = this.indicators.metadatos(this.dominio, this.indicador.id)
.pipe(catchError(() => of(undefined))).subscribe(value => {
if (this.metadataKey !== key) return;
if (!value) this.metadataKey = '';
this.metadata = value;
this.changeDetector.markForCheck();
});
}
}