mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
src property added
This commit is contained in:
@ -22,11 +22,31 @@ function onUrlPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
// register the setNativeValue callback
|
||||
(<proxy.PropertyMetadata>urlProperty.metadata).onSetNativeValue = onUrlPropertyChanged;
|
||||
|
||||
var srcProperty = new dependencyObservable.Property(
|
||||
"src",
|
||||
"WebView",
|
||||
new proxy.PropertyMetadata("")
|
||||
);
|
||||
|
||||
function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
var webView = <WebView>data.object;
|
||||
|
||||
if (webView._suspendLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
webView._loadSrc(data.newValue);
|
||||
}
|
||||
|
||||
// register the setNativeValue callback
|
||||
(<proxy.PropertyMetadata>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.");
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
7
ui/web-view/web-view.d.ts
vendored
7
ui/web-view/web-view.d.ts
vendored
@ -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.
|
||||
*/
|
||||
|
@ -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);
|
||||
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user