| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + <!DOCTYPE html> | ||
| 2 | + <html> | ||
| 3 | + <head> | ||
| 4 | + <title>MyTitle</title> | ||
| 5 | + <meta charset="utf-8" /> | ||
| 6 | + </head> | ||
| 7 | + <body> | ||
| 8 | + <span style="color:red">Test</span> | ||
| 9 | + </body> | ||
| 10 | + </html> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,9 +13,10 @@ import webViewModule = require("ui/web-view"); | |||
| 13 | 13 | // ### Declaring a WebView. | |
| 14 | 14 | //```XML | |
| 15 | 15 | // <Page> | |
| 16 | - // <WebView url="{{ someUrl }}" /> | ||
| 16 | + // <WebView src="{{ someUrl | pathToLocalFile | htmlString }}" /> | ||
| 17 | 17 | // </Page> | |
| 18 | 18 | //``` | |
| 19 | + | ||
| 19 | 20 | // </snippet> | |
| 20 | 21 | ||
| 21 | 22 | var _createWebViewFunc = function (): webViewModule.WebView { | |
@@ -76,6 +77,128 @@ export var testLoadExistingUrl = function () { | |||
| 76 | 77 | } | |
| 77 | 78 | } | |
| 78 | 79 | ||
| 80 | + export var testLoadLocalFile = function () { | ||
| 81 | + var newPage: page.Page; | ||
| 82 | + var webView = _createWebViewFunc(); | ||
| 83 | + var pageFactory = function (): page.Page { | ||
| 84 | + newPage = new page.Page(); | ||
| 85 | + newPage.content = webView; | ||
| 86 | + return newPage; | ||
| 87 | + }; | ||
| 88 | + | ||
| 89 | + helper.navigate(pageFactory); | ||
| 90 | + | ||
| 91 | + var testFinished = false; | ||
| 92 | + var actualHtml; | ||
| 93 | + var actualTitle; | ||
| 94 | + var actualError; | ||
| 95 | + | ||
| 96 | + var expectedTitle = 'MyTitle'; | ||
| 97 | + var expectedHtml = '<span style="color:red">Test</span>'; | ||
| 98 | + | ||
| 99 | + // <snippet module="ui/web-view" title="WebView"> | ||
| 100 | + // ### Using WebView | ||
| 101 | + // ``` JavaScript | ||
| 102 | + webView.on(webViewModule.WebView.loadFinishedEvent, function (args: webViewModule.LoadEventData) { | ||
| 103 | + // <hide> | ||
| 104 | + if (webView.ios) { | ||
| 105 | + actualHtml = webView.ios.stringByEvaluatingJavaScriptFromString("document.body.innerHTML").trim(); | ||
| 106 | + } else if (webView.android) { | ||
| 107 | + actualTitle = webView.android.getTitle() | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + actualError = args.error; | ||
| 111 | + testFinished = true; | ||
| 112 | + // </hide> | ||
| 113 | + var message; | ||
| 114 | + if (!args.error) { | ||
| 115 | + message = "WebView finished loading " + args.url; | ||
| 116 | + } | ||
| 117 | + else { | ||
| 118 | + message = "Error loading " + args.url + ": " + args.error; | ||
| 119 | + } | ||
| 120 | + //console.log(message); | ||
| 121 | + }); | ||
| 122 | + webView.src = "~/ui/web-view/test.html"; | ||
| 123 | + | ||
| 124 | + TKUnit.wait(4); | ||
| 125 | + | ||
| 126 | + helper.goBack(); | ||
| 127 | + | ||
| 128 | + if (testFinished) { | ||
| 129 | + if (webView.ios) { | ||
| 130 | + TKUnit.assert(actualHtml === expectedHtml, "File ~/ui/web-view/test.html not loaded properly. Actual: " + actualHtml); | ||
| 131 | + } else if (webView.android) { | ||
| 132 | + TKUnit.assert(actualTitle === expectedTitle, "File ~/ui/web-view/test.html not loaded properly. Actual: " + actualTitle); | ||
| 133 | + } | ||
| 134 | + TKUnit.assert(actualError === undefined, actualError); | ||
| 135 | + } | ||
| 136 | + else { | ||
| 137 | + TKUnit.assert(false, "TIMEOUT"); | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + export var testLoadHTMLString = function () { | ||
| 142 | + var newPage: page.Page; | ||
| 143 | + var webView = _createWebViewFunc(); | ||
| 144 | + var pageFactory = function (): page.Page { | ||
| 145 | + newPage = new page.Page(); | ||
| 146 | + newPage.content = webView; | ||
| 147 | + return newPage; | ||
| 148 | + }; | ||
| 149 | + | ||
| 150 | + helper.navigate(pageFactory); | ||
| 151 | + | ||
| 152 | + var testFinished = false; | ||
| 153 | + var actualHtml; | ||
| 154 | + var actualTitle; | ||
| 155 | + var actualError; | ||
| 156 | + | ||
| 157 | + var expectedTitle = 'MyTitle'; | ||
| 158 | + var expectedHtml = '<span style="color:red">Test</span>'; | ||
| 159 | + | ||
| 160 | + // <snippet module="ui/web-view" title="WebView"> | ||
| 161 | + // ### Using WebView | ||
| 162 | + // ``` JavaScript | ||
| 163 | + webView.on(webViewModule.WebView.loadFinishedEvent, function (args: webViewModule.LoadEventData) { | ||
| 164 | + // <hide> | ||
| 165 | + if (webView.ios) { | ||
| 166 | + actualHtml = webView.ios.stringByEvaluatingJavaScriptFromString("document.body.innerHTML").trim(); | ||
| 167 | + } else if (webView.android) { | ||
| 168 | + actualTitle = webView.android.getTitle() | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + actualError = args.error; | ||
| 172 | + testFinished = true; | ||
| 173 | + // </hide> | ||
| 174 | + var message; | ||
| 175 | + if (!args.error) { | ||
| 176 | + message = "WebView finished loading " + args.url; | ||
| 177 | + } | ||
| 178 | + else { | ||
| 179 | + message = "Error loading " + args.url + ": " + args.error; | ||
| 180 | + } | ||
| 181 | + //console.log(message); | ||
| 182 | + }); | ||
| 183 | + webView.src = '<!DOCTYPE html><html><head><title>MyTitle</title><meta charset="utf-8" /></head><body><span style="color:red">Test</span></body></html>'; | ||
| 184 | + | ||
| 185 | + TKUnit.wait(4); | ||
| 186 | + | ||
| 187 | + helper.goBack(); | ||
| 188 | + | ||
| 189 | + if (testFinished) { | ||
| 190 | + if (webView.ios) { | ||
| 191 | + TKUnit.assert(actualHtml === expectedHtml, "HTML string not loaded properly. Actual: " + actualHtml); | ||
| 192 | + } else if (webView.android) { | ||
| 193 | + TKUnit.assert(actualTitle === expectedTitle, "HTML string not loaded properly. Actual: " + actualTitle); | ||
| 194 | + } | ||
| 195 | + TKUnit.assert(actualError === undefined, actualError); | ||
| 196 | + } | ||
| 197 | + else { | ||
| 198 | + TKUnit.assert(false, "TIMEOUT"); | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | + | ||
| 79 | 202 | export var testLoadInvalidUrl = function () { | |
| 80 | 203 | var newPage: page.Page; | |
| 81 | 204 | var webView = _createWebViewFunc(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,8 @@ | |||
| 7 | 7 | <TabViewItem.view> | |
| 8 | 8 | <StackLayout> | |
| 9 | 9 | ||
| 10 | + <WebView src="<html><body><span style='color:red'>Test</span></body></html>" /> | ||
| 11 | + | ||
| 10 | 12 | <!--<ToolBar> | |
| 11 | 13 | <ToolBar.items> | |
| 12 | 14 | <ToolBarItem> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,9 @@ | |||
| 1 | 1 | import http = require("http"); | |
| 2 | - import types = require("utils/types"); | ||
| 2 | + import utils = require("utils/utils"); | ||
| 3 | 3 | ||
| 4 | 4 | // This is used for definition purposes only, it does not generate JavaScript for it. | |
| 5 | 5 | import definition = require("image-source"); | |
| 6 | 6 | ||
| 7 | - var RESOURCE_PREFIX = "res://"; | ||
| 8 | - | ||
| 9 | 7 | export function fromResource(name: string): definition.ImageSource { | |
| 10 | 8 | var image = new definition.ImageSource(); | |
| 11 | 9 | return image.loadFromResource(name) ? image : null; | |
@@ -40,18 +38,12 @@ export function fromFileOrResource(path: string): definition.ImageSource { | |||
| 40 | 38 | throw new Error("Path \"" + "\" is not a valid file or resource."); | |
| 41 | 39 | } | |
| 42 | 40 | ||
| 43 | - if (path.indexOf(RESOURCE_PREFIX) === 0) { | ||
| 44 | - return fromResource(path.substr(RESOURCE_PREFIX.length)); | ||
| 41 | + if (path.indexOf(utils.RESOURCE_PREFIX) === 0) { | ||
| 42 | + return fromResource(path.substr(utils.RESOURCE_PREFIX.length)); | ||
| 45 | 43 | } | |
| 46 | 44 | return fromFile(path); | |
| 47 | 45 | } | |
| 48 | 46 | ||
| 49 | 47 | export function isFileOrResourcePath(path: string): boolean { | |
| 50 | - if (!types.isString(path)) { | ||
| 51 | - return false; | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - return path.indexOf("~/") === 0 || // relative to AppRoot | ||
| 55 | - path.indexOf("/") === 0 || // absolute path | ||
| 56 | - path.indexOf(RESOURCE_PREFIX) === 0; // resource | ||
| 48 | + return utils.isFileOrResourcePath(path); | ||
| 57 | 49 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -118,7 +118,7 @@ declare module "image-source" { | |||
| 118 | 118 | export function fromFileOrResource(path: string): ImageSource; | |
| 119 | 119 | ||
| 120 | 120 | /** | |
| 121 | - * Returns true if the specified path points to a resource or local file. | ||
| 121 | + * [Obsolete. Please use utils.isFileOrResourcePath instead!] Returns true if the specified path points to a resource or local file. | ||
| 122 | 122 | * @param path The path. | |
| 123 | 123 | */ | |
| 124 | 124 | export function isFileOrResourcePath(path: string): boolean | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,11 +22,31 @@ function onUrlPropertyChanged(data: dependencyObservable.PropertyChangeData) { | |||
| 22 | 22 | // register the setNativeValue callback | |
| 23 | 23 | (<proxy.PropertyMetadata>urlProperty.metadata).onSetNativeValue = onUrlPropertyChanged; | |
| 24 | 24 | ||
| 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 | + | ||
| 25 | 44 | export class WebView extends view.View implements definition.WebView { | |
| 26 | 45 | public static loadStartedEvent = "loadStarted"; | |
| 27 | 46 | public static loadFinishedEvent = "loadFinished"; | |
| 28 | 47 | ||
| 29 | 48 | public static urlProperty = urlProperty; | |
| 49 | + public static srcProperty = srcProperty; | ||
| 30 | 50 | ||
| 31 | 51 | public _suspendLoading: boolean; | |
| 32 | 52 | ||
@@ -42,6 +62,14 @@ export class WebView extends view.View implements definition.WebView { | |||
| 42 | 62 | this._setValue(WebView.urlProperty, value); | |
| 43 | 63 | } | |
| 44 | 64 | ||
| 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 | + | ||
| 45 | 73 | public _onLoadFinished(url: string, error?: string) { | |
| 46 | 74 | ||
| 47 | 75 | this._suspendLoading = true; | |
@@ -73,6 +101,10 @@ export class WebView extends view.View implements definition.WebView { | |||
| 73 | 101 | throw new Error("This member is abstract."); | |
| 74 | 102 | } | |
| 75 | 103 | ||
| 104 | + public _loadSrc(src: string) { | ||
| 105 | + throw new Error("This member is abstract."); | ||
| 106 | + } | ||
| 107 | + | ||
| 76 | 108 | get canGoBack(): boolean { | |
| 77 | 109 | throw new Error("This member is abstract."); | |
| 78 | 110 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,7 @@ | |||
| 1 | 1 | import common = require("ui/web-view/web-view-common"); | |
| 2 | 2 | import trace = require("trace"); | |
| 3 | + import utils = require("utils/utils"); | ||
| 4 | + import fs = require("file-system"); | ||
| 3 | 5 | ||
| 4 | 6 | declare var exports; | |
| 5 | 7 | require("utils/module-merge").merge(common, exports); | |
@@ -74,6 +76,31 @@ export class WebView extends common.WebView { | |||
| 74 | 76 | this._android.loadUrl(url); | |
| 75 | 77 | } | |
| 76 | 78 | ||
| 79 | + public _loadSrc(src: string) { | ||
| 80 | + trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug); | ||
| 81 | + | ||
| 82 | + this._android.stopLoading(); | ||
| 83 | + this._android.loadUrl("about:blank"); | ||
| 84 | + | ||
| 85 | + if (utils.isFileOrResourcePath(src)) { | ||
| 86 | + | ||
| 87 | + if (src.indexOf("~/") === 0) { | ||
| 88 | + src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", "")); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + var file = fs.File.fromPath(src); | ||
| 92 | + if (file) { | ||
| 93 | + file.readText().then((r) => { | ||
| 94 | + this._android.loadData(r, "text/html", null); | ||
| 95 | + }); | ||
| 96 | + } | ||
| 97 | + } else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) { | ||
| 98 | + this._android.loadUrl(src); | ||
| 99 | + } else { | ||
| 100 | + this._android.loadData(src, "text/html", null); | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | + | ||
| 77 | 104 | get canGoBack(): boolean { | |
| 78 | 105 | return this._android.canGoBack(); | |
| 79 | 106 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,10 +36,15 @@ declare module "ui/web-view" { | |||
| 36 | 36 | ios: UIWebView; | |
| 37 | 37 | ||
| 38 | 38 | /** | |
| 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. | ||
| 40 | 40 | */ | |
| 41 | 41 | url: string; | |
| 42 | 42 | ||
| 43 | + /** | ||
| 44 | + * Gets or sets the url, local file path or HTML string. | ||
| 45 | + */ | ||
| 46 | + src: string; | ||
| 47 | + | ||
| 43 | 48 | /** | |
| 44 | 49 | * Gets a value indicating whether the WebView can navigate back. | |
| 45 | 50 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,7 @@ | |||
| 1 | 1 | import common = require("ui/web-view/web-view-common"); | |
| 2 | 2 | import trace = require("trace"); | |
| 3 | + import utils = require("utils/utils"); | ||
| 4 | + import fs = require("file-system"); | ||
| 3 | 5 | ||
| 4 | 6 | declare var exports; | |
| 5 | 7 | require("utils/module-merge").merge(common, exports); | |
@@ -17,16 +19,16 @@ class UIWebViewDelegateImpl extends NSObject implements UIWebViewDelegate { | |||
| 17 | 19 | this._owner = owner; | |
| 18 | 20 | return this; | |
| 19 | 21 | } | |
| 20 | - | ||
| 22 | + | ||
| 21 | 23 | public webViewShouldStartLoadWithRequestNavigationType(webView: UIWebView, request: NSURLRequest, navigationType: number) { | |
| 22 | 24 | if (request.URL) { | |
| 23 | 25 | trace.write("UIWebViewDelegateClass.webViewShouldStartLoadWithRequestNavigationType(" + request.URL.absoluteString + ", " + navigationType + ")", trace.categories.Debug); | |
| 24 | 26 | this._owner._onLoadStarted(request.URL.absoluteString); | |
| 25 | 27 | } | |
| 26 | - | ||
| 28 | + | ||
| 27 | 29 | return true; | |
| 28 | 30 | } | |
| 29 | - | ||
| 31 | + | ||
| 30 | 32 | public webViewDidStartLoad(webView: UIWebView) { | |
| 31 | 33 | trace.write("UIWebViewDelegateClass.webViewDidStartLoad(" + webView.request.URL + ")", trace.categories.Debug); | |
| 32 | 34 | } | |
@@ -81,6 +83,32 @@ export class WebView extends common.WebView { | |||
| 81 | 83 | this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(url))); | |
| 82 | 84 | } | |
| 83 | 85 | ||
| 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 | + | ||
| 84 | 112 | get canGoBack(): boolean { | |
| 85 | 113 | return this._ios.canGoBack; | |
| 86 | 114 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments