forked from shripalsoni04/nativescript-webview-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
63 lines (53 loc) · 2.15 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
var common = require("./index-common");
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);
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){
_super.call(this, webView);
this._initWebView();
}
/**
* Initializes webView for communication between android and webView.
*/
WebViewInterface.prototype._initWebView = function(){
var oJSInterface = getAndroidJSInterface(this);
var androidSettings = this.webView.android.getSettings();
androidSettings.setJavaScriptEnabled(true);
this.webView.android.addJavascriptInterface(oJSInterface, 'androidWebViewInterface');
};
/**
* Executes event/command/jsFunction in webView.
*/
WebViewInterface.prototype._executeJS = function(strJSFunction){
var url = 'javascript:'+strJSFunction;
this.webView.android.loadUrl(url);
};
return WebViewInterface;
})(common.WebViewInterface);
exports.WebViewInterface = WebViewInterface;