All files / UI_Component ScrollBar.js

100% Statements 54/54
100% Branches 24/24
100% Functions 11/11
100% Lines 54/54
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                                          9x 9x       9x 9x 6x 6x 3x 2x 2x       9x 9x 9x       9x 9x       27x 27x 25x 25x 2x 1x 1x   27x       31x             9x                     36x   36x 4x                       27x 27x 27x                     45x 36x 36x           27x     27x                 27x                     27x                 27x                     27x 21x 21x 2x   21x 21x 6x 5x 5x 2x   5x 5x     27x           27x                 2x                                     2x                                      
/**
 * @author Song Zhang (song8.zhang@samsung.com) and Haipeng Zhang(hp.zhang@samsung.com)
 * @fileoverview This module manages scroll control.
 * @date    2017/05/18 (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';
 
export default class ScrollBar extends React.Component {
 
    constructor(props) {
        super(props);
        this.state = {
            allOpacity: 1,    //allOpacity controls the ScrollBar shown or not
        };
 
        this.hideTimer = null;
        if (props.barType === 'v') {
            this.barHeight = Math.floor(props.barPercent * props.bgHeight);
            this.barWidth = props.bgWidth;
        } else if (props.barType === 'h') {
            this.barWidth = Math.floor(props.barPercent * props.bgWidth);
            this.barHeight = props.bgHeight;
        }
 
        // bind function
        this.setTimer = this.setTimer.bind(this);
        this.clearTimer = this.clearTimer.bind(this);
        this.showBar = this.showBar.bind(this);
    }
 
    componentDidMount() {
        this.clearTimer();
        this.setTimer();
    }
 
    componentWillReceiveProps(nextProps) {
        const { bgWidth, bgHeight, scrollPercent } = this.props;
        if (nextProps.barType === 'v') {
            this.barHeight = Math.floor(nextProps.barPercent * nextProps.bgHeight);
            this.barWidth = bgWidth;
        } else if (nextProps.barType === 'h') {
            this.barWidth = Math.floor(nextProps.barPercent * nextProps.bgWidth);
            this.barHeight = bgHeight;
        }
        this.showBar();
    }
 
    shouldComponentUpdate(nextProps, nextState) {
        return (JSON.stringify(nextProps) !== JSON.stringify(this.props)) || (JSON.stringify(nextState) !== JSON.stringify(this.state));
    }
 
    componentDidUpdate(prevProps, prevState) {
    }
 
    componentWillUnmount() {
        this.clearTimer();
    }
 
    /**
    * set timer of scroll bar
    * @name setTimer
    * @method
    * @memberof
    * @param
    */
    setTimer() {
        const { duration } = this.props;
 
        this.hideTimer = setTimeout(() => {
            this.setState({ allOpacity: 0 });
        }, duration);// The timeout applies as 1 sec.
    }
 
    /**
    * show scroll bar
    * @name showBar
    * @method
    * @memberof
    * @param
    */
    showBar() {
        this.setState({ allOpacity: 1 });
        this.clearTimer();
        this.setTimer();
    }
 
    /**
    * clear timer of scroll bar
    * @name clearTimer
    * @method
    * @memberof
    * @param
    */
    clearTimer() {
        if (this.hideTimer) {
            clearTimeout(this.hideTimer);
            this.hideTimer = null;
        }
    }
 
    render() {
        const { left, top, bgWidth, bgHeight, bgColor, bgOpacity, barColor, barOpacity,
            scrollPercent, barType, highContarstValue, highContarstBgColor, highContarstBarColor, transAnimation } = this.props;
 
        // Normal style,highContarstValue=false
        const bgStyle = {
            position: 'absolute',
            width: bgWidth,
            height: bgHeight,
            backgroundColor: bgColor,
            opacity: bgOpacity,
            zIndex: 10,
        };
 
        const barStyle = {
            position: 'absolute',
            width: this.barWidth,
            height: this.barHeight,
            backgroundColor: barColor,
            opacity: barOpacity,
            transition: transAnimation,
            zIndex: 20,
        };
 
        // HighContrast style, highContarstValue=true
        const highContrastBgStyle = {
            position: 'absolute',
            width: bgWidth,
            height: bgHeight,
            backgroundColor: highContarstBgColor,
            opacity: bgOpacity,
            zIndex: 10,
        };
 
        const highContrastBarStyle = {
            position: 'absolute',
            width: this.barWidth,
            height: this.barHeight,
            backgroundColor: highContarstBarColor,
            opacity: barOpacity,
            transition: transAnimation,
            zIndex: 20,
        };
 
        // calculate the transform value
        if (barType === 'v') {
            let transPy = (bgHeight - this.barHeight) * scrollPercent;
            if ((transPy + this.barHeight) > bgHeight) {
                transPy = bgHeight - this.barHeight;
            }
            barStyle.transform = `translateY(${transPy}px)`;
            highContrastBarStyle.transform = `translateY(${transPy}px)`;
        } else if (barType === 'h') {
            let transPx = (bgWidth - this.barWidth) * scrollPercent;
            if ((transPx + this.barWidth) > bgWidth) {
                transPx = bgWidth - this.barWidth;
            }
            barStyle.transform = `translateX(${transPx}px)`;
            highContrastBarStyle.transform = `translateX(${transPx}px)`;
        }
 
        const style = {
            opacity: this.state.allOpacity,
            position: 'absolute',
            left,
            top,
        };
        return (
          <div style={style}>
            <div style={(highContarstValue) ? highContrastBgStyle : bgStyle} />
            <div style={(highContarstValue) ? highContrastBarStyle : barStyle} />
          </div>
        );
    }
}
 
ScrollBar.propTypes = {
    barType: PropTypes.string.isRequired,
    left: PropTypes.number,
    top: PropTypes.number,
    bgWidth: PropTypes.number.isRequired,
    bgHeight: PropTypes.number.isRequired,
    bgColor: PropTypes.string,
    bgOpacity: PropTypes.string,
    barColor: PropTypes.string,
    barOpacity: PropTypes.string,
    barPercent: PropTypes.number.isRequired,
    scrollPercent: PropTypes.number.isRequired,
    highContarstValue: PropTypes.bool,
    highContarstBgColor: PropTypes.string,
    highContarstBarColor: PropTypes.string,
    duration: PropTypes.number,
    transAnimation: PropTypes.string,
};
 
ScrollBar.defaultProps = {
    barType: 'v',                                       // The direction of scroll control, "h" means horizontal scroll control, "v" means vertical scroll control
    left: 0,                                            // layout position left
    top: 0,                                             // layout position top
    bgWidth: 3,                                         // Background width of scroll control
    bgHeight: 958,                                      // Background height of scroll control
    bgColor: '#60696e',                                 // Background color of scroll control
    bgOpacity: '0.6',                                   // Background opacity of scroll control
    barColor: '#0d0d0d',                                // Color of scroll bar
    barOpacity: '1',                                    // Opacity of scroll bar
    barPercent: 0,                                      // The scroll bar percent of the list
    scrollPercent: 0,                                   // The scroll percent of the list,It will change according to user operation.
    highContarstValue: false,                           // HighContarst flag,it should be true or false
    highContarstBgColor: '#ffffff',                     // HighContarst background color
    highContarstBarColor: '#ffff00',                    // HighContarst background color  of scroll bar
    duration: 1000,                                     // Timeout value of scroll bar
    transAnimation: `0.7s ${AnimationEffect.Basic}`,    //Transition value
};