UNPKG

ng2-uploader

Version:
87 lines (86 loc) 3.53 kB
"use strict"; var core_1 = require('@angular/core'); var ng2_uploader_1 = require('../services/ng2-uploader'); var classes_1 = require('../classes'); var NgFileSelectDirective = (function () { function NgFileSelectDirective(el, uploader) { this.el = el; this.uploader = uploader; this.onUpload = new core_1.EventEmitter(); this.onPreviewData = new core_1.EventEmitter(); this.onUploadRejected = new core_1.EventEmitter(); this.beforeUpload = new core_1.EventEmitter(); this.files = []; } NgFileSelectDirective.prototype.ngOnChanges = function () { var _this = this; if (!this.options) { return; } this.uploader.setOptions(new classes_1.Ng2UploaderOptions(this.options)); this.uploader._emitter.subscribe(function (data) { _this.onUpload.emit(data); if (data.done) { _this.files = _this.files.filter(function (f) { return f.name !== data.originalName; }); } }); this.uploader._previewEmitter.subscribe(function (data) { _this.onPreviewData.emit(data); }); this.uploader._beforeEmitter.subscribe(function (uploadingFile) { _this.beforeUpload.emit(uploadingFile); }); if (this.events instanceof core_1.EventEmitter) { this.events.subscribe(function (data) { if (data === 'startUpload') { _this.uploader.uploadFilesInQueue(); } }); } }; NgFileSelectDirective.prototype.onChange = function () { var _this = this; this.files = this.el.nativeElement.files; if (!this.files) { console.log('return'); return; } if (this.options.filterExtensions && this.options.allowedExtensions) { this.files = this.files.filter(function (f) { if (_this.options.allowedExtensions.indexOf(f.type) !== -1) { return true; } var ext = f.name.split('.').pop(); if (_this.options.allowedExtensions.indexOf(ext) !== -1) { return true; } _this.onUploadRejected.emit({ file: f, reason: classes_1.UploadRejected.EXTENSION_NOT_ALLOWED }); return false; }); } if (this.files.length) { this.uploader.addFilesToQueue(this.files); } }; NgFileSelectDirective.decorators = [ { type: core_1.Directive, args: [{ selector: '[ngFileSelect]' },] }, ]; /** @nocollapse */ NgFileSelectDirective.ctorParameters = function () { return [ { type: core_1.ElementRef, decorators: [{ type: core_1.Inject, args: [core_1.ElementRef,] },] }, { type: ng2_uploader_1.Ng2UploaderService, decorators: [{ type: core_1.Inject, args: [ng2_uploader_1.Ng2UploaderService,] },] }, ]; }; NgFileSelectDirective.propDecorators = { 'options': [{ type: core_1.Input },], 'events': [{ type: core_1.Input },], 'onUpload': [{ type: core_1.Output },], 'onPreviewData': [{ type: core_1.Output },], 'onUploadRejected': [{ type: core_1.Output },], 'beforeUpload': [{ type: core_1.Output },], 'onChange': [{ type: core_1.HostListener, args: ['change',] },], }; return NgFileSelectDirective; }()); exports.NgFileSelectDirective = NgFileSelectDirective;