mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
Respect query params in web-view
This commit is contained in:
@ -1,6 +1,5 @@
|
|||||||
import { WebView as WebViewDefinition, LoadEventData, NavigationType } from ".";
|
import { WebView as WebViewDefinition, LoadEventData, NavigationType } from ".";
|
||||||
import { View, Property } from "../core/view";
|
import { View, Property } from "../core/view";
|
||||||
import { isFileOrResourcePath } from "../../utils/utils";
|
|
||||||
import { File, knownFolders, path } from "../../file-system";
|
import { File, knownFolders, path } from "../../file-system";
|
||||||
|
|
||||||
export { File, knownFolders, path, NavigationType };
|
export { File, knownFolders, path, NavigationType };
|
||||||
@ -38,9 +37,7 @@ export abstract class WebViewBase extends View implements WebViewDefinition {
|
|||||||
this.notify(args);
|
this.notify(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract _loadFileOrResource(path: string, content: string): void;
|
abstract _loadUrl(src: string): void;
|
||||||
|
|
||||||
abstract _loadHttp(src: string): void;
|
|
||||||
|
|
||||||
abstract _loadData(src: string): void;
|
abstract _loadData(src: string): void;
|
||||||
|
|
||||||
@ -66,28 +63,28 @@ export abstract class WebViewBase extends View implements WebViewDefinition {
|
|||||||
[srcProperty.setNative](src: string) {
|
[srcProperty.setNative](src: string) {
|
||||||
this.stopLoading();
|
this.stopLoading();
|
||||||
|
|
||||||
if (isFileOrResourcePath(src)) {
|
// Add file:/// prefix for local files.
|
||||||
|
// They should be loaded with _loadUrl() method as it handles query params.
|
||||||
if (src.indexOf("~/") === 0) {
|
if (src.indexOf("~/") === 0) {
|
||||||
src = path.join(knownFolders.currentApp().path, src.replace("~/", ""));
|
src = `file:///${knownFolders.currentApp().path}/` + src.substr(2);
|
||||||
|
} else if (src.indexOf("/") === 0) {
|
||||||
|
src = "file://" + src;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (File.exists(src)) {
|
if (src.toLowerCase().indexOf("http://") === 0 ||
|
||||||
let file = File.fromPath(src);
|
src.toLowerCase().indexOf("https://") === 0 ||
|
||||||
let content = file.readTextSync();
|
src.toLowerCase().indexOf("file:///") === 0) {
|
||||||
this._loadFileOrResource(src, content);
|
this._loadUrl(src);
|
||||||
}
|
|
||||||
} else if (src.toLowerCase().indexOf("http://") === 0 || src.toLowerCase().indexOf("https://") === 0) {
|
|
||||||
this._loadHttp(src);
|
|
||||||
} else {
|
} else {
|
||||||
this._loadData(src);
|
this._loadData(src);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get url(): string {
|
get url(): string {
|
||||||
throw new Error("Property url of WebView is deprecated. Use src istead");
|
throw new Error("Property url of WebView is deprecated. Use src instead");
|
||||||
}
|
}
|
||||||
set url(value: string) {
|
set url(value: string) {
|
||||||
throw new Error("Property url of WebView is deprecated. Use src istead")
|
throw new Error("Property url of WebView is deprecated. Use src instead")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,17 +115,7 @@ export class WebView extends WebViewBase {
|
|||||||
super.resetNativeView();
|
super.resetNativeView();
|
||||||
}
|
}
|
||||||
|
|
||||||
public _loadFileOrResource(path: string, content: string) {
|
public _loadUrl(src: string) {
|
||||||
const nativeView = this.nativeView;
|
|
||||||
if (!nativeView) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseUrl = `file:///${path.substring(0, path.lastIndexOf('/') + 1)}`;
|
|
||||||
nativeView.loadDataWithBaseURL(baseUrl, content, "text/html", "utf-8", null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public _loadHttp(src: string) {
|
|
||||||
const nativeView = this.nativeView;
|
const nativeView = this.nativeView;
|
||||||
if (!nativeView) {
|
if (!nativeView) {
|
||||||
return;
|
return;
|
||||||
|
@ -109,12 +109,7 @@ export class WebView extends WebViewBase {
|
|||||||
this._ios.stopLoading();
|
this._ios.stopLoading();
|
||||||
}
|
}
|
||||||
|
|
||||||
public _loadFileOrResource(path: string, content: string) {
|
public _loadUrl(src: string) {
|
||||||
var baseURL = NSURL.fileURLWithPath(NSString.stringWithString(path).stringByDeletingLastPathComponent);
|
|
||||||
this._ios.loadHTMLStringBaseURL(content, baseURL);
|
|
||||||
}
|
|
||||||
|
|
||||||
public _loadHttp(src: string) {
|
|
||||||
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
|
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user