@duoduo-oba/ng-devui
Version:
DevUI components based on Angular
552 lines (543 loc) • 20 kB
JavaScript
import { Directive, ElementRef, Input, Pipe, HostListener, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { I18nFormat } from '@duoduo-oba/ng-devui/i18n';
import { HttpParams } from '@angular/common/http';
/**
* @fileoverview added by tsickle
* Generated from: auto-focus.directive.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class AutoFocusDirective {
/**
* @param {?} elementRef
*/
constructor(elementRef) {
this.elementRef = elementRef;
}
/**
* @return {?}
*/
ngAfterViewInit() {
if (this.autoFocus) {
this.elementRef.nativeElement.focus();
}
}
}
AutoFocusDirective.decorators = [
{ type: Directive, args: [{
selector: '[dAutoFocus]',
},] }
];
/** @nocollapse */
AutoFocusDirective.ctorParameters = () => [
{ type: ElementRef }
];
AutoFocusDirective.propDecorators = {
autoFocus: [{ type: Input, args: ['dAutoFocus',] }]
};
if (false) {
/** @type {?} */
AutoFocusDirective.prototype.autoFocus;
/**
* @type {?}
* @private
*/
AutoFocusDirective.prototype.elementRef;
}
/**
* @fileoverview added by tsickle
* Generated from: date-pipe.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DatePipe {
/**
* @param {?} date
* @param {?} pattern
* @return {?}
*/
transform(date, pattern) {
if (!date) {
return;
}
return I18nFormat.formatDate(date, pattern);
}
}
DatePipe.decorators = [
{ type: Pipe, args: [{
name: 'dDatePipe'
},] }
];
/**
* @fileoverview added by tsickle
* Generated from: helper-utils.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class HelperUtils {
/**
* @param {?} url
* @param {?=} target
* @return {?}
*/
static jumpOuterUrl(url, target = '_blank') {
if (url !== undefined) {
/** @type {?} */
const tempLink = document.createElement('a');
tempLink.style.display = 'none'; // for IE 11
tempLink.target = target;
tempLink.href = url;
document.body.appendChild(tempLink); // for IE 11, IE11需要append到document.body里面的a链接才会生效
// for IE 11, IE11需要append到document.body里面的a链接才会生效
/** @type {?} */
const event = document.createEvent('MouseEvents');
event.initEvent('click', false, true);
tempLink.dispatchEvent(event);
document.body.removeChild(tempLink); // for IE 11
}
}
/**
* @param {?} url
* @param {?=} option
* @param {?=} onError
* @return {?}
*/
static downloadFile(url, option, onError) {
if (document.querySelector('iframe[name=' + (option && option.iframename || 'download') + ']')) {
document.body.removeChild(document.querySelector('iframe[name=' + (option && option.iframename || 'download') + ']'));
}
/** @type {?} */
const tempiframe = document.createElement('iframe');
tempiframe.name = option && option.iframename || 'download';
tempiframe.style.display = 'none';
/** @type {?} */
const tempform = document.createElement('form');
tempform.action = url;
tempform.method = option && option.method || 'post';
tempform.target = option && option.iframename || 'download';
tempform.enctype = option && option.enctype || 'application/x-www-form-urlencoded';
tempform.style.display = 'none';
if (option && option.params) {
Object.keys(option.params).forEach((/**
* @param {?} key
* @return {?}
*/
key => {
/** @type {?} */
const opt = document.createElement('input');
opt.name = key;
opt.value = option.params[key];
tempform.appendChild(opt);
}));
}
/** @type {?} */
const submit = document.createElement('input');
submit.type = 'submit';
tempform.appendChild(submit);
tempiframe.appendChild(tempform);
document.body.appendChild(tempiframe);
// 下载错误处理。下载成功并不会响应,因为响应头中带有Content-Disposition(下载头)的url,无法监听iframe的load事件,load事件不会触发
tempiframe.addEventListener('load', (/**
* @param {?} event
* @return {?}
*/
(event) => {
try {
/** @type {?} */
const iframeDoc = tempiframe.contentDocument;
if (onError !== undefined) {
/** @type {?} */
let response;
try {
response = JSON.parse(iframeDoc.body && iframeDoc.body.textContent);
}
catch (e) {
response = iframeDoc.body && iframeDoc.body.textContent;
}
if (!response) {
response = 'Error';
}
onError(response);
}
}
catch (e) {
onError('Error');
}
document.body.removeChild(tempiframe);
}));
tempform.submit();
}
/**
* @param {?} httpClient
* @param {?} url
* @param {?=} option
* @param {?=} onError
* @param {?=} onSuccess
* @return {?}
*/
static downloadFileByHttpClient(httpClient, url, option, onError, onSuccess) {
/** @type {?} */
const requestMethod = option && option.method && option.method.toLowerCase() || 'post';
/** @type {?} */
const requestHeaderContentType = option.enctype || 'application/x-www-form-urlencoded';
/** @type {?} */
const requestParams = option.params ? new HttpParams({
fromObject: option.params
}) : null;
/** @type {?} */
const requestUrl = url;
/** @type {?} */
const requestOptionParams = requestMethod === 'get' ? requestParams : undefined;
/** @type {?} */
const requestBody = requestMethod === 'post' ? requestParams && requestParams.toString() : undefined;
/** @type {?} */
const responseOption = option.responseOption;
/** @type {?} */
const requestOption = Object.assign({}, {
body: requestBody,
observe: 'response',
params: requestOptionParams,
headers: {
'Content-Type': requestHeaderContentType
},
responseType: 'arraybuffer'
}, {
headers: option.header
});
/** @type {?} */
const handleResponse = ((/**
* @param {?} resOption
* @return {?}
*/
resOption => {
switch (resOption) {
case 'response':
return (/**
* @param {?} res
* @return {?}
*/
(res) => res);
case 'body':
return (/**
* @param {?} res
* @return {?}
*/
(res) => {
/** @type {?} */
const arrayBuffer = (/** @type {?} */ (((/** @type {?} */ (res))).body)) || ((/** @type {?} */ (res))).error;
/** @type {?} */
const body = HelperUtils.utf8ArrayToStr(arrayBuffer);
return body;
});
case 'json':
default:
return (/**
* @param {?} res
* @return {?}
*/
(res) => {
/** @type {?} */
const arrayBuffer = (/** @type {?} */ (((/** @type {?} */ (res))).body)) || ((/** @type {?} */ (res))).error;
/** @type {?} */
let response;
try {
/** @type {?} */
const body = HelperUtils.utf8ArrayToStr(arrayBuffer);
try {
response = JSON.parse(body);
}
catch (e) {
/** @type {?} */
const parser = new DOMParser();
/** @type {?} */
const html = parser.parseFromString(body, 'text/html');
response = html.body.textContent;
}
}
catch (e) {
throw new Error('Parsing Error:' + e);
}
if (!response) {
response = 'Error';
}
return response;
});
}
}))(responseOption);
/** @type {?} */
const downloadFileFromArrayBuffer = (/**
* @param {?} data
* @param {?} filename
* @param {?} contentType
* @return {?}
*/
(data, filename, contentType) => {
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE11 support
// IE11 support
/** @type {?} */
const blob = new Blob([data], { type: contentType });
window.navigator.msSaveOrOpenBlob(blob, filename);
}
else { // other browsers
if ('download' in document.createElement('a')) {
/** @type {?} */
const blob = new Blob([data], { type: contentType });
/** @type {?} */
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
}
else {
// not support tag a download attribute use file download, filename won't support
/** @type {?} */
const file = new File([data], filename, { type: contentType });
/** @type {?} */
const exportUrl = URL.createObjectURL(file);
window.location.assign(exportUrl);
URL.revokeObjectURL(exportUrl);
}
}
});
/** @type {?} */
const getFilenameFromDisposition = (/**
* @param {?} disposition
* @return {?}
*/
(disposition) => {
/** @type {?} */
const filenamePair = disposition.split(';').filter((/**
* @param {?} str
* @return {?}
*/
str => /^filename=/.test(str.trim()))).pop();
if (filenamePair) {
/** @type {?} */
let str = filenamePair.trim();
str = str.split('=')[1];
str = str.replace(/['"]/g, '');
return decodeURIComponent(str);
}
else {
return null;
}
});
httpClient.request(requestMethod, requestUrl, requestOption).subscribe((/**
* @param {?} res
* @return {?}
*/
(res) => {
/** @type {?} */
const disposition = ((/** @type {?} */ (res.headers))).get('content-disposition');
/** @type {?} */
const contentType = ((/** @type {?} */ (res.headers))).get('content-type');
if (/^attachment/i.test(disposition) || option.downloadWithoutDispositionHeader) {
/** @type {?} */
const downloadFilename = option.filename || disposition && getFilenameFromDisposition(disposition) || url;
downloadFileFromArrayBuffer(res.body, downloadFilename, contentType);
if (onSuccess) {
onSuccess(res);
}
}
else {
if (onError) {
/** @type {?} */
let response;
try {
response = handleResponse(res);
}
catch (e) {
response = res;
}
onError(response);
}
}
}), (/**
* @param {?} err
* @return {?}
*/
err => {
if (onError) {
/** @type {?} */
let response;
try {
response = handleResponse(err);
}
catch (e) {
response = err;
}
onError(response);
}
}));
}
/**
* @private
* @param {?} arrayBuffer
* @return {?}
*/
static utf8ArrayToStr(arrayBuffer) {
if (typeof TextDecoder !== 'undefined') {
return new TextDecoder('utf-8').decode(arrayBuffer);
}
else {
/**
* fallback 方案,无TextDecoder场景使用直接解析,英文无问题,中文会存在乱码
* ie 11 下支持中文需要使用MDN推荐的 fastestsmallesttextencoderdecoder以支持TextDecoder
* npm install fastestsmallesttextencoderdecoder
* polyfill.ts includes
* ```
* // polyfill for TextDecoder on IE 11
* import 'fastestsmallesttextencoderdecoder';
* ```
*/
return String.fromCharCode.apply(null, new Uint8Array(arrayBuffer));
}
}
}
class SimulateATagDirective {
constructor() {
this.target = '_blank';
}
/**
* @return {?}
*/
onClick() {
HelperUtils.jumpOuterUrl(this.href, this.target);
}
}
SimulateATagDirective.decorators = [
{ type: Directive, args: [{
selector: '[dSimulateATag]'
},] }
];
/** @nocollapse */
SimulateATagDirective.ctorParameters = () => [];
SimulateATagDirective.propDecorators = {
href: [{ type: Input }],
target: [{ type: Input }],
onClick: [{ type: HostListener, args: ['click',] }]
};
if (false) {
/** @type {?} */
SimulateATagDirective.prototype.href;
/** @type {?} */
SimulateATagDirective.prototype.target;
}
/**
* @fileoverview added by tsickle
* Generated from: iframe-event-propagate.directive.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class IframeEventPropagateDirective {
/**
* @param {?} el
*/
constructor(el) {
this.event = 'click';
this.AddIframeContentDocumentClickListener = (/**
* @return {?}
*/
() => {
/** @type {?} */
const iframe = this.element.querySelector('iframe');
if (iframe !== null) {
if (iframe.contentDocument !== null) {
iframe.contentDocument.addEventListener(this.event, this.dispatchClickEvent);
}
else {
/** @type {?} */
const loadHandler = (/**
* @return {?}
*/
() => {
iframe.contentDocument.addEventListener(this.event, this.dispatchClickEvent);
iframe.removeEventListener('load', loadHandler);
});
iframe.addEventListener('load', loadHandler);
}
this.element.removeEventListener('DOMSubtreeModified', this.AddIframeContentDocumentClickListener);
}
});
this.dispatchClickEvent = (/**
* @param {?} $event
* @return {?}
*/
($event) => {
/** @type {?} */
const event = document.createEvent('MouseEvents');
event.initEvent(this.event, true, true);
event['originEvent'] = $event;
this.element.dispatchEvent(event);
});
this.element = el.nativeElement;
}
/**
* @return {?}
*/
ngAfterViewInit() {
this.element.addEventListener('DOMSubtreeModified', this.AddIframeContentDocumentClickListener);
if (this.element.querySelector('iframe') !== null) {
this.AddIframeContentDocumentClickListener();
}
}
}
IframeEventPropagateDirective.decorators = [
{ type: Directive, args: [{
selector: '[dIframeEventPropagate]'
},] }
];
/** @nocollapse */
IframeEventPropagateDirective.ctorParameters = () => [
{ type: ElementRef }
];
IframeEventPropagateDirective.propDecorators = {
event: [{ type: Input }]
};
if (false) {
/** @type {?} */
IframeEventPropagateDirective.prototype.event;
/** @type {?} */
IframeEventPropagateDirective.prototype.element;
/** @type {?} */
IframeEventPropagateDirective.prototype.AddIframeContentDocumentClickListener;
/** @type {?} */
IframeEventPropagateDirective.prototype.dispatchClickEvent;
}
/**
* @fileoverview added by tsickle
* Generated from: common.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DCommonModule {
}
DCommonModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
exports: [
AutoFocusDirective,
DatePipe,
SimulateATagDirective,
IframeEventPropagateDirective
],
declarations: [
AutoFocusDirective,
DatePipe,
SimulateATagDirective,
IframeEventPropagateDirective
],
providers: [],
},] }
];
/**
* @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: ng-devui-common.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { AutoFocusDirective, DCommonModule, DatePipe, HelperUtils, IframeEventPropagateDirective, SimulateATagDirective };
//# sourceMappingURL=ng-devui-common.js.map