2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* @providesModule Image
|
2015-03-26 17:06:50 +00:00
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var EdgeInsetsPropType = require('EdgeInsetsPropType');
|
2015-04-21 17:48:54 +00:00
|
|
|
var ImageResizeMode = require('ImageResizeMode');
|
|
|
|
var ImageStylePropTypes = require('ImageStylePropTypes');
|
2015-02-20 04:10:52 +00:00
|
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
2015-03-17 10:08:52 +00:00
|
|
|
var NativeModules = require('NativeModules');
|
2015-02-20 04:10:52 +00:00
|
|
|
var PropTypes = require('ReactPropTypes');
|
|
|
|
var React = require('React');
|
2015-05-08 16:45:43 +00:00
|
|
|
var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
2015-02-20 04:10:52 +00:00
|
|
|
var StyleSheet = require('StyleSheet');
|
|
|
|
var StyleSheetPropType = require('StyleSheetPropType');
|
|
|
|
|
|
|
|
var flattenStyle = require('flattenStyle');
|
|
|
|
var invariant = require('invariant');
|
|
|
|
var merge = require('merge');
|
2015-04-17 01:17:19 +00:00
|
|
|
var requireNativeComponent = require('requireNativeComponent');
|
2015-04-21 17:48:54 +00:00
|
|
|
var resolveAssetSource = require('resolveAssetSource');
|
2015-04-17 01:17:19 +00:00
|
|
|
var verifyPropTypes = require('verifyPropTypes');
|
2015-04-21 17:48:54 +00:00
|
|
|
var warning = require('warning');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-27 20:55:01 +00:00
|
|
|
* A React component for displaying different types of images,
|
2015-02-20 04:10:52 +00:00
|
|
|
* including network images, static resources, temporary local images, and
|
2015-03-09 16:28:51 +00:00
|
|
|
* images from local disk, such as the camera roll.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-09 16:28:51 +00:00
|
|
|
* Example usage:
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-09 16:28:51 +00:00
|
|
|
* ```
|
|
|
|
* renderImages: function() {
|
|
|
|
* return (
|
|
|
|
* <View>
|
|
|
|
* <Image
|
|
|
|
* style={styles.icon}
|
2015-03-20 22:51:11 +00:00
|
|
|
* source={require('image!myIcon')}
|
2015-03-09 16:28:51 +00:00
|
|
|
* />
|
|
|
|
* <Image
|
|
|
|
* style={styles.logo}
|
|
|
|
* source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}
|
|
|
|
* />
|
|
|
|
* </View>
|
|
|
|
* );
|
|
|
|
* },
|
|
|
|
* ```
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var Image = React.createClass({
|
|
|
|
propTypes: {
|
2015-04-01 17:56:21 +00:00
|
|
|
/**
|
|
|
|
* `uri` is a string representing the resource identifier for the image, which
|
|
|
|
* could be an http address, a local file path, or the name of a static image
|
2015-05-18 20:03:57 +00:00
|
|
|
* resource (which should be wrapped in the `require('image!name')` function).
|
2015-04-01 17:56:21 +00:00
|
|
|
*/
|
2015-03-09 16:29:16 +00:00
|
|
|
source: PropTypes.shape({
|
|
|
|
uri: PropTypes.string,
|
2015-03-11 21:48:22 +00:00
|
|
|
}),
|
2015-04-17 01:17:19 +00:00
|
|
|
/**
|
|
|
|
* A static image to display while downloading the final image off the
|
|
|
|
* network.
|
|
|
|
*/
|
|
|
|
defaultSource: PropTypes.shape({
|
|
|
|
uri: PropTypes.string,
|
|
|
|
}),
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-04-01 17:56:21 +00:00
|
|
|
* Whether this element should be revealed as an accessible element.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
accessible: PropTypes.bool,
|
|
|
|
/**
|
2015-04-01 17:56:21 +00:00
|
|
|
* Custom string to display for accessibility.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
accessibilityLabel: PropTypes.string,
|
|
|
|
/**
|
2015-04-01 17:56:21 +00:00
|
|
|
* When the image is resized, the corners of the size specified
|
2015-02-20 04:10:52 +00:00
|
|
|
* by capInsets will stay a fixed size, but the center content and borders
|
|
|
|
* of the image will be stretched. This is useful for creating resizable
|
2015-04-01 17:56:21 +00:00
|
|
|
* rounded buttons, shadows, and other resizable assets. More info on
|
|
|
|
* [Apple documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/instm/UIImage/resizableImageWithCapInsets)
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
capInsets: EdgeInsetsPropType,
|
2015-04-17 01:17:19 +00:00
|
|
|
/**
|
|
|
|
* Determines how to resize the image when the frame doesn't match the raw
|
|
|
|
* image dimensions.
|
|
|
|
*/
|
|
|
|
resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch']),
|
2015-02-20 04:10:52 +00:00
|
|
|
style: StyleSheetPropType(ImageStylePropTypes),
|
|
|
|
/**
|
2015-04-01 17:56:21 +00:00
|
|
|
* A unique identifier for this element to be used in UI Automation
|
2015-02-20 04:10:52 +00:00
|
|
|
* testing scripts.
|
|
|
|
*/
|
|
|
|
testID: PropTypes.string,
|
2015-05-16 01:05:49 +00:00
|
|
|
/**
|
|
|
|
* Invoked on mount and layout changes with
|
|
|
|
*
|
|
|
|
* {nativeEvent: { layout: {x, y, width, height}}}.
|
|
|
|
*/
|
|
|
|
onLayout: PropTypes.func,
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
statics: {
|
|
|
|
resizeMode: ImageResizeMode,
|
|
|
|
},
|
|
|
|
|
|
|
|
mixins: [NativeMethodsMixin],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `NativeMethodsMixin` will look for this when invoking `setNativeProps`. We
|
|
|
|
* make `this` look like an actual native component class.
|
|
|
|
*/
|
|
|
|
viewConfig: {
|
|
|
|
uiViewClassName: 'UIView',
|
2015-05-08 16:45:43 +00:00
|
|
|
validAttributes: ReactNativeViewAttributes.UIView
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2015-04-17 01:17:19 +00:00
|
|
|
for (var prop in nativeOnlyProps) {
|
|
|
|
if (this.props[prop] !== undefined) {
|
|
|
|
console.warn('Prop `' + prop + ' = ' + this.props[prop] + '` should ' +
|
|
|
|
'not be set directly on Image.');
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 22:51:59 +00:00
|
|
|
var source = resolveAssetSource(this.props.source) || {};
|
2015-04-21 17:48:54 +00:00
|
|
|
|
|
|
|
var {width, height} = source;
|
|
|
|
var style = flattenStyle([{width, height}, styles.base, this.props.style]);
|
|
|
|
invariant(style, 'style must be initialized');
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
var isNetwork = source.uri && source.uri.match(/^https?:/);
|
|
|
|
invariant(
|
|
|
|
!(isNetwork && source.isStatic),
|
|
|
|
'static image uris cannot start with "http": "' + source.uri + '"'
|
|
|
|
);
|
|
|
|
var isStored = !source.isStatic && !isNetwork;
|
2015-03-17 10:08:46 +00:00
|
|
|
var RawImage = isNetwork ? RCTNetworkImage : RCTStaticImage;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
if (this.props.style && this.props.style.tintColor) {
|
2015-03-17 10:08:46 +00:00
|
|
|
warning(RawImage === RCTStaticImage, 'tintColor style only supported on static images.');
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-06-05 15:46:17 +00:00
|
|
|
var resizeMode = this.props.resizeMode || style.resizeMode || 'cover';
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
var nativeProps = merge(this.props, {
|
|
|
|
style,
|
2015-06-05 15:46:17 +00:00
|
|
|
resizeMode,
|
2015-02-20 04:10:52 +00:00
|
|
|
tintColor: style.tintColor,
|
|
|
|
});
|
|
|
|
if (isStored) {
|
|
|
|
nativeProps.imageTag = source.uri;
|
|
|
|
} else {
|
|
|
|
nativeProps.src = source.uri;
|
|
|
|
}
|
2015-04-17 01:17:19 +00:00
|
|
|
if (this.props.defaultSource) {
|
|
|
|
nativeProps.defaultImageSrc = this.props.defaultSource.uri;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
return <RawImage {...nativeProps} />;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
base: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-04-17 22:37:07 +00:00
|
|
|
var RCTNetworkImage = requireNativeComponent('RCTNetworkImageView', null);
|
2015-04-21 17:48:54 +00:00
|
|
|
var RCTStaticImage = requireNativeComponent('RCTStaticImage', null);
|
2015-04-17 01:17:19 +00:00
|
|
|
|
|
|
|
var nativeOnlyProps = {
|
2015-02-20 04:10:52 +00:00
|
|
|
src: true,
|
2015-04-17 01:17:19 +00:00
|
|
|
defaultImageSrc: true,
|
|
|
|
imageTag: true,
|
|
|
|
};
|
|
|
|
if (__DEV__) {
|
|
|
|
verifyPropTypes(Image, RCTStaticImage.viewConfig, nativeOnlyProps);
|
|
|
|
verifyPropTypes(Image, RCTNetworkImage.viewConfig, nativeOnlyProps);
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
module.exports = Image;
|