jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/browser.js

298 lines
8.5 KiB
JavaScript
Raw Normal View History

define([], function () {
2016-10-02 20:49:52 -07:00
'use strict';
2015-12-26 11:35:53 -07:00
2016-03-08 11:47:36 -07:00
function isTv() {
// This is going to be really difficult to get right
var userAgent = navigator.userAgent.toLowerCase();
2016-10-02 20:49:52 -07:00
if (userAgent.indexOf('tv') !== -1) {
2016-03-08 11:47:36 -07:00
return true;
}
2016-10-02 20:49:52 -07:00
if (userAgent.indexOf('samsungbrowser') !== -1) {
2016-03-08 11:47:36 -07:00
return true;
}
2016-10-02 20:49:52 -07:00
if (userAgent.indexOf('nintendo') !== -1) {
2016-03-08 11:47:36 -07:00
return true;
}
2016-10-02 20:49:52 -07:00
if (userAgent.indexOf('viera') !== -1) {
2016-03-08 11:47:36 -07:00
return true;
}
2016-10-02 20:49:52 -07:00
if (userAgent.indexOf('webos') !== -1) {
2016-03-09 10:40:22 -07:00
return true;
}
2016-03-08 11:47:36 -07:00
return false;
}
2016-08-06 07:07:44 -07:00
function isMobile(userAgent) {
var terms = [
'mobi',
'ipad',
'iphone',
'ipod',
'silk',
'gt-p1000',
'nexus 7',
'kindle fire',
'opera mini'
];
var lower = userAgent.toLowerCase();
for (var i = 0, length = terms.length; i < length; i++) {
2016-10-02 20:49:52 -07:00
if (lower.indexOf(terms[i]) !== -1) {
2016-08-06 07:07:44 -07:00
return true;
}
}
return false;
}
2016-07-04 22:40:18 -07:00
function isStyleSupported(prop, value) {
2016-12-10 00:16:15 -07:00
if (typeof window === 'undefined') {
return false;
}
2016-07-04 22:40:18 -07:00
// If no value is supplied, use "inherit"
value = arguments.length === 2 ? value : 'inherit';
// Try the native standard method first
if ('CSS' in window && 'supports' in window.CSS) {
return window.CSS.supports(prop, value);
}
// Check Opera's native method
if ('supportsCSS' in window) {
return window.supportsCSS(prop, value);
}
// need try/catch because it's failing on tizen
try {
// Convert to camel-case for DOM interactions
var camel = prop.replace(/-([a-z]|[0-9])/ig, function (all, letter) {
return (letter + '').toUpperCase();
});
// Check if the property is supported
var support = (camel in el.style);
// Create test element
var el = document.createElement('div');
// Assign the property and value to invoke
// the CSS interpreter
el.style.cssText = prop + ':' + value;
// Ensure both the property and value are
// supported and return
return support && (el.style[camel] !== '');
} catch (err) {
return false;
}
}
2016-09-15 13:32:49 -07:00
function hasKeyboard(browser) {
if (browser.touch) {
return true;
}
if (browser.xboxOne) {
return true;
}
if (browser.ps4) {
return true;
}
if (browser.edgeUwp) {
// This is OK for now, but this won't always be true
// Should we use this?
// https://gist.github.com/wagonli/40d8a31bd0d6f0dd7a5d
return true;
}
if (browser.tv) {
return true;
}
return false;
}
2016-11-27 12:36:56 -07:00
var _supportsCssAnimation;
2016-11-28 12:27:21 -07:00
var _supportsCssAnimationWithPrefix;
function supportsCssAnimation(allowPrefix) {
2016-11-27 12:36:56 -07:00
2016-11-28 12:27:21 -07:00
if (allowPrefix) {
if (_supportsCssAnimationWithPrefix === true || _supportsCssAnimationWithPrefix === false) {
return _supportsCssAnimationWithPrefix;
}
} else {
if (_supportsCssAnimation === true || _supportsCssAnimation === false) {
return _supportsCssAnimation;
}
2016-11-27 12:36:56 -07:00
}
var animation = false,
animationstring = 'animation',
keyframeprefix = '',
2016-11-28 12:27:21 -07:00
domPrefixes = ['Webkit', 'O', 'Moz'],
2016-11-27 12:36:56 -07:00
pfx = '',
elm = document.createElement('div');
if (elm.style.animationName !== undefined) { animation = true; }
2016-11-28 12:27:21 -07:00
if (animation === false && allowPrefix) {
2016-11-27 12:36:56 -07:00
for (var i = 0; i < domPrefixes.length; i++) {
if (elm.style[domPrefixes[i] + 'AnimationName'] !== undefined) {
pfx = domPrefixes[i];
animationstring = pfx + 'Animation';
keyframeprefix = '-' + pfx.toLowerCase() + '-';
animation = true;
break;
}
}
}
2016-11-28 12:27:21 -07:00
if (allowPrefix) {
_supportsCssAnimationWithPrefix = animation;
return _supportsCssAnimationWithPrefix;
} else {
_supportsCssAnimation = animation;
return _supportsCssAnimation;
}
2016-11-27 12:36:56 -07:00
}
2015-12-26 11:35:53 -07:00
var uaMatch = function (ua) {
ua = ua.toLowerCase();
var match = /(edge)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(opr)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(chrome)[ \/]([\w.]+)/.exec(ua) ||
/(safari)[ \/]([\w.]+)/.exec(ua) ||
2016-01-13 22:18:46 -07:00
/(firefox)[ \/]([\w.]+)/.exec(ua) ||
2015-12-26 11:35:53 -07:00
/(msie) ([\w.]+)/.exec(ua) ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
[];
2016-10-05 21:28:10 -07:00
var versionMatch = /(version)[ \/]([\w.]+)/.exec(ua);
2015-12-26 11:35:53 -07:00
var platform_match = /(ipad)/.exec(ua) ||
/(iphone)/.exec(ua) ||
/(android)/.exec(ua) ||
[];
var browser = match[1] || "";
2016-10-02 20:49:52 -07:00
if (browser === "edge") {
2016-02-13 19:26:51 -07:00
platform_match = [""];
} else {
2016-10-02 20:49:52 -07:00
if (ua.indexOf("windows phone") !== -1 || ua.indexOf("iemobile") !== -1) {
2015-12-26 11:35:53 -07:00
2016-02-13 19:26:51 -07:00
// http://www.neowin.net/news/ie11-fakes-user-agent-to-fool-gmail-in-windows-phone-81-gdr1-update
browser = "msie";
}
2016-10-02 20:49:52 -07:00
else if (ua.indexOf("like gecko") !== -1 && ua.indexOf('webkit') === -1 && ua.indexOf('opera') === -1 && ua.indexOf('chrome') === -1 && ua.indexOf('safari') === -1) {
2016-02-13 19:26:51 -07:00
browser = "msie";
}
2015-12-26 11:35:53 -07:00
}
2016-10-02 20:49:52 -07:00
if (browser === 'opr') {
2015-12-26 11:35:53 -07:00
browser = 'opera';
}
2016-10-05 21:28:10 -07:00
var version;
if (versionMatch && versionMatch.length > 2) {
version = versionMatch[2];
}
version = version || match[2] || "0";
var versionMajor = parseInt(version.split('.')[0]);
if (isNaN(versionMajor)) {
versionMajor = 0;
}
2015-12-26 11:35:53 -07:00
return {
browser: browser,
2016-10-05 21:28:10 -07:00
version: version,
platform: platform_match[0] || "",
versionMajor: versionMajor
2015-12-26 11:35:53 -07:00
};
};
2016-12-10 00:16:15 -07:00
var userAgent = navigator.userAgent;
2015-12-26 11:35:53 -07:00
var matched = uaMatch(userAgent);
var browser = {};
if (matched.browser) {
browser[matched.browser] = true;
browser.version = matched.version;
2016-10-05 21:28:10 -07:00
browser.versionMajor = matched.versionMajor;
2015-12-26 11:35:53 -07:00
}
if (matched.platform) {
browser[matched.platform] = true;
}
2016-10-02 20:49:52 -07:00
if (!browser.chrome && !browser.msie && !browser.edge && !browser.opera && userAgent.toLowerCase().indexOf("webkit") !== -1) {
2016-09-03 12:31:28 -07:00
browser.safari = true;
}
2016-10-02 20:49:52 -07:00
if (userAgent.toLowerCase().indexOf("playstation 4") !== -1) {
2016-06-21 21:39:24 -07:00
browser.ps4 = true;
2016-11-28 12:27:21 -07:00
browser.tv = true;
2016-06-21 21:39:24 -07:00
}
2017-01-01 23:06:05 -07:00
if (userAgent.toLowerCase().indexOf("embytheaterpi") !== -1) {
browser.slow = true;
2017-01-06 12:37:57 -07:00
browser.noAnimation = true;
2017-01-01 23:06:05 -07:00
}
2016-08-06 07:07:44 -07:00
if (isMobile(userAgent)) {
2015-12-26 11:35:53 -07:00
browser.mobile = true;
}
2016-10-02 20:49:52 -07:00
browser.xboxOne = userAgent.toLowerCase().indexOf('xbox') !== -1;
2016-12-10 00:16:15 -07:00
browser.animate = typeof document !== 'undefined' && document.documentElement.animate != null;
2017-01-22 20:54:51 -07:00
browser.tizen = userAgent.toLowerCase().indexOf('tizen') !== -1 || self.tizen != null;
2016-10-02 20:49:52 -07:00
browser.web0s = userAgent.toLowerCase().indexOf('Web0S'.toLowerCase()) !== -1;
browser.edgeUwp = browser.edge && userAgent.toLowerCase().indexOf('msapphost') !== -1;
2015-12-26 11:35:53 -07:00
2017-01-22 20:51:49 -07:00
if (!browser.tizen) {
browser.orsay = userAgent.toLowerCase().indexOf('smarthub') !== -1;
}
2016-12-22 14:52:18 -07:00
if (browser.edgeUwp) {
browser.edge = true;
}
2016-03-08 11:47:36 -07:00
browser.tv = isTv();
2016-10-02 20:49:52 -07:00
browser.operaTv = browser.tv && userAgent.toLowerCase().indexOf('opr/') !== -1;
2016-03-08 11:47:36 -07:00
2016-07-04 22:40:18 -07:00
if (!isStyleSupported('display', 'flex')) {
browser.noFlex = true;
}
2016-08-06 07:07:44 -07:00
if (browser.mobile || browser.tv) {
browser.slow = true;
}
2016-12-10 00:16:15 -07:00
if (typeof document !== 'undefined') {
if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
browser.touch = true;
}
2016-08-06 07:07:44 -07:00
}
2016-06-04 17:17:35 -07:00
2016-09-15 13:32:49 -07:00
browser.keyboard = hasKeyboard(browser);
2016-11-27 12:36:56 -07:00
browser.supportsCssAnimation = supportsCssAnimation;
2016-09-15 13:32:49 -07:00
2016-11-28 12:27:21 -07:00
browser.osx = userAgent.toLowerCase().indexOf('os x') !== -1;
browser.iOS = browser.ipad || browser.iphone || browser.ipod;
2015-12-26 11:35:53 -07:00
return browser;
});