forked from jordaaash/react-native-webview-crosswalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.js
151 lines (133 loc) · 4.18 KB
/
index.android.js
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
'use strict';
import React, { PropTypes } from 'react';
import ReactNative, { requireNativeComponent, View, StyleSheet } from 'react-native';
var {
NativeModules: { UIManager, CrosswalkWebViewManager: { JSNavigationScheme } }
} = ReactNative;
var resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource')
var WEBVIEW_REF = 'crosswalkWebView';
var hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
function is(x, y) {
// SameValue algorithm
if (x === y) {
// Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
// Added the nonzero y check to make Flow happy, but it is redundant
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
}
/**
* Performs equality by iterating through keys on an object and returning false
* when any key has values which are not strictly equal between the arguments.
* Returns true when the values of all keys are strictly equal.
*/
function shallowEqual(objA, objB) {
if (is(objA, objB)) {
return true;
}
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
var keysA = Object.keys(objA);
var keysB = Object.keys(objB);
if (keysA.length !== keysB.length) {
return false;
}
// Test for A's keys different from B.
for (var i = 0; i < keysA.length; i++) {
if (!hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
var CrosswalkWebView = React.createClass({
statics: { JSNavigationScheme },
propTypes: {
localhost: PropTypes.bool.isRequired,
onNavigationStateChange: PropTypes.func,
onError: PropTypes.func,
url: PropTypes.string,
injectedJavaScript: PropTypes.string,
source: PropTypes.oneOfType([
PropTypes.shape({
uri: PropTypes.string, // uri to load in webview
}),
PropTypes.shape({
html: PropTypes.string, // static html to load in webview
}),
PropTypes.number, // used internally by packager
]),
...View.propTypes
},
getDefaultProps () {
return {
localhost: false
};
},
shouldComponentUpdate (nextProps, nextState) {
return (
!shallowEqual(this.props, nextProps) ||
!shallowEqual(this.state, nextState)
);
},
render () {
var source = this.props.source || {};
if (this.props.url) {
source.uri = this.props.url;
}
return (
<NativeCrosswalkWebView
{ ...this.props }
ref={ WEBVIEW_REF }
source={resolveAssetSource(source)}
onNavigationStateChange={ this.onNavigationStateChange }
onError={ this.onError } />
);
},
getWebViewHandle () {
return React.findNodeHandle(this.refs[WEBVIEW_REF]);
},
onNavigationStateChange (event) {
var { onNavigationStateChange } = this.props;
if (onNavigationStateChange) {
onNavigationStateChange(event.nativeEvent);
}
},
onError (event) {
var { onError } = this.props;
if (onError) {
onError(event.nativeEvent);
}
},
goBack () {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
UIManager.NativeCrosswalkWebView.Commands.goBack,
null
);
},
goForward () {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
UIManager.NativeCrosswalkWebView.Commands.goForward,
null
);
},
reload () {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
UIManager.NativeCrosswalkWebView.Commands.reload,
null
);
}
});
var NativeCrosswalkWebView = requireNativeComponent('CrosswalkWebView', CrosswalkWebView);
export default CrosswalkWebView;