@ohayojp.com/components
Version:
Common business components of ohayojp.
225 lines (219 loc) • 6.25 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { Platform } from '@angular/cdk/platform';
import { Directive, ElementRef, Input, NgModule } from '@angular/core';
import { _HttpClient } from '@ohayojp.com/theme';
import { OhayoConfigService, InputNumber, InputBoolean, OhayoUtilModule } from '@ohayojp.com/util';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* Generated from: image.directive.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class ImageDirective {
/**
* @param {?} el
* @param {?} configSrv
* @param {?} http
* @param {?} platform
*/
constructor(el, configSrv, http, platform) {
this.http = http;
this.platform = platform;
this.useHttp = false;
this.inited = false;
configSrv.attach(this, 'image', { size: 64, error: `./assets/img/logo.svg` });
this.imgEl = el.nativeElement;
}
/**
* @return {?}
*/
ngOnInit() {
this.update();
this.updateError();
this.inited = true;
}
/**
* @param {?} changes
* @return {?}
*/
ngOnChanges(changes) {
const { size, imgEl } = this;
imgEl.height = size;
imgEl.width = size;
if (this.inited) {
if (changes.error) {
this.updateError();
}
this.update();
}
}
/**
* @private
* @return {?}
*/
update() {
const { size, imgEl, useHttp } = this;
if (useHttp) {
this.getByHttp();
return;
}
/** @type {?} */
let newSrc = this.src;
if (newSrc.includes('qlogo.cn')) {
/** @type {?} */
const arr = newSrc.split('/');
/** @type {?} */
const imgSize = arr[arr.length - 1];
arr[arr.length - 1] = imgSize === '0' || +imgSize !== size ? size.toString() : imgSize;
newSrc = arr.join('/');
}
newSrc = newSrc.replace(/^(?:https?:)/i, '');
imgEl.src = newSrc;
}
/**
* @private
* @return {?}
*/
getByHttp() {
if (!this.platform.isBrowser) {
return;
}
const { imgEl } = this;
this.http.get(this.src, null, { responseType: 'blob' }).subscribe((/**
* @param {?} blob
* @return {?}
*/
(blob) => {
/** @type {?} */
const reader = new FileReader();
reader.onloadend = (/**
* @return {?}
*/
() => (imgEl.src = (/** @type {?} */ (reader.result))));
reader.onerror = (/**
* @return {?}
*/
() => this.setError());
reader.readAsDataURL(blob);
}), (/**
* @return {?}
*/
() => this.setError()));
}
/**
* @private
* @return {?}
*/
updateError() {
const { imgEl, error } = this;
// tslint:disable-next-line: only-arrow-functions, typedef
imgEl.onerror = (/**
* @return {?}
*/
function () {
this.onerror = null;
this.src = error;
});
}
/**
* @private
* @return {?}
*/
setError() {
const { imgEl, error } = this;
imgEl.src = error;
}
}
ImageDirective.decorators = [
{
type: Directive, args: [{
selector: '[_src]',
exportAs: '_src',
},]
}
];
/** @nocollapse */
ImageDirective.ctorParameters = () => [
{ type: ElementRef },
{ type: OhayoConfigService },
{ type: _HttpClient },
{ type: Platform }
];
ImageDirective.propDecorators = {
src: [{ type: Input, args: ['_src',] }],
size: [{ type: Input }],
error: [{ type: Input }],
useHttp: [{ type: Input }]
};
__decorate([
InputNumber(),
__metadata("design:type", Number)
], ImageDirective.prototype, "size", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], ImageDirective.prototype, "useHttp", void 0);
if (false) {
/** @type {?} */
ImageDirective.ngAcceptInputType_size;
/** @type {?} */
ImageDirective.ngAcceptInputType_useHttp;
/** @type {?} */
ImageDirective.prototype.src;
/** @type {?} */
ImageDirective.prototype.size;
/** @type {?} */
ImageDirective.prototype.error;
/** @type {?} */
ImageDirective.prototype.useHttp;
/**
* @type {?}
* @private
*/
ImageDirective.prototype.inited;
/**
* @type {?}
* @private
*/
ImageDirective.prototype.imgEl;
/**
* @type {?}
* @private
*/
ImageDirective.prototype.http;
/**
* @type {?}
* @private
*/
ImageDirective.prototype.platform;
}
/**
* @fileoverview added by tsickle
* Generated from: image.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const DIRECTIVES = [ImageDirective];
class ImageModule {
}
ImageModule.decorators = [
{
type: NgModule, args: [{
imports: [CommonModule, OhayoUtilModule],
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: image.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { ImageDirective, ImageModule };
//# sourceMappingURL=image.js.map