koel/resources/assets/js/utils/supports.js
2017-12-10 22:14:18 +01:00

79 lines
1.5 KiB
JavaScript

import isMobile from 'ismobilejs'
import Vue from 'vue'
import { each } from 'lodash'
/**
* Check if AudioContext is supported by the current browser.
*
* @return {Boolean}
*/
export function isAudioContextSupported () {
// Apple device just doesn't love AudioContext that much.
if (isMobile.apple.device) {
return false
}
const AudioContext = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext)
if (!AudioContext) {
return false
}
// Safari (MacOS & iOS alike) has webkitAudioContext, but is buggy.
// @link http://caniuse.com/#search=audiocontext
if (!(new AudioContext()).createMediaElementSource) {
return false
}
return true
}
/**
* Checks if HTML5 clipboard can be used.
* @return {Boolean}
*/
export function isClipboardSupported () {
return 'execCommand' in document
}
/**
* Checks if Media Session API is supported.
* @return {Boolean}
*/
export function isMediaSessionSupported () {
return 'mediaSession' in navigator
}
/**
* A simple event bus.
*
* @type {Object}
*/
class EventBus {
constructor () {
this.bus = new Vue()
}
emit (name, ...args) {
this.bus.$emit(name, ...args)
return this
}
on () {
if (arguments.length === 2) {
this.bus.$on(arguments[0], arguments[1])
} else {
each(Object.keys(arguments[0]), key => this.bus.$on(key, arguments[0][key]))
}
return this
}
}
const event = new EventBus()
export { event }