import { LocaleService } from '@farris/ui-locale'; import { Injectable, ApplicationRef, ComponentFactoryResolver, Injector, ComponentRef, ElementRef } from '@angular/core'; import { LoadingConfig, LOADING_DEFAULT_CONFIG, loaddingDefaultConfig } from './loading.config'; import { LoadingComponent } from './loading.component'; import { max } from 'lodash-es'; @Injectable() export class LoadingService { config: LoadingConfig; private currentLoadingInstanceID = null; private loadingInstances: { [key: number]: ComponentRef } = {}; private localeSer: LocaleService; constructor(private appRef: ApplicationRef, private cfr: ComponentFactoryResolver, private injecotr: Injector) { this.localeSer = this.injecotr.get(LocaleService); } show(config?: LoadingConfig): LoadingComponent { this.config = this.injecotr.get(LOADING_DEFAULT_CONFIG) || {}; this.config = Object.assign(loaddingDefaultConfig, this.config); let _loadingCmpRef: ComponentRef; const loadingFactory = this.cfr.resolveComponentFactory(LoadingComponent); _loadingCmpRef = loadingFactory.create(this.injecotr); if (config) { this.config = Object.assign({}, this.config, config); } const languageCode = localStorage.getItem('languageCode'); // if (languageCode === 'en') { // this.config.message = 'Loading...'; // } if (this.localeSer) { if (!this.config.message || this.config.message === '正在加载中,请稍候...') { this.config.message = this.localeSer.getValue('loading.message'); } } const container = this.config.container; if (container === 'body') { document.querySelector(container as string).appendChild( _loadingCmpRef.location.nativeElement); } else { if (container instanceof ElementRef ) { container.nativeElement.appendChild( _loadingCmpRef.location.nativeElement ); } else { if (container instanceof Element) { container.appendChild( _loadingCmpRef.location.nativeElement ); } } } _loadingCmpRef.instance.delay = this.config.delay; _loadingCmpRef.instance.isActive = true; Object.assign(_loadingCmpRef.instance, this.config); _loadingCmpRef.instance.closed.subscribe(state => { if (!state) { delete this.loadingInstances[_loadingCmpRef.instance.id]; this.currentLoadingInstanceID = this.getMaxLoadingID(); this.clearDom(_loadingCmpRef); } }); _loadingCmpRef.changeDetectorRef.markForCheck(); _loadingCmpRef.changeDetectorRef.detectChanges(); const loadingID = this.createInsId(); _loadingCmpRef.instance.id = loadingID; this.loadingInstances[loadingID] = _loadingCmpRef; this.currentLoadingInstanceID = loadingID; return _loadingCmpRef.instance; } close(loadingId?: number) { const id = loadingId || this.currentLoadingInstanceID; const loadingRef = this.loadingInstances[id]; if (loadingRef) { loadingRef.instance.close(); } } clearAll() { const keys = Object.keys(this.loadingInstances); if (keys.length) { keys.forEach(id => { this.clearDom(this.loadingInstances[id]); }); } else { const loadings = document.querySelectorAll('farris-loading'); if (loadings.length) { loadings.forEach( el => el.remove() ); } this.loadingInstances = {}; } } private clearDom(_loadingCmpRef: ComponentRef) { if (_loadingCmpRef && _loadingCmpRef.location) { const loadingEl = _loadingCmpRef.location.nativeElement; if (loadingEl.parentNode) { loadingEl.parentNode.removeChild(loadingEl); } _loadingCmpRef.destroy(); } _loadingCmpRef = null; } private createInsId() { return this.getMaxLoadingID() + 1; } private getMaxLoadingID() { const ids = Object.keys(this.loadingInstances).map(k => parseInt(k, 10)); if (ids.length) { return max(ids); } return 0; } }