@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.
150 lines (141 loc) • 7.16 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs'), require('rxjs/operators')) :
typeof define === 'function' && define.amd ? define('@anexia/platform-tools', ['exports', '@angular/core', 'rxjs', 'rxjs/operators'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.anexia = global.anexia || {}, global.anexia['platform-tools'] = {}), global.ng.core, global.rxjs, global.rxjs.operators));
}(this, (function (exports, i0, rxjs, operators) { 'use strict';
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var i0__namespace = /*#__PURE__*/_interopNamespace(i0);
var PlatformToolsService = /** @class */ (function () {
function PlatformToolsService() {
setupPlatforms(window);
rxjs.fromEvent(window, 'resize').pipe(operators.startWith(true), operators.tap(function (_) { return updateOrientation(window); })).subscribe();
}
return PlatformToolsService;
}());
PlatformToolsService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0__namespace, type: PlatformToolsService, deps: [], target: i0__namespace.ɵɵFactoryTarget.Injectable });
PlatformToolsService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0__namespace, type: PlatformToolsService, providedIn: 'root' });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0__namespace, type: PlatformToolsService, decorators: [{
type: i0.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');
}
}
var setupPlatforms = function (win) {
if (win === void 0) { win = window; }
if (typeof win === 'undefined') {
return [];
}
var platforms = null;
platforms = detectPlatforms(win);
platforms.forEach(function (p) { return win.document.documentElement.classList.add("plt-" + p); });
return platforms;
};
var detectPlatforms = function (win) { return Object.keys(PLATFORMS_MAP).filter(function (p) { return PLATFORMS_MAP[p](win); }); };
var isMobileWeb = function (win) { return isMobile(win); };
var isIpad = function (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;
};
var isIphone = function (win) { return testUserAgent(win, /iPhone/i); };
var isIOS = function (win) { return testUserAgent(win, /iPhone|iPod/i) || isIpad(win); };
var isAndroid = function (win) { return testUserAgent(win, /android|sink/i); };
var isAndroidTablet = function (win) {
return isAndroid(win) && !testUserAgent(win, /mobile/i);
};
var isPhablet = function (win) {
var width = win.innerWidth;
var height = win.innerHeight;
var smallest = Math.min(width, height);
var largest = Math.max(width, height);
return (smallest > 390 && smallest < 520) &&
(largest > 620 && largest < 800);
};
var isTablet = function (win) {
var width = win.innerWidth;
var height = win.innerHeight;
var smallest = Math.min(width, height);
var largest = Math.max(width, height);
return (isIpad(win) ||
isAndroidTablet(win) ||
((smallest > 460 && smallest < 820) &&
(largest > 780 && largest < 1400)));
};
var isMobile = function (win) { return matchMedia(win, '(any-pointer:coarse)'); };
var isDesktop = function (win) { return !isMobile(win); };
var isPWA = function (win) { return !!(win.matchMedia('(display-mode: standalone)').matches || win.navigator.standalone); };
var testUserAgent = function (win, expr) { return expr.test(win.navigator.userAgent); };
var matchMedia = function (win, query) { return win.matchMedia(query).matches; };
var PLATFORMS_MAP = {
'ipad': isIpad,
'iphone': isIphone,
'ios': isIOS,
'android': isAndroid,
'phablet': isPhablet,
'tablet': isTablet,
'pwa': isPWA,
'mobile': isMobile,
'mobileweb': isMobileWeb,
'desktop': isDesktop
};
var PlatformToolsModule = /** @class */ (function () {
function PlatformToolsModule() {
}
return PlatformToolsModule;
}());
PlatformToolsModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0__namespace, type: PlatformToolsModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule });
PlatformToolsModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0__namespace, type: PlatformToolsModule });
PlatformToolsModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0__namespace, type: PlatformToolsModule, imports: [[]] });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.3", ngImport: i0__namespace, type: PlatformToolsModule, decorators: [{
type: i0.NgModule,
args: [{
declarations: [],
imports: [],
exports: []
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
exports.PlatformToolsModule = PlatformToolsModule;
exports.PlatformToolsService = PlatformToolsService;
exports.setupPlatforms = setupPlatforms;
exports.testUserAgent = testUserAgent;
exports.updateOrientation = updateOrientation;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=anexia-platform-tools.umd.js.map