Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import { Directive, ElementRef, OnInit, OnChanges, SimpleChanges } from '@angular/core'; import { Input } from '@angular/core'; import { GenericUtils } from '@unpispas/upp-defs'; /** * A directive to normalize a given text and bind it to the `data-id` attribute of the host element. * * This directive accepts a string as input, normalizes it using a utility function, and sets the * `data-id` attribute directly on the DOM element. * * @example * <div uppDataid="My Custom Text"></div> * <!-- Result: <div data-id="my-custom-text"></div> --> */ @Directive({ selector: '[uppDataid]' }) export class DataIdDirective implements OnInit, OnChanges { private _id: string | null = null; /** * Input property that accepts the text to be normalized. */ @Input('uppDataid') set id(value: string | null) { this._id = value; this._apply(); } get id(): string | null { return this._id; } constructor(private el: ElementRef<HTMLElement>) {} ngOnInit(): void { this._apply(); } ngOnChanges(changes: SimpleChanges): void { Iif (changes['id']) { this._apply(); } } private _apply(): void { const value = this._id ? GenericUtils.Normalize(this._id) : null; Iif (this.el?.nativeElement) { if (value) { this.el.nativeElement.setAttribute('data-id', value); } else { this.el.nativeElement.removeAttribute('data-id'); } } } } |