UNPKG

@lxlib/seed

Version:

This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.

259 lines (253 loc) 7.55 kB
import { EventEmitter, Directive, ElementRef, Input, Output, NgModule } from '@angular/core'; import { _HttpClient, LxlibThemeModule } from '@lxlib/theme'; import { saveAs } from 'file-saver'; import { __spread } from 'tslib'; import { CommonModule } from '@angular/common'; /** * @fileoverview added by tsickle * Generated from: down-file.directive.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var DownFileDirective = /** @class */ (function () { function DownFileDirective(el, _http) { this.el = el; this._http = _http; this.isFileSaverSupported = true; /** * 请求类型 */ this.httpMethod = 'get'; /** * 成功回调 */ // tslint:disable-next-line:no-output-native this.success = new EventEmitter(); /** * 错误回调 */ // tslint:disable-next-line:no-output-native this.error = new EventEmitter(); /** @type {?} */ var isFileSaverSupported = false; try { isFileSaverSupported = !!new Blob(); } catch (_a) { } this.isFileSaverSupported = isFileSaverSupported; if (!isFileSaverSupported) { el.nativeElement.classList.add("down-file__not-support"); } } /** * @private * @param {?} data * @return {?} */ DownFileDirective.prototype.getDisposition = /** * @private * @param {?} data * @return {?} */ function (data) { /** @type {?} */ var arr = (data || '') .split(';') .filter((/** * @param {?} i * @return {?} */ function (i) { return i.includes('='); })) .map((/** * @param {?} v * @return {?} */ function (v) { var _a; /** @type {?} */ var strArr = v.split('='); /** @type {?} */ var utfId = "UTF-8''"; /** @type {?} */ var value = strArr[1]; if (value.startsWith(utfId)) value = value.substr(utfId.length); return _a = {}, _a[strArr[0].trim()] = value, _a; })); return arr.reduce((/** * @param {?} _o * @param {?} item * @return {?} */ function (_o, item) { return item; }), {}); }; /** * @private * @param {?} status * @return {?} */ DownFileDirective.prototype.setDisabled = /** * @private * @param {?} status * @return {?} */ function (status) { /** @type {?} */ var el = this.el.nativeElement; el.disabled = status; el.classList[status ? 'add' : 'remove']("down-file__disabled"); }; /** * @return {?} */ DownFileDirective.prototype._click = /** * @return {?} */ function () { var _this = this; if (!this.isFileSaverSupported) { return; } this.setDisabled(true); this._http .request(this.httpMethod, this.httpUrl, { params: this.httpData || {}, responseType: 'blob', observe: 'response', body: this.httpBody, }) .subscribe((/** * @param {?} res * @return {?} */ function (res) { if (res.status !== 200 || (/** @type {?} */ (res.body)).size <= 0) { _this.error.emit(res); return; } /** @type {?} */ var disposition = _this.getDisposition(res.headers.get('content-disposition')); /** @type {?} */ var fileName = _this.fileName; if (typeof fileName === 'function') fileName = fileName(res); fileName = fileName || disposition["filename*"] || disposition["filename"] || res.headers.get('filename') || res.headers.get('x-filename'); saveAs((/** @type {?} */ (res.body)), decodeURI((/** @type {?} */ (fileName)))); _this.success.emit(res); }), (/** * @param {?} err * @return {?} */ function (err) { return _this.error.emit(err); }), (/** * @return {?} */ function () { return _this.setDisabled(false); })); }; DownFileDirective.decorators = [ { type: Directive, args: [{ selector: '[down-file]', exportAs: 'downFile', host: { '(click)': '_click()', }, },] } ]; /** @nocollapse */ DownFileDirective.ctorParameters = function () { return [ { type: ElementRef }, { type: _HttpClient } ]; }; DownFileDirective.propDecorators = { httpData: [{ type: Input, args: ['http-data',] }], httpBody: [{ type: Input, args: ['http-body',] }], httpMethod: [{ type: Input, args: ['http-method',] }], httpUrl: [{ type: Input, args: ['http-url',] }], fileName: [{ type: Input, args: ['file-name',] }], success: [{ type: Output }], error: [{ type: Output }] }; return DownFileDirective; }()); if (false) { /** * @type {?} * @private */ DownFileDirective.prototype.isFileSaverSupported; /** * URL请求参数 * @type {?} */ DownFileDirective.prototype.httpData; /** * URL请求参数 * @type {?} */ DownFileDirective.prototype.httpBody; /** * 请求类型 * @type {?} */ DownFileDirective.prototype.httpMethod; /** * 下载地址 * @type {?} */ DownFileDirective.prototype.httpUrl; /** * 指定文件名,若为空从服务端返回的 `header` 中获取 `filename`、`x-filename` * @type {?} */ DownFileDirective.prototype.fileName; /** * 成功回调 * @type {?} */ DownFileDirective.prototype.success; /** * 错误回调 * @type {?} */ DownFileDirective.prototype.error; /** * @type {?} * @private */ DownFileDirective.prototype.el; /** * @type {?} * @private */ DownFileDirective.prototype._http; } /** * @fileoverview added by tsickle * Generated from: down-file.module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var DIRECTIVES = [DownFileDirective]; var DownFileModule = /** @class */ (function () { function DownFileModule() { } DownFileModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule, LxlibThemeModule], declarations: __spread(DIRECTIVES), exports: __spread(DIRECTIVES), },] } ]; return DownFileModule; }()); /** * @fileoverview added by tsickle * Generated from: public_api.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * Generated from: downFile.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { DownFileDirective, DownFileModule }; //# sourceMappingURL=downFile.js.map