-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
49 lines (38 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {ContainerPlugin} from 'clappr'
export default class ClapprPauseTabVisibility extends ContainerPlugin {
get name() {return 'clappr_pause_tab_visibility'}
bindEvents() {
var [hidden, visibilityEvent] = this.resoleVisibilityAPI()
this.visibilityEvent = visibilityEvent
this.originalTitle = document.title
this.onVisibilityChange = () => this.togglePauseResume(hidden)
document.addEventListener(this.visibilityEvent, this.onVisibilityChange)
}
resoleVisibilityAPI() {
var compatiblePropertyAndEvent = []
var hiddenApi = ['hidden','webkithidden','mozhidden','mshidden','ohidden']
var visibilityEventApi = ['visibilitychange','webkitvisibilitychange','mozvisibilitychange','msvisibility','ovisibility']
hiddenApi.forEach((property, index) => {
if (property in document) {
compatiblePropertyAndEvent.push(property)
compatiblePropertyAndEvent.push(visibilityEventApi[index])
}
})
return compatiblePropertyAndEvent
}
togglePauseResume(hidden) {
var command = document[hidden] ? 'pause': 'play'
hidden && this[command]()
}
play() {
this.container.play()
if (this.options.visibilityEnableIcon) { document.title = this.originalTitle }
}
pause() {
this.container.pause()
if (this.options.visibilityEnableIcon) { document.title = `\u23F8 ${this.originalTitle}` }
}
stopListening() {
document.removeEventListener(this.visibilityEvent, this.onVisibilityChange)
}
}