aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/components/ui/file-opener/file-opener.component.ts
blob: 1d1d4cef0dcff6744b0c718768366b68bda7d657 (plain)
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
import {Component, Input, Output, EventEmitter, SimpleChanges} from "@angular/core";

@Component({
    selector: 'file-opener',
    templateUrl: './file-opener.component.html',
    styleUrls: ['./file-opener.component.less']
})
export class FileOpenerComponent {
    @Input() public testsId: string;
    @Input() public extensions: string;
    @Output() public onFileUpload: EventEmitter<any>;

    public extensionsWithDot: string;

    constructor() {
        this.onFileUpload = new EventEmitter<any>();
    }

    public ngOnChanges(changes:SimpleChanges) {
        if (changes.extensions) {
            this.extensionsWithDot = this.getExtensionsWithDot(changes.extensions.currentValue);
        }
    }

    public onFileSelect(event) {
        const importFile:any = event.target.files[0];
        const reader = new FileReader();
        reader.readAsBinaryString(importFile);
        reader.onload = () => {
            this.onFileUpload.emit({
                filename: importFile.name,
                filetype: importFile.type,
                filesize: importFile.size,
                base64: btoa(reader.result)
            });
        };
    }

    public getExtensionsWithDot(extensions:string):string {
        extensions = extensions || this.extensions || '';
        return extensions.split(',')
            .map(ext => '.' + ext.toString())
            .join(',');
    }
}