-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathFacebook.js
117 lines (100 loc) · 3.08 KB
/
Facebook.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React, { Component } from 'react'
import { callPlayer, getSDK, randomString } from '../utils'
import createSinglePlayer from '../singlePlayer'
const SDK_URL = '//connect.facebook.net/en_US/sdk.js'
const SDK_GLOBAL = 'FB'
const SDK_GLOBAL_READY = 'fbAsyncInit'
const MATCH_URL = /facebook\.com\/([^/?].+\/)?video(s|\.php)[/?].*$/
const PLAYER_ID_PREFIX = 'facebook-player-'
export class Facebook extends Component {
static displayName = 'Facebook'
static canPlay = url => MATCH_URL.test(url)
static loopOnEnded = true
callPlayer = callPlayer
playerID = PLAYER_ID_PREFIX + randomString()
load (url, isReady) {
if (isReady) {
getSDK(SDK_URL, SDK_GLOBAL, SDK_GLOBAL_READY).then(FB => FB.XFBML.parse())
return
}
getSDK(SDK_URL, SDK_GLOBAL, SDK_GLOBAL_READY).then(FB => {
FB.init({
appId: this.props.config.facebook.appId,
xfbml: true,
version: 'v2.5'
})
FB.Event.subscribe('xfbml.render', msg => {
// Here we know the SDK has loaded, even if onReady/onPlay
// is not called due to a video that cannot be embedded
this.props.onLoaded()
})
FB.Event.subscribe('xfbml.ready', msg => {
if (msg.type === 'video' && msg.id === this.playerID) {
this.player = msg.instance
this.player.subscribe('startedPlaying', this.props.onPlay)
this.player.subscribe('paused', this.props.onPause)
this.player.subscribe('finishedPlaying', this.props.onEnded)
this.player.subscribe('startedBuffering', this.props.onBuffer)
this.player.subscribe('finishedBuffering', this.props.onBufferEnd)
this.player.subscribe('error', this.props.onError)
if (!this.props.muted) {
// Player is muted by default
this.callPlayer('unmute')
}
this.props.onReady()
// For some reason Facebook have added `visibility: hidden`
// to the iframe when autoplay fails, so here we set it back
document.getElementById(this.playerID).querySelector('iframe').style.visibility = 'visible'
}
})
})
}
play () {
this.callPlayer('play')
}
pause () {
this.callPlayer('pause')
}
stop () {
// Nothing to do
}
seekTo (seconds) {
this.callPlayer('seek', seconds)
}
setVolume (fraction) {
this.callPlayer('setVolume', fraction)
}
mute = () => {
this.callPlayer('mute')
}
unmute = () => {
this.callPlayer('unmute')
}
getDuration () {
return this.callPlayer('getDuration')
}
getCurrentTime () {
return this.callPlayer('getCurrentPosition')
}
getSecondsLoaded () {
return null
}
render () {
const style = {
width: '100%',
height: '100%'
}
return (
<div
style={style}
id={this.playerID}
className='fb-video'
data-href={this.props.url}
data-autoplay={this.props.playing ? 'true' : 'false'}
data-allowfullscreen='true'
data-controls={this.props.controls ? 'true' : 'false'}
/>
)
}
}
export default createSinglePlayer(Facebook)