forked from caferrari/nativescript-webview-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
90 lines (78 loc) · 3.1 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
const common = require("./index-common");
const platformModule = require("platform");
global.moduleMerge(common, exports);
/**
* Factory function to provide instance of Android JavascriptInterface.
*/
function getAndroidJSInterface(oWebViewInterface){
var AndroidWebViewInterface = com.shripalsoni.natiescriptwebviewinterface.WebViewInterface.extend({
/**
* On call from webView to android, this function is called from handleEventFromWebView method of WebViewInerface class
*/
onWebViewEvent: function(webViewId, eventName, jsonData){
// getting webviewInterface object by webViewId from static map.
var oWebViewInterface = getWebViewIntefaceObjByWebViewId(webViewId);
if (!oWebViewInterface) {
return;
}
oWebViewInterface._onWebViewEvent(eventName, jsonData);
}
});
// creating androidWebViewInterface with unique web-view id.
return new AndroidWebViewInterface(new java.lang.String(''+oWebViewInterface.id));
}
/**
* Returns webViewInterface object mapped with the passed webViewId.
*/
function getWebViewIntefaceObjByWebViewId(webViewId){
return common.WebViewInterface.webViewInterfaceIdMap[webViewId];
}
/**
* Android Specific WebViewInterface Class
*/
var WebViewInterface = (function(_super){
__extends(WebViewInterface, _super);
function WebViewInterface(webView, src){
_super.call(this, webView);
this._initWebView(src);
}
/**
* Initializes webView for communication between android and webView.
*/
WebViewInterface.prototype._initWebView = function(src){
var _this = this;
if(this.webView.isLoaded) {
_this._setAndroidWebViewSettings(src);
} else {
var handlerRef = _this.webView.on('loaded', function(){
_this._setAndroidWebViewSettings(src);
_this.webView.off('loaded', handlerRef);
});
}
};
WebViewInterface.prototype._setAndroidWebViewSettings = function(src) {
var oJSInterface = getAndroidJSInterface(this);
var androidSettings = this.webView.android.getSettings();
androidSettings.setJavaScriptEnabled(true);
this.webView.android.addJavascriptInterface(oJSInterface, 'androidWebViewInterface');
// If src is provided, then setting it.
// To make javascriptInterface available in web-view, it should be set before
// web-view's loadUrl method is called. So setting src after javascriptInterface is set.
if(src){
this.webView.src = src;
}
}
/**
* Executes event/command/jsFunction in webView.
*/
WebViewInterface.prototype._executeJS = function(strJSFunction){
if (platformModule.device.sdkVersion >= 19) {
this.webView.android.evaluateJavascript(strJSFunction, null);
}
else {
this.webView.android.loadUrl('javascript:'+strJSFunction);
}
};
return WebViewInterface;
})(common.WebViewInterface);
exports.WebViewInterface = WebViewInterface;