2
2
* Copyright (c) 2015-present, Facebook, Inc.
3
3
* All rights reserved.
4
4
*
5
+ * Copyright (c) 2016-present, Ali Najafizadeh
6
+ * All rights reserved.
7
+ *
5
8
* This source code is licensed under the BSD-style license found in the
6
9
* LICENSE file in the root directory of this source tree. An additional grant
7
10
* of patent rights can be found in the PATENTS file in the same directory.
8
11
*
9
- * Copyright (c) 2015-present, Ali Najafizadeh (github.com/alinz)
10
- * All rights reserved
11
- *
12
12
* @providesModule WebViewBridge
13
13
* @flow
14
14
*/
@@ -26,13 +26,16 @@ var {
26
26
View,
27
27
requireNativeComponent,
28
28
PropTypes,
29
+ UIManager,
29
30
NativeModules : {
30
31
WebViewBridgeManager
31
32
}
32
33
} = React ;
33
34
34
35
var BGWASH = 'rgba(255,255,255,0.8)' ;
35
- var RCT_WEBVIEW_BRIDGE_REF = 'webviewbridge' ;
36
+ var RCT_WEBVIEWBRIDGE_REF = 'webviewbridge' ;
37
+
38
+ var RCTWebViewBridgeManager = WebViewBridgeManager ;
36
39
37
40
var WebViewBridgeState = keyMirror ( {
38
41
IDLE : null ,
@@ -41,15 +44,15 @@ var WebViewBridgeState = keyMirror({
41
44
} ) ;
42
45
43
46
var NavigationType = {
44
- click : WebViewBridgeManager . NavigationType . LinkClicked ,
45
- formsubmit : WebViewBridgeManager . NavigationType . FormSubmitted ,
46
- backforward : WebViewBridgeManager . NavigationType . BackForward ,
47
- reload : WebViewBridgeManager . NavigationType . Reload ,
48
- formresubmit : WebViewBridgeManager . NavigationType . FormResubmitted ,
49
- other : WebViewBridgeManager . NavigationType . Other ,
47
+ click : RCTWebViewBridgeManager . NavigationType . LinkClicked ,
48
+ formsubmit : RCTWebViewBridgeManager . NavigationType . FormSubmitted ,
49
+ backforward : RCTWebViewBridgeManager . NavigationType . BackForward ,
50
+ reload : RCTWebViewBridgeManager . NavigationType . Reload ,
51
+ formresubmit : RCTWebViewBridgeManager . NavigationType . FormResubmitted ,
52
+ other : RCTWebViewBridgeManager . NavigationType . Other ,
50
53
} ;
51
54
52
- var JSNavigationScheme = WebViewBridgeManager . JSNavigationScheme ;
55
+ var JSNavigationScheme = RCTWebViewBridgeManager . JSNavigationScheme ;
53
56
54
57
type ErrorEvent = {
55
58
domain : any ;
@@ -83,9 +86,6 @@ var defaultRenderError = (errorDomain, errorCode, errorDesc) => (
83
86
84
87
/**
85
88
* Renders a native WebView.
86
- *
87
- * Note that WebView is only supported on iOS for now,
88
- * see https://facebook.github.io/react-native/docs/known-issues.html
89
89
*/
90
90
var WebViewBridge = React . createClass ( {
91
91
statics : {
@@ -97,9 +97,37 @@ var WebViewBridge = React.createClass({
97
97
...View . propTypes ,
98
98
url : PropTypes . string ,
99
99
html : PropTypes . string ,
100
+ /**
101
+ * Function that returns a view to show if there's an error.
102
+ */
100
103
renderError : PropTypes . func , // view to show if there's an error
101
- renderLoading : PropTypes . func , // loading indicator to show
104
+ /**
105
+ * Function that returns a loading indicator.
106
+ */
107
+ renderLoading : PropTypes . func ,
108
+ /**
109
+ * Invoked when load finish
110
+ */
111
+ onLoad : PropTypes . func ,
112
+ /**
113
+ * Invoked when load either succeeds or fails
114
+ */
115
+ onLoadEnd : PropTypes . func ,
116
+ /**
117
+ * Invoked on load start
118
+ */
119
+ onLoadStart : PropTypes . func ,
120
+ /**
121
+ * Invoked when load fails
122
+ */
123
+ onError : PropTypes . func ,
124
+ /**
125
+ * @platform ios
126
+ */
102
127
bounces : PropTypes . bool ,
128
+ /**
129
+ * @platform ios
130
+ */
103
131
scrollEnabled : PropTypes . bool ,
104
132
automaticallyAdjustContentInsets : PropTypes . bool ,
105
133
contentInset : EdgeInsetsPropType ,
@@ -108,10 +136,16 @@ var WebViewBridge = React.createClass({
108
136
style : View . propTypes . style ,
109
137
110
138
/**
111
- * Used for android only, JS is enabled by default for WebView on iOS
139
+ * Used on Android only, JS is enabled by default for WebView on iOS
140
+ * @platform android
141
+ */
142
+ javaScriptEnabled : PropTypes . bool ,
143
+
144
+ /**
145
+ * Used on Android only, controls whether DOM Storage is enabled or not
112
146
* @platform android
113
147
*/
114
- javaScriptEnabledAndroid : PropTypes . bool ,
148
+ domStorageEnabled : PropTypes . bool ,
115
149
116
150
/**
117
151
* Sets the JS to be injected when the webpage loads.
@@ -184,19 +218,29 @@ var WebViewBridge = React.createClass({
184
218
) ;
185
219
}
186
220
187
- var webViewBridgeStyles = [ styles . container , styles . webViewBridge , this . props . style ] ;
221
+ var webViewStyles = [ styles . container , styles . webView , this . props . style ] ;
188
222
if ( this . state . viewState === WebViewBridgeState . LOADING ||
189
223
this . state . viewState === WebViewBridgeState . ERROR ) {
190
224
// if we're in either LOADING or ERROR states, don't show the webView
191
- webViewBridgeStyles . push ( styles . hidden ) ;
225
+ webViewStyles . push ( styles . hidden ) ;
192
226
}
193
227
194
228
var onShouldStartLoadWithRequest = this . props . onShouldStartLoadWithRequest && ( ( event : Event ) => {
195
229
var shouldStart = this . props . onShouldStartLoadWithRequest &&
196
230
this . props . onShouldStartLoadWithRequest ( event . nativeEvent ) ;
197
- WebViewBridgeManager . startLoadWithResult ( ! ! shouldStart , event . nativeEvent . lockIdentifier ) ;
231
+ RCTWebViewBridgeManager . startLoadWithResult ( ! ! shouldStart , event . nativeEvent . lockIdentifier ) ;
198
232
} ) ;
199
233
234
+ var { javaScriptEnabled, domStorageEnabled} = this . props ;
235
+ if ( this . props . javaScriptEnabledAndroid ) {
236
+ console . warn ( 'javaScriptEnabledAndroid is deprecated. Use javaScriptEnabled instead' ) ;
237
+ javaScriptEnabled = this . props . javaScriptEnabledAndroid ;
238
+ }
239
+ if ( this . props . domStorageEnabledAndroid ) {
240
+ console . warn ( 'domStorageEnabledAndroid is deprecated. Use domStorageEnabled instead' ) ;
241
+ domStorageEnabled = this . props . domStorageEnabledAndroid ;
242
+ }
243
+
200
244
var onBridgeMessage = ( event : Event ) => {
201
245
const onBridgeMessageCallback = this . props . onBridgeMessage ;
202
246
if ( onBridgeMessageCallback ) {
@@ -207,11 +251,11 @@ var WebViewBridge = React.createClass({
207
251
}
208
252
} ;
209
253
210
- var webViewBridge =
254
+ var webView =
211
255
< RCTWebViewBridge
212
- ref = { RCT_WEBVIEW_BRIDGE_REF }
213
- key = "webViewBridgeKey "
214
- style = { webViewBridgeStyles }
256
+ ref = { RCT_WEBVIEWBRIDGE_REF }
257
+ key = "webViewKey "
258
+ style = { webViewStyles }
215
259
url = { this . props . url }
216
260
html = { this . props . html }
217
261
injectedJavaScript = { this . props . injectedJavaScript }
@@ -230,22 +274,34 @@ var WebViewBridge = React.createClass({
230
274
231
275
return (
232
276
< View style = { styles . container } >
233
- { webViewBridge }
277
+ { webView }
234
278
{ otherView }
235
279
</ View >
236
280
) ;
237
281
} ,
238
282
239
283
goForward : function ( ) {
240
- WebViewBridgeManager . goForward ( this . getWebViewBridgeHandle ( ) ) ;
284
+ UIManager . dispatchViewManagerCommand (
285
+ this . getWebViewBridgeHandle ( ) ,
286
+ UIManager . RCTWebViewBridge . Commands . goForward ,
287
+ null
288
+ ) ;
241
289
} ,
242
290
243
291
goBack : function ( ) {
244
- WebViewBridgeManager . goBack ( this . getWebViewBridgeHandle ( ) ) ;
292
+ UIManager . dispatchViewManagerCommand (
293
+ this . getWebViewBridgeHandle ( ) ,
294
+ UIManager . RCTWebViewBridge . Commands . goBack ,
295
+ null
296
+ ) ;
245
297
} ,
246
298
247
299
reload : function ( ) {
248
- WebViewBridgeManager . reload ( this . getWebViewBridgeHandle ( ) ) ;
300
+ UIManager . dispatchViewManagerCommand (
301
+ this . getWebViewBridgeHandle ( ) ,
302
+ UIManager . RCTWebViewBridge . Commands . reload ,
303
+ null
304
+ ) ;
249
305
} ,
250
306
251
307
sendToBridge : function ( message : string ) {
@@ -263,15 +319,20 @@ var WebViewBridge = React.createClass({
263
319
} ,
264
320
265
321
getWebViewBridgeHandle : function ( ) : any {
266
- return React . findNodeHandle ( this . refs [ RCT_WEBVIEW_BRIDGE_REF ] ) ;
322
+ return React . findNodeHandle ( this . refs [ RCT_WEBVIEWBRIDGE_REF ] ) ;
267
323
} ,
268
324
269
325
onLoadingStart : function ( event : Event ) {
326
+ var onLoadStart = this . props . onLoadStart ;
327
+ onLoadStart && onLoadStart ( event ) ;
270
328
this . updateNavigationState ( event ) ;
271
329
} ,
272
330
273
331
onLoadingError : function ( event : Event ) {
274
332
event . persist ( ) ; // persist this event because we need to store it
333
+ var { onError, onLoadEnd} = this . props ;
334
+ onError && onError ( event ) ;
335
+ onLoadEnd && onLoadEnd ( event ) ;
275
336
console . warn ( 'Encountered an error loading page' , event . nativeEvent ) ;
276
337
277
338
this . setState ( {
@@ -281,6 +342,9 @@ var WebViewBridge = React.createClass({
281
342
} ,
282
343
283
344
onLoadingFinish : function ( event : Event ) {
345
+ var { onLoad, onLoadEnd} = this . props ;
346
+ onLoad && onLoad ( event ) ;
347
+ onLoadEnd && onLoadEnd ( event ) ;
284
348
this . setState ( {
285
349
viewState : WebViewBridgeState . IDLE ,
286
350
} ) ;
@@ -326,7 +390,7 @@ var styles = StyleSheet.create({
326
390
justifyContent : 'center' ,
327
391
alignItems : 'center' ,
328
392
} ,
329
- webViewBridge : {
393
+ webView : {
330
394
backgroundColor : '#ffffff' ,
331
395
}
332
396
} ) ;
0 commit comments