All files / lib/table table-cell-simple.component.ts

95.74% Statements 315/329
90% Branches 54/60
94.44% Functions 17/18
95.74% Lines 315/329

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 3301x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 2956x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 262x 262x 262x 262x 262x 1x 1x 305x 303x 303x 303x 303x 303x 305x 305x 305x 1x 1x 303x 303x 303x 303x 303x 303x 303x 303x 303x 303x 1x 1x 303x 303x 1203x 1203x 303x 303x 303x 303x 303x 303x 1203x 303x 303x 1x 1x 1x 1x 1x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 45849x 108x     108x 108x 108x 45741x 2956x 42785x 26504x 1455x 25049x 25049x 16281x 16281x 45849x 2058x 2058x 2058x 2058x 2058x 2058x 1x 1x 2457x 305x 2457x 2457x 1x 1x 1x 41384x 41384x 1x 1x 1x 11824x 11816x 8x 8x     11824x 11824x 1x 1x 70818x 70818x 1x 1x 1x 3x 3x 1x 1x 1x           1x 1x 1x 45x 45x 42x 45x 45x 1x 1x 1x 42x 42x 38x 4x 42x 42x 42x 42x 42x 42x 42x 42x 38x 38x 42x 42x 1x 1x 1x 3x 1x 1x     1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 26504x 26504x 1x 1x 1x 305x 305x 305x 305x 305x       305x 305x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  NgTemplateOutlet,
  formatNumber,
  registerLocaleData,
} from '@angular/common';
import localeDe from '@angular/common/locales/de';
import localeDeExtra from '@angular/common/locales/extra/de';
import {
  Component,
  ElementRef,
  Input,
  NgZone,
  OnChanges,
  QueryList,
  SimpleChanges,
  TemplateRef,
  ViewChild,
  ViewChildren,
} from '@angular/core';
 
import { LinkEvent } from '../utils/event.types';
import {
  BadgeStatus,
  BadgeVariant,
  Consumer,
  HasHighlight,
} from '../utils/util.types';
 
registerLocaleData(localeDe, 'de-DE', localeDeExtra);
 
@Component({
  selector: 'gc-aligned-number',
  template:
    '<span class="gc-num-integer">{{ integer }}</span><ng-container *ngIf="fraction.length > 0">,</ng-container><span class="gc-num-fraction">{{ fraction }}</span>',
  styles: [
    `
      :host {
        white-space: nowrap;
      }
      .gc-num-fraction {
        display: inline-block;
        text-align: left;
      }
    `,
  ],
})
export class AlignedNumberComponent implements OnChanges {
  @Input()
  public integer = '';
 
  @Input()
  public fraction = '';
 
  private readonly componentTag: string;
 
  constructor(
    private readonly el: ElementRef<HTMLElement>,
    private readonly zone: NgZone,
  ) {
    this.componentTag = el.nativeElement.tagName;
  }
 
  public ngOnChanges(changes: SimpleChanges): void {
    if ('fraction' in changes) {
      this.zone.runOutsideAngular(() => {
        requestAnimationFrame(() => {
          NgZone.assertNotInAngularZone();
          this.fixIt();
        });
      });
    }
  }
 
  private fixIt(): void {
    const table = this.el.nativeElement.closest('table');
    const td = this.el.nativeElement.closest('td');
    const columnId = td?.dataset.gcTableColumnKey;
    if (columnId && table) {
      const numFractionsInColumn = table.querySelectorAll<HTMLElement>(
        `td[data-gc-table-column-key='${columnId}'] ${this.componentTag} .gc-num-fraction`,
      );
      this.fixNumberAlignments(numFractionsInColumn);
    }
  }
 
  private fixNumberAlignments(columnNodes: NodeListOf<HTMLElement>): void {
    let max = 0;
    columnNodes.forEach(node => {
      const cur = node.textContent?.length ?? 0;
      max = Math.max(cur, max);
    });
    if (max > 0) {
      max += 0.05; // add some buffer to allow for font variance between width of "0" (1ch) and other characters
    }
 
    columnNodes.forEach(node => {
      node.style.width = max + 'ch';
    });
  }
}
 
/**
 * A simple table cell.
 */
@Component({
  selector: 'gc-table-cell-simple',
  templateUrl: './table-cell-simple.component.html',
  styleUrls: ['./table-cell-simple.component.css'],
})
export class TableCellSimpleComponent<
  C extends NonNullable<NgTemplateOutlet['ngTemplateOutletContext']>,
> implements OnChanges
{
  /** Context passed when instantiating templates for the cell, e.g. icons. */
  @Input()
  public templateContext: C | null = null;
 
  /** The text to be shown. */
  @Input()
  public text?: string | [string, string] | null;
 
  /** The text to be shown in the tooltip. */
  @Input()
  public tooltipText?: string | null;
 
  /** The url to be linked to. If specified the cell will be a hyperlink. */
  @Input()
  public link?: string;
 
  /** Optional handler to customize handling of navigation requests */
  @Input()
  public linkHandler?: Consumer<LinkEvent>;
 
  /** The badge label to be shown. */
  @Input()
  public badge?: string;
 
  /** The badge variant. */
  @Input()
  public badgeVariant: BadgeVariant = 'primary';
 
  /** The badge status. */
  @Input()
  public badgeStatus: BadgeStatus = 'basic';
 
  /**
   * Icon provider for badge
   */
  @Input()
  public badgeIconProvider?: TemplateRef<unknown>;
 
  /** The template to the icon to be used. */
  @Input()
  public icon?: TemplateRef<C>;
 
  /** The number to be shown. */
  @Input()
  public number?: number;
 
  /**
   * Format that should be used to display the number.
   *
   * Specified as a string in the following format: `{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}.
   */
  @Input()
  public numberFormat = '1.2-4';
 
  /** Optional visual highlight for the cell. */
  @Input()
  public highlight?: HasHighlight;
 
  /** Turn on / off text highlighting */
  @Input()
  public showTextHighlight = true;
 
  /**
   * Mark the content as new entry.
   */
  @Input()
  public newItem = false;
 
  /**
   * (Optional) Sublabel to display
   */
  @Input()
  public sublabel = '';
 
  /** @ignore */
  @ViewChild('cellEl')
  protected cellElement!: ElementRef<HTMLElement>;
 
  /** @ignore */
  @ViewChildren('textHighlight', { read: ElementRef })
  protected textHighlights?: QueryList<ElementRef<HTMLElement>>;
 
  /** @ignore */
  @ViewChildren('textNode')
  protected textNodes?: QueryList<ElementRef<HTMLElement>>;
 
  /** @ignore */
  protected get titleText(): string {
    if (this.text && this.sublabel) {
      if (this.badge) {
        return `${this.badge} ${this.straightenText(this.text)} ${
          this.sublabel
        }`;
      }
      return `${this.text.toString()} ${this.sublabel}`;
    } else if (this.number) {
      return formatNumber(this.number, 'de-DE', this.numberFormat);
    } else if (this.text) {
      if (this.badge) {
        return `${this.badge} ${this.straightenText(this.text)}`;
      }
      return this.straightenText(this.text);
    }
    return this.badge ? this.badge : '';
  }
 
  /** @ignore */
  protected numberInt = '';
 
  /** @ignore */
  protected numberFraction = '';
 
  ngOnChanges(changes: SimpleChanges): void {
    if ('number' in changes || 'numberFormat' in changes) {
      this.updateNumberStrings();
    }
  }
 
  /** @ignore */
  public _hasHighlight(): boolean {
    return this.highlight !== undefined;
  }
 
  /** @ignore */
  protected singleValueText(): string | undefined {
    if (this.isSingleValue(this.text)) {
      return this.text;
    } else if (Array.isArray(this.text)) {
      return this.text[0];
    } else {
      return undefined;
    }
  }
 
  protected isSingleValue(v: unknown): v is string {
    return typeof this.text === 'string';
  }
 
  /** @ignore */
  protected _handleClick(): boolean {
    return this._internalHandleClick(true);
  }
 
  /** @ignore */
  protected _handleAuxClick(event: MouseEvent): boolean {
    if (event.button === 1) {
      return this._internalHandleClick(false);
    }
    return true;
  }
 
  /** @ignore */
  protected permitTooltip(): boolean {
    return (
      (this.tooltipText !== undefined && this.tooltipText !== this.text) ||
      this.isTextTruncated()
    );
  }
 
  /** @ignore */
  protected isTextTruncated(): boolean {
    const elements =
      this.textHighlights && this.textHighlights.length > 0
        ? this.textHighlights
        : this.textNodes;
    if (!elements) {
      return false;
    }
 
    return elements
      .toArray()
      .some(
        el =>
          el.nativeElement.scrollWidth > el.nativeElement.offsetWidth ||
          el.nativeElement.scrollHeight > el.nativeElement.offsetHeight,
      );
  }
 
  /** @ignore */
  private _internalHandleClick(replace: boolean): boolean {
    if (this.linkHandler) {
      try {
        this.linkHandler({ linkInfo: this.link ?? '', replace });
      } catch (e) {
        console.error('Failed to execute link handler', e);
      }
 
      return false;
    }
 
    return true;
  }
 
  /** @ignore */
  private straightenText(value: string | [string, string]): string {
    return this.isSingleValue(value) ? value : `${value[0]} ${value[1]}`;
  }
 
  /** @ignore */
  private updateNumberStrings(): void {
    if (this.number) {
      const formatted = formatNumber(this.number, 'de-DE', this.numberFormat);
      const split = formatted.split(',');
      this.numberInt = split[0];
      this.numberFraction = split[1];
    } else {
      this.numberInt = '';
      this.numberFraction = '';
    }
  }
}