import { ConnectedPosition } from '@angular/cdk/overlay'; import { AfterContentInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChildren, Directive, EventEmitter, HostBinding, HostListener, InjectionToken, Input, OnDestroy, Output, QueryList, ViewEncapsulation } from '@angular/core'; import { Subscription } from 'rxjs'; export type YapCardStatus = 'selected' | 'unselected'; const OVERLAY_POSITIONS: ConnectedPosition[] = [ { originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top' }, { originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top' }, { originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom' }, { originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom' } ]; const YAP_CARD_ICON = new InjectionToken('yap-card-icon'); @Directive({ selector: `yap-card-icon, [yap-card-icon], [yapCardIcon]`, providers: [{ provide: YAP_CARD_ICON, useExisting: YapCardIconDirective }] }) export class YapCardIconDirective { @HostBinding('class') class = 'o-yap-card-icon'; } /** * Title of a card, needed as it's used as a selector in the API. */ @Directive({ selector: `yap-card-title, [o-yap-card-title], [yapCardTitle]` }) export class YapCardTitleDirective { @HostBinding('class') class = 'o-yap-card-title'; } /** * Subtitle of a card, needed as it's used as a selector in the API. */ @Directive({ selector: `yap-card-subtitle, [yap-card-subtitle], [yapCardSubtitle]` }) export class YapCardSubtitleDirective { @HostBinding('class') class = 'yap-card-subtitle'; } const YAP_CARD_MENU = new InjectionToken('yap-card-menu'); @Directive({ selector: 'yap-card-menu', exportAs: 'yapCardMenu', providers: [{ provide: YAP_CARD_MENU, useExisting: YapCardMenuDirective }] }) export class YapCardMenuDirective { @HostBinding('class') class = 'o-yap-card-menu'; @Output() readonly closeMenu$ = new EventEmitter(); closeMenu(): void { this.closeMenu$.emit(true); } } @Component({ selector: 'yap-card', templateUrl: 'card.component.html', encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush }) export class YapCardComponent { @HostBinding('class') class = 'yap-card'; @HostListener('click') _selectCard() { this.selected = !this.selected; this._status = this.selected ? 'selected' : 'unselected'; this.statusChange.emit(this._status); } @Output() statusChange = new EventEmitter(); @HostBinding('class.yap-card-selected') @Input() get selected(): boolean { return this._selected; } set selected(value: boolean) { if (value !== this._selected) { this._selected = value; this._status = value ? 'selected' : 'unselected'; } } protected _selected: boolean = false; protected _status: YapCardStatus = 'unselected'; } @Component({ selector: 'yap-card-header', templateUrl: 'card-header.component.html', encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush }) export class YapCardHeaderComponent implements AfterContentInit, OnDestroy { @HostBinding('class') class = 'o-yap-card-header-wrapper'; @ContentChildren(YAP_CARD_MENU, { descendants: true }) _menuChildren!: QueryList; @ContentChildren(YAP_CARD_ICON, { descendants: true }) _iconChildren!: QueryList; readonly overlayPositions = OVERLAY_POSITIONS; @Input() marker = true; get menuOpen(): boolean { return this._menuOpen; } set menuOpen(value: boolean) { this._menuOpen = value; } protected _menuOpen: boolean = false; private menuSub?: Subscription; private childrensSub: Subscription = new Subscription(); constructor(private _detect: ChangeDetectorRef) {} ngAfterContentInit(): void { // after init it subscribes closeMenu$ if there's a menu const firstMenu = this._menuChildren.get(0); this.manageMenuCloseSubscription(firstMenu); // after each change of template it subscribes at the new first menu this.childrensSub.add( this._menuChildren.changes.subscribe((menus: QueryList) => { const menu = menus.get(0); this.manageMenuCloseSubscription(menu); this._detect.detectChanges(); }) ); this.childrensSub.add( this._iconChildren.changes.subscribe(() => { this._detect.detectChanges(); }) ); } ngOnDestroy(): void { this.menuSub?.unsubscribe(); this.childrensSub.unsubscribe(); } private manageMenuCloseSubscription(menu: YapCardMenuDirective | undefined): void { if (!menu) { this.menuSub?.unsubscribe(); return; } this.menuSub = menu.closeMenu$.subscribe(() => { this.menuOpen = false; this._detect.detectChanges(); }); } }