All files / ip-input si-ip6-input.directive.ts

81.81% Statements 9/11
0% Branches 0/1
100% Functions 3/3
80% Lines 8/10

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                                                                                                          1x       30x 33x       381x       319x 319x           319x             319x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { computed, Directive } from '@angular/core';
import {
  AbstractControl,
  ControlValueAccessor,
  NG_VALIDATORS,
  NG_VALUE_ACCESSOR,
  ValidationErrors,
  Validator
} from '@angular/forms';
 
import { splitIpV6Sections } from './address-utils';
import { ipV6Validator } from './address-validators';
import { AddrInputEvent, SiIpInputDirective } from './si-ip-input.directive';
 
/**
 * Directive for IPv6 address input fields.
 *
 * Usage:
 *
 * ```ts
 * import { SiFormItemComponent } from '@siemens/element-ng/form';
 * import { SiIp6InputDirective } from '@siemens/element-ng/ip-input';
 *
 * @Component({
 *   template: `
 *     <si-form-item label="IPv6 address">
 *       <input type="text" class="form-control" siIpV6 />
 *     </si-form-item>
 *   `,
 *   imports: [SiFormItemComponent, SiIp6InputDirective, ...]
 * })
 * ```
 */
@Directive({
  selector: 'input[siIpV6]',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: SiIp6InputDirective,
      multi: true
    },
    {
      provide: NG_VALIDATORS,
      useExisting: SiIp6InputDirective,
      multi: true
    }
  ],
  exportAs: 'siIpV6'
})
export class SiIp6InputDirective
  extends SiIpInputDirective
  implements ControlValueAccessor, Validator
{
  protected readonly validatorFn = computed(() =>
    ipV6Validator({ zeroCompression: true, cidr: this.cidr() })
  );
 
  validate(control: AbstractControl): ValidationErrors | null {
    return this.validatorFn()(control);
  }
 
  protected maskInput(e: AddrInputEvent): void {
    const { value, pos, type } = e;
    Iif (!value) {
      this.renderer.setProperty(this.inputEl, 'value', '');
      return;
    }
 
    // TODO: Restore cursor position
    const ipv6 = splitIpV6Sections({
      type,
      input: value,
      pos,
      zeroCompression: true,
      cidr: this.cidr()
    });
    this.renderer.setProperty(this.inputEl, 'value', ipv6.value);
  }
}