Skip to content

Commit 1dc3de7

Browse files
author
Vladimir Enchev
committed
src property added
1 parent 6b71ea0 commit 1dc3de7

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

ui/web-view/web-view-common.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,31 @@ function onUrlPropertyChanged(data: dependencyObservable.PropertyChangeData) {
2222
// register the setNativeValue callback
2323
(<proxy.PropertyMetadata>urlProperty.metadata).onSetNativeValue = onUrlPropertyChanged;
2424

25+
var srcProperty = new dependencyObservable.Property(
26+
"src",
27+
"WebView",
28+
new proxy.PropertyMetadata("")
29+
);
30+
31+
function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
32+
var webView = <WebView>data.object;
33+
34+
if (webView._suspendLoading) {
35+
return;
36+
}
37+
38+
webView._loadSrc(data.newValue);
39+
}
40+
41+
// register the setNativeValue callback
42+
(<proxy.PropertyMetadata>srcProperty.metadata).onSetNativeValue = onSrcPropertyChanged;
43+
2544
export class WebView extends view.View implements definition.WebView {
2645
public static loadStartedEvent = "loadStarted";
2746
public static loadFinishedEvent = "loadFinished";
2847

2948
public static urlProperty = urlProperty;
49+
public static srcProperty = srcProperty;
3050

3151
public _suspendLoading: boolean;
3252

@@ -42,6 +62,14 @@ export class WebView extends view.View implements definition.WebView {
4262
this._setValue(WebView.urlProperty, value);
4363
}
4464

65+
get src(): string {
66+
return this._getValue(WebView.srcProperty);
67+
}
68+
69+
set src(value: string) {
70+
this._setValue(WebView.srcProperty, value);
71+
}
72+
4573
public _onLoadFinished(url: string, error?: string) {
4674

4775
this._suspendLoading = true;
@@ -73,6 +101,10 @@ export class WebView extends view.View implements definition.WebView {
73101
throw new Error("This member is abstract.");
74102
}
75103

104+
public _loadSrc(src: string) {
105+
throw new Error("This member is abstract.");
106+
}
107+
76108
get canGoBack(): boolean {
77109
throw new Error("This member is abstract.");
78110
}

ui/web-view/web-view.android.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import common = require("ui/web-view/web-view-common");
22
import trace = require("trace");
3+
import utils = require("utils/utils");
4+
import fs = require("file-system");
35

46
declare var exports;
57
require("utils/module-merge").merge(common, exports);
@@ -74,6 +76,30 @@ export class WebView extends common.WebView {
7476
this._android.loadUrl(url);
7577
}
7678

79+
public _loadSrc(src: string) {
80+
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
81+
82+
this._android.stopLoading();
83+
84+
if (utils.isFileOrResourcePath(src)) {
85+
86+
if (src.indexOf("~/") === 0) {
87+
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
88+
}
89+
90+
var file = fs.File.fromPath(src);
91+
if (file) {
92+
file.readText().then((r) => {
93+
this._android.loadData(r, "text/html", null);
94+
});
95+
}
96+
} else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) {
97+
this._android.loadUrl(src);
98+
} else {
99+
this._android.loadData(src, "text/html", null);
100+
}
101+
}
102+
77103
get canGoBack(): boolean {
78104
return this._android.canGoBack();
79105
}

ui/web-view/web-view.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ declare module "ui/web-view" {
3636
ios: UIWebView;
3737

3838
/**
39-
* Gets or sets the url displayed by this instance.
39+
* [Obsolete. Please use src instead!] Gets or sets the url displayed by this instance.
4040
*/
4141
url: string;
4242

43+
/**
44+
* Gets or sets the url, local file path or HTML string.
45+
*/
46+
src: string;
47+
4348
/**
4449
* Gets a value indicating whether the WebView can navigate back.
4550
*/

ui/web-view/web-view.ios.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import common = require("ui/web-view/web-view-common");
22
import trace = require("trace");
3+
import utils = require("utils/utils");
4+
import fs = require("file-system");
35

46
declare var exports;
57
require("utils/module-merge").merge(common, exports);
@@ -17,16 +19,16 @@ class UIWebViewDelegateImpl extends NSObject implements UIWebViewDelegate {
1719
this._owner = owner;
1820
return this;
1921
}
20-
22+
2123
public webViewShouldStartLoadWithRequestNavigationType(webView: UIWebView, request: NSURLRequest, navigationType: number) {
2224
if (request.URL) {
2325
trace.write("UIWebViewDelegateClass.webViewShouldStartLoadWithRequestNavigationType(" + request.URL.absoluteString + ", " + navigationType + ")", trace.categories.Debug);
2426
this._owner._onLoadStarted(request.URL.absoluteString);
2527
}
26-
28+
2729
return true;
2830
}
29-
31+
3032
public webViewDidStartLoad(webView: UIWebView) {
3133
trace.write("UIWebViewDelegateClass.webViewDidStartLoad(" + webView.request.URL + ")", trace.categories.Debug);
3234
}
@@ -81,6 +83,32 @@ export class WebView extends common.WebView {
8183
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(url)));
8284
}
8385

86+
public _loadSrc(src: string) {
87+
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
88+
89+
if (this._ios.loading) {
90+
this._ios.stopLoading();
91+
}
92+
93+
if (utils.isFileOrResourcePath(src)) {
94+
95+
if (src.indexOf("~/") === 0) {
96+
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
97+
}
98+
99+
var file = fs.File.fromPath(src);
100+
if (file) {
101+
file.readText().then((r) => {
102+
this._ios.loadHTMLStringBaseURL(r, null);
103+
});
104+
}
105+
} else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) {
106+
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
107+
} else {
108+
this._ios.loadHTMLStringBaseURL(src, null);
109+
}
110+
}
111+
84112
get canGoBack(): boolean {
85113
return this._ios.canGoBack;
86114
}

0 commit comments

Comments
 (0)