forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-common.ts
138 lines (123 loc) · 5.65 KB
/
image-common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Image as ImageDefinition, Stretch } from ".";
import { View, Property, InheritedCssProperty, Length, Style, Color, isIOS, booleanConverter, CSSType, traceEnabled, traceWrite, traceCategories } from "../core/view";
import { ImageAsset } from "../../image-asset";
import { ImageSource, fromAsset, fromNativeSource, fromUrl } from "../../image-source";
import { isDataURI, isFileOrResourcePath, RESOURCE_PREFIX } from "../../utils/utils";
export * from "../core/view";
export { ImageSource, ImageAsset, fromAsset, fromNativeSource, fromUrl, isDataURI, isFileOrResourcePath, RESOURCE_PREFIX };
@CSSType("Image")
export abstract class ImageBase extends View implements ImageDefinition {
public imageSource: ImageSource;
public src: string | ImageSource;
public isLoading: boolean;
public stretch: Stretch;
public loadMode: "sync" | "async";
public decodeWidth: Length;
public decodeHeight: Length;
get tintColor(): Color {
return this.style.tintColor;
}
set tintColor(value: Color) {
this.style.tintColor = value;
}
/**
* @internal
*/
public _createImageSourceFromSrc(value: string | ImageSource | ImageAsset): void {
const originalValue = value;
const sync = this.loadMode === "sync";
if (typeof value === "string" || value instanceof String) {
value = value.trim();
this.imageSource = null;
this["_url"] = value;
this.isLoading = true;
const source = new ImageSource();
const imageLoaded = () => {
let currentValue = this.src;
if (currentValue !== originalValue) {
return;
}
this.imageSource = source;
this.isLoading = false;
};
if (isDataURI(value)) {
const base64Data = value.split(",")[1];
if (base64Data !== undefined) {
if (sync) {
source.loadFromBase64(base64Data);
imageLoaded();
} else {
source.fromBase64(base64Data).then(imageLoaded);
}
}
} else if (isFileOrResourcePath(value)) {
if (value.indexOf(RESOURCE_PREFIX) === 0) {
const resPath = value.substr(RESOURCE_PREFIX.length);
if (sync) {
source.loadFromResource(resPath);
imageLoaded();
} else {
this.imageSource = null;
source.fromResource(resPath).then(imageLoaded);
}
} else {
if (sync) {
source.loadFromFile(value);
imageLoaded();
} else {
this.imageSource = null;
source.fromFile(value).then(imageLoaded);
}
}
} else {
this.imageSource = null;
fromUrl(value).then((r) => {
if (this["_url"] === value) {
this.imageSource = r;
this.isLoading = false;
}
}, err => {
// catch: Response content may not be converted to an Image
this.isLoading = false;
if (traceEnabled()) {
if (typeof err === "object" && err.message) {
err = err.message;
}
traceWrite(err, traceCategories.Debug);
}
});
}
} else if (value instanceof ImageSource) {
// Support binding the imageSource trough the src property
this.imageSource = value;
this.isLoading = false;
}
else if (value instanceof ImageAsset) {
fromAsset(value).then((result) => {
this.imageSource = result;
this.isLoading = false;
});
}
else {
this.imageSource = fromNativeSource(value);
this.isLoading = false;
}
}
}
ImageBase.prototype.recycleNativeView = "auto";
export const imageSourceProperty = new Property<ImageBase, ImageSource>({ name: "imageSource" });
imageSourceProperty.register(ImageBase);
export const srcProperty = new Property<ImageBase, any>({ name: "src" });
srcProperty.register(ImageBase);
export const loadModeProperty = new Property<ImageBase, "sync" | "async">({ name: "loadMode", defaultValue: "sync" });
loadModeProperty.register(ImageBase);
export const isLoadingProperty = new Property<ImageBase, boolean>({ name: "isLoading", defaultValue: false, valueConverter: booleanConverter });
isLoadingProperty.register(ImageBase);
export const stretchProperty = new Property<ImageBase, Stretch>({ name: "stretch", defaultValue: "aspectFit", affectsLayout: isIOS });
stretchProperty.register(ImageBase);
export const tintColorProperty = new InheritedCssProperty<Style, Color>({ name: "tintColor", cssName: "tint-color", equalityComparer: Color.equals, valueConverter: (value) => new Color(value) });
tintColorProperty.register(Style);
export const decodeHeightProperty = new Property<ImageBase, Length>({ name: "decodeHeight", defaultValue: { value: 0, unit: "dip" }, valueConverter: Length.parse });
decodeHeightProperty.register(ImageBase);
export const decodeWidthProperty = new Property<ImageBase, Length>({ name: "decodeWidth", defaultValue: { value: 0, unit: "dip" }, valueConverter: Length.parse });
decodeWidthProperty.register(ImageBase);