mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
Extracts common logic into web-view-common. The check for http/https is now case insensitive. Fixes #819
This commit is contained in:
@ -2,6 +2,9 @@
|
||||
import view = require("ui/core/view");
|
||||
import dependencyObservable = require("ui/core/dependency-observable");
|
||||
import proxy = require("ui/core/proxy");
|
||||
import utils = require("utils/utils");
|
||||
import fs = require("file-system");
|
||||
import trace = require("trace");
|
||||
|
||||
var urlProperty = new dependencyObservable.Property(
|
||||
"url",
|
||||
@ -35,7 +38,26 @@ function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
return;
|
||||
}
|
||||
|
||||
webView._loadSrc(data.newValue);
|
||||
webView.stopLoading();
|
||||
|
||||
var src = <string>data.newValue;
|
||||
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
|
||||
|
||||
if (utils.isFileOrResourcePath(src)) {
|
||||
if (src.indexOf("~/") === 0) {
|
||||
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
|
||||
}
|
||||
|
||||
if (fs.File.exists(src)) {
|
||||
var file = fs.File.fromPath(src);
|
||||
var content = file.readTextSync();
|
||||
webView._loadFileOrResource(src, content);
|
||||
}
|
||||
} else if (src.toLowerCase().indexOf("http://") === 0 || src.toLowerCase().indexOf("https://") === 0) {
|
||||
webView._loadHttp(src);
|
||||
} else {
|
||||
webView._loadData(src);
|
||||
}
|
||||
}
|
||||
|
||||
// register the setNativeValue callback
|
||||
@ -101,7 +123,19 @@ export class WebView extends view.View implements definition.WebView {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
|
||||
public _loadSrc(src: string) {
|
||||
public _loadFileOrResource(path: string, content: string) {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
|
||||
public _loadHttp(src: string) {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
|
||||
public _loadData(src: string) {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
|
||||
public stopLoading(): void {
|
||||
throw new Error("This member is abstract.");
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
import common = require("./web-view-common");
|
||||
import trace = require("trace");
|
||||
import utils = require("utils/utils");
|
||||
import fs = require("file-system");
|
||||
|
||||
global.moduleMerge(common, exports);
|
||||
|
||||
@ -70,41 +68,50 @@ export class WebView extends common.WebView {
|
||||
}
|
||||
|
||||
public _loadUrl(url: string) {
|
||||
if (!this._android) {
|
||||
return;
|
||||
}
|
||||
|
||||
trace.write("WebView._loadUrl(" + url + ")", trace.categories.Debug);
|
||||
this._android.stopLoading();
|
||||
this._android.loadUrl(url);
|
||||
}
|
||||
|
||||
public _loadSrc(src: string) {
|
||||
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
|
||||
|
||||
this._android.stopLoading();
|
||||
this._android.loadUrl("about:blank");
|
||||
|
||||
if (utils.isFileOrResourcePath(src)) {
|
||||
|
||||
if (src.indexOf("~/") === 0) {
|
||||
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
|
||||
}
|
||||
|
||||
var file = fs.File.fromPath(src);
|
||||
if (file) {
|
||||
var baseUrl = `file:///${src.substring(0, src.lastIndexOf('/') + 1)}`;
|
||||
file.readText().then(r => {
|
||||
this._android.loadDataWithBaseURL(baseUrl, r, "text/html; charset=utf-8", "utf-8", null);
|
||||
});
|
||||
}
|
||||
} else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) {
|
||||
this._android.loadUrl(src);
|
||||
} else {
|
||||
this._android.loadData(src, "text/html; charset=utf-8", "utf-8");
|
||||
public _loadFileOrResource(path: string, content: string) {
|
||||
if (!this._android) {
|
||||
return;
|
||||
}
|
||||
|
||||
var baseUrl = `file:///${path.substring(0, path.lastIndexOf('/') + 1) }`;
|
||||
this._android.loadDataWithBaseURL(baseUrl, content, "text/html", null, null);
|
||||
}
|
||||
|
||||
public _loadHttp(src: string) {
|
||||
if (!this._android) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._android.loadUrl(src);
|
||||
}
|
||||
|
||||
public _loadData(src: string) {
|
||||
if (!this._android) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._android.loadData(src, "text/html", null);
|
||||
}
|
||||
|
||||
get canGoBack(): boolean {
|
||||
return this._android.canGoBack();
|
||||
}
|
||||
|
||||
public stopLoading() {
|
||||
if (this._android) {
|
||||
this._android.stopLoading();
|
||||
}
|
||||
}
|
||||
|
||||
get canGoForward(): boolean {
|
||||
return this._android.canGoForward();
|
||||
}
|
||||
|
5
ui/web-view/web-view.d.ts
vendored
5
ui/web-view/web-view.d.ts
vendored
@ -55,6 +55,11 @@ declare module "ui/web-view" {
|
||||
*/
|
||||
canGoForward: boolean;
|
||||
|
||||
/**
|
||||
* Stops loading the current content (if any).
|
||||
*/
|
||||
stopLoading(): void;
|
||||
|
||||
/**
|
||||
* Navigates back.
|
||||
*/
|
||||
|
@ -1,7 +1,5 @@
|
||||
import common = require("./web-view-common");
|
||||
import trace = require("trace");
|
||||
import utils = require("utils/utils");
|
||||
import fs = require("file-system");
|
||||
|
||||
global.moduleMerge(common, exports);
|
||||
|
||||
@ -73,6 +71,10 @@ export class WebView extends common.WebView {
|
||||
return this._ios;
|
||||
}
|
||||
|
||||
public stopLoading() {
|
||||
this._ios.stopLoading();
|
||||
}
|
||||
|
||||
public _loadUrl(url: string) {
|
||||
trace.write("WebView._loadUrl(" + url + ")", trace.categories.Debug);
|
||||
|
||||
@ -82,31 +84,17 @@ export class WebView extends common.WebView {
|
||||
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(url)));
|
||||
}
|
||||
|
||||
public _loadSrc(src: string) {
|
||||
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
|
||||
public _loadFileOrResource(path: string, content: string) {
|
||||
var baseURL = NSURL.fileURLWithPath(NSString.stringWithString(path).stringByDeletingLastPathComponent);
|
||||
this._ios.loadHTMLStringBaseURL(content, baseURL);
|
||||
}
|
||||
|
||||
if (this._ios.loading) {
|
||||
this._ios.stopLoading();
|
||||
}
|
||||
public _loadHttp(src: string) {
|
||||
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
|
||||
}
|
||||
|
||||
if (utils.isFileOrResourcePath(src)) {
|
||||
|
||||
if (src.indexOf("~/") === 0) {
|
||||
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
|
||||
}
|
||||
|
||||
var file = fs.File.fromPath(src);
|
||||
if (file) {
|
||||
var baseURL = NSURL.fileURLWithPath(NSString.stringWithString(src).stringByDeletingLastPathComponent);
|
||||
file.readText().then((r) => {
|
||||
this._ios.loadHTMLStringBaseURL(r, baseURL);
|
||||
});
|
||||
}
|
||||
} else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) {
|
||||
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
|
||||
} else {
|
||||
this._ios.loadHTMLStringBaseURL(src, null);
|
||||
}
|
||||
public _loadData(src: string) {
|
||||
this._ios.loadHTMLStringBaseURL(src, null);
|
||||
}
|
||||
|
||||
get canGoBack(): boolean {
|
||||
|
Reference in New Issue
Block a user