All files / UI_Component ProgressBar.js

100% Statements 50/50
94.44% Branches 34/36
100% Functions 13/13
100% Lines 50/50
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                                        3x 3x 3x               110x 110x         110x 110x 110x       110x 110x 110x   110x 1x 1x         159x       162x       25x 2x 2x         110x 110x 110x                     3x 3x 2x         1x 1x 1x 1x 1x                         113x 3x 3x           135x     135x       135x                 135x 135x                     135x                 135x                   135x                 135x             135x         135x   135x               135x 270x           270x                 3x                                 3x                                
/**
 * @author Haipeng Zhang(hp.zhang@samsung.com)
 * @fileoverview This module manages progress bar.
 * @date    2017/06/16 (last modified date)
 *
 * Copyright 2017 by Samsung Electronics, Inc.,
 *
 * This software is the confidential and proprietary information
 * of Samsung Electronics, Inc. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Samsung.
 */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { is, Map, fromJS } from 'immutable';
import { AnimationEffect } from './common/CommonDefine';
 
 
/* ProgressBar Define */
export const ProgressBarLength = 1360;
export const ProgressDuration = 16.567 / 1127.4;
const ResourceMap = {
    NORMAL: 'images/progressbar/c_progressbar_white_buffering.png',
    HIGHCONTRAST: 'images/progressbar/c_progressbar_highcontrast_buffering.png',
};
 
export default class ProgressBar extends React.Component {
 
    constructor(props) {
        super(props);
        this.state = {
            movePx: 0,
            time: 0,
            bufferingOpacity: 0,
        };
        this.animationDone = this.animationDone.bind(this);
        this.startTimer = null;
        this.barHeight = props.highContarstValue ? 6 : 3;
    }
 
    componentDidMount() {
        const elem = this.buffering;
        elem.removeEventListener('transitionend', this.animationDone, false);
        elem.addEventListener('transitionend', this.animationDone, false);
 
        if (this.state.movePx === 0 && this.props.progressRate === 100 && this.props.bufferingShowValue) {
            this.clearTimer();
            this.setTimer();
        }
    }
 
    componentWillReceiveProps(nextProps) {
        this.barHeight = nextProps.highContarstValue ? 6 : 3;
    }
 
    shouldComponentUpdate(nextProps, nextState) {
        return (JSON.stringify(nextProps) !== JSON.stringify(this.props)) || (JSON.stringify(nextState) !== JSON.stringify(this.state));
    }
 
    componentDidUpdate() {
        if (this.state.movePx === 0 && this.props.progressRate === 100 && this.props.bufferingShowValue) {
            this.clearTimer();
            this.setTimer();
        }
    }
 
    componentWillUnmount() {
        const elem = this.buffering;
        elem.removeEventListener('transitionend', this.animationDone, false);
        this.clearTimer();
    }
 
    /**
    * set timer of progress bar
    * @name setTimer
    * @method
    * @memberof
    * @param
    */
    setTimer() {
        const { duration, bgWidth } = this.props;
        this.startTimer = setTimeout(() => {
            this.setState({ movePx: (ProgressBarLength - bgWidth), time: (ProgressDuration * (ProgressBarLength - bgWidth)), bufferingOpacity: 1 });
        }, duration);// The timeout applies as 1 sec.
    }
 
    animationDone(e) {
        e.preventDefault();
        const target = e.target;
        Eif (target === e.currentTarget) {
            Eif (e.type === 'transitionend') {
                this.setState({ movePx: 0, time: 0 });
            }
        }
    }
 
    /**
    * clear timer of progress bar
    * @name clearTimer
    * @method
    * @memberof
    * @param
    */
    clearTimer() {
        if (this.startTimer) {
            clearTimeout(this.startTimer);
            this.startTimer = null;
        }
    }
 
    render() {
        const { left, top, bgWidth, bgColor, bgOpacity, barColor, barOpacity, highContarstValue,
            highContarstBgColor, highContarstBarColor, transAnimation, progressRate } = this.props;
 
        // Normal style,highContarstValue=false
        const bgOpacityStyle = {
            opacity: !this.state.bufferingOpacity,
        };
 
        const bgStyle = {
            position: 'absolute',
            width: bgWidth,
            height: this.barHeight,
            backgroundColor: bgColor,
            opacity: bgOpacity,
            zIndex: 10,
        };
 
        const barWidth = (progressRate >= 0 && progressRate <= 100) ? Math.floor((progressRate / 100) * bgWidth) : 0;
        const barStyle = {
            position: 'absolute',
            width: barWidth,
            height: this.barHeight,
            backgroundColor: barColor,
            opacity: barOpacity,
            // transition: transAnimation,
            zIndex: 20,
        };
 
        // HighContrast style, highContarstValue=true
        const highContrastBgStyle = {
            position: 'absolute',
            width: bgWidth,
            height: this.barHeight,
            backgroundColor: highContarstBgColor,
            opacity: bgOpacity,
            zIndex: 10,
        };
 
        const highContrastBarStyle = {
            position: 'absolute',
            width: barWidth,
            height: this.barHeight,
            backgroundColor: highContarstBarColor,
            opacity: barOpacity,
            // transition: transAnimation,
            zIndex: 20,
        };
 
        const imgOpacityStyle = {
            position: 'relative',
            overflow: 'hidden',
            width: bgWidth,
            height: this.barHeight,
            zIndex: 50,
            opacity: this.state.bufferingOpacity,
        };
 
        const imgPosStyle = {
            position: 'absolute',
            width: ProgressBarLength,
            height: this.barHeight,
            left: bgWidth - ProgressBarLength,
        };
 
        const imgAniStyle = {
            transform: `translateX(${this.state.movePx}px)  translateZ(100px)`,
            transition: `all ${this.state.time}s ${AnimationEffect.Basic}`,
        };
 
        const imgSrc = highContarstValue ? ResourceMap.HIGHCONTRAST : ResourceMap.NORMAL;
 
        const posStyle = {
            position: 'absolute',
            left,
            top,
            display: (progressRate >= 0 && progressRate <= 100) ? 'inline' : 'none',
            overflow: 'hidden',
        };
 
        return (
          <div ref={(node) => { this.node = node; }} style={posStyle}>
            <div style={bgOpacityStyle}>
              <div style={(highContarstValue) ? highContrastBgStyle : bgStyle} />
              <div style={(highContarstValue) ? highContrastBarStyle : barStyle} />
            </div>
            <div style={imgOpacityStyle}>
              <div style={imgAniStyle} ref={(buffering) => { this.buffering = buffering; }}>
                <img src={imgSrc} style={imgPosStyle} />
              </div>
            </div>
          </div>
        );
    }
}
 
ProgressBar.propTypes = {
    left: PropTypes.number,                              // Background left of progress control
    top: PropTypes.number,                               // Background top of progress control
    bgWidth: PropTypes.number.isRequired,                // Background width of progress control
    bgColor: PropTypes.string,                           // Background color of progress control
    bgOpacity: PropTypes.string,                         // Background opacity of progress control
    barColor: PropTypes.string,                          // Color of progress bar
    barOpacity: PropTypes.string,                        // Opacity of progress bar
    highContarstValue: PropTypes.bool,                    // HighContarst flag,it should be true or false
    highContarstBgColor: PropTypes.string,                // HighContarst background color
    highContarstBarColor: PropTypes.string,               // HighContarst background color of progress bar
    bufferingShowValue: PropTypes.bool,                   // Buffering png show flag, it should be true or false
    duration: PropTypes.number,                           // Start value of progress bar
    // transAnimation: PropTypes.string,                     // Transition value
    progressRate: PropTypes.number,
};
 
ProgressBar.defaultProps = {
    left: 0,       // Background left of progress control
    top: 0,        // Background top of progress control
    bgColor: '#60696e',  // Background color of progress control
    bgOpacity: '0.6',   // Background opacity of progress control
    barColor: '#0d0d0d', // Color of progress bar
    barOpacity: '1',     // Opacity of progress bar
    highContarstValue: false,                // HighContarst flag,it should be true or false
    highContarstBgColor: '#ffffff',          // HighContarst background color
    highContarstBarColor: '#ffff00',         // HighContarst background color  of progress bar
    bufferingShowValue: false,               // Buffering png show flag, it should be true or false
    duration: 1000,                         // Start value of progress bar
    // transAnimation: `0.07s ${AnimationEffect.Basic}`,  // Transition value
    progressRate: -1,                                   // progress,[0~100], -1: hide
};