From 6b71ea07b455cf31f06cb5be1bb107b95cf8a9d1 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Mon, 18 May 2015 17:15:04 +0300 Subject: [PATCH 1/4] isFileOrResourcePath and RESOURCE_PREFIX moved in utils --- image-source/image-source-common.ts | 16 ++++------------ image-source/image-source.d.ts | 2 +- utils/utils-common.ts | 12 ++++++++++++ utils/utils.d.ts | 8 ++++++++ 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/image-source/image-source-common.ts b/image-source/image-source-common.ts index 4b4b1c49a..06a56d203 100644 --- a/image-source/image-source-common.ts +++ b/image-source/image-source-common.ts @@ -1,11 +1,9 @@ import http = require("http"); -import types = require("utils/types"); +import utils = require("utils/utils"); // This is used for definition purposes only, it does not generate JavaScript for it. import definition = require("image-source"); -var RESOURCE_PREFIX = "res://"; - export function fromResource(name: string): definition.ImageSource { var image = new definition.ImageSource(); return image.loadFromResource(name) ? image : null; @@ -40,18 +38,12 @@ export function fromFileOrResource(path: string): definition.ImageSource { throw new Error("Path \"" + "\" is not a valid file or resource."); } - if (path.indexOf(RESOURCE_PREFIX) === 0) { - return fromResource(path.substr(RESOURCE_PREFIX.length)); + if (path.indexOf(utils.RESOURCE_PREFIX) === 0) { + return fromResource(path.substr(utils.RESOURCE_PREFIX.length)); } return fromFile(path); } export function isFileOrResourcePath(path: string): boolean { - if (!types.isString(path)) { - return false; - } - - return path.indexOf("~/") === 0 || // relative to AppRoot - path.indexOf("/") === 0 || // absolute path - path.indexOf(RESOURCE_PREFIX) === 0; // resource + return utils.isFileOrResourcePath(path); } \ No newline at end of file diff --git a/image-source/image-source.d.ts b/image-source/image-source.d.ts index 2e1a3ee16..956612c6e 100644 --- a/image-source/image-source.d.ts +++ b/image-source/image-source.d.ts @@ -118,7 +118,7 @@ declare module "image-source" { export function fromFileOrResource(path: string): ImageSource; /** - * Returns true if the specified path points to a resource or local file. + * [Obsolete. Please use utils.isFileOrResourcePath instead!] Returns true if the specified path points to a resource or local file. * @param path The path. */ export function isFileOrResourcePath(path: string): boolean diff --git a/utils/utils-common.ts b/utils/utils-common.ts index 118209c41..92d38d4df 100644 --- a/utils/utils-common.ts +++ b/utils/utils-common.ts @@ -1,5 +1,7 @@ import types = require("utils/types"); +export var RESOURCE_PREFIX = "res://"; + export function copyFrom(source: any, target: any) { if (types.isDefined(source) && types.isDefined(target)) { var i: number; @@ -50,4 +52,14 @@ export module layout { export function getMeasureSpecSize(spec: number): number { return (spec & ~MODE_MASK); } +} + +export function isFileOrResourcePath(path: string): boolean { + if (!types.isString(path)) { + return false; + } + + return path.indexOf("~/") === 0 || // relative to AppRoot + path.indexOf("/") === 0 || // absolute path + path.indexOf(RESOURCE_PREFIX) === 0; // resource } \ No newline at end of file diff --git a/utils/utils.d.ts b/utils/utils.d.ts index f0e877009..25a22009d 100644 --- a/utils/utils.d.ts +++ b/utils/utils.d.ts @@ -2,6 +2,8 @@ import colorModule = require("color"); import view = require("ui/core/view"); + export var RESOURCE_PREFIX: string; + /** * Utility module related to layout. */ @@ -132,4 +134,10 @@ * An utility function that invokes garbage collection on the JavaScript side. */ export function GC(); + + /** + * Returns true if the specified path points to a resource or local file. + * @param path The path. + */ + export function isFileOrResourcePath(path: string): boolean } From 1dc3de774c55b1e9ab1c85a2690a79a286b0b5f0 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Mon, 18 May 2015 17:19:17 +0300 Subject: [PATCH 2/4] src property added --- ui/web-view/web-view-common.ts | 32 +++++++++++++++++++++++++++++++ ui/web-view/web-view.android.ts | 26 +++++++++++++++++++++++++ ui/web-view/web-view.d.ts | 7 ++++++- ui/web-view/web-view.ios.ts | 34 ++++++++++++++++++++++++++++++--- 4 files changed, 95 insertions(+), 4 deletions(-) diff --git a/ui/web-view/web-view-common.ts b/ui/web-view/web-view-common.ts index 8ee40e8bb..1d96e1b69 100644 --- a/ui/web-view/web-view-common.ts +++ b/ui/web-view/web-view-common.ts @@ -22,11 +22,31 @@ function onUrlPropertyChanged(data: dependencyObservable.PropertyChangeData) { // register the setNativeValue callback (urlProperty.metadata).onSetNativeValue = onUrlPropertyChanged; +var srcProperty = new dependencyObservable.Property( + "src", + "WebView", + new proxy.PropertyMetadata("") + ); + +function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) { + var webView = data.object; + + if (webView._suspendLoading) { + return; + } + + webView._loadSrc(data.newValue); +} + +// register the setNativeValue callback +(srcProperty.metadata).onSetNativeValue = onSrcPropertyChanged; + export class WebView extends view.View implements definition.WebView { public static loadStartedEvent = "loadStarted"; public static loadFinishedEvent = "loadFinished"; public static urlProperty = urlProperty; + public static srcProperty = srcProperty; public _suspendLoading: boolean; @@ -42,6 +62,14 @@ export class WebView extends view.View implements definition.WebView { this._setValue(WebView.urlProperty, value); } + get src(): string { + return this._getValue(WebView.srcProperty); + } + + set src(value: string) { + this._setValue(WebView.srcProperty, value); + } + public _onLoadFinished(url: string, error?: string) { this._suspendLoading = true; @@ -73,6 +101,10 @@ export class WebView extends view.View implements definition.WebView { throw new Error("This member is abstract."); } + public _loadSrc(src: string) { + throw new Error("This member is abstract."); + } + get canGoBack(): boolean { 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 a11e68719..b293ac2aa 100644 --- a/ui/web-view/web-view.android.ts +++ b/ui/web-view/web-view.android.ts @@ -1,5 +1,7 @@ import common = require("ui/web-view/web-view-common"); import trace = require("trace"); +import utils = require("utils/utils"); +import fs = require("file-system"); declare var exports; require("utils/module-merge").merge(common, exports); @@ -74,6 +76,30 @@ export class WebView extends common.WebView { this._android.loadUrl(url); } + public _loadSrc(src: string) { + trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug); + + this._android.stopLoading(); + + 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) { + file.readText().then((r) => { + this._android.loadData(r, "text/html", null); + }); + } + } else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) { + this._android.loadUrl(src); + } else { + this._android.loadData(src, "text/html", null); + } + } + get canGoBack(): boolean { return this._android.canGoBack(); } diff --git a/ui/web-view/web-view.d.ts b/ui/web-view/web-view.d.ts index 29956a8ec..d8f6e5305 100644 --- a/ui/web-view/web-view.d.ts +++ b/ui/web-view/web-view.d.ts @@ -36,10 +36,15 @@ declare module "ui/web-view" { ios: UIWebView; /** - * Gets or sets the url displayed by this instance. + * [Obsolete. Please use src instead!] Gets or sets the url displayed by this instance. */ url: string; + /** + * Gets or sets the url, local file path or HTML string. + */ + src: string; + /** * Gets a value indicating whether the WebView can navigate back. */ diff --git a/ui/web-view/web-view.ios.ts b/ui/web-view/web-view.ios.ts index dd98f5214..edee9ce0a 100644 --- a/ui/web-view/web-view.ios.ts +++ b/ui/web-view/web-view.ios.ts @@ -1,5 +1,7 @@ import common = require("ui/web-view/web-view-common"); import trace = require("trace"); +import utils = require("utils/utils"); +import fs = require("file-system"); declare var exports; require("utils/module-merge").merge(common, exports); @@ -17,16 +19,16 @@ class UIWebViewDelegateImpl extends NSObject implements UIWebViewDelegate { this._owner = owner; return this; } - + public webViewShouldStartLoadWithRequestNavigationType(webView: UIWebView, request: NSURLRequest, navigationType: number) { if (request.URL) { trace.write("UIWebViewDelegateClass.webViewShouldStartLoadWithRequestNavigationType(" + request.URL.absoluteString + ", " + navigationType + ")", trace.categories.Debug); this._owner._onLoadStarted(request.URL.absoluteString); } - + return true; } - + public webViewDidStartLoad(webView: UIWebView) { trace.write("UIWebViewDelegateClass.webViewDidStartLoad(" + webView.request.URL + ")", trace.categories.Debug); } @@ -81,6 +83,32 @@ 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); + + if (this._ios.loading) { + this._ios.stopLoading(); + } + + 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) { + file.readText().then((r) => { + this._ios.loadHTMLStringBaseURL(r, null); + }); + } + } else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) { + this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src))); + } else { + this._ios.loadHTMLStringBaseURL(src, null); + } + } + get canGoBack(): boolean { return this._ios.canGoBack; } From 8e2a5fe3fde06b37ff154efd8c8e707581f7ba89 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Mon, 18 May 2015 17:19:25 +0300 Subject: [PATCH 3/4] WebView added --- apps/tests/xml-declaration/mainPage.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/tests/xml-declaration/mainPage.xml b/apps/tests/xml-declaration/mainPage.xml index a6ea20df9..d801544c8 100644 --- a/apps/tests/xml-declaration/mainPage.xml +++ b/apps/tests/xml-declaration/mainPage.xml @@ -7,6 +7,8 @@ + +