Header.js
7.51 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'use strict';
import React from 'react';
import ReactNative from 'react-native';
import Immutable, {Map} from 'immutable';
import DeviceInfo from 'react-native-device-info';
import {getSlicedUrl} from '../../../classify/utils/Utils';
import {removeHTMLTag} from '../../utils/Helper';
import YH_Image from '../../../common/components/YH_Image';
const {
View,
Image,
Text,
TouchableOpacity,
Dimensions,
StyleSheet,
WebView,
Platform,
} = ReactNative;
const BODY_TAG_PATTERN = /\<\/ *body\>/;
var script = `
;(function() {
var wrapper = document.createElement("div");
wrapper.id = "height-wrapper";
while (document.body.firstChild) {
wrapper.appendChild(document.body.firstChild);
}
document.body.appendChild(wrapper);
var i = 0;
function updateHeight() {
document.title = wrapper.clientHeight;
window.location.hash = ++i;
}
updateHeight();
window.addEventListener("load", function() {
updateHeight();
setTimeout(updateHeight, 1000);
});
window.addEventListener("resize", updateHeight);
}());
`;
const style = `
<style>
body, html, #height-wrapper {
margin: 0;
padding: 0;
}
#height-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
}
</style>
<script>
${script}
</script>
`;
const codeInject = (html) => html.replace(BODY_TAG_PATTERN, style + "</body>");
export default class Header extends React.Component {
constructor(props) {
super (props);
this.handleNavigationChange = this.handleNavigationChange.bind(this);
this.shouldStartLoadWithRequest = this.shouldStartLoadWithRequest.bind(this);
this.state = {
realContentHeight : 40,
};
}
shouldComponentUpdate(nextProps,nextState){
if (Immutable.is(nextProps.resource, this.props.resource) && nextProps.launchProfile==this.props.launchProfile && nextState.realContentHeight == this.state.realContentHeight) {
return false;
} else {
return true;
}
}
handleNavigationChange(navState) {
let heightT = parseInt(navState.title, 10) || 0; // turn NaN to 0
heightT>0?this.setState({
realContentHeight: heightT,
}):null;
}
shouldStartLoadWithRequest(event) {
if (event.navigationType == 'click') {
this.props.onPressLink && this.props.onPressLink(event.url);
return false;
}
return true;
}
render() {
let {resource,launchProfile} = this.props;
if (!resource) {
return null;
}
let data = resource.toJS();
let htmlText = data.shop_intro;
let text = htmlText?removeHTMLTag(htmlText):'';
htmlText = '<html><style type="text/css">img {max-width: 100%;}</style><body>' + htmlText + '</body></html>';
let hasLink = false;
if (htmlText.indexOf('href=') >= 0) {
hasLink = true;
}
let brandName = data.is_show_shop_name?data.shop_name:'';
let num = data.favoriteCount;
let numberOfFav = '粉丝: '+ num;
let backgroundImage = data.shopBanner?data.shopBanner.module_data.data[0].pic:'null';
let brandIcon = getSlicedUrl(data.shop_logo, 60, 60, 2);
let isFav = data.is_addFav;
let favImage = isFav?require('../../image/heart_h.png'):require('../../image/heart_n.png');
//下拉图标
let sourceIcon = launchProfile?require('../../image/arrow_ic_up.png'):require('../../image/arrow_ic.png');
//计算控件高度,图片+标题+简介+下拉图标+20空白
let cellHeight = launchProfile ? (imageHeigth + titleHeigth + this.state.realContentHeight + 20 + 20) : (imageHeigth + titleHeigth + 60 + 20);
//获取字体
let fontFamilyStyle = {};
if (Platform.OS === 'ios') {
let systemVersion = DeviceInfo.getSystemVersion();
systemVersion = parseFloat(systemVersion);
if (systemVersion >= 9.0) {
fontFamilyStyle = {fontFamily: 'PingFang SC'};
}
}
return (
<View style={{width: width, height: cellHeight,backgroundColor: 'white'}}>
<YH_Image
url={backgroundImage}
style={{width, height: imageHeigth }}
/>
<View style={styles.titleView}>
<Text style={styles.name} numberOfLines={1}>{brandName}</Text>
<TouchableOpacity activeOpacity={0.5} onPress={() => {
this.props.onPressCollection && this.props.onPressCollection(isFav);
}}>
<Image source={favImage} style={styles.favImage}/>
</TouchableOpacity>
</View>
<Text style={styles.number}>{numberOfFav}</Text>
<YH_Image
url={brandIcon}
style={styles.brandIcon}
/>
<TouchableOpacity activeOpacity={0.5} onPress={() => {
this.props.onPressLaunchProfile && this.props.onPressLaunchProfile(!launchProfile);
}}>
{launchProfile ?<View style={{width: width,height: this.state.realContentHeight,backgroundColor: 'white'}}>
<WebView style={{width: width-40,height: this.state.realContentHeight,backgroundColor: 'white',marginRight: 20,marginLeft: 20}}
source= {{html: codeInject(htmlText)}}
scrollEnabled={false}
javaScriptEnabled={true}
decelerationRate="normal"
domStorageEnabled={true}
onNavigationStateChange={this.handleNavigationChange}
onShouldStartLoadWithRequest={hasLink?this.shouldStartLoadWithRequest:null}
>
</WebView>
</View>:<Text style={[styles.text, fontFamilyStyle]} numberOfLines={2}>{text}</Text>}
<View style={styles.fliter}>
<Image source={sourceIcon} style={styles.pullDown}/>
</View>
</TouchableOpacity>
</View>
);
}
}
let {width, height} = Dimensions.get('window');
let titleHeigth = 60;
let imageHeigth = Math.ceil((234/750)*width);
let styles = StyleSheet.create({
container: {
backgroundColor:'white',
},
brandIcon: {
width: 60,
height: 60,
position: 'absolute',
marginLeft: 15,
marginTop: -(titleHeigth+20),
borderWidth: 1,
borderColor: '#e0e0e0',
},
titleView: {
flexDirection: 'row',
backgroundColor:'white',
width: width,
height: titleHeigth,
justifyContent:'space-between',
},
name: {
marginLeft: 15 + 60 + 15,
backgroundColor: 'white',
width: width-85-120,
marginTop: 22,
color: '#444444',
fontWeight: 'bold',
fontSize: 16,
},
number: {
marginTop: - (60 - 22),
color: '#444444',
fontSize: 13,
backgroundColor: 'white',
position: 'absolute',
marginLeft: width - 15 - 60 - 15 - 17,
},
favImage: {
width: 17,
height: 16,
marginTop: 22,
backgroundColor: 'white',
marginRight: 15,
},
text: {
marginLeft: 20,
width: width-40,
height: 40,
backgroundColor: 'white',
lineHeight: 18,
fontSize: 14,
color: '#444444',
},
fliter: {
width: width,
height: 30,
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
},
pullDown: {
width: 15,
height: 8,
},
});