-
-
Save ingmarh/3d2d77fc6a62b4227170 to your computer and use it in GitHub Desktop.
Basic environment flags for Cordova apps with installed Device plugin. Note that the Device plugin is not available before the Cordova "deviceready" event has been fired.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
env = (function(){ | |
var flags = {}, i, osVersion = device.version + '', androidVersion, | |
el = document.createElement('div'), root = document.documentElement | |
function flag(names) { | |
names = names.split(' ') | |
for (i = 0; i < names.length; i++) flags[names[i]] = true | |
} | |
function classnames() { | |
var names = [], name | |
for(name in flags) if (flags.hasOwnProperty(name)) names.push(name) | |
root.className += (root.className ? ' ' : '') + names.join(' ') | |
} | |
if (device.platform) flag(device.platform.replace(/-/g, '').toLowerCase()) | |
if (flags.amazonfireos) flag('android') | |
if (flags.ios) { | |
flag('ios' + osVersion.match(/\d+/)) | |
if ('i386 x86_64'.indexOf(device.model) > -1) { | |
flag('simulator') | |
} else if (device.model.match(/^(iPad|iPod|iPhone)/)) { | |
flag(RegExp.$1.toLowerCase()) | |
} | |
} | |
if (flags.android) { | |
androidVersion = osVersion.match(/^\d+\.\d+/) + '' | |
if (androidVersion === '2.2') flag('froyo') | |
if (androidVersion === '2.3') flag('gingerbread') | |
if (androidVersion === '3.2') flag('honeycomb') | |
if (androidVersion === '4.0') flag('icecreamsandwich') | |
if ('4.1 4.2 4.3'.indexOf(androidVersion) > -1) flag('jellybean') | |
if (androidVersion === '4.4') flag('kitkat') | |
if (androidVersion === '5.0') flag('lollipop') | |
// Android Virtual Device (emulator) | |
if ('sdk google_sdk'.indexOf(device.model) > -1) flag('avd') | |
} | |
el.setAttribute('ontouchstart', 'return;') | |
if (typeof el.ontouchstart === 'function') flag('touch') | |
if (navigator.msPointerEnabled && navigator.msMaxTouchPoints > 1) flag('touch') | |
if (navigator.devicePixelRatio && navigator.devicePixelRatio >= 2) flag('retina') | |
if (screen.width > 1280) flag('hd') | |
if (screen.width < 768) flag('narrow') | |
classnames() | |
return flags | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment