Skip to content

Instantly share code, notes, and snippets.

@inear
Created January 31, 2017 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inear/e4a75f2aeb839a600f6f9cc97db33509 to your computer and use it in GitHub Desktop.
Save inear/e4a75f2aeb839a600f6f9cc97db33509 to your computer and use it in GitHub Desktop.
'use strict';
var $html = $('html');
var ua = navigator.userAgent;
var rtcSupported = require('peer-data').supported;
function _modernizr(feature) {
return $html.hasClass(feature);
}
/*
* CONST
*/
var TYPE_MOBILE = 1;
var TYPE_TOUCH = 2;
var TYPE_DESKTOP = 3;
var TABLET_BREAKPOINT = { width: 645, height: 645 };
/**
* Detect if the device is a touch device or not.
*
* @return {Boolean}
* @public
*/
var isTouchDevice = !!('ontouchstart' in window) || !!('onmsgesturechange' in window);
/**
* Detect if it's a mobile/tablet.
*
* @return {Boolean}
* @public
*/
var isMobile = (/android|webos|ip(hone|ad|od)|blackberry|iemobile|windows (ce|phone)|opera mini/i).test(ua.toLowerCase());
var isTablet = isMobile && (window.innerWidth > TABLET_BREAKPOINT.width || window.innerHeight > TABLET_BREAKPOINT.height);
var isChromeOs = /cros/.test(ua.toLowerCase());
/**
* Returns the type of the device (TYPE_MOBILE, TYPE_TOUCH, TYPE_DESKTOP).
*
* @return {Int} see const (TYPE_MOBILE, TYPE_TOUCH, TYPE_DESKTOP).
* @public
*/
var getType = (function() {
if (isMobile) {
return TYPE_MOBILE;
}
if (isTouchDevice) {
return TYPE_TOUCH;
}
return TYPE_DESKTOP;
}());
/**
* Use modernizr to detect if the "browser" support WebGL.
* @return {Boolean}
* @public
*/
var webgl = (function() {
try {
return !!window.WebGLRenderingContext && (!!document.createElement('canvas').getContext('experimental-webgl') || !!document.createElement('canvas').getContext('webgl'));
} catch(e) {
return false;
}
}());
var rtc = (function(){
return rtcSupported;
}());
/**
* Detect if we support this browser or not.
* @return {Boolean}
* @public
*/
var isBrowserSupported = _modernizr('canvas') && _modernizr('csstransforms') && _modernizr('csstransitions') && _modernizr('svg');
var isRetina = window.devicePixelRatio >= 2;
var isNexusPhone = (/nexus\s4|galaxy\snexus/i).test(ua);
var isNexusTablet = (/nexus\s7|nexus\s10/i).test(ua);
var isMozilla = !!~ua.indexOf('Gecko') && !~ua.indexOf('KHTML');
var isIE = (/MSIE (\d+\.\d+);/).test(ua);
var isiOS = (/ip(hone|od)/i).test(ua);
// Quick fix for ipad.
// Use the same layout/perf optimisation as the mobile version
if (isiOS) {
isMobile = true;
isTablet = false;
}
var hasPointerEvents = (function() {
if(navigator.appName == 'Microsoft Internet Explorer')
{
var agent = navigator.userAgent;
if (agent.match(/MSIE ([0-9]{1,}[\.0-9]{0,})/) != null){
var version = parseFloat( RegExp.$1 );
if(version < 11)
return false;
}
}
return true;
}());
var locale = (window.navigator.userLanguage || window.navigator.language).replace('-', '_');
$('html').addClass('locale_' + locale);
/**
* Expose data.
*/
module.exports = {
TYPE_MOBILE: TYPE_MOBILE,
TYPE_TOUCH: TYPE_TOUCH,
TYPE_DESKTOP: TYPE_DESKTOP,
isBrowserSupported: isBrowserSupported,
isTouchDevice: isTouchDevice,
isMobile: isMobile,
isTablet: isTablet,
isDesktop: !isMobile && !isTablet,
isRetina: isRetina,
getType: getType,
webgl: webgl,
rtc: rtc,
hasPointerEvents: hasPointerEvents,
//set from dmaf init
hasAudioSupport: false,
isNexusPhone: isNexusPhone,
isNexusTablet: isNexusTablet,
isMozilla: isMozilla,
isIE: isIE,
isiOS: isiOS,
isChromeOs: isChromeOs
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment