2015-03-14 08:22:25 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-03-14 08:22:25 +00:00
|
|
|
*
|
|
|
|
* @providesModule WebView
|
2015-03-25 19:55:10 +00:00
|
|
|
* @flow
|
2015-03-14 08:22:25 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-04-01 01:46:14 +00:00
|
|
|
var ActivityIndicatorIOS = require('ActivityIndicatorIOS');
|
2015-03-14 08:22:25 +00:00
|
|
|
var EdgeInsetsPropType = require('EdgeInsetsPropType');
|
|
|
|
var React = require('React');
|
|
|
|
var ReactIOSViewAttributes = require('ReactIOSViewAttributes');
|
|
|
|
var StyleSheet = require('StyleSheet');
|
2015-04-01 01:46:14 +00:00
|
|
|
var Text = require('Text');
|
2015-03-14 08:22:25 +00:00
|
|
|
var View = require('View');
|
|
|
|
|
2015-04-22 04:07:17 +00:00
|
|
|
var invariant = require('invariant');
|
2015-03-14 08:22:25 +00:00
|
|
|
var keyMirror = require('keyMirror');
|
2015-04-22 04:07:17 +00:00
|
|
|
var requireNativeComponent = require('requireNativeComponent');
|
2015-03-14 08:22:25 +00:00
|
|
|
|
|
|
|
var PropTypes = React.PropTypes;
|
2015-03-18 05:22:03 +00:00
|
|
|
var RCTWebViewManager = require('NativeModules').WebViewManager;
|
2015-03-14 08:22:25 +00:00
|
|
|
|
2015-04-01 01:46:14 +00:00
|
|
|
var BGWASH = 'rgba(255,255,255,0.8)';
|
2015-03-17 10:08:46 +00:00
|
|
|
var RCT_WEBVIEW_REF = 'webview';
|
2015-03-14 08:22:25 +00:00
|
|
|
|
|
|
|
var WebViewState = keyMirror({
|
|
|
|
IDLE: null,
|
|
|
|
LOADING: null,
|
|
|
|
ERROR: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
var NavigationType = {
|
2015-03-17 10:08:46 +00:00
|
|
|
click: RCTWebViewManager.NavigationType.LinkClicked,
|
|
|
|
formsubmit: RCTWebViewManager.NavigationType.FormSubmitted,
|
|
|
|
backforward: RCTWebViewManager.NavigationType.BackForward,
|
|
|
|
reload: RCTWebViewManager.NavigationType.Reload,
|
|
|
|
formresubmit: RCTWebViewManager.NavigationType.FormResubmitted,
|
|
|
|
other: RCTWebViewManager.NavigationType.Other,
|
2015-03-14 08:22:25 +00:00
|
|
|
};
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
type ErrorEvent = {
|
|
|
|
domain: any;
|
|
|
|
code: any;
|
|
|
|
description: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Event = Object;
|
|
|
|
|
2015-04-01 01:46:14 +00:00
|
|
|
var defaultRenderLoading = () => (
|
|
|
|
<View style={styles.loadingView}>
|
|
|
|
<ActivityIndicatorIOS />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
var defaultRenderError = (errorDomain, errorCode, errorDesc) => (
|
|
|
|
<View style={styles.errorContainer}>
|
|
|
|
<Text style={styles.errorTextTitle}>
|
|
|
|
Error loading page
|
|
|
|
</Text>
|
|
|
|
<Text style={styles.errorText}>
|
|
|
|
{'Domain: ' + errorDomain}
|
|
|
|
</Text>
|
|
|
|
<Text style={styles.errorText}>
|
|
|
|
{'Error Code: ' + errorCode}
|
|
|
|
</Text>
|
|
|
|
<Text style={styles.errorText}>
|
|
|
|
{'Description: ' + errorDesc}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
var WebView = React.createClass({
|
|
|
|
statics: {
|
|
|
|
NavigationType: NavigationType,
|
|
|
|
},
|
|
|
|
|
|
|
|
propTypes: {
|
2015-03-31 17:05:59 +00:00
|
|
|
url: PropTypes.string,
|
|
|
|
html: PropTypes.string,
|
2015-04-01 01:46:14 +00:00
|
|
|
renderError: PropTypes.func, // view to show if there's an error
|
|
|
|
renderLoading: PropTypes.func, // loading indicator to show
|
2015-04-21 01:01:46 +00:00
|
|
|
bounces: PropTypes.bool,
|
|
|
|
scrollEnabled: PropTypes.bool,
|
2015-03-14 08:22:25 +00:00
|
|
|
automaticallyAdjustContentInsets: PropTypes.bool,
|
|
|
|
shouldInjectAJAXHandler: PropTypes.bool,
|
|
|
|
contentInset: EdgeInsetsPropType,
|
|
|
|
onNavigationStateChange: PropTypes.func,
|
|
|
|
startInLoadingState: PropTypes.bool, // force WebView to show loadingView on first load
|
|
|
|
style: View.propTypes.style,
|
2015-04-24 18:58:31 +00:00
|
|
|
/**
|
|
|
|
* Used for android only, JS is enabled by default for WebView on iOS
|
|
|
|
*/
|
|
|
|
javaScriptEnabledAndroid: PropTypes.bool,
|
2015-03-14 08:22:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
viewState: WebViewState.IDLE,
|
2015-03-25 19:55:10 +00:00
|
|
|
lastErrorEvent: (null: ?ErrorEvent),
|
2015-03-14 08:22:25 +00:00
|
|
|
startInLoadingState: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
if (this.props.startInLoadingState) {
|
|
|
|
this.setState({viewState: WebViewState.LOADING});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var otherView = null;
|
|
|
|
|
2015-04-01 01:46:14 +00:00
|
|
|
if (this.state.viewState === WebViewState.LOADING) {
|
|
|
|
otherView = (this.props.renderLoading || defaultRenderLoading)();
|
2015-03-14 08:22:25 +00:00
|
|
|
} else if (this.state.viewState === WebViewState.ERROR) {
|
|
|
|
var errorEvent = this.state.lastErrorEvent;
|
2015-03-25 19:55:10 +00:00
|
|
|
invariant(
|
|
|
|
errorEvent != null,
|
|
|
|
'lastErrorEvent expected to be non-null'
|
|
|
|
);
|
2015-04-01 01:46:14 +00:00
|
|
|
otherView = (this.props.renderError || defaultRenderError)(
|
2015-03-14 08:22:25 +00:00
|
|
|
errorEvent.domain,
|
|
|
|
errorEvent.code,
|
2015-04-01 01:46:14 +00:00
|
|
|
errorEvent.description
|
|
|
|
);
|
2015-03-14 08:22:25 +00:00
|
|
|
} else if (this.state.viewState !== WebViewState.IDLE) {
|
2015-04-01 01:46:14 +00:00
|
|
|
console.error(
|
|
|
|
'RCTWebView invalid state encountered: ' + this.state.loading
|
|
|
|
);
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 01:01:46 +00:00
|
|
|
var webViewStyles = [styles.container, styles.webView, this.props.style];
|
2015-03-14 08:22:25 +00:00
|
|
|
if (this.state.viewState === WebViewState.LOADING ||
|
|
|
|
this.state.viewState === WebViewState.ERROR) {
|
|
|
|
// if we're in either LOADING or ERROR states, don't show the webView
|
|
|
|
webViewStyles.push(styles.hidden);
|
|
|
|
}
|
|
|
|
|
|
|
|
var webView =
|
|
|
|
<RCTWebView
|
2015-03-17 10:08:46 +00:00
|
|
|
ref={RCT_WEBVIEW_REF}
|
2015-03-14 08:22:25 +00:00
|
|
|
key="webViewKey"
|
|
|
|
style={webViewStyles}
|
|
|
|
url={this.props.url}
|
2015-03-31 17:05:59 +00:00
|
|
|
html={this.props.html}
|
2015-04-21 01:01:46 +00:00
|
|
|
bounces={this.props.bounces}
|
|
|
|
scrollEnabled={this.props.scrollEnabled}
|
2015-03-14 08:22:25 +00:00
|
|
|
shouldInjectAJAXHandler={this.props.shouldInjectAJAXHandler}
|
|
|
|
contentInset={this.props.contentInset}
|
|
|
|
automaticallyAdjustContentInsets={this.props.automaticallyAdjustContentInsets}
|
|
|
|
onLoadingStart={this.onLoadingStart}
|
|
|
|
onLoadingFinish={this.onLoadingFinish}
|
|
|
|
onLoadingError={this.onLoadingError}
|
|
|
|
/>;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
{webView}
|
|
|
|
{otherView}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
goForward: function() {
|
2015-03-17 10:08:46 +00:00
|
|
|
RCTWebViewManager.goForward(this.getWebWiewHandle());
|
2015-03-14 08:22:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
goBack: function() {
|
2015-03-17 10:08:46 +00:00
|
|
|
RCTWebViewManager.goBack(this.getWebWiewHandle());
|
2015-03-14 08:22:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
reload: function() {
|
2015-03-17 10:08:46 +00:00
|
|
|
RCTWebViewManager.reload(this.getWebWiewHandle());
|
2015-03-14 08:22:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We return an event with a bunch of fields including:
|
|
|
|
* url, title, loading, canGoBack, canGoForward
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
updateNavigationState: function(event: Event) {
|
2015-03-14 08:22:25 +00:00
|
|
|
if (this.props.onNavigationStateChange) {
|
|
|
|
this.props.onNavigationStateChange(event.nativeEvent);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
getWebWiewHandle: function(): any {
|
2015-03-17 10:08:46 +00:00
|
|
|
return this.refs[RCT_WEBVIEW_REF].getNodeHandle();
|
2015-03-14 08:22:25 +00:00
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
onLoadingStart: function(event: Event) {
|
2015-03-14 08:22:25 +00:00
|
|
|
this.updateNavigationState(event);
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
onLoadingError: function(event: Event) {
|
2015-03-14 08:22:25 +00:00
|
|
|
event.persist(); // persist this event because we need to store it
|
|
|
|
console.error("encountered an error loading page", event.nativeEvent);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
lastErrorEvent: event.nativeEvent,
|
|
|
|
viewState: WebViewState.ERROR
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
onLoadingFinish: function(event: Event) {
|
2015-03-14 08:22:25 +00:00
|
|
|
this.setState({
|
|
|
|
viewState: WebViewState.IDLE,
|
|
|
|
});
|
|
|
|
this.updateNavigationState(event);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-04-22 04:07:17 +00:00
|
|
|
var RCTWebView = requireNativeComponent('RCTWebView', WebView);
|
2015-03-14 08:22:25 +00:00
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
2015-04-01 01:46:14 +00:00
|
|
|
errorContainer: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: BGWASH,
|
|
|
|
},
|
|
|
|
errorText: {
|
|
|
|
fontSize: 14,
|
|
|
|
textAlign: 'center',
|
|
|
|
marginBottom: 2,
|
|
|
|
},
|
|
|
|
errorTextTitle: {
|
|
|
|
fontSize: 15,
|
|
|
|
fontWeight: '500',
|
|
|
|
marginBottom: 10,
|
|
|
|
},
|
2015-03-14 08:22:25 +00:00
|
|
|
hidden: {
|
|
|
|
height: 0,
|
|
|
|
flex: 0, // disable 'flex:1' when hiding a View
|
|
|
|
},
|
2015-04-01 01:46:14 +00:00
|
|
|
loadingView: {
|
|
|
|
backgroundColor: BGWASH,
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
2015-04-21 01:01:46 +00:00
|
|
|
webView: {
|
|
|
|
backgroundColor: '#ffffff',
|
|
|
|
}
|
2015-03-14 08:22:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = WebView;
|