1
0
mirror of https://github.com/beestat/app.git synced 2025-08-07 09:01:32 -04:00

Added ability to detect touch-enabled devices...because why not?

This commit is contained in:
Jon Ziebell 2021-12-15 08:50:37 -05:00
parent b096b4e6c5
commit 3c30398304
2 changed files with 39 additions and 0 deletions

38
js/beestat/touch.js Normal file
View File

@ -0,0 +1,38 @@
/**
* Detect if the device is touch enabled.
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
* @return {boolean} Whether or not the device is touch enabled.
*/
beestat.touch = function() {
return true;
if (beestat.touch_ !== undefined) {
return beestat.touch_;
}
let touch = false;
if ('maxTouchPoints' in navigator) {
touch = navigator.maxTouchPoints > 0;
} else if ('msMaxTouchPoints' in navigator) {
touch = navigator.msMaxTouchPoints > 0;
} else {
var mq = window.matchMedia && matchMedia('(pointer:coarse)');
if (mq && mq.media === '(pointer:coarse)') {
touch = Boolean(mq.matches);
} else if ('orientation' in window) {
// Deprecated, but good fallback
touch = true;
} else {
// Only as a last resort, fall back to user agent sniffing
var UA = navigator.userAgent;
touch = (
(/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i).test(UA) ||
(/\b(Android|Windows Phone|iPad|iPod)\b/i).test(UA)
);
}
}
// Cache result
beestat.touch_ = touch;
return beestat.touch_;
};

View File

@ -37,6 +37,7 @@ if($setting->get('environment') === 'dev' || $setting->get('environment') === 'd
echo '<script src="/js/beestat/runtime_sensor.js"></script>' . PHP_EOL;
echo '<script src="/js/beestat/sensor.js"></script>' . PHP_EOL;
echo '<script src="/js/beestat/requestor.js"></script>' . PHP_EOL;
echo '<script src="/js/beestat/touch.js"></script>' . PHP_EOL;
// Layer
echo '<script src="/js/layer.js"></script>' . PHP_EOL;