react-native/Libraries/Components/WebView/WebView.ios.js

254 lines
6.7 KiB
JavaScript
Raw Normal View History

2015-03-14 08:22:25 +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';
var ActivityIndicatorIOS = require('ActivityIndicatorIOS');
2015-03-14 08:22:25 +00:00
var EdgeInsetsPropType = require('EdgeInsetsPropType');
var React = require('React');
var StyleSheet = require('StyleSheet');
var Text = require('Text');
2015-03-14 08:22:25 +00:00
var View = require('View');
var invariant = require('invariant');
2015-03-14 08:22:25 +00:00
var keyMirror = require('keyMirror');
var requireNativeComponent = require('requireNativeComponent');
2015-03-14 08:22:25 +00:00
var PropTypes = React.PropTypes;
var RCTWebViewManager = require('NativeModules').WebViewManager;
2015-03-14 08:22:25 +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;
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: {
url: PropTypes.string,
html: PropTypes.string,
renderError: PropTypes.func, // view to show if there's an error
renderLoading: PropTypes.func, // loading indicator to show
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,
/**
* 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;
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'
);
otherView = (this.props.renderError || defaultRenderError)(
2015-03-14 08:22:25 +00:00
errorEvent.domain,
errorEvent.code,
errorEvent.description
);
2015-03-14 08:22:25 +00:00
} else if (this.state.viewState !== WebViewState.IDLE) {
console.error(
'RCTWebView invalid state encountered: ' + this.state.loading
);
2015-03-14 08:22:25 +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}
html={this.props.html}
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 {
return React.findNodeHandle(this.refs[RCT_WEBVIEW_REF]);
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);
2015-03-14 08:22:25 +00:00
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);
},
});
var RCTWebView = requireNativeComponent('RCTWebView', WebView);
2015-03-14 08:22:25 +00:00
var styles = StyleSheet.create({
container: {
flex: 1,
},
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
},
loadingView: {
backgroundColor: BGWASH,
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
webView: {
backgroundColor: '#ffffff',
}
2015-03-14 08:22:25 +00:00
});
module.exports = WebView;