@anexia/platform-tools
Version:
Provides Ionic Framework way class mapping of current platform and orientation on the root document node ´html´ for usage inside scss or :host-context selecting.
114 lines (109 loc) • 4.71 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, NgModule } from '@angular/core';
import { fromEvent } from 'rxjs';
import { startWith, tap } from 'rxjs/operators';
class PlatformToolsService {
constructor() {
setupPlatforms(window);
fromEvent(window, 'resize').pipe(startWith(true), tap(_ => updateOrientation(window))).subscribe();
}
}
PlatformToolsService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0, type: PlatformToolsService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
PlatformToolsService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0, type: PlatformToolsService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0, type: PlatformToolsService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () { return []; } });
function updateOrientation(win) {
if (win.matchMedia("(orientation: portrait)").matches) {
win.document.documentElement.classList.add('is-portrait');
win.document.documentElement.classList.remove('is-landscape');
}
if (win.matchMedia("(orientation: landscape)").matches) {
win.document.documentElement.classList.add('is-landscape');
win.document.documentElement.classList.remove('is-portrait');
}
}
const setupPlatforms = (win = window) => {
if (typeof win === 'undefined') {
return [];
}
let platforms = null;
platforms = detectPlatforms(win);
platforms.forEach(p => win.document.documentElement.classList.add(`plt-${p}`));
return platforms;
};
const detectPlatforms = (win) => Object.keys(PLATFORMS_MAP).filter(p => PLATFORMS_MAP[p](win));
const isMobileWeb = (win) => isMobile(win);
const isIpad = (win) => {
// iOS 12 and below
if (testUserAgent(win, /iPad/i)) {
return true;
}
// iOS 13+
if (testUserAgent(win, /Macintosh/i) && isMobile(win)) {
return true;
}
return false;
};
const isIphone = (win) => testUserAgent(win, /iPhone/i);
const isIOS = (win) => testUserAgent(win, /iPhone|iPod/i) || isIpad(win);
const isAndroid = (win) => testUserAgent(win, /android|sink/i);
const isAndroidTablet = (win) => {
return isAndroid(win) && !testUserAgent(win, /mobile/i);
};
const isPhablet = (win) => {
const width = win.innerWidth;
const height = win.innerHeight;
const smallest = Math.min(width, height);
const largest = Math.max(width, height);
return (smallest > 390 && smallest < 520) &&
(largest > 620 && largest < 800);
};
const isTablet = (win) => {
const width = win.innerWidth;
const height = win.innerHeight;
const smallest = Math.min(width, height);
const largest = Math.max(width, height);
return (isIpad(win) ||
isAndroidTablet(win) ||
((smallest > 460 && smallest < 820) &&
(largest > 780 && largest < 1400)));
};
const isMobile = (win) => matchMedia(win, '(any-pointer:coarse)');
const isDesktop = (win) => !isMobile(win);
const isPWA = (win) => !!(win.matchMedia('(display-mode: standalone)').matches || win.navigator.standalone);
const testUserAgent = (win, expr) => expr.test(win.navigator.userAgent);
const matchMedia = (win, query) => win.matchMedia(query).matches;
const PLATFORMS_MAP = {
'ipad': isIpad,
'iphone': isIphone,
'ios': isIOS,
'android': isAndroid,
'phablet': isPhablet,
'tablet': isTablet,
'pwa': isPWA,
'mobile': isMobile,
'mobileweb': isMobileWeb,
'desktop': isDesktop
};
class PlatformToolsModule {
}
PlatformToolsModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0, type: PlatformToolsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
PlatformToolsModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0, type: PlatformToolsModule });
PlatformToolsModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0, type: PlatformToolsModule, imports: [[]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0, type: PlatformToolsModule, decorators: [{
type: NgModule,
args: [{
declarations: [],
imports: [],
exports: []
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { PlatformToolsModule, PlatformToolsService, setupPlatforms, testUserAgent, updateOrientation };
//# sourceMappingURL=anexia-platform-tools.js.map