|
| 1 | +/** |
| 2 | + * @providesModule ImageProgress |
| 3 | + */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +var _ = require('lodash'); |
| 7 | +var React = require('react-native'); |
| 8 | +var { |
| 9 | + Image, |
| 10 | + View, |
| 11 | + StyleSheet, |
| 12 | +} = React |
| 13 | +var flattenStyle = require('react-native/Libraries/StyleSheet/flattenStyle'); |
| 14 | + |
| 15 | +var BAR_WIDTH = 150; |
| 16 | +var BAR_HEIGHT = 6; |
| 17 | +var MIN_PADDING = 10; |
| 18 | + |
| 19 | +var ImageProgress = React.createClass({ |
| 20 | + getInitialState: function() { |
| 21 | + return { |
| 22 | + loading: false, |
| 23 | + error: false, |
| 24 | + progress: 0, |
| 25 | + }; |
| 26 | + }, |
| 27 | + |
| 28 | + setNativeProps: function(nativeProps) { |
| 29 | + this._root.setNativeProps(nativeProps); |
| 30 | + }, |
| 31 | + |
| 32 | + handleLayoutChange: function(event) { |
| 33 | + this.setState({ layout: event.nativeEvent.layout }); |
| 34 | + }, |
| 35 | + |
| 36 | + handleLoadStart: function() { |
| 37 | + this.setState({ |
| 38 | + error: false, |
| 39 | + loading: true, |
| 40 | + progress: 0, |
| 41 | + }); |
| 42 | + }, |
| 43 | + |
| 44 | + handleLoadProgress: function(event) { |
| 45 | + this.setState({ |
| 46 | + progress: event.nativeEvent.written / event.nativeEvent.total, |
| 47 | + }); |
| 48 | + }, |
| 49 | + |
| 50 | + handleLoaded: function() { |
| 51 | + this.setState({ |
| 52 | + loading: false, |
| 53 | + progress: 1, |
| 54 | + }); |
| 55 | + }, |
| 56 | + |
| 57 | + handleLoadAbort: function() { |
| 58 | + this.setState({ |
| 59 | + loading: false, |
| 60 | + }); |
| 61 | + }, |
| 62 | + |
| 63 | + handleLoadError: function() { |
| 64 | + this.setState({ |
| 65 | + error: true, |
| 66 | + loading: false, |
| 67 | + }); |
| 68 | + }, |
| 69 | + |
| 70 | + render: function() { |
| 71 | + var indicator = false; |
| 72 | + var props = _.omit(this.props, 'onLoadStart', 'onLoadProgress', 'onLoaded', 'onLoadAbort', 'onLoadError', 'handleLayoutChange'); |
| 73 | + |
| 74 | + if(this.state.loading) { |
| 75 | + props.style = flattenStyle([styles.container, props.style]); |
| 76 | + switch(props.indicator) { |
| 77 | + case 'circle': throw new Error('Not yet implemented'); break; |
| 78 | + |
| 79 | + default: { |
| 80 | + var barWidth = BAR_WIDTH; |
| 81 | + if(this.state.layout) { |
| 82 | + barWidth = Math.min(BAR_WIDTH, this.state.layout.width - MIN_PADDING * 2); |
| 83 | + } |
| 84 | + |
| 85 | + var barWidthStyle = { width: barWidth }; |
| 86 | + var barProgressStyle = { width: (barWidth - 2) * this.state.progress }; |
| 87 | + |
| 88 | + indicator = ( |
| 89 | + <View style={[styles.barContainer, barWidthStyle]}> |
| 90 | + <View style={[styles.bar, barProgressStyle]}></View> |
| 91 | + </View>) |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return ( |
| 97 | + <Image |
| 98 | + {...props} |
| 99 | + ref={component => this._root = component} |
| 100 | + onLoadStart={this.handleLoadStart} |
| 101 | + onLoadProgress={this.handleLoadProgress} |
| 102 | + onLoaded={this.handleLoaded} |
| 103 | + onLoadAbort={this.handleLoadAbort} |
| 104 | + onLoadError={this.handleLoadError} |
| 105 | + onLayout={this.handleLayoutChange} |
| 106 | + > |
| 107 | + {indicator} |
| 108 | + </Image> |
| 109 | + ); |
| 110 | + } |
| 111 | +}); |
| 112 | + |
| 113 | +var styles = StyleSheet.create({ |
| 114 | + container: { |
| 115 | + alignItems: 'center', |
| 116 | + justifyContent: 'center', |
| 117 | + }, |
| 118 | + barContainer: { |
| 119 | + height: BAR_HEIGHT, |
| 120 | + borderRadius: BAR_HEIGHT/2, |
| 121 | + backgroundColor: 'rgba(255, 255, 255, 0.5)', |
| 122 | + }, |
| 123 | + bar: { |
| 124 | + margin: 1, |
| 125 | + backgroundColor: 'black', |
| 126 | + borderRadius: BAR_HEIGHT/2 - 1, |
| 127 | + height: BAR_HEIGHT - 2, |
| 128 | + } |
| 129 | +}); |
| 130 | + |
| 131 | +module.exports = ImageProgress; |
0 commit comments