SingleImage.js 3.21 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import Immutable, {Map} from 'immutable';

const {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image,
  ListView,
  Dimensions,
  TouchableOpacity,
} = ReactNative;


export default class SingleImage extends React.Component {
    constructor(props) {
        super(props);
		this.state = {
			width: Dimensions.get('window').width,
			height: 0,
		};
    }

	shouldComponentUpdate(nextProps,nextState){
		if (Immutable.is(nextProps.resource, this.props.resource) && nextState.height==this.state.height) {
			return false;
		} else {
			return true;
		}
	}

	componentDidMount() {
		let {resource} = this.props;
		let template_name = resource.get('data');
		let src = template_name.get('src');
        let imageHeight = template_name.get('height');
        let imageWidth = template_name.get('width');
        if (!imageHeight || !imageWidth) {
            Image.getSize(src, (width, height) => {
                if (this.imageView) {
                    this.setState({width, height});
                }
            });
        }
     }

    render() {
		let {resource} = this.props;
		let template_name = resource.get('data');
		let src = template_name.get('src');
        let linkUrl = template_name.get('url');
        let imageHeight = template_name.get('height');
        let imageWidth = template_name.get('width');

        if (imageHeight && imageWidth) {
            return (
                <View style={{width: screenWidth,height: (imageHeight/imageWidth)*screenWidth}}>
                    {linkUrl?<TouchableOpacity activeOpacity={0.5} onPress={() => {
                        this.props.onPressLink && this.props.onPressLink(linkUrl);
                    }}>
                        <Image
                            source={{uri: src}}
                            style={{width: screenWidth,height: (imageHeight/imageWidth)*screenWidth}}
                        />
                    </TouchableOpacity>:<Image
                        source={{uri: src}}
                        style={{width: screenWidth,height: (imageHeight/imageWidth)*screenWidth}}
                    />}
                    </View>
                );
        }else {
            return (
                <View style={{width: screenWidth,height: (this.state.height/this.state.width)*screenWidth}}>
                    {linkUrl?<TouchableOpacity activeOpacity={0.5} onPress={() => {
                        this.props.onPressLink && this.props.onPressLink(linkUrl);
                    }}>
                        <Image
                            ref={(ref)=>this.imageView=ref}
                            source={{uri: src}}
                            style={{width: screenWidth,height: (this.state.height/this.state.width)*screenWidth}}
                        />
                    </TouchableOpacity>:<Image
                        ref={(ref)=>this.imageView=ref}
                        source={{uri: src}}
                        style={{width: screenWidth,height: (this.state.height/this.state.width)*screenWidth}}
                    />}
                </View>
    		);
        }
    }
};

let screenWidth = Dimensions.get('window').width;
let imageView = null;
let styles = StyleSheet.create({

});