-
Notifications
You must be signed in to change notification settings - Fork 31
/
Vimeo.ios.js
100 lines (85 loc) · 2.27 KB
/
Vimeo.ios.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
/**
* @providesModule Vimeo
* @flow
*/
import React from 'react-native';
const {
StyleSheet,
PropTypes
} = React;
import WebViewBridge from 'react-native-webview-bridge';
function getVimeoPageURL(videoId) {
return 'https://myagi.github.io/react-native-vimeo/v0.3.0.html?vid=' + videoId;
}
export default class Vimeo extends React.Component {
static propTypes = {
videoId: PropTypes.string.isRequired,
onReady: PropTypes.func,
onPlay: PropTypes.func,
onPlayProgress: PropTypes.func,
onPause: PropTypes.func,
onFinish: PropTypes.func
}
constructor() {
super();
this.handlers = {};
this.state = {
ready: false
};
}
componentDidMount() {
this.registerHandlers();
}
componentWillReceiveProps() {
this.registerHandlers();
}
api(method, cb) {
if (!this.state.ready) {
throw new Error('You cannot use the `api` method until `onReady` has been called');
}
this.refs.webviewBridge.sendToBridge(method);
this.registerBridgeEventHandler(method, cb);
}
registerHandlers() {
this.registerBridgeEventHandler('ready', this.onReady);
this.registerBridgeEventHandler('play', this.props.onPlay);
this.registerBridgeEventHandler('playProgress', this.props.onPlayProgress);
this.registerBridgeEventHandler('pause', this.props.onPause);
this.registerBridgeEventHandler('finish', this.props.onFinish);
}
registerBridgeEventHandler(eventName, handler) {
this.handlers[eventName] = handler;
}
onBridgeMessage = (message) => {
let payload;
try {
payload = JSON.parse(message);
} catch (err) {
return;
}
let handler = this.handlers[payload.name];
if (handler) handler(payload.data);
}
onReady = () => {
this.setState({ready: true});
if (this.props.onReady) this.props.onReady();
}
render() {
return (
<WebViewBridge
ref='webviewBridge'
style={{
// Accounts for player border
marginTop: -8,
marginLeft: -10,
height: this.props.height
}}
source={{uri: getVimeoPageURL(this.props.videoId)}}
scalesPageToFit={false}
scrollEnabled={false}
onBridgeMessage={this.onBridgeMessage}
onError={(error)=> console.error(error)}
/>
);
}
}