All files / UI_Component Loading.js

100% Statements 35/35
94.44% Branches 17/18
100% Functions 11/11
100% Lines 35/35
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                                  1x   1x                             1x                   2x   2x         2x       3x 3x   3x 43x 42x 42x 42x                 45x       45x 1x 1x 1x           2x 1x 1x                       2x 1x         47x   47x 19x   28x     47x       47x 47x                     47x                 47x 94x             1x                     1x                    
/**
 * @author Xiuliang Li (xiuliang.li@samsung.com)
 * @fileoverview This module manages list item.
 * @date    2017/06/20 (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';
 
const MAX_NUM = 36;
 
const ResourceMap = {
    BLACK: {
        L: 'images/loading/loading_blackl/',
        M: 'images/loading/loading_blackm/',
        S: 'images/loading/loading_blacks/',
        XS: 'images/loading/loading_blackxs/',
    },
    WHITE: {
        L: 'images/loading/loading_whitel/',
        M: 'images/loading/loading_whitem/',
        S: 'images/loading/loading_whites/',
        XS: 'images/loading/loading_whitexs/',
    },
};
 
const SizeMap = {
    XS: 36,
    S: 57,
    M: 86,
    L: 100,
};
 
export default class Loading extends Component {
 
    constructor(props) {
        super(props);
 
        this.state = {
            index: 0,
            display: true,
        };
 
        this.playLoadingID = null;
    }
 
    componentDidMount() {
        clearInterval(this.playLoadingID);
        this.playLoadingID = null;
 
        this.playLoadingID = setInterval(() => {
            if (this.node) {                    // checking if the ref exists before setting the state:
                let idx = this.state.index;
                idx = (idx === (MAX_NUM - 1)) ? 0 : (idx + 1);
                this.setState({ index: idx });
            }
        }, 70);
    }
 
    componentWillReceiveProps(nextProps) {
    }
 
    shouldComponentUpdate(nextProps, nextState) {
        return (JSON.stringify(nextProps) !== JSON.stringify(this.props)) || (JSON.stringify(nextState) !== JSON.stringify(this.state));
    }
 
    componentDidUpdate(prevProps, prevState) {
        if (!prevState.display) {
            Eif (this.playLoadingID) {
                clearInterval(this.playLoadingID);
                this.playLoadingID = null;
            }
        }
    }
 
    componentWillUnmount() {
        if (this.playLoadingID) {
            clearInterval(this.playLoadingID);
            this.playLoadingID = null;
        }
    }
 
    /**
    * show or hide the loading component
    * @name setShowState
    * @method
    * @param  {flag}
    * @memberof
    */
    setShowState(flag) {
        if (this.node) {
            this.setState({ display: flag });
        }
    }
 
    getThumbnail() {
        const { theme, size } = this.props;
        let idx;
        if (this.state.index < 10) {
            idx = `0${this.state.index}`;
        } else {
            idx = `${this.state.index}`;
        }
 
        return `${ResourceMap[theme][size]}loading_${theme.toLowerCase()}${size.toLowerCase()}_${idx}.png`;
    }
 
    render() {
        const { size, theme, OSD } = this.props;
        const dimStyle = {
            position: 'absolute',
            top: OSD.t,
            left: OSD.l,
            width: OSD.w,
            height: OSD.h,
            zIndex: 200,
            backgroundColor: 'rgba(19,29,34,0.4)',
            display: this.state.display ? 'inline' : 'none',
        };
 
        const loadingStyle = {
            position: 'absolute',
            top: (OSD.h - SizeMap[size]) / 2,
            left: (OSD.w - SizeMap[size]) / 2,
            width: SizeMap[size],
            height: SizeMap[size],
            zIndex: 300,
        };
 
        return (
          <div ref={(node) => { this.node = node; }} style={dimStyle} >
            <img style={loadingStyle} src={this.getThumbnail()} />
          </div>
        );
    }
}
 
Loading.defaultProps = {
    theme: 'BLACK',
    size: 'L',
    OSD: {
        l: 0,
        t: 0,
        w: 1920,
        h: 1080,
    },
};
 
Loading.propTypes = {
    theme: PropTypes.oneOf(['WHITE', 'BLACK']),
    size: PropTypes.oneOf(['XS', 'S', 'M', 'L']),
    OSD: PropTypes.shape({
        l: PropTypes.number,
        t: PropTypes.number,
        w: PropTypes.number,
        h: PropTypes.number,
    }),
};