UNPKG

@igo2/core

Version:
102 lines (97 loc) 3.61 kB
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'; import * as i0 from '@angular/core'; import { inject, Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; var Media; (function (Media) { Media["Mobile"] = "mobile"; Media["Tablet"] = "tablet"; Media["Desktop"] = "desktop"; })(Media || (Media = {})); var MediaOrientation; (function (MediaOrientation) { MediaOrientation["Portrait"] = "portrait"; MediaOrientation["Landscape"] = "landscape"; })(MediaOrientation || (MediaOrientation = {})); class MediaService { media$ = new BehaviorSubject(undefined); orientation$ = new BehaviorSubject(undefined); constructor() { const breakpointObserver = inject(BreakpointObserver); breakpointObserver .observe([Breakpoints.HandsetLandscape]) .subscribe((res) => { if (res.matches) { this.media$.next(Media.Mobile); this.orientation$.next(MediaOrientation.Landscape); } }); breakpointObserver .observe([Breakpoints.HandsetPortrait]) .subscribe((res) => { if (res.matches) { this.media$.next(Media.Mobile); this.orientation$.next(MediaOrientation.Portrait); } }); breakpointObserver .observe([Breakpoints.TabletLandscape]) .subscribe((res) => { if (res.matches) { this.media$.next(Media.Tablet); this.orientation$.next(MediaOrientation.Landscape); } }); breakpointObserver .observe([Breakpoints.TabletPortrait]) .subscribe((res) => { if (res.matches) { this.media$.next(Media.Tablet); this.orientation$.next(MediaOrientation.Portrait); } }); breakpointObserver.observe([Breakpoints.WebLandscape]).subscribe((res) => { if (res.matches) { this.media$.next(Media.Desktop); this.orientation$.next(MediaOrientation.Landscape); } }); breakpointObserver.observe([Breakpoints.WebPortrait]).subscribe((res) => { if (res.matches) { this.media$.next(Media.Desktop); this.orientation$.next(MediaOrientation.Portrait); } }); } getMedia() { return this.media$.value; } getOrientation() { return this.orientation$.value; } isTouchScreen() { return 'ontouchstart' in document.documentElement ? true : false; } isMobile() { return this.getMedia() === Media.Mobile; } isTablet() { return this.getMedia() === Media.Tablet; } isDesktop() { return this.getMedia() === Media.Desktop; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: MediaService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: MediaService, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: MediaService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [] }); /** * Generated bundle index. Do not edit. */ export { Media, MediaOrientation, MediaService }; //# sourceMappingURL=igo2-core-media.mjs.map