Skip to content

Commit f2974aa

Browse files
committed
Moved functionality to base class(new directives will be based on it)
1 parent e129992 commit f2974aa

File tree

3 files changed

+87
-69
lines changed

3 files changed

+87
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fctrlx-file-to-base64",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"peerDependencies": {
55
"@angular/common": "^7.2.0",
66
"@angular/core": "^7.2.0"

src/lib/Base.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {
2+
ElementRef,
3+
EventEmitter,
4+
OnDestroy,
5+
OnInit,
6+
} from '@angular/core';
7+
import { Converted } from './converted';
8+
9+
export class Base implements OnInit, OnDestroy {
10+
public type: string;
11+
public multiple: undefined | null | string | boolean;
12+
public filesChange: EventEmitter<any>;
13+
14+
private readonly TYPE_FILE: string = 'file';
15+
private readonly directiveName: string;
16+
17+
private element: ElementRef;
18+
private converted: Converted[] = [];
19+
private currentIndex: number = 0;
20+
21+
constructor(name: string, element: ElementRef) {
22+
this.directiveName = name;
23+
this.element = element;
24+
}
25+
26+
ngOnInit(): void {
27+
if (this.type === this.TYPE_FILE && this.isSupported) {
28+
this.element.nativeElement.addEventListener('change', this.filesChanged.bind(this), false);
29+
} else {
30+
let msg: string = this.directiveName;
31+
32+
if (!this.isSupported) {
33+
msg += ' is not supported by your browser.';
34+
} else {
35+
msg += ' working only with input type=file.';
36+
}
37+
38+
console.warn(msg, this.element.nativeElement);
39+
}
40+
}
41+
42+
filesChanged(event: Event, readerHandleFn: string, saveKey: string): void {
43+
const files = (<HTMLInputElement>event.target).files;
44+
45+
this.converted = [];
46+
this.currentIndex = 0;
47+
48+
Object.keys(files).forEach((key: string) => {
49+
const reader = new FileReader();
50+
const { name, size, type } = files[key];
51+
52+
reader.onload = (file) => {
53+
this.store(file, saveKey);
54+
};
55+
56+
this.converted.push({ name, size, type });
57+
58+
reader[readerHandleFn](files[key]);
59+
});
60+
61+
this.filesChange.next(this.isMultiple ? this.converted : this.converted[0]);
62+
}
63+
64+
store(file: { target }, key: string): void {
65+
this.converted[this.currentIndex][key] = file.target.result;
66+
this.currentIndex = this.currentIndex + 1;
67+
}
68+
69+
ngOnDestroy(): void {
70+
this.element.nativeElement.removeEventListener('change', this.filesChanged.bind(this), false);
71+
}
72+
73+
get isSupported(): boolean {
74+
return !!((window as any).File && (window as any).FileReader &&
75+
(window as any).FileList && window.Blob);
76+
}
77+
78+
get isMultiple(): boolean {
79+
return !(typeof this.multiple === 'undefined');
80+
}
81+
}

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

Lines changed: 5 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,25 @@ import {
44
EventEmitter,
55
Input,
66
Output,
7-
OnInit,
8-
OnDestroy,
97
} from '@angular/core';
10-
import { Converted } from './converted';
8+
import { Base } from './Base';
119

1210
@Directive({
1311
selector: '[fileToBase64]',
1412
})
15-
export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
13+
export class FctrlxFileToBase64Directive extends Base {
1614

1715
@Input() files: any;
1816
@Input() type: string;
1917
@Input() multiple: undefined | null | string | boolean;
2018

2119
@Output() filesChange: EventEmitter<any> = new EventEmitter();
2220

23-
private readonly TYPE_FILE: string = 'file';
24-
25-
private converted: Converted[] = [];
26-
private currentIndex: number = 0;
27-
28-
constructor(private element: ElementRef) {}
29-
30-
ngOnInit(): void {
31-
if (this.type === this.TYPE_FILE && this.isSupported) {
32-
this.element.nativeElement.addEventListener('change', this.filesChanged.bind(this), false);
33-
} else {
34-
let msg: string = 'fileToBase64 ';
35-
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-
}
21+
constructor(element: ElementRef) {
22+
super('fileToBase64', element);
4423
}
4524

4625
filesChanged(event: Event): void {
47-
const files = (<HTMLInputElement>event.target).files;
48-
49-
this.converted = [];
50-
this.currentIndex = 0;
51-
52-
Object.keys(files).forEach((key: string) => {
53-
const reader = new FileReader();
54-
55-
reader.onload = (file) => {
56-
this.storeBase64(file);
57-
};
58-
59-
const { name, size, type, base64 } = files[key];
60-
61-
this.converted.push({
62-
name,
63-
size,
64-
type,
65-
base64,
66-
});
67-
68-
reader.readAsDataURL(files[key]);
69-
});
70-
71-
this.filesChange.next(this.isMultiple ? this.converted : this.converted[0]);
72-
}
73-
74-
storeBase64(file: { target }) {
75-
this.converted[this.currentIndex].base64 = file.target.result;
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);
86-
}
87-
88-
ngOnDestroy(): void {
89-
this.element.nativeElement.removeEventListener('change', this.filesChanged.bind(this), false);
26+
super.filesChanged(event, 'readAsDataURL', 'base64');
9027
}
9128
}

0 commit comments

Comments
 (0)