mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: format
This commit is contained in:
@@ -1,182 +1,233 @@
|
||||
import { WebViewBase, WebViewClient } from "./web-view-common";
|
||||
import { Trace } from '../../trace';
|
||||
import { Trace } from "../../trace";
|
||||
import { knownFolders } from "../../file-system";
|
||||
|
||||
|
||||
export * from "./web-view-common";
|
||||
|
||||
let WebViewClient: WebViewClient;
|
||||
|
||||
function initializeWebViewClient(): void {
|
||||
if (WebViewClient) {
|
||||
return;
|
||||
}
|
||||
if (WebViewClient) {
|
||||
return;
|
||||
}
|
||||
|
||||
class WebViewClientImpl extends android.webkit.WebViewClient {
|
||||
class WebViewClientImpl extends android.webkit.WebViewClient {
|
||||
constructor(public owner: WebViewBase) {
|
||||
super();
|
||||
|
||||
constructor(public owner: WebViewBase) {
|
||||
super();
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
return global.__native(this);
|
||||
}
|
||||
public shouldOverrideUrlLoading(
|
||||
view: android.webkit.WebView,
|
||||
url: string
|
||||
) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WebViewClientClass.shouldOverrideUrlLoading(" + url + ")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
|
||||
public shouldOverrideUrlLoading(view: android.webkit.WebView, url: string) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WebViewClientClass.shouldOverrideUrlLoading(" + url + ")", Trace.categories.Debug);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public onPageStarted(
|
||||
view: android.webkit.WebView,
|
||||
url: string,
|
||||
favicon: android.graphics.Bitmap
|
||||
) {
|
||||
super.onPageStarted(view, url, favicon);
|
||||
const owner = this.owner;
|
||||
if (owner) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WebViewClientClass.onPageStarted(" +
|
||||
url +
|
||||
", " +
|
||||
favicon +
|
||||
")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
owner._onLoadStarted(url, undefined);
|
||||
}
|
||||
}
|
||||
|
||||
public onPageStarted(view: android.webkit.WebView, url: string, favicon: android.graphics.Bitmap) {
|
||||
super.onPageStarted(view, url, favicon);
|
||||
const owner = this.owner;
|
||||
if (owner) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WebViewClientClass.onPageStarted(" + url + ", " + favicon + ")", Trace.categories.Debug);
|
||||
}
|
||||
owner._onLoadStarted(url, undefined);
|
||||
}
|
||||
}
|
||||
public onPageFinished(view: android.webkit.WebView, url: string) {
|
||||
super.onPageFinished(view, url);
|
||||
const owner = this.owner;
|
||||
if (owner) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WebViewClientClass.onPageFinished(" + url + ")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
owner._onLoadFinished(url, undefined);
|
||||
}
|
||||
}
|
||||
|
||||
public onPageFinished(view: android.webkit.WebView, url: string) {
|
||||
super.onPageFinished(view, url);
|
||||
const owner = this.owner;
|
||||
if (owner) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WebViewClientClass.onPageFinished(" + url + ")", Trace.categories.Debug);
|
||||
}
|
||||
owner._onLoadFinished(url, undefined);
|
||||
}
|
||||
}
|
||||
public onReceivedError() {
|
||||
let view: android.webkit.WebView = arguments[0];
|
||||
|
||||
public onReceivedError() {
|
||||
let view: android.webkit.WebView = arguments[0];
|
||||
if (arguments.length === 4) {
|
||||
let errorCode: number = arguments[1];
|
||||
let description: string = arguments[2];
|
||||
let failingUrl: string = arguments[3];
|
||||
|
||||
if (arguments.length === 4) {
|
||||
let errorCode: number = arguments[1];
|
||||
let description: string = arguments[2];
|
||||
let failingUrl: string = arguments[3];
|
||||
super.onReceivedError(view, errorCode, description, failingUrl);
|
||||
|
||||
super.onReceivedError(view, errorCode, description, failingUrl);
|
||||
const owner = this.owner;
|
||||
if (owner) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WebViewClientClass.onReceivedError(" +
|
||||
errorCode +
|
||||
", " +
|
||||
description +
|
||||
", " +
|
||||
failingUrl +
|
||||
")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
owner._onLoadFinished(
|
||||
failingUrl,
|
||||
description + "(" + errorCode + ")"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
let request: any = arguments[1];
|
||||
let error: any = arguments[2];
|
||||
|
||||
const owner = this.owner;
|
||||
if (owner) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WebViewClientClass.onReceivedError(" + errorCode + ", " + description + ", " + failingUrl + ")", Trace.categories.Debug);
|
||||
}
|
||||
owner._onLoadFinished(failingUrl, description + "(" + errorCode + ")");
|
||||
}
|
||||
} else {
|
||||
let request: any = arguments[1];
|
||||
let error: any = arguments[2];
|
||||
// before API version 23 there's no onReceiveError with 3 parameters, so it shouldn't come here
|
||||
// but we don't have the onReceivedError with 3 parameters there and that's why we are ignorint tye typescript error
|
||||
// @ts-ignore TS2554
|
||||
super.onReceivedError(view, request, error);
|
||||
|
||||
// before API version 23 there's no onReceiveError with 3 parameters, so it shouldn't come here
|
||||
// but we don't have the onReceivedError with 3 parameters there and that's why we are ignorint tye typescript error
|
||||
// @ts-ignore TS2554
|
||||
super.onReceivedError(view, request, error);
|
||||
const owner = this.owner;
|
||||
if (owner) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WebViewClientClass.onReceivedError(" +
|
||||
error.getErrorCode() +
|
||||
", " +
|
||||
error.getDescription() +
|
||||
", " +
|
||||
(error.getUrl && error.getUrl()) +
|
||||
")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
owner._onLoadFinished(
|
||||
error.getUrl && error.getUrl(),
|
||||
error.getDescription() +
|
||||
"(" +
|
||||
error.getErrorCode() +
|
||||
")"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const owner = this.owner;
|
||||
if (owner) {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WebViewClientClass.onReceivedError(" + error.getErrorCode() + ", " + error.getDescription() + ", " + (error.getUrl && error.getUrl()) + ")", Trace.categories.Debug);
|
||||
}
|
||||
owner._onLoadFinished(error.getUrl && error.getUrl(), error.getDescription() + "(" + error.getErrorCode() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WebViewClient = <any>WebViewClientImpl;
|
||||
WebViewClient = <any>WebViewClientImpl;
|
||||
}
|
||||
|
||||
export class WebView extends WebViewBase {
|
||||
nativeViewProtected: android.webkit.WebView;
|
||||
nativeViewProtected: android.webkit.WebView;
|
||||
|
||||
public createNativeView() {
|
||||
const nativeView = new android.webkit.WebView(this._context);
|
||||
nativeView.getSettings().setJavaScriptEnabled(true);
|
||||
nativeView.getSettings().setBuiltInZoomControls(true);
|
||||
public createNativeView() {
|
||||
const nativeView = new android.webkit.WebView(this._context);
|
||||
nativeView.getSettings().setJavaScriptEnabled(true);
|
||||
nativeView.getSettings().setBuiltInZoomControls(true);
|
||||
|
||||
return nativeView;
|
||||
}
|
||||
return nativeView;
|
||||
}
|
||||
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
initializeWebViewClient();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
const client = new WebViewClient(<any>this);
|
||||
nativeView.setWebViewClient(client);
|
||||
(<any>nativeView).client = client;
|
||||
}
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
initializeWebViewClient();
|
||||
const nativeView = this.nativeViewProtected;
|
||||
const client = new WebViewClient(<any>this);
|
||||
nativeView.setWebViewClient(client);
|
||||
(<any>nativeView).client = client;
|
||||
}
|
||||
|
||||
public disposeNativeView() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
nativeView.destroy();
|
||||
}
|
||||
public disposeNativeView() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
nativeView.destroy();
|
||||
}
|
||||
|
||||
(<any>nativeView).client.owner = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
(<any>nativeView).client.owner = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
|
||||
public _loadUrl(src: string) {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (!nativeView) {
|
||||
return;
|
||||
}
|
||||
public _loadUrl(src: string) {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (!nativeView) {
|
||||
return;
|
||||
}
|
||||
|
||||
nativeView.loadUrl(src);
|
||||
}
|
||||
nativeView.loadUrl(src);
|
||||
}
|
||||
|
||||
public _loadData(src: string) {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (!nativeView) {
|
||||
return;
|
||||
}
|
||||
public _loadData(src: string) {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (!nativeView) {
|
||||
return;
|
||||
}
|
||||
|
||||
const baseUrl = `file:///${knownFolders.currentApp().path}/`;
|
||||
nativeView.loadDataWithBaseURL(baseUrl, src, "text/html", "utf-8", null);
|
||||
}
|
||||
const baseUrl = `file:///${knownFolders.currentApp().path}/`;
|
||||
nativeView.loadDataWithBaseURL(
|
||||
baseUrl,
|
||||
src,
|
||||
"text/html",
|
||||
"utf-8",
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
get canGoBack(): boolean {
|
||||
return this.nativeViewProtected.canGoBack();
|
||||
}
|
||||
get canGoBack(): boolean {
|
||||
return this.nativeViewProtected.canGoBack();
|
||||
}
|
||||
|
||||
public stopLoading() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
nativeView.stopLoading();
|
||||
}
|
||||
}
|
||||
public stopLoading() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
nativeView.stopLoading();
|
||||
}
|
||||
}
|
||||
|
||||
get canGoForward(): boolean {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
return nativeView.canGoForward();
|
||||
}
|
||||
get canGoForward(): boolean {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
return nativeView.canGoForward();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public goBack() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
return nativeView.goBack();
|
||||
}
|
||||
}
|
||||
public goBack() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
return nativeView.goBack();
|
||||
}
|
||||
}
|
||||
|
||||
public goForward() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
return nativeView.goForward();
|
||||
}
|
||||
}
|
||||
public goForward() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
return nativeView.goForward();
|
||||
}
|
||||
}
|
||||
|
||||
public reload() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
return nativeView.reload();
|
||||
}
|
||||
}
|
||||
public reload() {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView) {
|
||||
return nativeView.reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
161
nativescript-core/ui/web-view/index.d.ts
vendored
161
nativescript-core/ui/web-view/index.d.ts
vendored
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
|
||||
import { View } from "../core/view";
|
||||
import { Property } from "../core/properties";
|
||||
import { Property } from "../core/properties";
|
||||
import { EventData } from "../../data/observable";
|
||||
|
||||
/**
|
||||
@@ -14,102 +14,117 @@ export const urlProperty: Property<WebView, string>;
|
||||
/**
|
||||
* Represents navigation type
|
||||
*/
|
||||
export type NavigationType = "linkClicked" | "formSubmitted" | "backForward" | "reload" | "formResubmitted" | "other" | undefined;
|
||||
export type NavigationType =
|
||||
| "linkClicked"
|
||||
| "formSubmitted"
|
||||
| "backForward"
|
||||
| "reload"
|
||||
| "formResubmitted"
|
||||
| "other"
|
||||
| undefined;
|
||||
|
||||
/**
|
||||
* Represents a standard WebView widget.
|
||||
*/
|
||||
export class WebView extends View {
|
||||
/**
|
||||
* String value used when hooking to loadStarted event.
|
||||
*/
|
||||
public static loadStartedEvent: string;
|
||||
/**
|
||||
* String value used when hooking to loadStarted event.
|
||||
*/
|
||||
public static loadStartedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to loadFinished event.
|
||||
*/
|
||||
public static loadFinishedEvent: string;
|
||||
/**
|
||||
* String value used when hooking to loadFinished event.
|
||||
*/
|
||||
public static loadFinishedEvent: string;
|
||||
|
||||
/**
|
||||
* Gets the native [android widget](http://developer.android.com/reference/android/webkit/WebView.html) that represents the user interface for this component. Valid only when running on Android OS.
|
||||
*/
|
||||
android: any /* android.webkit.WebView */;
|
||||
/**
|
||||
* Gets the native [android widget](http://developer.android.com/reference/android/webkit/WebView.html) that represents the user interface for this component. Valid only when running on Android OS.
|
||||
*/
|
||||
android: any /* android.webkit.WebView */;
|
||||
|
||||
/**
|
||||
* Gets the native [WKWebView](https://developer.apple.com/documentation/webkit/wkwebview/) that represents the user interface for this component. Valid only when running on iOS.
|
||||
*/
|
||||
ios: any /* WKWebView */;
|
||||
/**
|
||||
* Gets the native [WKWebView](https://developer.apple.com/documentation/webkit/wkwebview/) that represents the user interface for this component. Valid only when running on iOS.
|
||||
*/
|
||||
ios: any /* WKWebView */;
|
||||
|
||||
/**
|
||||
* Gets or sets the url, local file path or HTML string.
|
||||
*/
|
||||
src: string;
|
||||
/**
|
||||
* Gets or sets the url, local file path or HTML string.
|
||||
*/
|
||||
src: string;
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the WebView can navigate back.
|
||||
*/
|
||||
canGoBack: boolean;
|
||||
/**
|
||||
* Gets a value indicating whether the WebView can navigate back.
|
||||
*/
|
||||
canGoBack: boolean;
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the WebView can navigate forward.
|
||||
*/
|
||||
canGoForward: boolean;
|
||||
/**
|
||||
* Gets a value indicating whether the WebView can navigate forward.
|
||||
*/
|
||||
canGoForward: boolean;
|
||||
|
||||
/**
|
||||
* Stops loading the current content (if any).
|
||||
*/
|
||||
stopLoading(): void;
|
||||
/**
|
||||
* Stops loading the current content (if any).
|
||||
*/
|
||||
stopLoading(): void;
|
||||
|
||||
/**
|
||||
* Navigates back.
|
||||
*/
|
||||
goBack();
|
||||
/**
|
||||
* Navigates back.
|
||||
*/
|
||||
goBack();
|
||||
|
||||
/**
|
||||
* Navigates forward.
|
||||
*/
|
||||
goForward();
|
||||
/**
|
||||
* Navigates forward.
|
||||
*/
|
||||
goForward();
|
||||
|
||||
/**
|
||||
* Reloads the current url.
|
||||
*/
|
||||
reload();
|
||||
/**
|
||||
* Reloads the current url.
|
||||
*/
|
||||
reload();
|
||||
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Raised when a loadFinished event occurs.
|
||||
*/
|
||||
on(event: "loadFinished", callback: (args: LoadEventData) => void, thisArg?: any);
|
||||
/**
|
||||
* Raised when a loadFinished event occurs.
|
||||
*/
|
||||
on(
|
||||
event: "loadFinished",
|
||||
callback: (args: LoadEventData) => void,
|
||||
thisArg?: any
|
||||
);
|
||||
|
||||
/**
|
||||
* Raised when a loadStarted event occurs.
|
||||
*/
|
||||
on(event: "loadStarted", callback: (args: LoadEventData) => void, thisArg?: any);
|
||||
/**
|
||||
* Raised when a loadStarted event occurs.
|
||||
*/
|
||||
on(
|
||||
event: "loadStarted",
|
||||
callback: (args: LoadEventData) => void,
|
||||
thisArg?: any
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Event data containing information for the loading events of a WebView.
|
||||
*/
|
||||
export interface LoadEventData extends EventData {
|
||||
/**
|
||||
* Gets the url of the web-view.
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* Gets the url of the web-view.
|
||||
*/
|
||||
url: string;
|
||||
|
||||
/**
|
||||
* Gets the navigation type of the web-view.
|
||||
*/
|
||||
navigationType: NavigationType;
|
||||
/**
|
||||
* Gets the navigation type of the web-view.
|
||||
*/
|
||||
navigationType: NavigationType;
|
||||
|
||||
/**
|
||||
* Gets the error (if any).
|
||||
*/
|
||||
error: string;
|
||||
/**
|
||||
* Gets the error (if any).
|
||||
*/
|
||||
error: string;
|
||||
}
|
||||
|
||||
@@ -1,183 +1,251 @@
|
||||
import { NavigationType } from ".";
|
||||
import { WebViewBase } from "./web-view-common";
|
||||
import { profile } from "../../profiling";
|
||||
import { Trace } from '../../trace';
|
||||
import { Trace } from "../../trace";
|
||||
export * from "./web-view-common";
|
||||
import { knownFolders } from "../../file-system";
|
||||
|
||||
class WKNavigationDelegateImpl extends NSObject
|
||||
implements WKNavigationDelegate {
|
||||
public static ObjCProtocols = [WKNavigationDelegate];
|
||||
public static initWithOwner(owner: WeakRef<WebView>): WKNavigationDelegateImpl {
|
||||
const handler = <WKNavigationDelegateImpl>WKNavigationDelegateImpl.new();
|
||||
handler._owner = owner;
|
||||
implements WKNavigationDelegate {
|
||||
public static ObjCProtocols = [WKNavigationDelegate];
|
||||
public static initWithOwner(
|
||||
owner: WeakRef<WebView>
|
||||
): WKNavigationDelegateImpl {
|
||||
const handler = <WKNavigationDelegateImpl>(
|
||||
WKNavigationDelegateImpl.new()
|
||||
);
|
||||
handler._owner = owner;
|
||||
|
||||
return handler;
|
||||
}
|
||||
private _owner: WeakRef<WebView>;
|
||||
return handler;
|
||||
}
|
||||
private _owner: WeakRef<WebView>;
|
||||
|
||||
public webViewDecidePolicyForNavigationActionDecisionHandler(webView: WKWebView, navigationAction: WKNavigationAction, decisionHandler: any): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner && navigationAction.request.URL) {
|
||||
let navType: NavigationType = "other";
|
||||
public webViewDecidePolicyForNavigationActionDecisionHandler(
|
||||
webView: WKWebView,
|
||||
navigationAction: WKNavigationAction,
|
||||
decisionHandler: any
|
||||
): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner && navigationAction.request.URL) {
|
||||
let navType: NavigationType = "other";
|
||||
|
||||
switch (navigationAction.navigationType) {
|
||||
case WKNavigationType.LinkActivated:
|
||||
navType = "linkClicked";
|
||||
break;
|
||||
case WKNavigationType.FormSubmitted:
|
||||
navType = "formSubmitted";
|
||||
break;
|
||||
case WKNavigationType.BackForward:
|
||||
navType = "backForward";
|
||||
break;
|
||||
case WKNavigationType.Reload:
|
||||
navType = "reload";
|
||||
break;
|
||||
case WKNavigationType.FormResubmitted:
|
||||
navType = "formResubmitted";
|
||||
break;
|
||||
}
|
||||
decisionHandler(WKNavigationActionPolicy.Allow);
|
||||
switch (navigationAction.navigationType) {
|
||||
case WKNavigationType.LinkActivated:
|
||||
navType = "linkClicked";
|
||||
break;
|
||||
case WKNavigationType.FormSubmitted:
|
||||
navType = "formSubmitted";
|
||||
break;
|
||||
case WKNavigationType.BackForward:
|
||||
navType = "backForward";
|
||||
break;
|
||||
case WKNavigationType.Reload:
|
||||
navType = "reload";
|
||||
break;
|
||||
case WKNavigationType.FormResubmitted:
|
||||
navType = "formResubmitted";
|
||||
break;
|
||||
}
|
||||
decisionHandler(WKNavigationActionPolicy.Allow);
|
||||
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WKNavigationDelegateClass.webViewDecidePolicyForNavigationActionDecisionHandler(" + navigationAction.request.URL.absoluteString + ", " + navigationAction.navigationType + ")", Trace.categories.Debug);
|
||||
}
|
||||
owner._onLoadStarted(navigationAction.request.URL.absoluteString, navType);
|
||||
}
|
||||
}
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WKNavigationDelegateClass.webViewDecidePolicyForNavigationActionDecisionHandler(" +
|
||||
navigationAction.request.URL.absoluteString +
|
||||
", " +
|
||||
navigationAction.navigationType +
|
||||
")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
owner._onLoadStarted(
|
||||
navigationAction.request.URL.absoluteString,
|
||||
navType
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public webViewDidStartProvisionalNavigation(webView: WKWebView, navigation: WKNavigation): void {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WKNavigationDelegateClass.webViewDidStartProvisionalNavigation(" + webView.URL + ")", Trace.categories.Debug);
|
||||
}
|
||||
}
|
||||
public webViewDidStartProvisionalNavigation(
|
||||
webView: WKWebView,
|
||||
navigation: WKNavigation
|
||||
): void {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WKNavigationDelegateClass.webViewDidStartProvisionalNavigation(" +
|
||||
webView.URL +
|
||||
")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public webViewDidFinishNavigation(webView: WKWebView, navigation: WKNavigation): void {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WKNavigationDelegateClass.webViewDidFinishNavigation(" + webView.URL + ")", Trace.categories.Debug);
|
||||
}
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
let src = owner.src;
|
||||
if (webView.URL) {
|
||||
src = webView.URL.absoluteString;
|
||||
}
|
||||
owner._onLoadFinished(src);
|
||||
}
|
||||
}
|
||||
public webViewDidFinishNavigation(
|
||||
webView: WKWebView,
|
||||
navigation: WKNavigation
|
||||
): void {
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WKNavigationDelegateClass.webViewDidFinishNavigation(" +
|
||||
webView.URL +
|
||||
")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
let src = owner.src;
|
||||
if (webView.URL) {
|
||||
src = webView.URL.absoluteString;
|
||||
}
|
||||
owner._onLoadFinished(src);
|
||||
}
|
||||
}
|
||||
|
||||
public webViewDidFailNavigationWithError(webView: WKWebView, navigation: WKNavigation, error: NSError): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
let src = owner.src;
|
||||
if (webView.URL) {
|
||||
src = webView.URL.absoluteString;
|
||||
}
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WKNavigationDelegateClass.webViewDidFailNavigationWithError(" + error.localizedDescription + ")", Trace.categories.Debug);
|
||||
}
|
||||
owner._onLoadFinished(src, error.localizedDescription);
|
||||
}
|
||||
}
|
||||
|
||||
public webViewDidFailProvisionalNavigationWithError(webView: WKWebView, navigation: WKNavigation, error: NSError): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
let src = owner.src;
|
||||
if (webView.URL) {
|
||||
src = webView.URL.absoluteString;
|
||||
}
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write("WKNavigationDelegateClass.webViewDidFailProvisionalNavigationWithError(" + error.localizedDescription + ")", Trace.categories.Debug);
|
||||
}
|
||||
owner._onLoadFinished(src, error.localizedDescription);
|
||||
}
|
||||
}
|
||||
public webViewDidFailNavigationWithError(
|
||||
webView: WKWebView,
|
||||
navigation: WKNavigation,
|
||||
error: NSError
|
||||
): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
let src = owner.src;
|
||||
if (webView.URL) {
|
||||
src = webView.URL.absoluteString;
|
||||
}
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WKNavigationDelegateClass.webViewDidFailNavigationWithError(" +
|
||||
error.localizedDescription +
|
||||
")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
owner._onLoadFinished(src, error.localizedDescription);
|
||||
}
|
||||
}
|
||||
|
||||
public webViewDidFailProvisionalNavigationWithError(
|
||||
webView: WKWebView,
|
||||
navigation: WKNavigation,
|
||||
error: NSError
|
||||
): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
let src = owner.src;
|
||||
if (webView.URL) {
|
||||
src = webView.URL.absoluteString;
|
||||
}
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(
|
||||
"WKNavigationDelegateClass.webViewDidFailProvisionalNavigationWithError(" +
|
||||
error.localizedDescription +
|
||||
")",
|
||||
Trace.categories.Debug
|
||||
);
|
||||
}
|
||||
owner._onLoadFinished(src, error.localizedDescription);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class WebView extends WebViewBase {
|
||||
nativeViewProtected: WKWebView;
|
||||
private _delegate: any;
|
||||
nativeViewProtected: WKWebView;
|
||||
private _delegate: any;
|
||||
|
||||
createNativeView() {
|
||||
const jScript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'initial-scale=1.0'); document.getElementsByTagName('head')[0].appendChild(meta);";
|
||||
const wkUScript = WKUserScript.alloc().initWithSourceInjectionTimeForMainFrameOnly(jScript, WKUserScriptInjectionTime.AtDocumentEnd, true);
|
||||
const wkUController = WKUserContentController.new();
|
||||
wkUController.addUserScript(wkUScript);
|
||||
const configuration = WKWebViewConfiguration.new();
|
||||
configuration.userContentController = wkUController;
|
||||
configuration.preferences.setValueForKey(
|
||||
true,
|
||||
"allowFileAccessFromFileURLs"
|
||||
);
|
||||
createNativeView() {
|
||||
const jScript =
|
||||
"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'initial-scale=1.0'); document.getElementsByTagName('head')[0].appendChild(meta);";
|
||||
const wkUScript = WKUserScript.alloc().initWithSourceInjectionTimeForMainFrameOnly(
|
||||
jScript,
|
||||
WKUserScriptInjectionTime.AtDocumentEnd,
|
||||
true
|
||||
);
|
||||
const wkUController = WKUserContentController.new();
|
||||
wkUController.addUserScript(wkUScript);
|
||||
const configuration = WKWebViewConfiguration.new();
|
||||
configuration.userContentController = wkUController;
|
||||
configuration.preferences.setValueForKey(
|
||||
true,
|
||||
"allowFileAccessFromFileURLs"
|
||||
);
|
||||
|
||||
return new WKWebView({
|
||||
frame: CGRectZero,
|
||||
configuration: configuration
|
||||
});
|
||||
}
|
||||
return new WKWebView({
|
||||
frame: CGRectZero,
|
||||
configuration: configuration,
|
||||
});
|
||||
}
|
||||
|
||||
initNativeView() {
|
||||
super.initNativeView();
|
||||
this._delegate = WKNavigationDelegateImpl.initWithOwner(new WeakRef(this));
|
||||
}
|
||||
initNativeView() {
|
||||
super.initNativeView();
|
||||
this._delegate = WKNavigationDelegateImpl.initWithOwner(
|
||||
new WeakRef(this)
|
||||
);
|
||||
}
|
||||
|
||||
disposeNativeView() {
|
||||
this._delegate = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
disposeNativeView() {
|
||||
this._delegate = null;
|
||||
super.disposeNativeView();
|
||||
}
|
||||
|
||||
@profile()
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this.ios.navigationDelegate = this._delegate;
|
||||
}
|
||||
@profile()
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this.ios.navigationDelegate = this._delegate;
|
||||
}
|
||||
|
||||
public onUnloaded() {
|
||||
this.ios.navigationDelegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
public onUnloaded() {
|
||||
this.ios.navigationDelegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
|
||||
get ios(): WKWebView {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
get ios(): WKWebView {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
|
||||
public stopLoading() {
|
||||
this.ios.stopLoading();
|
||||
}
|
||||
public stopLoading() {
|
||||
this.ios.stopLoading();
|
||||
}
|
||||
|
||||
public _loadUrl(src: string) {
|
||||
if (src.startsWith("file:///")) {
|
||||
const cachePath = src.substring(0, src.lastIndexOf("/"));
|
||||
this.ios.loadFileURLAllowingReadAccessToURL(NSURL.URLWithString(src), NSURL.URLWithString(cachePath));
|
||||
} else {
|
||||
this.ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
|
||||
}
|
||||
}
|
||||
public _loadUrl(src: string) {
|
||||
if (src.startsWith("file:///")) {
|
||||
const cachePath = src.substring(0, src.lastIndexOf("/"));
|
||||
this.ios.loadFileURLAllowingReadAccessToURL(
|
||||
NSURL.URLWithString(src),
|
||||
NSURL.URLWithString(cachePath)
|
||||
);
|
||||
} else {
|
||||
this.ios.loadRequest(
|
||||
NSURLRequest.requestWithURL(NSURL.URLWithString(src))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public _loadData(content: string) {
|
||||
this.ios.loadHTMLStringBaseURL(content, NSURL.alloc().initWithString(`file:///${knownFolders.currentApp().path}/`));
|
||||
}
|
||||
public _loadData(content: string) {
|
||||
this.ios.loadHTMLStringBaseURL(
|
||||
content,
|
||||
NSURL.alloc().initWithString(
|
||||
`file:///${knownFolders.currentApp().path}/`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
get canGoBack(): boolean {
|
||||
return this.ios.canGoBack;
|
||||
}
|
||||
get canGoBack(): boolean {
|
||||
return this.ios.canGoBack;
|
||||
}
|
||||
|
||||
get canGoForward(): boolean {
|
||||
return this.ios.canGoForward;
|
||||
}
|
||||
get canGoForward(): boolean {
|
||||
return this.ios.canGoForward;
|
||||
}
|
||||
|
||||
public goBack() {
|
||||
this.ios.goBack();
|
||||
}
|
||||
public goBack() {
|
||||
this.ios.goBack();
|
||||
}
|
||||
|
||||
public goForward() {
|
||||
this.ios.goForward();
|
||||
}
|
||||
public goForward() {
|
||||
this.ios.goForward();
|
||||
}
|
||||
|
||||
public reload() {
|
||||
this.ios.reload();
|
||||
}
|
||||
public reload() {
|
||||
this.ios.reload();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,82 +10,84 @@ export const srcProperty = new Property<WebViewBase, string>({ name: "src" });
|
||||
|
||||
@CSSType("WebView")
|
||||
export abstract class WebViewBase extends ContainerView {
|
||||
public static loadStartedEvent = "loadStarted";
|
||||
public static loadFinishedEvent = "loadFinished";
|
||||
public static loadStartedEvent = "loadStarted";
|
||||
public static loadFinishedEvent = "loadFinished";
|
||||
|
||||
public src: string;
|
||||
public src: string;
|
||||
|
||||
public _onLoadFinished(url: string, error?: string) {
|
||||
let args = <LoadEventData>{
|
||||
eventName: WebViewBase.loadFinishedEvent,
|
||||
object: this,
|
||||
url: url,
|
||||
navigationType: undefined,
|
||||
error: error
|
||||
};
|
||||
public _onLoadFinished(url: string, error?: string) {
|
||||
let args = <LoadEventData>{
|
||||
eventName: WebViewBase.loadFinishedEvent,
|
||||
object: this,
|
||||
url: url,
|
||||
navigationType: undefined,
|
||||
error: error,
|
||||
};
|
||||
|
||||
this.notify(<any>args);
|
||||
}
|
||||
this.notify(<any>args);
|
||||
}
|
||||
|
||||
public _onLoadStarted(url: string, navigationType: NavigationType) {
|
||||
let args = <LoadEventData>{
|
||||
eventName: WebViewBase.loadStartedEvent,
|
||||
object: this,
|
||||
url: url,
|
||||
navigationType: navigationType,
|
||||
error: undefined
|
||||
};
|
||||
public _onLoadStarted(url: string, navigationType: NavigationType) {
|
||||
let args = <LoadEventData>{
|
||||
eventName: WebViewBase.loadStartedEvent,
|
||||
object: this,
|
||||
url: url,
|
||||
navigationType: navigationType,
|
||||
error: undefined,
|
||||
};
|
||||
|
||||
this.notify(<any>args);
|
||||
}
|
||||
this.notify(<any>args);
|
||||
}
|
||||
|
||||
abstract _loadUrl(src: string): void;
|
||||
abstract _loadUrl(src: string): void;
|
||||
|
||||
abstract _loadData(src: string): void;
|
||||
abstract _loadData(src: string): void;
|
||||
|
||||
abstract stopLoading(): void;
|
||||
abstract stopLoading(): void;
|
||||
|
||||
get canGoBack(): boolean {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
get canGoBack(): boolean {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
|
||||
get canGoForward(): boolean {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
get canGoForward(): boolean {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
|
||||
abstract goBack(): void;
|
||||
abstract goBack(): void;
|
||||
|
||||
abstract goForward(): void;
|
||||
abstract goForward(): void;
|
||||
|
||||
abstract reload(): void;
|
||||
abstract reload(): void;
|
||||
|
||||
[srcProperty.getDefault](): string {
|
||||
return "";
|
||||
}
|
||||
[srcProperty.setNative](src: string) {
|
||||
this.stopLoading();
|
||||
[srcProperty.getDefault](): string {
|
||||
return "";
|
||||
}
|
||||
[srcProperty.setNative](src: string) {
|
||||
this.stopLoading();
|
||||
|
||||
// Add file:/// prefix for local files.
|
||||
// They should be loaded with _loadUrl() method as it handles query params.
|
||||
if (src.indexOf("~/") === 0) {
|
||||
src = `file:///${knownFolders.currentApp().path}/` + src.substr(2);
|
||||
} else if (src.indexOf("/") === 0) {
|
||||
src = "file://" + src;
|
||||
}
|
||||
// Add file:/// prefix for local files.
|
||||
// They should be loaded with _loadUrl() method as it handles query params.
|
||||
if (src.indexOf("~/") === 0) {
|
||||
src = `file:///${knownFolders.currentApp().path}/` + src.substr(2);
|
||||
} else if (src.indexOf("/") === 0) {
|
||||
src = "file://" + src;
|
||||
}
|
||||
|
||||
// loading local files from paths with spaces may fail
|
||||
if (src.toLowerCase().indexOf("file:///") === 0) {
|
||||
src = encodeURI(src);
|
||||
}
|
||||
// loading local files from paths with spaces may fail
|
||||
if (src.toLowerCase().indexOf("file:///") === 0) {
|
||||
src = encodeURI(src);
|
||||
}
|
||||
|
||||
if (src.toLowerCase().indexOf("http://") === 0 ||
|
||||
src.toLowerCase().indexOf("https://") === 0 ||
|
||||
src.toLowerCase().indexOf("file:///") === 0) {
|
||||
this._loadUrl(src);
|
||||
} else {
|
||||
this._loadData(src);
|
||||
}
|
||||
}
|
||||
if (
|
||||
src.toLowerCase().indexOf("http://") === 0 ||
|
||||
src.toLowerCase().indexOf("https://") === 0 ||
|
||||
src.toLowerCase().indexOf("file:///") === 0
|
||||
) {
|
||||
this._loadUrl(src);
|
||||
} else {
|
||||
this._loadData(src);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HACK: Use an interface with the same name, so that the class above fulfills the 'implements' requirement
|
||||
@@ -93,9 +95,21 @@ export abstract class WebViewBase extends ContainerView {
|
||||
// HACK: We declare all these 'on' statements, so that they can appear in the API reference
|
||||
// HACK: Do we need this? Is it useful? There are static fields to the WebViewBase class for the event names.
|
||||
export interface WebViewBase {
|
||||
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any): void;
|
||||
on(event: "loadFinished", callback: (args: LoadEventData) => void, thisArg?: any): void;
|
||||
on(event: "loadStarted", callback: (args: LoadEventData) => void, thisArg?: any): void;
|
||||
on(
|
||||
eventNames: string,
|
||||
callback: (data: EventData) => void,
|
||||
thisArg?: any
|
||||
): void;
|
||||
on(
|
||||
event: "loadFinished",
|
||||
callback: (args: LoadEventData) => void,
|
||||
thisArg?: any
|
||||
): void;
|
||||
on(
|
||||
event: "loadStarted",
|
||||
callback: (args: LoadEventData) => void,
|
||||
thisArg?: any
|
||||
): void;
|
||||
}
|
||||
|
||||
srcProperty.register(WebViewBase);
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
import { WebView } from ".";
|
||||
import { EventData } from "../../data/observable";
|
||||
|
||||
export type NavigationType = "linkClicked" | "formSubmitted" | "backForward" | "reload" | "formResubmitted" | "other" | undefined;
|
||||
export type NavigationType =
|
||||
| "linkClicked"
|
||||
| "formSubmitted"
|
||||
| "backForward"
|
||||
| "reload"
|
||||
| "formResubmitted"
|
||||
| "other"
|
||||
| undefined;
|
||||
|
||||
export interface LoadEventData extends EventData {
|
||||
url: string;
|
||||
navigationType: NavigationType;
|
||||
error: string;
|
||||
url: string;
|
||||
navigationType: NavigationType;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export interface WebViewClient {
|
||||
new(owner: WebView): any /* android.webkit.WebViewClient */;
|
||||
new (owner: WebView): any /* android.webkit.WebViewClient */;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user