From 1a76b58768e2223d5191a6651e451336228e8a40 Mon Sep 17 00:00:00 2001 From: atanasovg Date: Thu, 24 Sep 2015 18:02:51 +0300 Subject: [PATCH] Extracts common logic into web-view-common. The check for http/https is now case insensitive. Fixes #819 --- ui/web-view/web-view-common.ts | 38 ++++++++++++++++++++-- ui/web-view/web-view.android.ts | 57 ++++++++++++++++++--------------- ui/web-view/web-view.d.ts | 5 +++ ui/web-view/web-view.ios.ts | 38 ++++++++-------------- 4 files changed, 86 insertions(+), 52 deletions(-) diff --git a/ui/web-view/web-view-common.ts b/ui/web-view/web-view-common.ts index 1d96e1b69..c81fc5d60 100644 --- a/ui/web-view/web-view-common.ts +++ b/ui/web-view/web-view-common.ts @@ -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 = 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."); } diff --git a/ui/web-view/web-view.android.ts b/ui/web-view/web-view.android.ts index 5eb7fc0e6..f836bb38d 100644 --- a/ui/web-view/web-view.android.ts +++ b/ui/web-view/web-view.android.ts @@ -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(); } diff --git a/ui/web-view/web-view.d.ts b/ui/web-view/web-view.d.ts index d8f6e5305..038085ae5 100644 --- a/ui/web-view/web-view.d.ts +++ b/ui/web-view/web-view.d.ts @@ -55,6 +55,11 @@ declare module "ui/web-view" { */ canGoForward: boolean; + /** + * Stops loading the current content (if any). + */ + stopLoading(): void; + /** * Navigates back. */ diff --git a/ui/web-view/web-view.ios.ts b/ui/web-view/web-view.ios.ts index 0f1937abf..7d642a8d9 100644 --- a/ui/web-view/web-view.ios.ts +++ b/ui/web-view/web-view.ios.ts @@ -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 {