SingleImage.js 7.97 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,
  Platform,
} = 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');
        let tagList = template_name?template_name.get('tagList'):null;
        tagList = tagList?tagList.toJS():[];

        let tagListGlobal = template_name?template_name.get('tagListGlobal'):null;
        tagListGlobal = tagListGlobal?tagListGlobal.toJS():[];

        tagList = tagList.concat(tagListGlobal);

        let tagListLimit = template_name?template_name.get('tagListLimit'):null;
        tagListLimit = tagListLimit?tagListLimit.toJS():[];

        tagList = tagList.concat(tagListLimit);

        if (tagList.constructor != Array) {
            tagList = [];
        }

        if (imageHeight && imageWidth) {

            return (
                <View style={{width: screenWidth,height: (imageHeight/imageWidth)*screenWidth,marginLeft: 10}}>
                    {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}}
                    />}
                    {tagList?tagList.map((value, i) => {
                        let is_limitbuy = value.is_limitbuy == 'Y'?true:false;

                        return (
                            <View
                                key={'values' + i}
                                style={{
                                    maxWidth: screenWidth - 20,
                                    height: tagHeigth,
                                    position: 'absolute',
                                    left:  10,
                                    bottom: i*(tagHeigth+10)+10,
                                    backgroundColor: 'transparent',
                                    flexDirection: 'row',
                                    justifyContent: 'flex-end',
                                    alignItems: 'center',
                                    borderRadius: 10,
                                }}
                            >
                                <Image source={require('../../image/KA_dian.png')} style={styles.icon} resizeMode={'contain'}></Image>
                                <TouchableOpacity  activeOpacity={0.5} onPress={() => {
                                    let pos_id = 102;
                                    this.props.onPressProduct && this.props.onPressProduct(value.product_skn,i,pos_id,value.is_global);
                                }}>
                                    <View style={styles.tagView}>
                                        <Text style={{
                                            maxWidth: screenWidth - 20 - 2*tagHeigth - 20,
                                            color: 'white',
                                            paddingLeft: 10,
                                            paddingRight: 10,
                                            backgroundColor: 'transparent',
                                        }} numberOfLines={1}>{value.tagName}</Text>
                                        {
                                            is_limitbuy ? null : <TouchableOpacity style={styles.button} activeOpacity={0.5} onPress={() => {
                                                let pos_id = 105;
                                                let product_id = value.product_id?value.product_id:'';
                                                this.props.onPressShopCar && this.props.onPressShopCar(value.product_skn,product_id,pos_id,value.is_global=='Y'?true:false);
                                            }}>
                                                <Image source={require('../../image/dian_gwc_bt.png')} style={styles.image} resizeMode={'contain'}></Image>
                                            </TouchableOpacity>
                                        }
                                    </View>
                                </TouchableOpacity>
                            </View>
                        );
                    }):null}
                </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 - 20;
let tagHeigth = Math.ceil(45 * Dimensions.get('window').width / 750);
let tagWidth = Math.ceil(65 * Dimensions.get('window').width / 750);

let imageView = null;
let styles = StyleSheet.create({
    tagView: {
        height : tagHeigth,
        backgroundColor: 'rgba(0, 0, 0, 0.6)',
        borderTopLeftRadius: 15,
        borderBottomLeftRadius: 15,
        borderTopRightRadius: 15,
        borderBottomRightRadius: 15,
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
    },
    button: {
        height : tagHeigth,
        width : tagWidth + 5,
        backgroundColor: 'rgba(0, 0, 0, 0.6)',
        borderTopRightRadius: 15,
        borderBottomRightRadius: 15,
        flexDirection: 'row',
    },
    image: {
        width : tagWidth - 7,
        height : tagHeigth,
        marginLeft: 8,
        backgroundColor: 'transparent',
    },
    icon: {
        width : tagHeigth - 5,
        height : tagHeigth - 5,
        marginRight: 10,
        backgroundColor: 'transparent',
    },
    rightDetail: {
        maxWidth: Math.ceil(screenWidth * (2/3)) - 2*tagHeigth,
        height : tagHeigth,
        color: 'white',
        paddingLeft: 10,
        paddingRight: 10,
        paddingTop: Platform.OS === 'ios' ? 5 : 2,
        paddingBottom: Platform.OS === 'ios' ? 5 : 2,
    },
});