All files / nexshop-web-contents/src/component/preview/playlist playlist-preview-detail-view.js

100% Statements 59/59
97.73% Branches 43/44
100% Functions 16/16
100% Lines 59/59
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 3011x 1x 1x   1x       44x   44x 44x   44x               44x 44x 44x 44x 44x 44x 44x 44x 44x       42x       5x 5x       1x       50x 4x 3x           48x       48x 48x       64x 64x 64x   64x 3x                       61x 2x             59x   39x   7x   7x   6x         7x                                                                                               39x                                                             7x                                                                                                       6x                                         48x 48x 48x   48x         48x         48x       64x 64x 64x   64x       64x 64x   64x 61x     64x                   1x        
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import ScenePreviewDetailView from "../scene/scene-preview-detail-view";
 
const PREVIEW_LIST_SIZE = 250;
 
class PlaylistPreviewDetailView extends Component {
    constructor(props) {
        super(props);
 
        const previewDetailSize = {width: props.previewWindowSize.width - PREVIEW_LIST_SIZE, height: props.previewWindowSize.height};
        const {contentItemSize, mountDepth} = this.getPreviewBodyFrameSize(props.resolution, previewDetailSize);
 
        this.state = {
            active: false,
            height: props.height,
            previewDetailSize,
            contentItemSize,
            mountDepth
        };
 
        this.timeoutNumber = 0;
        this.animate = this.animate.bind(this);
        this.drawContents = this.drawContents.bind(this);
        this.drawFadeImage = this.drawFadeImage.bind(this);
        this.drawBlindImage = this.drawBlindImage.bind(this);
        this.drawDefaultImage = this.drawDefaultImage.bind(this);
        this.drawSlideImage = this.drawSlideImage.bind(this);
        this.getPreviewBodyFrameSize = this.getPreviewBodyFrameSize.bind(this);
        this.getContentItemEffectPoint = this.getContentItemEffectPoint.bind(this);
    }
 
    componentDidMount() {
        this.animate();
    }
 
    componentWillReceiveProps() {
        this.setState({active: false});
        this.animate();
    }
 
    componentWillUnmount() {
        clearTimeout(this.timeoutNumber);
    }
 
    animate() {
        this.timeoutNumber = setTimeout(() => {
            if (!this.state.active) {
                this.setState({active: true});
            }
        }, 0);
    }
 
    getPreviewBaseSide(playlistRatio, windowRatio) {
        return playlistRatio < windowRatio ? 'height' : 'width';
    }
 
    getPreviewRatio(resolution, windowSize) {
        const baseSide = this.getPreviewBaseSide(resolution.width / resolution.height, windowSize.width / windowSize.height);
        return windowSize[baseSide] / resolution[baseSide];
    }
 
    drawContents(transitionEffect, contentsItem, previousContentsUrl, contentItemSize, mountDepth) {
        const {metaData, type} = contentsItem;
        const {contentUrl, mimeType} = metaData;
        const {effectPoint} = this.getContentItemEffectPoint(contentItemSize, mountDepth);
 
        if (type === 'video' && this.state.active) {
            return (
                <div className="preview-detail--video">
                    <div className="preview-detail--video__center">
                        {
                            <video preload="metadata" autoPlay controls
                                   src={contentUrl} type={mimeType}
                                   width={contentItemSize.width} height={contentItemSize.height}
                            />
                        }
                    </div>
                </div>
            );
        } else if (type === 'scene' && this.state.active){
            return (
                //TODO : another resolution case
                <ScenePreviewDetailView scene={metaData} previewWindowSize={this.state.previewDetailSize}
                                            sceneLayoutSize={this.props.resolution.orientation === 'horizontal' ? {width:960, height:540} : {width:540, height:960}}/>
            )
        }
 
        switch (transitionEffect) {
            case 'fade':
                return this.drawFadeImage(contentUrl, previousContentsUrl, contentItemSize, effectPoint);
            case 'blind':
                return this.drawBlindImage(contentUrl, previousContentsUrl, contentItemSize, mountDepth, effectPoint);
            case 'slide':
                return this.drawSlideImage(contentUrl, previousContentsUrl, contentItemSize, mountDepth, effectPoint);
            default :
                return this.drawDefaultImage(contentUrl, contentItemSize, effectPoint);
        }
    }
 
    drawSlideImage(contentUrl, previousContentUrl, contentItemSize, mountDepth, effectPoint) {
        return (
            <div className="preview-detail">
                <div style={{
                    backgroundColor: 'black',
                    width: `${mountDepth.width}px`,
                    height: `${contentItemSize.height + 2 * mountDepth.height}px`,
                    zIndex: 2
                }}/>
                <div style={{
                    display: 'flex',
                    justifyContent: 'center',
                    alignItems: 'center'
                }}>
                    <div className="preview-detail-image"
                         style={{
                             width: `${contentItemSize.width}px`,
                             height: `${contentItemSize.height}px`
                         }}
                    >
                        <div className={`preview-detail-slide`} style={{
                            top: `${effectPoint.basePoint.y}px`,
                            left: `${this.state.active ? effectPoint.basePoint.x : effectPoint.startPoint.x}px`,
                            transition: `${this.state.active ? 'left 1s' : ''}`,
                            backgroundImage: `url(${contentUrl})`,
                            width: `${contentItemSize.width}px`,
                            height: `${contentItemSize.height}px`
                        }}/>
                        <div className={`preview-detail-slide`} style={{
                            top: `${effectPoint.basePoint.y}px`,
                            left: `${this.state.active ? effectPoint.endPoint.x : effectPoint.basePoint.x}px`,
                            transition: `${this.state.active ? 'left 1s' : ''}`,
                            backgroundImage: `url(${previousContentUrl})`,
                            width: `${contentItemSize.width}px`,
                            height: `${contentItemSize.height}px`
                        }}/>
                    </div>
                </div>
                <div style={{
                    backgroundColor: 'black',
                    width: `${mountDepth.width + 1}px`,
                    height: `${contentItemSize.height + 2 * mountDepth.height}px`,
                    zIndex: 2
                }}/>
            </div>
        );
    }
 
    drawFadeImage(contentUrl, previousContentUrl, contentItemSize, effectPoint) {
        return (
            <div className="preview-detail">
                <div className="preview-detail-image"
                     style={{
                         width: `${contentItemSize.width}px`,
                         height: `${contentItemSize.height}px`
                     }}
                >
                    <div className={`preview-detail-fade`} style={{
                        opacity: `${this.state.active ? '1' : '0'}`,
                        transition: `${this.state.active ? 'opacity 1s' : ''}`,
                        backgroundImage: `url(${contentUrl})`,
                        top: `${effectPoint.basePoint.y}px`,
                        left: `${effectPoint.basePoint.x}px`,
                        width: `${contentItemSize.width}px`,
                        height: `${contentItemSize.height}px`
                    }}/>
                    <div className={`preview-detail-fade`} style={{
                        opacity: `${this.state.active ? '0' : '1'}`,
                        transition: `${this.state.active ? 'opacity 1s' : ''}`,
                        backgroundImage: `url(${previousContentUrl})`,
                        top: `${effectPoint.basePoint.y}px`,
                        left: `${effectPoint.basePoint.x}px`,
                        width: `${contentItemSize.width}px`,
                        height: `${contentItemSize.height}px`
                    }}/>
                </div>
            </div>);
    }
 
    drawBlindImage(contentUrl, previousContentUrl, contentItemSize, mountDepth, effectPoint) {
        return (
            <div className="preview-detail"
                 style={{flexDirection: 'column'}}
            >
                <div style={{
                    backgroundColor: 'black',
                    width: `${contentItemSize.width + 2 * mountDepth.width}px`,
                    height: `${mountDepth.height}px`,
                    zIndex: 3
                }}/>
                <div style={{
                    display: 'flex',
                    justifyContent: 'center',
                    alignItems: 'center'
                }}>
                    <div className="preview-detail-image"
                         style={{
                             width: `${contentItemSize.width}px`,
                             height: `${contentItemSize.height}px`
                         }}
                    >
                        <div className={'preview-detail-blind'} style={{
                            zIndex: 2,
                            top: `${this.state.active ? effectPoint.endPoint.y : effectPoint.basePoint.y}px`,
                            left: `${effectPoint.basePoint.x}px`,
                            visibility: `${!this.state.active}`,
                            overflow: 'hidden',
                            transition: `${this.state.active ? 'top 1s' : ''}`,
                            backgroundImage: `url(${previousContentUrl})`,
                            width: `${contentItemSize.width}px`,
                            height: `${contentItemSize.height}px`
                        }}/>
                        <div className={`preview-detail-blind`} style={{
                            zIndex: 1,
                            backgroundImage: `url(${contentUrl})`,
                            top: `${effectPoint.basePoint.y}px`,
                            left: `${effectPoint.basePoint.x}px`,
                            width: `${contentItemSize.width}px`,
                            height: `${contentItemSize.height}px`
                        }}/>
                    </div>
                </div>
                <div style={{
                    backgroundColor: 'black',
                    width: `${contentItemSize.width + 2 * mountDepth.width}px`,
                    height: `${mountDepth.height + 1}px`,
                    zIndex: 3
                }}/>
            </div>);
    }
 
    drawDefaultImage(contentUrl, contentItemSize, effectPoint) {
        return (
            <div className="preview-detail">
                <div className="preview-detail-image"
                     style={{
                         width: `${contentItemSize.width}px`,
                         height: `${contentItemSize.height}px`
                     }}>
                    <div className='preview-detail-default' style={{
                        position: 'fixed',
                        zIndex: 1,
                        backgroundImage: `url(${contentUrl})`,
                        top: `${effectPoint.basePoint.y}px`,
                        left: `${effectPoint.basePoint.x}px`,
                        width: `${contentItemSize.width}px`,
                        height: `${contentItemSize.height}px`
                    }}/>
                </div>
            </div>);
    }
 
    getPreviewBodyFrameSize(display, previewDetailSize) {
        const {text, orientation} = display;
        const resolution = orientation === 'horizontal' ? {width: text.split('x')[0], height: text.split('x')[1]} : {width: text.split('x')[1], height: text.split('x')[0]};
        const ratio = this.getPreviewRatio(resolution, previewDetailSize);
 
        let contentItemSize = {
            width: Math.round(ratio * resolution.width),
            height: Math.round(ratio * resolution.height)
        };
 
        let mountDepth = {
            width: Math.floor((previewDetailSize.width - contentItemSize.width) / 2),
            height: Math.floor((previewDetailSize.height - contentItemSize.height) / 2)
        };
 
        return {contentItemSize, mountDepth};
    }
 
    getContentItemEffectPoint(contentItemSize, mountDepth) {
        const basePoint = {x: (PREVIEW_LIST_SIZE + mountDepth.width), y: (mountDepth.height)};
        const startPoint = {x: (basePoint.x + contentItemSize.width), y: (basePoint.y - contentItemSize.height)};
        const endPoint = {x: (basePoint.x - contentItemSize.width), y: (basePoint.y + contentItemSize.height)};
 
        return {effectPoint: {basePoint, startPoint, endPoint}};
    }
 
    render() {
        const {contentItem: {contents, transitionEffect}, previousContentItem} = this.props;
        let previousContentsUrl = '';
 
        if (previousContentItem && previousContentItem.contents.metaData) {
            previousContentsUrl = previousContentItem.contents.metaData.contentUrl;
        }
 
        return (
            <div ref="preview">
                {
                    this.drawContents(transitionEffect, contents, previousContentsUrl, this.state.contentItemSize, this.state.mountDepth)
                }
            </div>
        );
    }
}
 
PlaylistPreviewDetailView.propTypes = {
    contentItem: PropTypes.object.isRequired,
};
 
export default PlaylistPreviewDetailView;