angular-file-picker-fixed
Version:
A simple file picker for Angular
249 lines (244 loc) • 7.96 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common')) :
typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/common'], factory) :
(factory((global['angular-file-picker-fixed'] = {}),global.ng.core,global.ng.common));
}(this, (function (exports,core,common) { 'use strict';
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 core.EventEmitter();
this.readStart = new core.EventEmitter();
this.readEnd = new core.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: core.Directive, args: [{
selector: '[ngFilePicker]'
},] },
];
/**
* @nocollapse
*/
FilePickerDirective.ctorParameters = function () { return [
{ type: core.ElementRef, },
{ type: core.Renderer2, },
]; };
FilePickerDirective.propDecorators = {
'accept': [{ type: core.Input },],
'multiple': [{ type: core.Input },],
'readMode': [{ type: core.Input, args: ['ngFilePicker',] },],
'filePick': [{ type: core.Output },],
'readStart': [{ type: core.Output },],
'readEnd': [{ type: core.Output },],
'browse': [{ type: core.HostListener, args: ['click',] },],
};
/**
* @param {?} value
* @return {?}
*/
function coerceBooleanProperty(value) {
return value != null && "" + value !== 'false';
}
var AngularFilePickerModule = (function () {
function AngularFilePickerModule() {
}
return AngularFilePickerModule;
}());
AngularFilePickerModule.decorators = [
{ type: core.NgModule, args: [{
imports: [
common.CommonModule
],
declarations: [FilePickerDirective],
exports: [FilePickerDirective]
},] },
];
/**
* @nocollapse
*/
AngularFilePickerModule.ctorParameters = function () { return []; };
exports.AngularFilePickerModule = AngularFilePickerModule;
exports.FilePickerDirective = FilePickerDirective;
exports.ReadMode = ReadMode;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=angular-file-picker-fixed.umd.js.map