/** * Copyright (c) Microblink Ltd. All rights reserved. */ import { Component, Element, Event, EventEmitter, Host, h, Method, Prop } from '@stencil/core'; import { setWebComponentParts, extractFilenameFromPath } from '../../../utils/generic.helpers'; @Component({ tag: 'mb-image-box', styleUrl: 'mb-image-box.scss', shadow: true, }) export class MbImageBox { private scanFromImageInput: HTMLInputElement; private ctaLabel: HTMLParagraphElement; private addIcon: SVGElement; private removeIcon: SVGElement; private hasImage: boolean = false; /** * Text which represents name of the image. */ @Prop() boxTitle: string; /** * Text which should be displayed inside 'Add image' anchor element when file * is not selected. */ @Prop() anchorText: string; /** * Event which is triggered when selected image file is changed. */ @Event() imageChange: EventEmitter; /** * Host element as variable for manipulation */ @Element() hostEl: HTMLElement; componentDidLoad() { setWebComponentParts(this.hostEl); } componentDidRender() { this.ctaLabel.innerText = this.anchorText; } /** * Clear input image. */ @Method() async clear() { this.onClearImage(); } private onFromImageClicked(ev: UIEvent) { ev.preventDefault(); ev.stopPropagation(); if (this.hasImage) { this.onClearImage(); } else { this.scanFromImageInput.click(); } } private onImageChange(ev: UIEvent) { const target = ev.target as HTMLInputElement; if (target.files && target.files.length) { this.ctaLabel.innerText = extractFilenameFromPath(target.value); this.ctaLabel.classList.add('filename'); this.addIcon.classList.remove('visible'); this.removeIcon.classList.add('visible'); this.hasImage = true; this.imageChange.emit(target.files); } else { this.onClearImage(); } } private onClearImage() { this.ctaLabel.innerText = this.anchorText; this.ctaLabel.classList.remove('filename'); this.addIcon.classList.add('visible'); this.removeIcon.classList.remove('visible'); this.hasImage = false; this.scanFromImageInput.value = ""; this.imageChange.emit(); } render() { return (

{this.boxTitle}

this.scanFromImageInput = el as HTMLInputElement } type="file" accept="image/*" onChange={ (ev: UIEvent) => this.onImageChange(ev) } />
); } }