File tree 5 files changed +27
-13
lines changed 5 files changed +27
-13
lines changed Original file line number Diff line number Diff line change 1
1
import http = require( "http" ) ;
2
+ import types = require( "utils/types" ) ;
2
3
3
4
// This is used for definition purposes only, it does not generate JavaScript for it.
4
5
import definition = require( "image-source" ) ;
@@ -30,8 +31,22 @@ export function fromUrl(url: string): Promise<definition.ImageSource> {
30
31
}
31
32
32
33
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
+
33
38
if ( path . indexOf ( RESOURCE_PREFIX ) === 0 ) {
34
39
return fromResource ( path . substr ( RESOURCE_PREFIX . length ) ) ;
35
40
}
36
41
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
37
52
}
Original file line number Diff line number Diff line change 4
4
declare module "image-source" {
5
5
6
6
/**
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
+ */
9
9
export class ImageSource {
10
10
/**
11
11
* Gets the height of this instance. This is a read-only property.
@@ -104,4 +104,10 @@ declare module "image-source" {
104
104
* @param path The location of the file on the file system.
105
105
*/
106
106
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
107
113
}
Original file line number Diff line number Diff line change 311
311
export var allCharacters : string ;
312
312
}
313
313
314
-
315
-
316
314
/**
317
315
* Defines the recognized image formats.
318
316
*/
Original file line number Diff line number Diff line change @@ -86,7 +86,6 @@ export module AutocapitalizationType {
86
86
export var allCharacters : string = "allCharacters" ;
87
87
}
88
88
89
-
90
89
export module ImageFormat {
91
90
export var png : string = "png" ;
92
91
export var jpeg : string = "jpeg" ;
Original file line number Diff line number Diff line change @@ -15,10 +15,6 @@ var IMAGE = "Image";
15
15
var ISLOADING = "isLoading" ;
16
16
var STRETCH = "stretch" ;
17
17
18
- function isUrl ( value : string ) : boolean {
19
- return value . indexOf ( "http://" ) === 0 || value . indexOf ( "https://" ) === 0 ;
20
- }
21
-
22
18
function isValidSrc ( src : any ) : boolean {
23
19
return types . isString ( src ) ;
24
20
}
@@ -34,16 +30,16 @@ function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
34
30
35
31
image . _setValue ( Image . isLoadingProperty , true ) ;
36
32
37
- if ( isUrl ( value ) ) {
33
+ if ( imageSource . isFileOrResourcePath ( value ) ) {
34
+ image . imageSource = imageSource . fromFileOrResource ( value ) ;
35
+ image . _setValue ( Image . isLoadingProperty , false ) ;
36
+ } else {
38
37
imageSource . fromUrl ( value ) . then ( ( r ) => {
39
38
if ( image [ "_url" ] === value ) {
40
39
image . imageSource = r ;
41
40
image . _setValue ( Image . isLoadingProperty , false ) ;
42
41
}
43
42
} ) ;
44
- } else {
45
- image . imageSource = imageSource . fromFileOrResource ( value ) ;
46
- image . _setValue ( Image . isLoadingProperty , false ) ;
47
43
}
48
44
}
49
45
else if ( value instanceof imageSource . ImageSource ) {
You can’t perform that action at this time.
0 commit comments