Header.js
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'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'
},
});