Skip to content

Commit cf1a015

Browse files
author
vakrilov
committed
Imporvements and lint fix
1 parent 36e0f80 commit cf1a015

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

image-source/image-source-common.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import http = require("http");
2+
import types = require("utils/types");
23

34
// This is used for definition purposes only, it does not generate JavaScript for it.
45
import definition = require("image-source");
@@ -30,8 +31,22 @@ export function fromUrl(url: string): Promise<definition.ImageSource> {
3031
}
3132

3233
export function fromFileOrResource(path: string): definition.ImageSource {
34+
if (!isFileOrResourcePath(path)) {
35+
throw new Error("Path \"" + "\" is not a valid file or resource.");
36+
}
37+
3338
if (path.indexOf(RESOURCE_PREFIX) === 0) {
3439
return fromResource(path.substr(RESOURCE_PREFIX.length));
3540
}
3641
return fromFile(path);
42+
}
43+
44+
export function isFileOrResourcePath(path: string): boolean {
45+
if (!types.isString(path)) {
46+
return false;
47+
}
48+
49+
return path.indexOf("~/") === 0 || // relative to AppRoot
50+
path.indexOf("/") === 0 || // absolute path
51+
path.indexOf(RESOURCE_PREFIX) === 0; // resource
3752
}

image-source/image-source.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
declare module "image-source" {
55

66
/**
7-
* Encapsulates the common abstraction behind a platform specific object (typically a Bitmap) that is used as a source for images.
8-
*/
7+
* Encapsulates the common abstraction behind a platform specific object (typically a Bitmap) that is used as a source for images.
8+
*/
99
export class ImageSource {
1010
/**
1111
* Gets the height of this instance. This is a read-only property.
@@ -104,4 +104,10 @@ declare module "image-source" {
104104
* @param path The location of the file on the file system.
105105
*/
106106
export function fromFileOrResource(path: string): ImageSource;
107+
108+
/**
109+
* Returns true if the specified path points to a resource or local file.
110+
* @param path The path.
111+
*/
112+
export function isFileOrResourcePath(path: string): boolean
107113
}

ui/enums/enums.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,6 @@
311311
export var allCharacters: string;
312312
}
313313

314-
315-
316314
/**
317315
* Defines the recognized image formats.
318316
*/

ui/enums/enums.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ export module AutocapitalizationType {
8686
export var allCharacters: string = "allCharacters";
8787
}
8888

89-
9089
export module ImageFormat {
9190
export var png: string = "png";
9291
export var jpeg: string = "jpeg";

ui/image/image-common.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ var IMAGE = "Image";
1515
var ISLOADING = "isLoading";
1616
var STRETCH = "stretch";
1717

18-
function isUrl(value: string): boolean {
19-
return value.indexOf("http://") === 0 || value.indexOf("https://") === 0;
20-
}
21-
2218
function isValidSrc(src: any): boolean {
2319
return types.isString(src);
2420
}
@@ -34,16 +30,16 @@ function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
3430

3531
image._setValue(Image.isLoadingProperty, true);
3632

37-
if (isUrl(value)) {
33+
if (imageSource.isFileOrResourcePath(value)) {
34+
image.imageSource = imageSource.fromFileOrResource(value);
35+
image._setValue(Image.isLoadingProperty, false);
36+
} else {
3837
imageSource.fromUrl(value).then((r) => {
3938
if (image["_url"] === value) {
4039
image.imageSource = r;
4140
image._setValue(Image.isLoadingProperty, false);
4241
}
4342
});
44-
} else {
45-
image.imageSource = imageSource.fromFileOrResource(value);
46-
image._setValue(Image.isLoadingProperty, false);
4743
}
4844
}
4945
else if (value instanceof imageSource.ImageSource) {

0 commit comments

Comments
 (0)