Skip to content

Commit a823037

Browse files
committed
Linting fixes/add /input type check message
1 parent 7dc4741 commit a823037

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# FctrlxAngularFileToBase64
1+
# FctrlxFileToBase64
22
--- TODO ---
33

44
## Build

src/lib/converted.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface Converted {
2-
name: string;
3-
size: number;
4-
type: string;
5-
base64?: string;
2+
name: string;
3+
size: number;
4+
type: string;
5+
base64?: string;
66
}

src/lib/fctrlx-file-to-base64.directive.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
1-
import { Directive, ElementRef, EventEmitter, Input, Output, OnInit, OnDestroy } from '@angular/core';
1+
import {
2+
Directive,
3+
ElementRef,
4+
EventEmitter,
5+
Input,
6+
Output,
7+
OnInit,
8+
OnDestroy,
9+
} from '@angular/core';
210
import { Converted } from './converted';
311

412
@Directive({
5-
selector: '[fileToBase64]'
13+
selector: '[fileToBase64]',
614
})
715
export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
816

917
@Input() files: any;
1018
@Input() type: string;
11-
@Input() multiple: any;
19+
@Input() multiple: undefined | null | string | boolean;
1220

1321
@Output() filesChange: EventEmitter<any> = new EventEmitter();
1422

1523
private readonly TYPE_FILE: string = 'file';
1624

17-
private converted: Array<Converted> = [];
25+
private converted: Converted[] = [];
1826
private currentIndex: number = 0;
1927

2028
constructor(private element: ElementRef) {}
2129

2230
ngOnInit(): void {
23-
if(this.type === this.TYPE_FILE) {
31+
if (this.type === this.TYPE_FILE && this.isSupported) {
2432
this.element.nativeElement.addEventListener('change', this.filesChanged.bind(this), false);
25-
}
26-
}
33+
} else {
34+
let msg: string = 'fileToBase64 ';
2735

28-
get isMultiple(): boolean {
29-
return !(typeof this.multiple === 'undefined');
36+
if (!this.isSupported) {
37+
msg += 'is not supported by your browser.';
38+
} else {
39+
msg += 'working only with input type=file.';
40+
}
41+
42+
console.warn(msg, this.element.nativeElement);
43+
}
3044
}
3145

3246
filesChanged(event: Event): void {
@@ -35,17 +49,20 @@ export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
3549
this.converted = [];
3650
this.currentIndex = 0;
3751

38-
Object.keys(files).forEach( (key: string) => {
52+
Object.keys(files).forEach((key: string) => {
3953
const reader = new FileReader();
40-
reader.onload = (file) => this.storeBase64(file);
54+
55+
reader.onload = (file) => {
56+
this.storeBase64(file);
57+
};
4158

4259
const { name, size, type, base64 } = files[key];
4360

4461
this.converted.push({
4562
name,
4663
size,
4764
type,
48-
base64
65+
base64,
4966
});
5067

5168
reader.readAsDataURL(files[key]);
@@ -56,7 +73,16 @@ export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
5673

5774
storeBase64(file: { target }) {
5875
this.converted[this.currentIndex].base64 = file.target.result;
59-
this.currentIndex++;
76+
this.currentIndex = this.currentIndex + 1;
77+
}
78+
79+
get isMultiple(): boolean {
80+
return !(typeof this.multiple === 'undefined');
81+
}
82+
83+
get isSupported(): boolean {
84+
return !!((window as any).File && (window as any).FileReader &&
85+
(window as any).FileList && window.Blob);
6086
}
6187

6288
ngOnDestroy(): void {

tslint.json

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
11
{
2-
"extends": "../../tslint.json",
3-
"rules": {
4-
"directive-selector": [
5-
true,
6-
"attribute",
7-
"lib",
8-
"camelCase"
9-
],
10-
"component-selector": [
11-
true,
12-
"element",
13-
"lib",
14-
"kebab-case"
15-
]
16-
}
2+
"extends": "../../tslint.json"
173
}

0 commit comments

Comments
 (0)