-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (137 loc) · 4.02 KB
/
index.js
File metadata and controls
157 lines (137 loc) · 4.02 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import Qs from 'qs';
import React, { Component } from 'react';
import { StyleSheet, View, ActivityIndicator, unstable_createElement as createElement } from 'react-native';
export class WebView extends Component {
static defaultProps = {
scrollEnabled: true,
};
state = { html: null };
constructor(props) {
super(props);
this.handleSource(props.source, props.newWindow);
}
setRef = (ref) => (this.frameRef = ref);
handleSource = (source, newWindow) => {
if (!source.method) return;
if (newWindow) {
this.handleSourceInNewWindow(source, newWindow);
} else {
this.handleSourceInIFrame(source);
}
};
handleSourceInIFrame = (source) => {
const { uri, ...options } = source;
const baseUrl = uri.substr(0, uri.lastIndexOf('/') + 1);
fetch(uri, options)
.then((response) => response.text())
.then((html) => this.setState({ html: `<base href="${baseUrl}" />` + html }));
};
handleSourceInNewWindow = (source, newWindow) => {
if (source.method === 'POST') {
const contentType = source.headers['Content-Type'];
let body = '';
if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
body = Qs.parse(source.body);
} else {
console.warn(
'[WebView] When opening a new window, this content-type is not supported yet, please make a PR!',
contentType
);
return;
}
window.open(
require('./postMock.html') +
'?' +
Qs.stringify({
uri: source.uri,
body: JSON.stringify(body),
}),
newWindow.name || 'webview',
newWindow.features || undefined
);
} else {
console.warn(
'[WebView] When opening a new window, this method is not supported yet, please make a PR!',
source.method
);
}
};
componentDidMount() {
if (this.props.onMessage) {
window.addEventListener('message', this.onMessage, true);
}
}
componentWillReceiveProps(nextProps) {
if (
this.props.source.uri !== nextProps.source.uri ||
this.props.source.method !== nextProps.source.method ||
this.props.source.body !== nextProps.source.body
) {
this.handleSource(nextProps.source, nextProps.newWindow);
}
}
componentWillUnmount() {
if (this.props.onMessage) {
window.removeEventListener('message', this.onMessage, true);
}
}
onMessage = (nativeEvent) => this.props.onMessage({ nativeEvent });
postMessage = (message, origin) => {
this.frameRef.contentWindow.postMessage(message, origin);
};
handleInjectedJavaScript = (html) => {
if (this.props.injectedJavaScript) {
if (html) {
return html.replace('</body>', `<script>${this.props.injectedJavaScript}</script></body>`);
} else {
return html;
}
} else {
return html;
}
};
injectJavaScript = (js) => {
this.frameRef.contentWindow.Function(js)();
};
render() {
if (this.props.newWindow) {
return (
<View style={styles.loadingContainer}>
<ActivityIndicator />
</View>
);
}
const { title, source, onLoad, scrollEnabled } = this.props;
const styleObj = StyleSheet.flatten(this.props.style);
return createElement('iframe', {
title,
ref: this.setRef,
src: !source.method ? source.uri : undefined,
srcDoc: this.handleInjectedJavaScript(this.state.html || source.html),
width: styleObj && styleObj.width,
height: styleObj && styleObj.height,
style: StyleSheet.flatten([styles.iframe, scrollEnabled && styles.noScroll, this.props.style]),
allowFullScreen: true,
allowpaymentrequest: 'true',
frameBorder: '0',
seamless: true,
onLoad,
});
}
}
export default WebView;
const styles = StyleSheet.create({
loadingContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
iframe: {
width: '100%',
height: '100%',
borderWidth: 0,
},
noScroll: {
overflow: 'hidden',
},
});