import { Injectable } from '@angular/core'; import { Platforms, getPlatforms, isPlatform } from './platform'; @Injectable({ providedIn: 'root' }) export class PlatformService { constructor() { } is(platformName: Platforms): boolean { return isPlatform(window, platformName); } platforms(): string[] { return getPlatforms(window); } get isRTL(): boolean { return document.dir === 'rtl'; } /** * Get the query string parameter */ getQueryParam(key: string): string | null { return readQueryParam(window.location.href, key); } isLandscape(): boolean { return !this.isPortrait(); } isPortrait(): boolean { return window.matchMedia('(orientation: portrait)').matches; } testUserAgent(expression: string): boolean { return navigator.userAgent.indexOf(expression) >= 0; } url() { return window.location.href; } width() { return window.innerWidth; } height(): number { return window.innerHeight; } } function readQueryParam(url: string, key: string) { key = key.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); const regex = new RegExp('[\\?&]' + key + '=([^&#]*)'); const results = regex.exec(url); return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null; }