Brand.js 8.79 KB
'use strict'

import React, {Component} from 'react';
import {
    StyleSheet,
    Dimensions,
    Platform,
    View,
    Text,
    Image,
    ListView,
    TouchableOpacity,
} from 'react-native';

import {Map} from 'immutable';
import BrandSwiper from './BrandSwiper';
import BannerReourceList from './BannerReourceList';
import BrandFliter from './BrandFliter';
import YH_SearchBar from '../../../common/components/YH_SearchBar';
import ChannelFliter from './ChannelFliter';
import AllBrandListCell from './AllBrandListCell';
import NewHotBannerListCell from './NewHotBannerListCell';
import BrandSearch from './search/BrandSearch';
import LoadingIndicator from '../../../common/components/LoadingIndicator';
import IndexListView from './IndexListView';
import {ScrollCount} from '../../utils/Utils';

export default class Brand extends Component {
    constructor(props) {
        super(props);

        this.renderSectionHeader = this.renderSectionHeader.bind(this);
        this.renderRow = this.renderRow.bind(this);
        this.renderHeader = this.renderHeader.bind(this);
        this.scrollToSection = this.scrollToSection.bind(this);

        this.dataSource = new ListView.DataSource({
            rowHasChanged: (r1, r2) => !Immutable.is(r1, r2),
            sectionHeaderHasChanged: (s1, s2) => !Immutable.is(s1, s2),
        });
        this.listView = null;
    }

    componentDidMount() {

    }

    componentWillUnmount() {

    }

    componentWillReceiveProps(nextProps) {
        if (this.props.brandChannelFliter != nextProps.brandChannelFliter) {
            this.listView.scrollTo({x: 0, y: 0, animated: false});
        }
    }
    scrollToSection(index,sectionID){
        let item = sectionData[sectionID];
        if (item) {
           if (item.y == null) {
               needScrollSection = sectionID;
               item.count = ScrollCount(sectionID,sectionData);
               this.props.setInitialListSize && this.props.setInitialListSize(item.count);
               this.props.setBrandData && this.props.setBrandData(sectionData,this.props.brandChannelFliter);
           } else {
               this.listView.scrollTo({x: 0, y: item.y, animated: false});
           }
        }
    }

    onLayout(sectionID, e) {
        let {y} = e.nativeEvent.layout;
        let list = sectionData;
        if (list) {
            list[sectionID].y = y;

            this.props.setBrandData && this.props.setBrandData(list,this.props.brandChannelFliter);
            if (needScrollSection == sectionID) {
               this.listView.scrollTo({x: 0, y, animated: false});
            }
        }
    }

    renderHeader(){
        let data = this.props.reourceForBoy;
        let brandFliter = this.props.brandFliter;
        let brandChannelFliter = this.props.brandChannelFliter;

        if (brandChannelFliter == 0) {
            data = this.props.reourceForBoy;
        } else if (brandChannelFliter == 1) {
            data = this.props.reourceForGirl;
        } else if (brandChannelFliter == 2) {
            data = this.props.reourceForKid;
        } else if (brandChannelFliter == 3) {
            data = this.props.reourceForLifeStyle;
        }

		let banner = data.get('banner');
        let custom_brands = data.get('custom_brands');
        let brandsText = data.get('brandsText');

        return (
            <View style={styles.header}>
                <TouchableOpacity
                    activeOpacity={1}
                    onPress={() => {
                        this.props.onPressSearch && this.props.onPressSearch();
                    }}
                >
                    <YH_SearchBar
                        ref={(c) => {
                            this.searchBar = c;
                        }}
                        editable={false}
                    />
                </TouchableOpacity>
                {banner?<BrandSwiper resource={banner} onPressSlideItem={this.props.onPressSlideItem}/>:null}
                {custom_brands?<BannerReourceList resource={custom_brands} onPressSlideItem={this.props.onPressSlideItem}/>:null}
                {brandsText?<BrandFliter dataSource={brandsText} selectID={brandFliter} onPressFilter={this.props.onPressFilter}/>:null}
            </View>
        );
    }

    renderSectionHeader(sectionData, sectionID) {
        let title = sectionID;
        if (sectionID == '0-9') {
            title = '0';
        }
        return (
            <View style={styles.sessionTitle} onLayout={this.onLayout.bind(this, sectionID)}>
                <Text style={styles.sessionText}>{title}</Text>
            </View>
        );
    }

    renderRow(rowData, sectionID, rowID, highlightRow) {
        if (this.props.brandFliter == 0) {
            return (
                <AllBrandListCell rowData={rowData} onPressSlideItem={this.props.onPressSlideItem}/>
            );
        }else {
            return (
                <NewHotBannerListCell rowData={rowData} onPressSlideItem={this.props.onPressSlideItem}/>
            );
        }
    }

    render() {

        let {
            showSearch,
            isFetching,
            brandFliter,
            brandChannelFliter,
            brandListForBoy,
            brandListForGirl,
            brandListForKid,
            brandListForLifeStyle,
            reourceForBoy,
            reourceForGirl,
            reourceForKid,
            reourceForLifeStyle,
            initialListSize,
            search,
        } = this.props;

        let data;

        if (brandChannelFliter == 0) {
            data = brandListForBoy?brandListForBoy.toJS():null;
        } else if (brandChannelFliter == 1) {
            data = brandListForGirl?brandListForGirl.toJS():null;
        } else if (brandChannelFliter == 2) {
            data = brandListForKid?brandListForKid.toJS():null;
        } else if (brandChannelFliter == 3) {
            data = brandListForLifeStyle?brandListForLifeStyle.toJS():null;
        }

        let list;
        let listDataSource;
        let contentContainerStyle;
        let renderSectionHeader = null;
        let showIndexForListView = false;

        if (brandFliter == 0) {
            list = data?data.all_list:null;
            listDataSource =  list?this.dataSource.cloneWithRowsAndSections(list):null;
            contentContainerStyle = styles.contentContainerOne;
            renderSectionHeader = this.renderSectionHeader;
            showIndexForListView = true;
            sectionData = list;
        } else if (brandFliter == 1) {
            list = data?data.new_list:null;
            listDataSource =  list?this.dataSource.cloneWithRows(list):null;
            contentContainerStyle = styles.contentContainerTwo;
        } else if (brandFliter == 2) {
            list = data?data.hot_list:null;
            listDataSource =  list?this.dataSource.cloneWithRows(list):null;
            contentContainerStyle = styles.contentContainerTwo;
        }

        if (!list) {
            return null;
        }

        return (
            <View style={styles.container}>
                <ChannelFliter selectID={brandChannelFliter} onChannelPressFliter={this.props.onChannelPressFliter}/>

                <ListView
                    ref={(ref)=>this.listView=ref}
                    initialListSize={initialListSize}
                    contentContainerStyle={contentContainerStyle}
                    enableEmptySections={true}
                    dataSource={listDataSource}
                    renderRow={this.renderRow}
                    showsVerticalScrollIndicator={false}
                    renderSectionHeader={renderSectionHeader}
                    renderHeader={this.renderHeader}
                />

                {showIndexForListView?<IndexListView dataSource={Object.keys(list)} onLetterPress={this.scrollToSection}/>: null}

                <LoadingIndicator
                    isVisible={isFetching}
                />

                {showSearch ? <BrandSearch
                    data={search}
                    onTextChange={this.props.onTextChange}
                    onClickCancel={this.props.onClickCancel}
                    onPressClearHistory={this.props.onPressClearHistory}
                    onPressBrandSearchItem={this.props.onPressBrandSearchItem} /> : null}
            </View>
        );
    }
}

let {width, height} = Dimensions.get('window');
let needScrollSection = null;
let sectionData = null;

let styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: 'white',
    },
    contentContainerOne:{

    },
    contentContainerTwo: {
		flexDirection: 'row',
        flexWrap: 'wrap',
		alignItems:'flex-start',
	},
    header: {
        width,
        backgroundColor: '#CCC'
    },
	sessionText: {
		textAlign: 'left',
		fontSize: 14,
		marginLeft:8,
		color: '#999999',
    },
	sessionTitle: {
		justifyContent: 'center',
		height: 20,
		width,
		backgroundColor: '#e5e5e5',
    },
    search: {
    },
});