UNPKG

@rxap/services

Version:

Provides Angular services for common tasks such as loading images, managing loading indicators, resetting data, and managing components in a window container sidenav. It also includes a service to generate avatar images. This package offers reusable servi

148 lines (139 loc) 6.11 kB
import * as i0 from '@angular/core'; import { Injectable } from '@angular/core'; import { HttpRequest, HttpParams } from '@angular/common/http'; import { Subscription, Subject } from 'rxjs'; import { map, delay } from 'rxjs/operators'; import { ToggleSubject } from '@rxap/rxjs'; class AvatarImageService { get({ name }) { return new HttpRequest('GET', 'https://eu.ui-avatars.com/api', { params: new HttpParams({ fromObject: { name } }), }).urlWithParams; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: AvatarImageService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: AvatarImageService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: AvatarImageService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }] }); class ImageLoaderService { constructor() { this.loading = new Map(); this.loaded = new Map(); } load(imageSrc) { if (this.loaded.has(imageSrc)) { return Promise.resolve(); } if (this.loading.has(imageSrc)) { return this.loading.get(imageSrc); } const loading = new Promise((resolve, reject) => { const image = new Image(); image.src = imageSrc; image.onerror = reject; image.onload = () => resolve(); }).then(() => { this.loaded.set(imageSrc, true); }).finally(() => { this.loading.delete(imageSrc); }); this.loading.set(imageSrc, loading); return loading; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: ImageLoaderService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: ImageLoaderService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: ImageLoaderService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }] }); class LoadingIndicatorService { constructor() { this.loading$ = new ToggleSubject(true); this._attachedLoadingSubscription = new Subscription(); this.isLoading$ = this.loading$.asObservable(); } attachLoading(loading$) { this._attachedLoadingSubscription.add(loading$.pipe(map(Boolean), delay(0)).subscribe(this.loading$.next)); } ngOnDestroy() { this._attachedLoadingSubscription.unsubscribe(); } disable() { setTimeout(() => { this.loading$.disable(); }); } enable() { setTimeout(() => { this.loading$.enable(); }); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: LoadingIndicatorService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: LoadingIndicatorService }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: LoadingIndicatorService, decorators: [{ type: Injectable }], ctorParameters: () => [] }); class ResetService { constructor() { this.view$ = new Subject(); this.dataSource$ = new Subject(); } resetView() { this.view$.next(); } resetDataSource() { // TODO : iterate over the list of active DataSources this.dataSource$.next(); } resetAll() { this.resetDataSource(); this.resetView(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: ResetService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: ResetService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: ResetService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }] }); class WindowContainerSidenavService { constructor() { this.add$ = new Subject(); this.remove$ = new Subject(); this.components = new Map(); } add(component) { if (this.components.has(component.id)) { this.remove$.next(component); } this.components.set(component.id, component); this.add$.next(component); } remove(componentOrId) { const id = typeof componentOrId === 'string' ? componentOrId : componentOrId.id; if (this.components.has(id)) { this.remove$.next(this.components.get(id)); this.components.delete(id); } } getAll() { return Array.from(this.components.values()); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: WindowContainerSidenavService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: WindowContainerSidenavService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: WindowContainerSidenavService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }] }); // region // endregion /** * Generated bundle index. Do not edit. */ export { AvatarImageService, ImageLoaderService, LoadingIndicatorService, ResetService, WindowContainerSidenavService }; //# sourceMappingURL=rxap-services.mjs.map