All files / app/formly/qr-scanner qr-scanner.component.ts

38.88% Statements 28/72
25.92% Branches 14/54
30.43% Functions 7/23
35.71% Lines 20/56

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  13x                     13x 13x 13x               13x           2x   2x       2x   2x     2x 2x       2x                                                                       2x 2x         2x               4x 4x 4x 4x       2x                                                          
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { TemplatePortal } from '@angular/cdk/portal';
import {
  Component,
  EventEmitter,
  Input,
  OnDestroy,
  Output,
  TemplateRef,
  ViewChild,
  ViewContainerRef
} from '@angular/core';
import { loadImage } from '../../util/image';
import { QrScanner, scanImage } from '../../util/qr-scanner';
import { Camera, hasCamera, listCameras } from '../../util/webcam';
 
@Component({
  selector: 'app-qr-scanner',
  templateUrl: './qr-scanner.component.html',
  styleUrls: ['./qr-scanner.component.scss'],
  host: { 'class': 'form-array' }
})
export class QrScannerComponent implements OnDestroy {
 
  @ViewChild('video')
  video!: TemplateRef<HTMLVideoElement>;
 
  @Input()
  upload = true;
  @Output()
  data = new EventEmitter<string>();
 
  scanner?: QrScanner;
  overlayRef?: OverlayRef;
  hasFlash = false;
  cameras?: Camera[];
  checkedCamera = false;
 
  constructor(
    private viewContainerRef: ViewContainerRef,
    private overlay: Overlay,
  ) { }
 
  ngOnDestroy() {
    this.stopScanQr();
  }
 
  readQr(files?: FileList) {
    if (!files || !files.length) return;
    const file = files[0]!;
    loadImage(file)
      .then(image => scanImage(image))
      .then(qr => qr?.data && this.data.next(qr.data));
  }
 
  scanQr() {
    if (this.scanner) {
      this.stopScanQr();
      return;
    }
    document.documentElement.style.overflowY = 'auto';
    this.overlayRef = this.overlay.create({
      height: '100vh',
      width: '100vw',
      positionStrategy: this.overlay.position().global().centerHorizontally().centerVertically(),
      hasBackdrop: true,
    });
    this.overlayRef.attach(new TemplatePortal(this.video, this.viewContainerRef));
    this.scanner ||= new QrScanner(this.overlayRef.overlayElement.firstElementChild as HTMLVideoElement, data => {
      if (data) this.data.next(data);
      this.stopScanQr();
    }, this.camera);
 
    this.scanner?.start()
      .then(() => listCameras().then(value => this.cameras = value))
      .then(() => this.scanner?.hasFlash())
      .then(value => this.hasFlash = !!value);
  }
 
  stopScanQr() {
    document.documentElement.style.overflowY = 'scroll';
    Eif (!this.scanner) return;
    this.scanner.stop();
    this.scanner.destroy();
    this.scanner = undefined;
    this.overlayRef?.detach();
    this.overlayRef?.dispose();
  }
 
  get hasMultipleCameras() {
    return (this.cameras?.length || 0) > 1;
  }
 
  get hasCamera() {
    Iif (localStorage.getItem('hasCamera') === 'true') return true;
    if (!this.checkedCamera) hasCamera().then(value => this.hasCamera = value);
    this.checkedCamera = true;
    return false;
  }
 
  set hasCamera(value: boolean) {
    localStorage.setItem('hasCamera', ''+value);
  }
 
  get camera() {
    return localStorage.getItem('cameraId')!;
  }
 
  set camera(id: string | undefined) {
    localStorage.setItem('cameraId', id!);
    if (id) {
      this.scanner?.setCamera(id)
        .then(() => this.scanner?.hasFlash())
        .then(value => this.hasFlash = !!value);
    }
  }
 
  nextCamera() {
    listCameras()
      .then(cameras => {
        if (!cameras.length) return;
        const cameraId = this.camera;
        for (let i = 0; i < cameras.length; i++) {
          if (!cameraId) return this.camera = cameras[i].id;
          if (cameraId === cameras[i].id) return this.camera = cameras[(i + 1) % cameras.length].id;
        }
        return this.camera = cameras[0].id;
      });
  }
}