UNPKG

@duoduo-oba/ng-devui

Version:

DevUI components based on Angular

596 lines (587 loc) 24.4 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('ng-devui/i18n'), require('@angular/common/http')) : typeof define === 'function' && define.amd ? define('ng-devui/common', ['exports', '@angular/core', '@angular/common', 'ng-devui/i18n', '@angular/common/http'], factory) : (global = global || self, factory((global['ng-devui'] = global['ng-devui'] || {}, global['ng-devui'].common = {}), global.ng.core, global.ng.common, global['ng-devui'].i18n, global.ng.common.http)); }(this, (function (exports, core, common, i18n, http) { 'use strict'; /** * @fileoverview added by tsickle * Generated from: auto-focus.directive.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var AutoFocusDirective = /** @class */ (function () { function AutoFocusDirective(elementRef) { this.elementRef = elementRef; } /** * @return {?} */ AutoFocusDirective.prototype.ngAfterViewInit = /** * @return {?} */ function () { if (this.autoFocus) { this.elementRef.nativeElement.focus(); } }; AutoFocusDirective.decorators = [ { type: core.Directive, args: [{ selector: '[dAutoFocus]', },] } ]; /** @nocollapse */ AutoFocusDirective.ctorParameters = function () { return [ { type: core.ElementRef } ]; }; AutoFocusDirective.propDecorators = { autoFocus: [{ type: core.Input, args: ['dAutoFocus',] }] }; return AutoFocusDirective; }()); 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 */ var DatePipe = /** @class */ (function () { function DatePipe() { } /** * @param {?} date * @param {?} pattern * @return {?} */ DatePipe.prototype.transform = /** * @param {?} date * @param {?} pattern * @return {?} */ function (date, pattern) { if (!date) { return; } return i18n.I18nFormat.formatDate(date, pattern); }; DatePipe.decorators = [ { type: core.Pipe, args: [{ name: 'dDatePipe' },] } ]; return DatePipe; }()); /** * @fileoverview added by tsickle * Generated from: helper-utils.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var HelperUtils = /** @class */ (function () { function HelperUtils() { } /** * @param {?} url * @param {?=} target * @return {?} */ HelperUtils.jumpOuterUrl = /** * @param {?} url * @param {?=} target * @return {?} */ function (url, target) { if (target === void 0) { target = '_blank'; } if (url !== undefined) { /** @type {?} */ var 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 {?} */ var event_1 = document.createEvent('MouseEvents'); event_1.initEvent('click', false, true); tempLink.dispatchEvent(event_1); document.body.removeChild(tempLink); // for IE 11 } }; /** * @param {?} url * @param {?=} option * @param {?=} onError * @return {?} */ HelperUtils.downloadFile = /** * @param {?} url * @param {?=} option * @param {?=} onError * @return {?} */ function (url, option, onError) { if (document.querySelector('iframe[name=' + (option && option.iframename || 'download') + ']')) { document.body.removeChild(document.querySelector('iframe[name=' + (option && option.iframename || 'download') + ']')); } /** @type {?} */ var tempiframe = document.createElement('iframe'); tempiframe.name = option && option.iframename || 'download'; tempiframe.style.display = 'none'; /** @type {?} */ var 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 {?} */ function (key) { /** @type {?} */ var opt = document.createElement('input'); opt.name = key; opt.value = option.params[key]; tempform.appendChild(opt); })); } /** @type {?} */ var 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 {?} */ function (event) { try { /** @type {?} */ var iframeDoc = tempiframe.contentDocument; if (onError !== undefined) { /** @type {?} */ var response = void 0; 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 {?} */ HelperUtils.downloadFileByHttpClient = /** * @param {?} httpClient * @param {?} url * @param {?=} option * @param {?=} onError * @param {?=} onSuccess * @return {?} */ function (httpClient, url, option, onError, onSuccess) { /** @type {?} */ var requestMethod = option && option.method && option.method.toLowerCase() || 'post'; /** @type {?} */ var requestHeaderContentType = option.enctype || 'application/x-www-form-urlencoded'; /** @type {?} */ var requestParams = option.params ? new http.HttpParams({ fromObject: option.params }) : null; /** @type {?} */ var requestUrl = url; /** @type {?} */ var requestOptionParams = requestMethod === 'get' ? requestParams : undefined; /** @type {?} */ var requestBody = requestMethod === 'post' ? requestParams && requestParams.toString() : undefined; /** @type {?} */ var responseOption = option.responseOption; /** @type {?} */ var requestOption = Object.assign({}, { body: requestBody, observe: 'response', params: requestOptionParams, headers: { 'Content-Type': requestHeaderContentType }, responseType: 'arraybuffer' }, { headers: option.header }); /** @type {?} */ var handleResponse = ((/** * @param {?} resOption * @return {?} */ function (resOption) { switch (resOption) { case 'response': return (/** * @param {?} res * @return {?} */ function (res) { return res; }); case 'body': return (/** * @param {?} res * @return {?} */ function (res) { /** @type {?} */ var arrayBuffer = (/** @type {?} */ (((/** @type {?} */ (res))).body)) || ((/** @type {?} */ (res))).error; /** @type {?} */ var body = HelperUtils.utf8ArrayToStr(arrayBuffer); return body; }); case 'json': default: return (/** * @param {?} res * @return {?} */ function (res) { /** @type {?} */ var arrayBuffer = (/** @type {?} */ (((/** @type {?} */ (res))).body)) || ((/** @type {?} */ (res))).error; /** @type {?} */ var response; try { /** @type {?} */ var body = HelperUtils.utf8ArrayToStr(arrayBuffer); try { response = JSON.parse(body); } catch (e) { /** @type {?} */ var parser = new DOMParser(); /** @type {?} */ var 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 {?} */ var downloadFileFromArrayBuffer = (/** * @param {?} data * @param {?} filename * @param {?} contentType * @return {?} */ function (data, filename, contentType) { if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE11 support // IE11 support /** @type {?} */ var blob = new Blob([data], { type: contentType }); window.navigator.msSaveOrOpenBlob(blob, filename); } else { // other browsers if ('download' in document.createElement('a')) { /** @type {?} */ var blob = new Blob([data], { type: contentType }); /** @type {?} */ var 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 {?} */ var file = new File([data], filename, { type: contentType }); /** @type {?} */ var exportUrl = URL.createObjectURL(file); window.location.assign(exportUrl); URL.revokeObjectURL(exportUrl); } } }); /** @type {?} */ var getFilenameFromDisposition = (/** * @param {?} disposition * @return {?} */ function (disposition) { /** @type {?} */ var filenamePair = disposition.split(';').filter((/** * @param {?} str * @return {?} */ function (str) { return /^filename=/.test(str.trim()); })).pop(); if (filenamePair) { /** @type {?} */ var 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 {?} */ function (res) { /** @type {?} */ var disposition = ((/** @type {?} */ (res.headers))).get('content-disposition'); /** @type {?} */ var contentType = ((/** @type {?} */ (res.headers))).get('content-type'); if (/^attachment/i.test(disposition) || option.downloadWithoutDispositionHeader) { /** @type {?} */ var downloadFilename = option.filename || disposition && getFilenameFromDisposition(disposition) || url; downloadFileFromArrayBuffer(res.body, downloadFilename, contentType); if (onSuccess) { onSuccess(res); } } else { if (onError) { /** @type {?} */ var response = void 0; try { response = handleResponse(res); } catch (e) { response = res; } onError(response); } } }), (/** * @param {?} err * @return {?} */ function (err) { if (onError) { /** @type {?} */ var response = void 0; try { response = handleResponse(err); } catch (e) { response = err; } onError(response); } })); }; /** * @private * @param {?} arrayBuffer * @return {?} */ HelperUtils.utf8ArrayToStr = /** * @private * @param {?} arrayBuffer * @return {?} */ function (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)); } }; return HelperUtils; }()); var SimulateATagDirective = /** @class */ (function () { function SimulateATagDirective() { this.target = '_blank'; } /** * @return {?} */ SimulateATagDirective.prototype.onClick = /** * @return {?} */ function () { HelperUtils.jumpOuterUrl(this.href, this.target); }; SimulateATagDirective.decorators = [ { type: core.Directive, args: [{ selector: '[dSimulateATag]' },] } ]; /** @nocollapse */ SimulateATagDirective.ctorParameters = function () { return []; }; SimulateATagDirective.propDecorators = { href: [{ type: core.Input }], target: [{ type: core.Input }], onClick: [{ type: core.HostListener, args: ['click',] }] }; return SimulateATagDirective; }()); 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 */ var IframeEventPropagateDirective = /** @class */ (function () { function IframeEventPropagateDirective(el) { var _this = this; this.event = 'click'; this.AddIframeContentDocumentClickListener = (/** * @return {?} */ function () { /** @type {?} */ var iframe = _this.element.querySelector('iframe'); if (iframe !== null) { if (iframe.contentDocument !== null) { iframe.contentDocument.addEventListener(_this.event, _this.dispatchClickEvent); } else { /** @type {?} */ var loadHandler_1 = (/** * @return {?} */ function () { iframe.contentDocument.addEventListener(_this.event, _this.dispatchClickEvent); iframe.removeEventListener('load', loadHandler_1); }); iframe.addEventListener('load', loadHandler_1); } _this.element.removeEventListener('DOMSubtreeModified', _this.AddIframeContentDocumentClickListener); } }); this.dispatchClickEvent = (/** * @param {?} $event * @return {?} */ function ($event) { /** @type {?} */ var event = document.createEvent('MouseEvents'); event.initEvent(_this.event, true, true); event['originEvent'] = $event; _this.element.dispatchEvent(event); }); this.element = el.nativeElement; } /** * @return {?} */ IframeEventPropagateDirective.prototype.ngAfterViewInit = /** * @return {?} */ function () { this.element.addEventListener('DOMSubtreeModified', this.AddIframeContentDocumentClickListener); if (this.element.querySelector('iframe') !== null) { this.AddIframeContentDocumentClickListener(); } }; IframeEventPropagateDirective.decorators = [ { type: core.Directive, args: [{ selector: '[dIframeEventPropagate]' },] } ]; /** @nocollapse */ IframeEventPropagateDirective.ctorParameters = function () { return [ { type: core.ElementRef } ]; }; IframeEventPropagateDirective.propDecorators = { event: [{ type: core.Input }] }; return IframeEventPropagateDirective; }()); 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 */ var DCommonModule = /** @class */ (function () { function DCommonModule() { } DCommonModule.decorators = [ { type: core.NgModule, args: [{ imports: [common.CommonModule], exports: [ AutoFocusDirective, DatePipe, SimulateATagDirective, IframeEventPropagateDirective ], declarations: [ AutoFocusDirective, DatePipe, SimulateATagDirective, IframeEventPropagateDirective ], providers: [], },] } ]; return DCommonModule; }()); exports.AutoFocusDirective = AutoFocusDirective; exports.DCommonModule = DCommonModule; exports.DatePipe = DatePipe; exports.HelperUtils = HelperUtils; exports.IframeEventPropagateDirective = IframeEventPropagateDirective; exports.SimulateATagDirective = SimulateATagDirective; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=ng-devui-common.umd.js.map