This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Magyarodi
authored and
Mark Magyarodi
committed
Aug 29, 2016
1 parent
0dfbb5b
commit 63ff0da
Showing
8 changed files
with
110 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './src/interfaces'; | ||
export * from './src/image-upload'; | ||
export * from './src/image-upload.directive'; | ||
export * from './src/image-upload.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,59 @@ | ||
import {Directive, ElementRef, | ||
OnInit, Input, Output, EventEmitter, Renderer} from '@angular/core'; | ||
|
||
import {ImageResult, ResizeOptions} from './interfaces'; | ||
import {createImage, resizeImage} from './utils'; | ||
|
||
@Directive({ | ||
selector: 'input[type=file][image-upload]' | ||
}) | ||
export class ImageUpload implements OnInit { | ||
|
||
@Output() imageSelected = new EventEmitter<ImageResult>(); | ||
|
||
@Input() resizeOptions: ResizeOptions; | ||
|
||
constructor(private _elementref: ElementRef, private _renderer: Renderer) { | ||
} | ||
|
||
ngOnInit() { | ||
this._renderer.listen(this._elementref.nativeElement, 'change', e => this.readFiles(e)); | ||
} | ||
|
||
private readFiles(event) { | ||
for (let file of event.target.files) { | ||
let result: ImageResult = { | ||
file: file, | ||
url: URL.createObjectURL(file) | ||
}; | ||
this.fileToDataURL(file, result).then(r => this.resize(r)).then(r => this.imageSelected.emit(r)); | ||
} | ||
} | ||
|
||
private resize(result: ImageResult): Promise<ImageResult> { | ||
return new Promise((resolve) => { | ||
if (this.resizeOptions) { | ||
createImage(result.url, image => { | ||
let dataUrl = resizeImage(image, this.resizeOptions); | ||
result.resized = { | ||
dataURL: dataUrl, | ||
type: dataUrl.match(/:(.+\/.+;)/)[1] | ||
}; | ||
resolve(result); | ||
}); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
} | ||
|
||
private fileToDataURL(file: File, result: ImageResult): Promise<ImageResult> { | ||
return new Promise((resolve) => { | ||
let reader = new FileReader(); | ||
reader.onload = function(e) { | ||
result.dataURL = reader.result; | ||
resolve(result); | ||
}; | ||
reader.readAsDataURL(file); | ||
}); | ||
} | ||
} | ||
|
||
|
||
import {Directive, ElementRef, | ||
OnInit, Input, Output, EventEmitter, Renderer, HostListener} from '@angular/core'; | ||
|
||
import {ImageResult, ResizeOptions} from './interfaces'; | ||
import {createImage, resizeImage} from './utils'; | ||
|
||
@Directive({ | ||
selector: 'input[type=file][image-upload]' | ||
}) | ||
export class ImageUploadDirective { | ||
|
||
@Output() imageSelected = new EventEmitter<ImageResult>(); | ||
|
||
@Input() resizeOptions: ResizeOptions; | ||
|
||
constructor(private _elementref: ElementRef, private _renderer: Renderer) { | ||
} | ||
|
||
@HostListener('change', ['$event']) | ||
private readFiles(event) { | ||
for (let file of event.target.files) { | ||
let result: ImageResult = { | ||
file: file, | ||
url: URL.createObjectURL(file) | ||
}; | ||
this.fileToDataURL(file, result).then(r => this.resize(r)).then(r => this.imageSelected.emit(r)); | ||
} | ||
} | ||
|
||
private resize(result: ImageResult): Promise<ImageResult> { | ||
return new Promise((resolve) => { | ||
if (this.resizeOptions) { | ||
createImage(result.url, image => { | ||
let dataUrl = resizeImage(image, this.resizeOptions); | ||
result.resized = { | ||
dataURL: dataUrl, | ||
type: dataUrl.match(/:(.+\/.+;)/)[1] | ||
}; | ||
resolve(result); | ||
}); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
} | ||
|
||
private fileToDataURL(file: File, result: ImageResult): Promise<ImageResult> { | ||
return new Promise((resolve) => { | ||
let reader = new FileReader(); | ||
reader.onload = function(e) { | ||
result.dataURL = reader.result; | ||
resolve(result); | ||
}; | ||
reader.readAsDataURL(file); | ||
}); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {NgModule} from '@angular/core'; | ||
import {ImageUploadDirective} from './image-upload.directive'; | ||
|
||
@NgModule({ | ||
declarations: [ImageUploadDirective], | ||
exports: [ImageUploadDirective] | ||
}) | ||
export class ImageUploadModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"ambientDependencies": { | ||
"es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654" | ||
"globalDependencies": { | ||
"core-js": "registry:dt/core-js#0.0.0+20160725163759" | ||
} | ||
} |