mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Image src and imageSource properties defined
This commit is contained in:
@@ -6,9 +6,11 @@ import definition = require("ui/image");
|
||||
import trace = require("trace");
|
||||
import enums = require("ui/enums");
|
||||
import utils = require("utils/utils");
|
||||
import types = require("utils/types");
|
||||
|
||||
var SRC = "src";
|
||||
var IMAGE_SOURCE = "imageSource";
|
||||
|
||||
var SOURCE = "source";
|
||||
var URL = "url";
|
||||
var IMAGE = "Image";
|
||||
var ISLOADING = "isLoading";
|
||||
var STRETCH = "stretch";
|
||||
@@ -27,54 +29,62 @@ function isAppFile(value: string): boolean {
|
||||
return value.indexOf("~/") === 0;
|
||||
}
|
||||
|
||||
function isValidUrl(url: string): boolean {
|
||||
function isValidUrl(url: any): boolean {
|
||||
if (!types.isString(url)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var value = url ? url.trim() : "";
|
||||
return value !== "" && (isResource(value) || isAppFile(value) || isUrl(value));
|
||||
}
|
||||
|
||||
function onUrlPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
var image = <Image>data.object;
|
||||
var value: string = data.newValue;
|
||||
var value = data.newValue;
|
||||
|
||||
if (isValidUrl(value)) {
|
||||
value = value.trim();
|
||||
image.source = null;
|
||||
image.imageSource = null;
|
||||
image["_url"] = value;
|
||||
|
||||
image._setValue(Image.isLoadingProperty, true);
|
||||
|
||||
if (isResource(value)) {
|
||||
image.source = imageSource.fromResource(value.substr(RESOURCE_PREFIX.length));
|
||||
image.imageSource = imageSource.fromResource(value.substr(RESOURCE_PREFIX.length));
|
||||
image._setValue(Image.isLoadingProperty, false);
|
||||
}
|
||||
else if (isAppFile(value)) {
|
||||
image.source = imageSource.fromFile(value);
|
||||
image.imageSource = imageSource.fromFile(value);
|
||||
image._setValue(Image.isLoadingProperty, false);
|
||||
} else {
|
||||
imageSource.fromUrl(value).then((r) => {
|
||||
if (image["_url"] === value) {
|
||||
image.source = r;
|
||||
image.imageSource = r;
|
||||
image._setValue(Image.isLoadingProperty, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (value instanceof imageSource.ImageSource) {
|
||||
// Support binding the iamgeSource trough the src propoerty
|
||||
image.imageSource = value;
|
||||
}
|
||||
}
|
||||
|
||||
export class Image extends view.View implements definition.Image {
|
||||
|
||||
public static urlProperty = new dependencyObservable.Property(
|
||||
URL,
|
||||
public static srcProperty = new dependencyObservable.Property(
|
||||
SRC,
|
||||
IMAGE,
|
||||
new proxy.PropertyMetadata(
|
||||
"",
|
||||
dependencyObservable.PropertyMetadataSettings.None,
|
||||
onUrlPropertyChanged
|
||||
onSrcPropertyChanged
|
||||
)
|
||||
);
|
||||
|
||||
public static sourceProperty = new dependencyObservable.Property(
|
||||
SOURCE,
|
||||
public static imageSourceProperty = new dependencyObservable.Property(
|
||||
IMAGE_SOURCE,
|
||||
IMAGE,
|
||||
new proxy.PropertyMetadata(
|
||||
undefined,
|
||||
@@ -104,18 +114,18 @@ export class Image extends view.View implements definition.Image {
|
||||
super(options);
|
||||
}
|
||||
|
||||
get source(): imageSource.ImageSource {
|
||||
return this._getValue(Image.sourceProperty);
|
||||
get imageSource(): imageSource.ImageSource {
|
||||
return this._getValue(Image.imageSourceProperty);
|
||||
}
|
||||
set source(value: imageSource.ImageSource) {
|
||||
this._setValue(Image.sourceProperty, value);
|
||||
set imageSource(value: imageSource.ImageSource) {
|
||||
this._setValue(Image.imageSourceProperty, value);
|
||||
}
|
||||
|
||||
get url(): string {
|
||||
return this._getValue(Image.urlProperty);
|
||||
get src(): string {
|
||||
return this._getValue(Image.srcProperty);
|
||||
}
|
||||
set url(value: string) {
|
||||
this._setValue(Image.urlProperty, value);
|
||||
set src(value: string) {
|
||||
this._setValue(Image.srcProperty, value);
|
||||
}
|
||||
|
||||
get isLoading(): boolean {
|
||||
@@ -139,8 +149,8 @@ export class Image extends view.View implements definition.Image {
|
||||
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
|
||||
trace.write(this + " :onMeasure: " + utils.layout.getMode(widthMode) + " " + width + ", " + utils.layout.getMode(heightMode) + " " + height, trace.categories.Layout);
|
||||
|
||||
var nativeWidth = this.source ? this.source.width : 0;
|
||||
var nativeHeight = this.source ? this.source.height : 0;
|
||||
var nativeWidth = this.imageSource ? this.imageSource.width : 0;
|
||||
var nativeHeight = this.imageSource ? this.imageSource.height : 0;
|
||||
|
||||
var measureWidth = Math.max(nativeWidth, this.minWidth);
|
||||
var measureHeight = Math.max(nativeHeight, this.minHeight);
|
||||
|
||||
@@ -30,7 +30,7 @@ function onStretchPropertyChanged(data: dependencyObservable.PropertyChangeData)
|
||||
}
|
||||
}
|
||||
|
||||
function onSourcePropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
function onImageSourcePropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
var image = <Image>data.object;
|
||||
if (!image.android) {
|
||||
return;
|
||||
@@ -42,7 +42,7 @@ function onSourcePropertyChanged(data: dependencyObservable.PropertyChangeData)
|
||||
}
|
||||
|
||||
// register the setNativeValue callback
|
||||
(<proxy.PropertyMetadata>imageCommon.Image.sourceProperty.metadata).onSetNativeValue = onSourcePropertyChanged;
|
||||
(<proxy.PropertyMetadata>imageCommon.Image.imageSourceProperty.metadata).onSetNativeValue = onImageSourcePropertyChanged;
|
||||
(<proxy.PropertyMetadata>imageCommon.Image.stretchProperty.metadata).onSetNativeValue = onStretchPropertyChanged;
|
||||
|
||||
export class Image extends imageCommon.Image {
|
||||
|
||||
4
ui/image/image.d.ts
vendored
4
ui/image/image.d.ts
vendored
@@ -28,12 +28,12 @@ declare module "ui/image" {
|
||||
/**
|
||||
* Gets or sets the image source of the image.
|
||||
*/
|
||||
source: imageSource.ImageSource;
|
||||
imageSource: imageSource.ImageSource;
|
||||
|
||||
/**
|
||||
* Gets or sets the URL of the image.
|
||||
*/
|
||||
url: string;
|
||||
src: string;
|
||||
|
||||
/**
|
||||
* Gets a value indicating if the image is currently loading
|
||||
|
||||
@@ -28,7 +28,7 @@ function onStretchPropertyChanged(data: dependencyObservable.PropertyChangeData)
|
||||
}
|
||||
}
|
||||
|
||||
function onSourcePropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
function onImageSourcePropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
var image = <Image>data.object;
|
||||
image.ios.image = data.newValue ? data.newValue.ios : null;
|
||||
|
||||
@@ -38,8 +38,8 @@ function onSourcePropertyChanged(data: dependencyObservable.PropertyChangeData)
|
||||
}
|
||||
|
||||
// register the setNativeValue callback
|
||||
(<proxy.PropertyMetadata>imageCommon.Image.imageSourceProperty.metadata).onSetNativeValue = onImageSourcePropertyChanged;
|
||||
(<proxy.PropertyMetadata>imageCommon.Image.stretchProperty.metadata).onSetNativeValue = onStretchPropertyChanged;
|
||||
(<proxy.PropertyMetadata>imageCommon.Image.sourceProperty.metadata).onSetNativeValue = onSourcePropertyChanged;
|
||||
|
||||
export class Image extends imageCommon.Image {
|
||||
private _ios: UIImageView;
|
||||
|
||||
Reference in New Issue
Block a user