@ohayojp.com/components
Version:
Common business components of ohayojp.
259 lines (253 loc) • 7.93 kB
JavaScript
import { __awaiter } from 'tslib';
import { EventEmitter, Directive, ElementRef, Input, Output, NgModule } from '@angular/core';
import { _HttpClient, OhayoThemeModule } from '@ohayojp.com/theme';
import { saveAs } from 'file-saver';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* Generated from: down-file.directive.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DownFileDirective {
/**
* @param {?} el
* @param {?} _http
*/
constructor(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 {?} */
let 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 {?}
*/
getDisposition(data) {
/** @type {?} */
const arr = (data || '')
.split(';')
.filter((/**
* @param {?} i
* @return {?}
*/
i => i.includes('=')))
.map((/**
* @param {?} v
* @return {?}
*/
v => {
/** @type {?} */
const strArr = v.split('=');
/** @type {?} */
const utfId = `UTF-8''`;
/** @type {?} */
let value = strArr[1];
if (value.startsWith(utfId))
value = value.substr(utfId.length);
return { [strArr[0].trim()]: value };
}));
return arr.reduce((/**
* @param {?} _o
* @param {?} item
* @return {?}
*/
(_o, item) => item), {});
}
/**
* @private
* @param {?} status
* @return {?}
*/
setDisabled(status) {
/** @type {?} */
const el = this.el.nativeElement;
el.disabled = status;
el.classList[status ? 'add' : 'remove'](`down-file__disabled`);
}
/**
* @param {?} ev
* @return {?}
*/
_click(ev) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.isFileSaverSupported || (typeof this.pre === 'function' && !(yield this.pre(ev)))) {
ev.stopPropagation();
ev.preventDefault();
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 {?}
*/
(res) => {
if (res.status !== 200 || (/** @type {?} */ (res.body)).size <= 0) {
this.error.emit(res);
return;
}
/** @type {?} */
const disposition = this.getDisposition(res.headers.get('content-disposition'));
/** @type {?} */
let 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 {?}
*/
(err) => this.error.emit(err)), (/**
* @return {?}
*/
() => this.setDisabled(false)));
});
}
}
DownFileDirective.decorators = [
{
type: Directive, args: [{
selector: '[down-file]',
exportAs: 'downFile',
host: {
'(click)': '_click($event)',
},
},]
}
];
/** @nocollapse */
DownFileDirective.ctorParameters = () => [
{ 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',] }],
pre: [{ type: Input }],
success: [{ type: Output }],
error: [{ type: Output }]
};
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.pre;
/**
* 成功回调
* @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,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const DIRECTIVES = [DownFileDirective];
class DownFileModule {
}
DownFileModule.decorators = [
{
type: NgModule, args: [{
imports: [CommonModule, OhayoThemeModule],
declarations: [...DIRECTIVES],
exports: [...DIRECTIVES],
},]
}
];
/**
* @fileoverview added by tsickle
* Generated from: public_api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: downFile.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { DownFileDirective, DownFileModule };
//# sourceMappingURL=downFile.js.map