import {css, html, LitElement} from 'lit' import '@material/mwc-button' import '@material/mwc-icon' import {translate} from '../../translate' class FragFileInput extends LitElement { static get properties () { return { accept: { type: String }, readAs: { type: String } } } static get styles () { return css` #drop-area { border: 2px dashed #ccc; font-family: "Roboto", sans-serif; padding: 20px; } #trigger:hover { cursor: pointer; } #drop-area.highlight { border-color: var(--mdc-theme-primary, #000); } p { margin-top: 0; } form { margin-bottom: 10px; } #fileInput { display: none; } ` } constructor () { super() this.readAs = this.readAs || 'Text' } render () { return html`
this.shadowRoot.getElementById('fileInput').click()} style="dispay:inline;"> cloud_upload  ${translate("fragfile.selectfile")}
${translate("fragfile.dragfile")}
` } readFile (file) { const fr = new FileReader() fr.onload = () => { this.dispatchEvent(new CustomEvent('file-read-success', { detail: { result: fr.result }, bubbles: true, composed: true })) } fr['readAs' + this.readAs](file) } firstUpdated () { this._dropArea = this.shadowRoot.getElementById('drop-area') const preventDefaults = e => { e.preventDefault() e.stopPropagation() } ;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => { this._dropArea.addEventListener(eventName, preventDefaults, false) }) const highlight = e => { this._dropArea.classList.add('highlight') } const unhighlight = e => { this._dropArea.classList.remove('highlight') } ;['dragenter', 'dragover'].forEach(eventName => { this._dropArea.addEventListener(eventName, highlight, false) }) ;['dragleave', 'drop'].forEach(eventName => { this._dropArea.addEventListener(eventName, unhighlight, false) }) this._dropArea.addEventListener('drop', e => { const dt = e.dataTransfer const file = dt.files[0] this.readFile(file) }, false) } } window.customElements.define('frag-file-input', FragFileInput)