@rws-framework/components
Version:
87 lines (62 loc) • 2.13 kB
text/typescript
import { observable, attr } from '@microsoft/fast-element';
import { RWSViewComponent, RWSView } from '@rws-framework/client';
('rws-uploader')
class RWSUploader extends RWSViewComponent {
uploadProgress: number;
uploadedFile: File | null = null;
chosenFile: File | null = null;
uploadParams: any;
onFinish: (uploadResponse: any) => void;
onStart: (chosenFile: File | null, context: any) => Promise<unknown> = async (chosenFile: File) => chosenFile;
onProgress: (progress: number) => void = (progress: number) => null;
async onUploadStart(): Promise<void>
{
if(!this.uploadProgress){
this.uploadProgress = 0;
}
const response = await this.onStart(this.chosenFile, this);
if(response === null){
return;
}
this.onFinish(response);
this.uploadedFile = this.chosenFile;
this.chosenFile = null;
}
onChoose(): void
{
const _self = this;
const fileInput = this.createFileInput();
this.triggerUpload(fileInput);
fileInput.addEventListener('change', () => {
if(fileInput.files?.length){
_self.chosenFile = fileInput.files[0];
_self.uploadedFile = null;
_self.removeFileInput(fileInput);
}
});
}
removeFile(){
this.chosenFile = null;
}
private createFileInput(): HTMLInputElement
{
const fileInput: HTMLInputElement = document.createElement('input');
fileInput.type = 'file';
fileInput.style.display = 'none';
this.shadowRoot?.appendChild(fileInput);
return fileInput;
}
private triggerUpload(fileInput: HTMLInputElement): void
{
fileInput.click();
}
private removeFileInput(fileInput: HTMLInputElement): void
{
this.shadowRoot?.removeChild(fileInput);
}
uploadProgressChanged(oldV: any, newV: any)
{
}
}
RWSUploader.defineComponent();
export { RWSUploader };