Header.js 4.29 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 Header extends React.Component {
    constructor(props) {
        super(props);
        this.getStrSize = this.getStrSize.bind(this);
    }

    shouldComponentUpdate(nextProps){
        if (Immutable.is(nextProps.resource, this.props.resource)) {
            return false;
        } else {
            return true;
        }
    }


    getStrSize(str) {
        let realLength = 0, len = str.length, charCode = -1;
        for (let i = 0; i < len; i++) {
            charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128) realLength += 1;
            else realLength += 2;
        }
        return realLength;
    }

    render() {

		let {resource} = this.props;
        let {author,article} = resource;

		let author_data = author.get('data');
        let article_data = article.get('data');
        let hasData = true;
		if (!author_data || !article_data) {
			hasData = false;
		}

        let author_desc = hasData?author_data.get('author_desc'):'';
        let author_avatar = hasData?author_data.get('avatar'):'';
        let author_name = hasData?author_data.get('name'):'';
        let author_url = hasData?author_data.get('url'):'';
        let num = this.getStrSize(author_desc);

        let article_title = hasData?article_data.get('article_title'):'';
        let pageViews = hasData?article_data.get('pageViews'):'99999';
        let publishTime = hasData?article_data.get('publishTime'):'12月12日 12:00';

        let author_id = hasData?article_data.get('author_id'):'';
        let id = hasData?article_data.get('id'):'';

        return(
            <View style={styles.contentContainer}>
                <TouchableOpacity activeOpacity={0.5} onPress={() => {
    				this.props.onPressAuthor && this.props.onPressAuthor(author_url, author_id, id);
    			}}>
                    <View style={styles.header}>
    					<Image source={{uri: author_avatar}} style={styles.thumb}/>
                        <View style={styles.headerTitle}>
                            <Text style={styles.name}>{author_name}</Text>
        					<Text style={styles.desc} numberOfLines={1}>{author_desc}</Text>
                        </View>
                    </View>
                </TouchableOpacity>
                <View style={{width: width,height: 0.5,backgroundColor: '#e5e5e5',}}/>
                <Text style={styles.article_title}>{article_title}</Text>
                <View style={styles.time}>
                    <Image source={require('../../image/time_icon.png')} style={styles.timeThumb}resizeMode={'contain'}/>
                    <Text style={styles.text}>{publishTime}</Text>
                    <Image source={require('../../image/shared_view_icon.png')} style={styles.timeThumb}resizeMode={'contain'}/>
                    <Text style={styles.text}>{pageViews}</Text>
                </View>
                <View style={{width: width,height: 10}}/>
            </View>
        );
    }
};


let {width, height} = Dimensions.get('window');
let headerHeight = Math.ceil(100 * width / 750);
let iconW = Math.ceil(60 * width / 750);

let styles = StyleSheet.create({
	contentContainer: {
        width:width,
		backgroundColor: 'white',
    },
	header: {
        flexDirection: 'row',
		height: headerHeight,
		width:width,
        alignItems: 'center',
	},
	thumb: {
		width: iconW,
		height: iconW,
		marginLeft: 20,
		borderRadius: iconW/2,
	},
    headerTitle: {
        width: width - iconW - 20 - 10 - 100,
		height: headerHeight,
		marginLeft: 10,
        justifyContent: 'center',
    },
	name: {
        fontSize: 16,
        fontWeight: 'bold',
	},
	desc: {
        marginTop: 5,
        fontSize: 12,
		color: '#e5e5e5',
	},
    article_title: {
        marginLeft: 20,
        marginRight: 10,
        fontSize: 23,
        fontWeight: 'bold',
        lineHeight: 35,
    },
    time: {
        flexDirection: 'row',
		height: 20,
		width:width,
		alignItems: 'center',
        marginTop: 5,
	},
    timeThumb: {
        marginLeft: 20,
		height: 16,
		width:16,
	},
    text: {
        marginLeft: 5,
        fontSize: 12,
        color: 'gray'
    },
});