iwmlib/lib/capabilities.js

183 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-03-21 09:57:27 +01:00
/** Report capabilities with guaranteed values.
*/
export class Capabilities {
/** Returns the browser userAgent.
@return {string}
*/
static get userAgent() {
return navigator.userAgent || 'Unknown Agent'
}
/** Tests whether the app is running on a mobile device.
Implemented as a readonly attribute.
@return {boolean}
*/
static get isMobile() {
2019-07-18 12:26:39 +02:00
return /Mobi/.test(navigator.userAgent)
2019-03-21 09:57:27 +01:00
}
/** Tests whether the app is running on a iOS device.
Implemented as a readonly attribute.
@return {boolean}
*/
static get isIOS() {
2019-07-18 12:26:39 +02:00
return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream
2019-03-21 09:57:27 +01:00
}
/** Tests whether the app is running in a Safari environment.
See https://stackoverflow.com/questions/7944460/detect-safari-browser
Implemented as a readonly attribute.
@return {boolean}
*/
static get isSafari() {
2019-07-18 12:26:39 +02:00
return (
navigator.vendor &&
navigator.vendor.indexOf('Apple') > -1 &&
navigator.userAgent &&
!navigator.userAgent.match('CriOS')
)
2019-03-21 09:57:27 +01:00
}
/**
* Distincts if the app is running inside electron or not.
2019-07-18 12:26:39 +02:00
*
* source: https://github.com/cheton/is-electron
2019-03-21 09:57:27 +01:00
*/
static get isElectron() {
// Renderer process
2019-07-18 12:26:39 +02:00
if (
typeof window !== 'undefined' &&
typeof window.process === 'object' &&
window.process.type === 'renderer'
) {
return true
}
// Main process
2019-07-18 12:26:39 +02:00
if (
typeof process !== 'undefined' &&
typeof process.versions === 'object' &&
!!process.versions.electron
) {
return true
}
// Detect the user agent when the `nodeIntegration` option is set to true
2019-07-18 12:26:39 +02:00
if (
typeof navigator === 'object' &&
typeof navigator.userAgent === 'string' &&
navigator.userAgent.indexOf('Electron') >= 0
) {
return true
}
return false
2019-03-21 09:57:27 +01:00
}
/** Returns the display resolution. Necessary for retina displays.
@return {number}
*/
static get devicePixelRatio() {
return window.devicePixelRatio || 1
}
/** Returns true if the device is a multi-touch table. This method is currently not universal usable and not sure!
@return {boolean}
*/
static get isMultiTouchTable() {
2019-07-18 12:26:39 +02:00
return (
Capabilities.devicePixelRatio > 2 &&
Capabilities.isMobile === false &&
/Windows/i.test(Capabilities.userAgent)
)
2019-03-21 09:57:27 +01:00
}
/** Returns true if mouse events are supported
@return {boolean}
*/
static supportsMouseEvents() {
2019-07-18 12:26:39 +02:00
return typeof window.MouseEvent != 'undefined'
2019-03-21 09:57:27 +01:00
}
/** Returns true if touch events are supported
@return {boolean}
*/
static supportsTouchEvents() {
2019-07-18 12:26:39 +02:00
return typeof window.TouchEvent != 'undefined'
2019-03-21 09:57:27 +01:00
}
/** Returns true if pointer events are supported
@return {boolean}
*/
static supportsPointerEvents() {
2019-07-18 12:26:39 +02:00
return typeof window.PointerEvent != 'undefined'
2019-03-21 09:57:27 +01:00
}
/** Returns true if DOM templates are supported
@return {boolean}
*/
static supportsTemplate() {
2019-07-18 12:26:39 +02:00
return 'content' in document.createElement('template')
2019-03-21 09:57:27 +01:00
}
}
/** Basic tests for Capabilities.
*/
export class CapabilitiesTests {
static testConfirm() {
let bool = confirm('Please confirm')
2019-07-18 12:26:39 +02:00
document.getElementById('demo').innerHTML = bool
? 'Confirmed'
: 'Not confirmed'
2019-03-21 09:57:27 +01:00
}
static testPrompt() {
let person = prompt('Please enter your name', 'Harry Potter')
if (person != null) {
2019-07-18 12:26:39 +02:00
demo.innerHTML = 'Hello ' + person + '! How are you today?'
2019-03-21 09:57:27 +01:00
}
}
static testUserAgent() {
let agent = 'User-agent: ' + Capabilities.userAgent
user_agent.innerHTML = agent
}
static testDevicePixelRatio() {
let value = 'Device Pixel Ratio: ' + Capabilities.devicePixelRatio
device_pixel_ratio.innerHTML = value
}
static testMultiTouchTable() {
2019-07-18 12:26:39 +02:00
let value =
'Is the device a multi-touch table? ' +
Capabilities.isMultiTouchTable
2019-03-21 09:57:27 +01:00
multi_touch_table.innerHTML = value
}
static testSupportedEvents() {
let events = []
if (Capabilities.supportsMouseEvents()) {
events.push('MouseEvents')
}
if (Capabilities.supportsTouchEvents()) {
events.push('TouchEvents')
}
if (Capabilities.supportsPointerEvents()) {
events.push('PointerEvents')
}
supported_events.innerHTML = 'Supported Events: ' + events.join(', ')
}
static testAll() {
this.testUserAgent()
this.testDevicePixelRatio()
this.testMultiTouchTable()
this.testSupportedEvents()
}
}
/* Optional global variables, needed in DocTests. */
window.Capabilities = Capabilities
window.CapabilitiesTests = CapabilitiesTests