Blame view

js/community/components/posting/AssertsPicker.js 4.58 KB
1
'use strict'
2
3 4 5 6 7 8 9 10 11
/**
 * 照片选择页面
 */
import React, {Component} from 'react';
import {
    View,
    Image,
    Text,
    ScrollView,
12 13 14 15
    TouchableOpacity,
    Platform,
    Dimensions,
    StyleSheet,
16 17
    NativeModules,
    NativeAppEventEmitter,
18
} from 'react-native';
19
import PropTypes from 'prop-types';
20 21 22

let YH_CommunityAssetsPicker = NativeModules.YH_CommunityAssetsPicker;
23 24
export default class AssertsPicker extends Component {
25
    static propTypes = {
26 27 28
        assets: PropTypes.array,
        hidden: PropTypes.bool,
        onSelectedAsset: PropTypes.func,
29 30
    };
31 32 33 34 35 36 37 38 39 40 41
    constructor(props) {
        super(props);

        this.observer = null;
        this.renderContent = this.renderContent.bind(this);
        this.renderPlus = this.renderPlus.bind(this);
        this.deleteAsset = this.deleteAsset.bind(this);
    }

    componentDidMount() {
        this.observer = NativeAppEventEmitter.addListener('EventReminder', (event) => {
42
            this.props.onSelectedAsset && this.props.onSelectedAsset(event.assets);
43 44 45 46 47 48 49 50 51 52 53 54 55
        })
    }

    componentWillUnmount() {
        this.observer.remove();
    }

    renderPlus(assets) {
        if (assets.length >= 9) {
            return null;
        }

        let plusStyle = styles.itemContainer;
56
        let itemStyle = null;
57 58 59
        if (assets.length == 0) {
            plusStyle = {
                flex: 1,
60
                height: 175,
61 62 63
                alignItems: 'center',
                justifyContent: 'center',
            };
64 65 66
            itemStyle = {
                top: -10,
            };
67
        }
68
69
        return (
70
            <TouchableOpacity
71 72 73 74 75
                style={plusStyle}
                onPress={() => {
                    YH_CommunityAssetsPicker.presentImagePicker(assets);
                }}
            >
76
                <Image style={[styles.item, itemStyle]} source={require('../../images/posting/add.png')}/>
77 78 79 80
            </TouchableOpacity>
        );
    }
81 82 83 84 85 86
    renderContent(assets) {
        if (assets.length == 0) {
            return this.renderPlus(assets);
        }

        return (
87
            <ScrollView
88
                ref={ref => this.scrollView = ref}
89
                style={styles.scroll}
90
91
                horizontal={true}
92 93 94 95 96 97 98
                onContentSizeChange={(width, height) => {
                    let screenWidth = Dimensions.get('window').width;
                    let offset;
                    if (width > screenWidth) {
                        this.scrollView.scrollTo({x: width - screenWidth});
                    }
                }}
99
            >
100
            <View style={styles.scrollContainer}>
101 102 103 104 105 106 107 108 109 110 111
                {assets.map((asset, index) => {
                    return (
                        <View style={styles.itemContainer} key={index}>
                            <Image style={styles.item} source={asset}/>
                            <TouchableOpacity onPress={() => {this.deleteAsset(index);}}>
                                <Image style={styles.itemDelete} source={require('../../images/posting/delete.png')}/>
                            </TouchableOpacity>
                        </View>
                    );
                })}
                {this.renderPlus(assets)}
112 113
            </View>
114 115 116 117 118
            </ScrollView>
        );
    }

    deleteAsset(assetIndex) {
119 120
        let existedAssets = this.props.assets;
        existedAssets.splice(assetIndex, 1);
121
        this.props.onSelectedAsset && this.props.onSelectedAsset(existedAssets);
盖剑秋 authored
122
        YH_CommunityAssetsPicker.deleteAssetAtIndex(assetIndex);
123 124 125
    }

    render() {
126 127 128 129 130
        let {assets, hidden} = this.props;
        if (hidden) {
            return null;
        }
131
        return (
132 133 134
            <View style={styles.container}>
                {this.renderContent(assets)}
                <Text style={styles.pageText}>{assets.length + '/9'}</Text>
135 136 137 138 139 140
            </View>
        );
    }
}

const styles = StyleSheet.create({
141 142
    container: {
        flex: 1,
143
        height: 200,
144 145
    },
    scroll: {
146
        height: 175,
147 148
    },
    scrollContainer: {
149
        flexDirection: 'row',
150
        justifyContent: 'center',
151
        alignItems: 'center',
152
        height: 175,
153 154
        paddingLeft: 24,
        paddingRight: 12,
155 156
    },
    itemContainer: {
157
        flexDirection: 'row',
158 159 160 161
        width: 90,
        height: 130,
    },
    item: {
162 163 164 165
        top: 10,
        width: 80,
        height: 120,
        backgroundColor: '#b0b0b0',
166 167
    },
    itemDelete: {
168 169 170
        width: 20,
        height: 20,
        left: -13,
171 172
    },
    pageText: {
173 174 175
        color: '#B0B0B0',
        fontFamily: 'Helvetica Light',
        fontSize: 13,
176
        textAlign: 'center',
177
        bottom: 15,
178 179
    },
});