forked from leandromoreira/clappr-pause-tab-visibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (46 loc) · 1.63 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
49
50
51
52
53
54
55
56
57
58
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.wasPlaying = false;
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() {
if(!this.wasPlaying) {
return;
}
this.container.play()
if (this.options.visibilityEnableIcon) { document.title = this.originalTitle }
}
pause() {
this.wasPlaying = this.container.isPlaying()
if(!this.wasPlaying) {
return;
}
this.container.pause()
if (this.options.visibilityEnableIcon) { document.title = `\u23F8 ${this.originalTitle}` }
}
stopListening() {
document.removeEventListener(this.visibilityEvent, this.onVisibilityChange)
}
}