All files / UI_Component/common ScrollText.js

100% Statements 73/73
92.31% Branches 36/39
100% Functions 13/13
100% Lines 72/72
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 242 243 244 245 246                229x   229x         229x       229x 229x 229x   229x 4x 4x 4x 4x                       4x 4x         229x 229x 229x 13x 13x 4x             351x 351x         96x   96x 96x 4x 4x 4x 4x 4x                       4x 4x     351x 276x                 359x       163x 152x 39x 39x 2x             229x 229x       6x               233x 8x 8x         2x 2x 2x 2x 2x 2x 1x   1x     2x           392x 392x 392x                           392x 16x 16x           16x 16x 16x 16x 16x             16x             16x 32x             376x                     752x           6x                             6x                            
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { is, Map } from 'immutable';
import CommonAPI from './CommonAPI';
 
export default class ScrollText extends Component {
 
    constructor(props) {
        super(props);
 
        this.state = {
            textAniName: '',
            textAniDuration: '',
            textAniDelay: '',
        };
        this.animationDone = this.animationDone.bind(this);
    }
 
    componentWillMount() {
        const { children, fontSize, fontFamily, width, textGap } = this.props;
        this.textWidth = CommonAPI.getTextWidth(children, fontSize, fontFamily);
        this.aniName = `Text${this.textWidth}${textGap}`;
 
        if (this.textWidth > width) {
            const movePx = this.textWidth + textGap;
            this.style = document.createElement('style');
            this.style.type = 'text/css';
            const keyFrames = `@keyframes ${this.aniName}1
                             {
                                0%   {transform: translateX(0px)          translateZ(100px);}
                                100% {transform: translateX(-${movePx}px) translateZ(100px);}
                             }
 
                             @keyframes ${this.aniName}2
                             {
                                0%   {transform: translateX(0px)          translateZ(100px);}
                                100% {transform: translateX(-${movePx}px) translateZ(100px);}
                             }
                             `;
            this.style.innerHTML = keyFrames;
            document.getElementsByTagName('head')[0].appendChild(this.style);
        }
    }
 
    componentDidMount() {
        this.text.addEventListener('animationend', this.animationDone, false);
        Eif (this.state.textAniName === '') {
            if (this.props.scroll) {
                const { width } = this.props;
                if (this.textWidth > width) {
                    this.onMount();
                }
            }
        }
    }
 
    componentWillReceiveProps(nextProps) {
        const { children, fontSize, fontFamily } = nextProps;
        if (nextProps.children !== this.props.children ||
            nextProps.fontSize !== this.props.fontSize ||
            nextProps.textGap !== this.props.textGap ||
            nextProps.width !== this.props.width ||
            nextProps.fontFamily !== this.props.fontFamily) {
            this.textWidth = CommonAPI.getTextWidth(children, fontSize, fontFamily);
 
            this.aniName = `Text${this.textWidth}${nextProps.textGap}`;
            if (this.textWidth > nextProps.width) {
                const movePx = this.textWidth + nextProps.textGap;
                this.removeStyleFromHeader();
                this.style = document.createElement('style');
                this.style.type = 'text/css';
                const keyFrames = `@keyframes ${this.aniName}1
                                         {
                                            0%   {transform: translateX(0px)          translateZ(100px);}
                                            100% {transform: translateX(-${movePx}px) translateZ(100px);}
                                         }
                 
                                         @keyframes ${this.aniName}2
                                         {
                                            0%   {transform: translateX(0px)          translateZ(100px);}
                                            100% {transform: translateX(-${movePx}px) translateZ(100px);}
                                         }
                                         `;
                this.style.innerHTML = keyFrames;
                document.getElementsByTagName('head')[0].appendChild(this.style);
            }
        }
        if (!nextProps.scroll) {
            this.setState({
                textAniName: '',
                textAniDuration: '',
                textAniDelay: '',
            });
        }
    }
 
    shouldComponentUpdate(nextProps, nextState) {
        return (JSON.stringify(nextProps) !== JSON.stringify(this.props)) || (JSON.stringify(nextState) !== JSON.stringify(this.state));
    }
 
    componentDidUpdate() {
        if (this.state.textAniName === '') {
            if (this.props.scroll) {
                const { width } = this.props;
                if (this.textWidth > width) {
                    this.onMount();
                }
            }
        }
    }
 
    componentWillUnmount() {
        this.removeStyleFromHeader();
        this.text.removeEventListener('animationend', this.animationDone, false);
    }
 
    onMount() {
        this.setState({
            textAniName: `${this.aniName}1`,
            textAniDuration: `${(this.textWidth + this.props.textGap) / 50}s`,
            textAniDelay: '1s',
        });
    }
 
    removeStyleFromHeader() {
        if (this.style) {
            document.getElementsByTagName('head')[0].removeChild(this.style);
            this.style = null;
        }
    }
 
    animationDone(e) {
        e.preventDefault();
        const target = e.target;
        Eif (target === e.currentTarget) {
            Eif (e.type === 'animationend') {
                let textAni = this.state.textAniName;
                if (textAni === `${this.aniName}1`) {
                    textAni = `${this.aniName}2`;
                } else {
                    textAni = `${this.aniName}1`;
                }
 
                this.setState({ textAniName: textAni });
            }
        }
    }
 
    render() {
        const { scroll, children, left, top, width, height, lineHeight, fontSize, fontFamily, color, textAlign, verticalAlign } = this.props;
        const { textAniName, textAniDuration, textAniDelay } = this.state;
        const style = {
            position: 'absolute',
            overflow: 'hidden',
            left,
            top,
            width,
            height,
            lineHeight,
            textAlign,
            verticalAlign,
            fontSize,
            fontFamily,
            color,
        };
        if (scroll && this.textWidth > width) {
            style.textOverflow = '';
            const aniStyle = {
                position: 'absolute',
                left: 0,
                top: 0,
                width: (2 * this.textWidth) + this.props.textGap,
            };
            aniStyle.animationName = textAniName;
            aniStyle.animationDuration = textAniDuration;
            aniStyle.animationDelay = textAniDelay;
            aniStyle.animationTimingFunction = 'linear';
            const text1Style = {
                position: 'absolute',
                left: 0,
                top: 0,
                animationTimingFunction: 'linear',
                animationFillMode: 'both',
            };
            const text2Style = {
                position: 'absolute',
                left: this.textWidth + this.props.textGap,
                top: 0,
                animationTimingFunction: 'linear',
                animationFillMode: 'both',
            };
            return (<div style={style}>
              <div style={aniStyle} ref={(text) => { this.text = text; }}>
                <div style={text1Style} ref='text1'>{children}</div>
                <div style={text2Style} ref='text2' aria-hidden='true'>{children}</div>
              </div>
            </div>);
        }
 
        const text3Style = {
            position: 'absolute',
            left: 0,
            top: 0,
            width,
            height,
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            whiteSpace: 'nowrap',
        };
 
        return (<div style={style} ref={(text) => { this.text = text; }}>
          <div style={text3Style} ref='text3'>{children}</div>
        </div>);
    }
}
 
ScrollText.defaultProps = {
    left: 0,
    top: 0,
    width: 195,
    height: 40,
    lineHeight: '40px',
    textAlign: 'left',
    verticalAlign: 'middle',
    scroll: true,
    fontSize: 26,
    fontFamily: 'SamsungOneGui_600',
    textGap: 60,
    color: '#000000',
};
 
ScrollText.propTypes = {
    left: PropTypes.number,
    top: PropTypes.number,
    width: PropTypes.number.isRequired,
    height: PropTypes.number.isRequired,
    lineHeight: PropTypes.string,
    textAlign: PropTypes.oneOf(['left', 'center', 'right']),
    verticalAlign: PropTypes.oneOf(['top', 'middle', 'bottom']),
    scroll: PropTypes.bool.isRequired,
    fontSize: PropTypes.number.isRequired,
    fontFamily: PropTypes.string.isRequired,
    textGap: PropTypes.number.isRequired,
    color: PropTypes.string,
};