angular-file-picker-fixed
Version:
A simple file picker for Angular
241 lines (240 loc) • 7.53 kB
JavaScript
import { Directive, ElementRef, EventEmitter, HostListener, Input, NgModule, Output, Renderer2 } from '@angular/core';
import { CommonModule } from '@angular/common';
var PickedFileImpl = (function () {
/**
* @param {?} _lastModifiedDate
* @param {?} _name
* @param {?} _size
* @param {?} _type
* @param {?} _readMode
* @param {?} _content
*/
function PickedFileImpl(_lastModifiedDate, _name, _size, _type, _readMode, _content) {
this._lastModifiedDate = _lastModifiedDate;
this._name = _name;
this._size = _size;
this._type = _type;
this._readMode = _readMode;
this._content = _content;
}
Object.defineProperty(PickedFileImpl.prototype, "lastModifiedDate", {
/**
* @return {?}
*/
get: function () {
return this._lastModifiedDate;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PickedFileImpl.prototype, "name", {
/**
* @return {?}
*/
get: function () {
return this._name;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PickedFileImpl.prototype, "size", {
/**
* @return {?}
*/
get: function () {
return this._size;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PickedFileImpl.prototype, "type", {
/**
* @return {?}
*/
get: function () {
return this._type;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PickedFileImpl.prototype, "readMode", {
/**
* @return {?}
*/
get: function () {
return this._readMode;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PickedFileImpl.prototype, "content", {
/**
* @return {?}
*/
get: function () {
return this._content;
},
enumerable: true,
configurable: true
});
return PickedFileImpl;
}());
var ReadMode = {};
ReadMode.arrayBuffer = 0;
ReadMode.binaryString = 1;
ReadMode.dataURL = 2;
ReadMode.text = 3;
ReadMode[ReadMode.arrayBuffer] = "arrayBuffer";
ReadMode[ReadMode.binaryString] = "binaryString";
ReadMode[ReadMode.dataURL] = "dataURL";
ReadMode[ReadMode.text] = "text";
var FilePickerDirective = (function () {
/**
* @param {?} el
* @param {?} renderer
*/
function FilePickerDirective(el, renderer) {
this.el = el;
this.renderer = renderer;
this.accept = '';
this.filePick = new EventEmitter();
this.readStart = new EventEmitter();
this.readEnd = new EventEmitter();
}
Object.defineProperty(FilePickerDirective.prototype, "multiple", {
/**
* @return {?}
*/
get: function () { return this._multiple; },
/**
* @param {?} value
* @return {?}
*/
set: function (value) { this._multiple = coerceBooleanProperty(value); },
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
FilePickerDirective.prototype.ngOnInit = function () {
var _this = this;
this.input = this.renderer.createElement('input');
this.renderer.appendChild(this.el.nativeElement, this.input);
this.renderer.setAttribute(this.input, 'type', 'file');
this.renderer.setAttribute(this.input, 'accept', this.accept);
this.renderer.setStyle(this.input, 'display', 'none');
if (this.multiple) {
this.renderer.setAttribute(this.input, 'multiple', 'multiple');
}
this.renderer.listen(this.input, 'change', function (event) {
var /** @type {?} */ fileCount = event.target.files.length;
_this.readStart.emit(event.target.files.length);
Promise.all(Array.from(event.target.files).map(function (file) { return _this.readFile(file); }))
.then(function () { return _this.readEnd.emit(fileCount); });
});
};
/**
* @return {?}
*/
FilePickerDirective.prototype.reset = function () {
if (!this.input) {
console.error('It seems that ngOnInit() has not been executed or that the hidden input element is null. Did you mess with the DOM?');
return;
}
this.input.value = null;
};
/**
* @return {?}
*/
FilePickerDirective.prototype.browse = function () {
if (!this.input) {
console.error('It seems that ngOnInit() has not been executed or that the hidden input element is null. Did you mess with the DOM?');
return;
}
this.input.click();
};
/**
* @param {?} file
* @return {?}
*/
FilePickerDirective.prototype.readFile = function (file) {
var _this = this;
return new Promise(function (resolve, reject) {
var /** @type {?} */ reader = new FileReader();
reader.onload = function (loaded) {
var /** @type {?} */ fileReader = (loaded.target);
var /** @type {?} */ pickedFile = new PickedFileImpl(file.lastModifiedDate, file.name, file.size, file.type, _this.readMode, fileReader.result);
_this.filePick.emit(pickedFile);
resolve();
};
switch (_this.readMode) {
case ReadMode.arrayBuffer:
reader.readAsArrayBuffer(file);
break;
case ReadMode.binaryString:
reader.readAsBinaryString(file);
break;
case ReadMode.text:
reader.readAsText(file);
break;
case ReadMode.dataURL:
default:
reader.readAsDataURL(file);
break;
}
});
};
return FilePickerDirective;
}());
FilePickerDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngFilePicker]'
},] },
];
/**
* @nocollapse
*/
FilePickerDirective.ctorParameters = function () { return [
{ type: ElementRef, },
{ type: Renderer2, },
]; };
FilePickerDirective.propDecorators = {
'accept': [{ type: Input },],
'multiple': [{ type: Input },],
'readMode': [{ type: Input, args: ['ngFilePicker',] },],
'filePick': [{ type: Output },],
'readStart': [{ type: Output },],
'readEnd': [{ type: Output },],
'browse': [{ type: HostListener, args: ['click',] },],
};
/**
* @param {?} value
* @return {?}
*/
function coerceBooleanProperty(value) {
return value != null && "" + value !== 'false';
}
var AngularFilePickerModule = (function () {
function AngularFilePickerModule() {
}
return AngularFilePickerModule;
}());
AngularFilePickerModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule
],
declarations: [FilePickerDirective],
exports: [FilePickerDirective]
},] },
];
/**
* @nocollapse
*/
AngularFilePickerModule.ctorParameters = function () { return []; };
/**
* Generated bundle index. Do not edit.
*/
export { AngularFilePickerModule, FilePickerDirective, ReadMode };
//# sourceMappingURL=angular-file-picker-fixed.es5.js.map