mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +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 { View, Property } from "../core/view";
|
||||
import { isFileOrResourcePath } from "../../utils/utils";
|
||||
import { File, knownFolders, path } from "../../file-system";
|
||||
|
||||
export { File, knownFolders, path, NavigationType };
|
||||
@ -38,9 +37,7 @@ export abstract class WebViewBase extends View implements WebViewDefinition {
|
||||
this.notify(args);
|
||||
}
|
||||
|
||||
abstract _loadFileOrResource(path: string, content: string): void;
|
||||
|
||||
abstract _loadHttp(src: string): void;
|
||||
abstract _loadUrl(src: string): void;
|
||||
|
||||
abstract _loadData(src: string): void;
|
||||
|
||||
@ -66,28 +63,28 @@ export abstract class WebViewBase extends View implements WebViewDefinition {
|
||||
[srcProperty.setNative](src: string) {
|
||||
this.stopLoading();
|
||||
|
||||
if (isFileOrResourcePath(src)) {
|
||||
if (src.indexOf("~/") === 0) {
|
||||
src = path.join(knownFolders.currentApp().path, src.replace("~/", ""));
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
if (File.exists(src)) {
|
||||
let file = File.fromPath(src);
|
||||
let content = file.readTextSync();
|
||||
this._loadFileOrResource(src, content);
|
||||
}
|
||||
} else if (src.toLowerCase().indexOf("http://") === 0 || src.toLowerCase().indexOf("https://") === 0) {
|
||||
this._loadHttp(src);
|
||||
if (src.toLowerCase().indexOf("http://") === 0 ||
|
||||
src.toLowerCase().indexOf("https://") === 0 ||
|
||||
src.toLowerCase().indexOf("file:///") === 0) {
|
||||
this._loadUrl(src);
|
||||
} else {
|
||||
this._loadData(src);
|
||||
}
|
||||
}
|
||||
|
||||
get url() : string {
|
||||
throw new Error("Property url of WebView is deprecated. Use src istead");
|
||||
get url(): string {
|
||||
throw new Error("Property url of WebView is deprecated. Use src instead");
|
||||
}
|
||||
set url(value:string){
|
||||
throw new Error("Property url of WebView is deprecated. Use src istead")
|
||||
set url(value: string) {
|
||||
throw new Error("Property url of WebView is deprecated. Use src instead")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,17 +115,7 @@ export class WebView extends WebViewBase {
|
||||
super.resetNativeView();
|
||||
}
|
||||
|
||||
public _loadFileOrResource(path: string, content: 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) {
|
||||
public _loadUrl(src: string) {
|
||||
const nativeView = this.nativeView;
|
||||
if (!nativeView) {
|
||||
return;
|
||||
|
@ -109,12 +109,7 @@ export class WebView extends WebViewBase {
|
||||
this._ios.stopLoading();
|
||||
}
|
||||
|
||||
public _loadFileOrResource(path: string, content: string) {
|
||||
var baseURL = NSURL.fileURLWithPath(NSString.stringWithString(path).stringByDeletingLastPathComponent);
|
||||
this._ios.loadHTMLStringBaseURL(content, baseURL);
|
||||
}
|
||||
|
||||
public _loadHttp(src: string) {
|
||||
public _loadUrl(src: string) {
|
||||
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user