| 1 |
/** |
| 2 |
* Optimole Device Detection Module |
| 3 |
* Handles device type detection based on screen width |
| 4 |
*/ |
| 5 |
|
| 6 |
import { optmlLogger } from './logger.js'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Device detection utilities |
| 10 |
*/ |
| 11 |
export const optmlDevice = { |
| 12 |
/** |
| 13 |
* Device type constants |
| 14 |
*/ |
| 15 |
DEVICE_TYPES: { |
| 16 |
MOBILE: 1, |
| 17 |
DESKTOP: 2 |
| 18 |
}, |
| 19 |
|
| 20 |
/** |
| 21 |
* Screen width threshold for mobile/desktop detection |
| 22 |
* This is similar to what PageSpeed Insights uses |
| 23 |
*/ |
| 24 |
MOBILE_BREAKPOINT: 600, |
| 25 |
|
| 26 |
/** |
| 27 |
* Determine device type based on screen width |
| 28 |
* @returns {number} Device type (1=mobile, 2=desktop) |
| 29 |
*/ |
| 30 |
getDeviceType: function() { |
| 31 |
const width = window.innerWidth; |
| 32 |
|
| 33 |
if (width <= this.MOBILE_BREAKPOINT) { |
| 34 |
optmlLogger.info('Device detected as mobile based on width:', width); |
| 35 |
return this.DEVICE_TYPES.MOBILE; |
| 36 |
} |
| 37 |
|
| 38 |
optmlLogger.info('Device detected as desktop based on width:', width); |
| 39 |
return this.DEVICE_TYPES.DESKTOP; |
| 40 |
}, |
| 41 |
|
| 42 |
/** |
| 43 |
* Check if current device is mobile |
| 44 |
* @returns {boolean} True if mobile device |
| 45 |
*/ |
| 46 |
isMobile: function() { |
| 47 |
return this.getDeviceType() === this.DEVICE_TYPES.MOBILE; |
| 48 |
}, |
| 49 |
|
| 50 |
/** |
| 51 |
* Check if current device is desktop |
| 52 |
* @returns {boolean} True if desktop device |
| 53 |
*/ |
| 54 |
isDesktop: function() { |
| 55 |
return this.getDeviceType() === this.DEVICE_TYPES.DESKTOP; |
| 56 |
} |
| 57 |
}; |
| 58 |
|