framework7
Version:
Full featured mobile HTML framework for building iOS & Android apps
152 lines (119 loc) • 4.28 kB
JavaScript
import { getWindow } from 'ssr-window';
import { getSupport } from './get-support';
var deviceCalculated;
function calcDevice(_temp) {
var _ref = _temp === void 0 ? {} : _temp,
userAgent = _ref.userAgent;
var support = getSupport();
var window = getWindow();
var platform = window.navigator.platform;
var ua = userAgent || window.navigator.userAgent;
var device = {
ios: false,
android: false,
androidChrome: false,
desktop: false,
iphone: false,
ipod: false,
ipad: false,
edge: false,
ie: false,
firefox: false,
macos: false,
windows: false,
cordova: !!(window.cordova || window.phonegap),
phonegap: !!(window.cordova || window.phonegap),
electron: false,
capacitor: !!window.Capacitor,
nwjs: false
};
var screenWidth = window.screen.width;
var screenHeight = window.screen.height;
var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/); // eslint-disable-line
var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
var iphone = !ipad && ua.match(/(iPhone\sOS|iOS|iPhone;\sCPU\sOS)\s([\d_]+)/);
var ie = ua.indexOf('MSIE ') >= 0 || ua.indexOf('Trident/') >= 0;
var edge = ua.indexOf('Edge/') >= 0;
var firefox = ua.indexOf('Gecko/') >= 0 && ua.indexOf('Firefox/') >= 0;
var windows = platform === 'Win32';
var electron = ua.toLowerCase().indexOf('electron') >= 0;
var nwjs = typeof nw !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.nw !== 'undefined';
var macos = platform === 'MacIntel'; // iPadOs 13 fix
var iPadScreens = ['1024x1366', '1366x1024', '834x1194', '1194x834', '834x1112', '1112x834', '768x1024', '1024x768', '820x1180', '1180x820', '810x1080', '1080x810'];
if (!ipad && macos && support.touch && iPadScreens.indexOf(screenWidth + "x" + screenHeight) >= 0) {
ipad = ua.match(/(Version)\/([\d.]+)/);
if (!ipad) ipad = [0, 1, '13_0_0'];
macos = false;
}
device.ie = ie;
device.edge = edge;
device.firefox = firefox; // Android
if (android) {
device.os = 'android';
device.osVersion = android[2];
device.android = true;
device.androidChrome = ua.toLowerCase().indexOf('chrome') >= 0;
}
if (ipad || iphone || ipod) {
device.os = 'ios';
device.ios = true;
} // iOS
if (iphone && !ipod) {
device.osVersion = iphone[2].replace(/_/g, '.');
device.iphone = true;
}
if (ipad) {
device.osVersion = ipad[2].replace(/_/g, '.');
device.ipad = true;
}
if (ipod) {
device.osVersion = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
device.ipod = true;
} // iOS 8+ changed UA
if (device.ios && device.osVersion && ua.indexOf('Version/') >= 0) {
if (device.osVersion.split('.')[0] === '10') {
device.osVersion = ua.toLowerCase().split('version/')[1].split(' ')[0];
}
} // Webview
device.webView = !!((iphone || ipad || ipod) && (ua.match(/.*AppleWebKit(?!.*Safari)/i) || window.navigator.standalone)) || window.matchMedia && window.matchMedia('(display-mode: standalone)').matches;
device.webview = device.webView;
device.standalone = device.webView; // Desktop
device.desktop = !(device.ios || device.android) || electron || nwjs;
if (device.desktop) {
device.electron = electron;
device.nwjs = nwjs;
device.macos = macos;
device.windows = windows;
if (device.macos) {
device.os = 'macos';
}
if (device.windows) {
device.os = 'windows';
}
} // Pixel Ratio
device.pixelRatio = window.devicePixelRatio || 1; // Color Scheme
var DARK = '(prefers-color-scheme: dark)';
var LIGHT = '(prefers-color-scheme: light)';
device.prefersColorScheme = function prefersColorTheme() {
var theme;
if (window.matchMedia && window.matchMedia(LIGHT).matches) {
theme = 'light';
}
if (window.matchMedia && window.matchMedia(DARK).matches) {
theme = 'dark';
}
return theme;
}; // Export object
return device;
}
function getDevice(overrides, reset) {
if (overrides === void 0) {
overrides = {};
}
if (!deviceCalculated || reset) {
deviceCalculated = calcDevice(overrides);
}
return deviceCalculated;
}
export { getDevice };