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 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-07-02 01:06:39 +00:00
|
|
|
var JSNavigationScheme = RCTWebViewManager.JSNavigationScheme;
|
|
|
|
|
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: {
|
2015-07-02 01:06:39 +00:00
|
|
|
JSNavigationScheme: JSNavigationScheme,
|
2015-03-14 08:22:25 +00:00
|
|
|
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,
|
|
|
|
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-07-02 01:06:39 +00:00
|
|
|
/**
|
2015-07-08 00:07:52 +00:00
|
|
|
* Sets the JS to be injected when the webpage loads.
|
2015-07-02 01:06:39 +00:00
|
|
|
*/
|
2015-07-08 00:07:52 +00:00
|
|
|
injectedJavaScript: PropTypes.string,
|
2015-07-02 01:06:39 +00:00
|
|
|
|
2015-06-17 20:56:14 +00:00
|
|
|
/**
|
|
|
|
* Used for iOS only, sets whether the webpage scales to fit the view and the
|
|
|
|
* user can change the scale
|
|
|
|
*/
|
|
|
|
scalesPageToFit: 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-07-08 00:07:52 +00:00
|
|
|
injectedJavaScript={this.props.injectedJavaScript}
|
2015-04-21 01:01:46 +00:00
|
|
|
bounces={this.props.bounces}
|
|
|
|
scrollEnabled={this.props.scrollEnabled}
|
2015-03-14 08:22:25 +00:00
|
|
|
contentInset={this.props.contentInset}
|
|
|
|
automaticallyAdjustContentInsets={this.props.automaticallyAdjustContentInsets}
|
|
|
|
onLoadingStart={this.onLoadingStart}
|
|
|
|
onLoadingFinish={this.onLoadingFinish}
|
|
|
|
onLoadingError={this.onLoadingError}
|
2015-06-17 20:56:14 +00:00
|
|
|
scalesPageToFit={this.props.scalesPageToFit}
|
2015-03-14 08:22:25 +00:00
|
|
|
/>;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
{webView}
|
|
|
|
{otherView}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
goForward: function() {
|
2015-07-08 00:07:52 +00:00
|
|
|
RCTWebViewManager.goForward(this.getWebViewHandle());
|
2015-03-14 08:22:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
goBack: function() {
|
2015-07-08 00:07:52 +00:00
|
|
|
RCTWebViewManager.goBack(this.getWebViewHandle());
|
2015-03-14 08:22:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
reload: function() {
|
2015-07-08 00:07:52 +00:00
|
|
|
RCTWebViewManager.reload(this.getWebViewHandle());
|
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-07-08 00:07:52 +00:00
|
|
|
getWebViewHandle: function(): any {
|
2015-05-13 01:55:13 +00:00
|
|
|
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
|
2015-08-27 21:04:59 +00:00
|
|
|
console.warn('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);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
var RCTWebView = requireNativeComponent('RCTWebView', WebView, {
|
|
|
|
nativeOnly: {
|
|
|
|
onLoadingStart: true,
|
|
|
|
onLoadingError: true,
|
|
|
|
onLoadingFinish: true,
|
|
|
|
},
|
|
|
|
});
|
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;
|