All files / lib/internal/two-slot-label-container two-slot-label-container.component.ts

95.73% Statements 247/258
94.59% Branches 35/37
86.66% Functions 13/15
95.73% Lines 247/258

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 2591x 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 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 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 1x 1x 1x 1x 3856x 3856x 3856x 1x 1x 125x 35x 35x 35x 125x 125x 125x 125x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 1x 1x 1x 58x 58x 58x 58x 104x 58x 58x 58x 58x 185x 58x 58x 58x 58x 58x 1x 1x 1x 167x   167x 167x 1x 1x 1x                 1x 1x 1x 3856x 3856x 1x 1x 1x 9680x 9680x 1270x 60x 60x 60x 9680x 9680x 9680x 9680x 9680x 1x 1x 1x 1928x 1928x 1x 1x 1x     1x 1x 1x 58x 58x 58x 1x 1x 1x 185x 185x 185x 185x 185x 185x 185x 185x 185x 185x 185x 185x 185x 185x 185x 67x 4x 185x 185x 185x 185x 185x 185x 185x 185x 1x  
import {
  AfterViewInit,
  ChangeDetectorRef,
  Component,
  ContentChildren,
  Directive,
  ElementRef,
  HostBinding,
  Input,
  NgZone,
  OnChanges,
  OnDestroy,
  QueryList,
  SimpleChanges,
  TemplateRef,
  ViewChild,
} from '@angular/core';
 
import { observeElementSize } from '../../utils/internal-utils';
import { Callback, RangeInputSlots } from '../../utils/util.types';
 
/**
 * Used for marking elements that should be renderes as error messages.
 */
@Directive({
  selector: 'ng-template[data-gc-error]',
})
export class LabelContainerErrorMessageDirective {
  constructor(public template: TemplateRef<unknown>) {}
}
 
/**
 * Component with two slots used for combining two input elements/components.
 */
@Component({
  selector: 'gc-two-slot-label-container',
  templateUrl: './two-slot-label-container.component.html',
  styleUrls: [
    '../../utils/styles/reset-form.css',
    './two-slot-label-container.component.css',
  ],
})
export class TwoSlotLabelContainerComponent
  implements OnChanges, OnDestroy, AfterViewInit
{
  /**
   * Manual required state.
   */
  @Input()
  public required?: boolean;
 
  /**
   * Manual readonly state.
   */
  @Input()
  public readonly: boolean | Record<RangeInputSlots, boolean> = false;
 
  /**
   * Manual disabled state.
   */
  @Input()
  public disabled?: boolean;
 
  /**
   * Manual active state.
   */
  @Input()
  public active?: boolean;
 
  /**
   * Decides which slots contain errors. Undefined === none
   */
  @Input()
  public errorSlots?: 'start' | 'end' | 'both';
 
  /** @ignore */
  @ViewChild('startSlot')
  public startSlotEl!: ElementRef<HTMLElement>;
 
  /** @ignore */
  @ViewChild('endSlot')
  public endSlotEl!: ElementRef<HTMLElement>;
 
  /** @ignore */
  @ViewChild('infoContainer')
  public infoContainerEl!: ElementRef<HTMLElement>;
 
  /** @ignore */
  @ViewChild('lastCharacterPositionEl')
  public lastCharacterPositionEl!: ElementRef<HTMLSpanElement>;
 
  /** @ignore */
  @ContentChildren(LabelContainerErrorMessageDirective)
  protected errorMessageElements?: QueryList<LabelContainerErrorMessageDirective>;
 
  /** @ignore */
  @HostBinding('style.padding-bottom.px')
  protected paddingBottom = 0;
 
  /** @ignore */
  @HostBinding('style.--gc-label-position-offset.px')
  private labelPositionOffset = 0;
 
  /** @ignore */
  @HostBinding('style.--gc-label-top-border-left-pos.px')
  private labelTopBorderLeftPos = 0;
 
  /** @ignore */
  @HostBinding('style.--gc-label-max-width.px')
  private labelMaxWidth = 0;
 
  /** @ignore */
  @ViewChild('labelEl')
  private labelEl!: ElementRef<HTMLElement>;
 
  /**
   * Decides which slot has focus. Undefined === none
   */
  @Input()
  public get focusedSlot() {
    return this._focusedSlot;
  }
 
  public set focusedSlot(value: 'start' | 'end' | undefined) {
    if (value && this._focusedSlot === undefined) {
      this.el.nativeElement.scrollIntoView({
        block: 'nearest',
        inline: 'nearest',
      });
    }
    this._focusedSlot = value;
  }
 
  /** @ignore */
  protected verticalMode = false;
 
  private _focusedSlot?: 'start' | 'end';
 
  /** @ignore */
  private resizeObserver: Callback;
 
  /** @ignore */
  private infoContainerResizeObserver?: Callback;
 
  /** @ignore */
  private legendResizeObserver?: ResizeObserver;
 
  constructor(
    private el: ElementRef<HTMLElement>,
    private zone: NgZone,
    private cdr: ChangeDetectorRef,
  ) {
    this.resizeObserver = observeElementSize(
      el.nativeElement,
      this.zone,
      this.resizeObserverCallback.bind(this),
    );
  }
 
  /** @ignore */
  ngAfterViewInit(): void {
    this.infoContainerResizeObserver = observeElementSize(
      this.infoContainerEl.nativeElement,
      this.zone,
      () => {
        this.paddingBottom = this.infoContainerEl.nativeElement.clientHeight;
      },
    );
    this.zone.runOutsideAngular(() => {
      this.legendResizeObserver = new ResizeObserver(() =>
        this.computeLabelPositioning(),
      );
      this.legendResizeObserver.observe(this.startSlotEl.nativeElement);
      this.legendResizeObserver.observe(this.labelEl.nativeElement);
    });
  }
 
  /** @ignore */
  ngOnChanges(changes: SimpleChanges): void {
    if ('required' in changes && !changes.required.firstChange) {
      this.computeLabelPositioning();
    }
  }
 
  /** @ignore */
  ngOnDestroy(): void {
    this.resizeObserver();
    if (this.infoContainerResizeObserver) {
      this.infoContainerResizeObserver();
    }
    if (this.legendResizeObserver) {
      this.legendResizeObserver.disconnect();
    }
  }
 
  /** @ignore */
  protected isActive(): boolean {
    return !this.disabled && (this.active ?? false);
  }
 
  /** @ignore */
  protected isReadonly(slot: RangeInputSlots): boolean {
    if (typeof this.readonly === 'object') {
      switch (slot) {
        case 'start':
          return this.readonly.start;
        case 'end':
          return this.readonly.end;
      }
    } else {
      return this.readonly;
    }
  }
 
  /** @ignore */
  protected isDisabled(): boolean {
    return this.disabled ?? false;
  }
 
  /** @ignore */
  protected isRequired(): boolean {
    return this.required ?? false;
  }
 
  /** @ignore */
  private resizeObserverCallback(): void {
    this.verticalMode = this.el.nativeElement.clientWidth < 380;
    this.paddingBottom = this.infoContainerEl.nativeElement.clientHeight;
  }
 
  /** @ignore */
  private computeLabelPositioning() {
    const lineHeight = Number.parseFloat(
      getComputedStyle(this.labelEl.nativeElement).lineHeight,
    );
    const labelVerticalOffset =
      Math.ceil(
        (this.labelEl.nativeElement.getBoundingClientRect().height -
          lineHeight) /
          10,
      ) * 10;
    const lastCharacterOffsetLeft =
      this.lastCharacterPositionEl.nativeElement.offsetLeft;
    const maxLabelWidth = this.startSlotEl.nativeElement.clientWidth;
 
    if (
      this.labelPositionOffset !== labelVerticalOffset ||
      this.labelTopBorderLeftPos !== lastCharacterOffsetLeft ||
      this.labelMaxWidth !== maxLabelWidth
    ) {
      this.zone.run(() => {
        this.labelPositionOffset = labelVerticalOffset;
        this.labelTopBorderLeftPos = lastCharacterOffsetLeft;
        this.labelMaxWidth = maxLabelWidth;
      });
    }
  }
}