/** * Copyright (c) Microblink Ltd. All rights reserved. */ import { Component, Element, Event, EventEmitter, Host, h, Method, Prop, State, Listen } from '@stencil/core'; import { CameraEntry } from '../../../utils/data-structures'; import * as DeviceHelpers from '../../../utils/device.helpers'; import { setWebComponentParts, classNames } from '../../../utils/generic.helpers'; @Component({ tag: 'mb-camera-toolbar', styleUrl: 'mb-camera-toolbar.scss', shadow: true }) export class MbCameraToolbar { private cameraSelection!: HTMLMbCameraSelectionElement; @State() showCloseButton: boolean = false; @State() isDesktop: boolean = DeviceHelpers.isDesktop(); /** * Set to `true` if close button should be displayed. */ @Prop() showClose: boolean = false; @Prop() clearIsCameraActive: boolean = false; /** * Whether to show 'Camera flip' button. */ @Prop() enableCameraFlip: boolean = false; /** * Whether the camera is flipped, this property will be flip the relevant icon. */ @Prop() cameraFlipped: boolean = false; /** * Emitted when camera stream becomes active. */ @Event() setIsCameraActive: EventEmitter /** * Event which is triggered when close button is clicked. */ @Event() closeEvent: EventEmitter; /** * Event which is triggered when flip camera button is clicked. */ @Event() flipEvent: EventEmitter; /** * Emitted when user selects a different camera device. */ @Event() changeCameraDevice: EventEmitter; /** * Host element as variable for manipulation */ @Element() hostEl: HTMLElement; componentDidLoad() { setWebComponentParts(this.hostEl); } connectedCallback() { window.addEventListener('resize', this.handleResize, false); this.handleResize(); } disconnectedCallback() { window.removeEventListener('resize', this.handleResize, false); } /** * Change active camera. */ @Method() async setActiveCamera(cameraId: string) { this.cameraSelection.setActiveCamera(cameraId); this.showCloseButton = this.showClose; } /** * Populate list of camera devices. */ @Method() async populateCameraDevices() { await this.cameraSelection.populateCameraDevices(); } private handleClose(ev: UIEvent) { ev.preventDefault(); ev.stopPropagation(); this.closeEvent.emit(); this.showCloseButton = false; } private handleFlip(ev: UIEvent) { ev.preventDefault(); ev.stopPropagation(); this.flipEvent.emit(); } private handleResize = () => { this.isDesktop = DeviceHelpers.isDesktop(); } private handleChangeCameraDevice(camera: CameraEntry) { this.changeCameraDevice.emit(camera); } @Listen('setIsCameraActive', { capture: true }) handleSetIsCameraActive(ev:CustomEvent) { if (ev.detail) { this.showCloseButton = this.showClose; } else { this.showCloseButton = ev.detail; } } render() { let flipButton = ''; if (this.enableCameraFlip) { flipButton = ( ) } let closeButton = ''; if (this.showCloseButton) { closeButton = ( ) } return (
{ flipButton }
) => this.handleChangeCameraDevice(ev.detail) } class={ classNames({ visible: this.isDesktop }) } ref={ el => this.cameraSelection = el as HTMLMbCameraSelectionElement } >
{ closeButton }
); } }