All files / nexshop-web-contents/src/component/scene scene.js

95.11% Statements 175/184
80.85% Branches 38/47
95.56% Functions 43/45
94.97% Lines 170/179
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 4351x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x     19x   19x 21x 21x   21x 9x       19x         57x 57x 57x 57x 57x 57x   57x   57x 53x 53x 53x 53x     57x                     57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x       2x       2x       1x           1x 1x             1x                   5x 5x   5x 1x       1x     8x 4x 4x     4x   4x             2x   2x 7x 7x 3x       2x       1x 1x 2x   1x   1x             12x 16x 11x   5x       12x 16x         13x       1x           2x 2x   2x 2x 2x 2x   2x 2x 1x   1x         1x         3x 3x 3x 7x 7x 5x       3x 4x                                                 20x       6x   6x 6x 6x 6x 6x   6x       18x 18x 11x   7x   18x       10x     10x 5x 5x   20x     5x   20x   5x     10x   10x 10x   10x       2x 2x 2x   2x 2x   2x 2x   2x   2x 1x         2x       1x 1x 2x   1x   1x   1x           1x               59x 59x 2x         1x       1x       88x 88x   88x                                                                                     1x 57x   57x                     1x 57x 57x 57x   57x              
import React, {Component} from 'react';
import SceneTitleView from "./scene-title-view";
import {connect} from "react-redux";
import _ from 'lodash';
import {bindActionCreators} from "redux";
import {sceneActions} from 'nexshop-web-store';
import {actions as dialogActions} from 'nexshop-web-dialog';
import {actions as alertActions, DiscardRefresh, ConfirmView} from 'nexshop-web-alert';
import SceneEditorView from "./scene-editor/scene-editor-view";
import SceneResourceView from "./scene-resource/scene-resource-view";
import {createEmptySceneFromLayouts, createScene} from "../../model-factory/scene";
import ConfirmUpdateContentsItem from "../dialog/confirm-update-contents-item/confirm-update-contents-item"
 
 
const DEFAULT_LAYOUT_ID = '';
const DEFAULT_TAB_ID = 'contents';
const MAX_PANEL_COUNT_WITH_VIDEO = 2;
 
function getAllPanel(panels, {direction} = 'row') {
    const allPanels = [];
 
    for (let i = 0; i < panels.length; i++) {
        allPanels.push(panels[i]);
        panels[i].parentDirection = direction;
 
        if (panels[i].type === 'group') {
            allPanels.push(getAllPanel(panels[i].panels, panels[i]));
        }
    }
 
    return allPanels;
}
 
class Scene extends Component {
    constructor(props) {
        super(props);
        let {orientation} = this.props;
        let {scene} = props;
        let selectedTabId = DEFAULT_TAB_ID;
        let selectedLayoutId = DEFAULT_LAYOUT_ID;
        let hasFullContent = true;
 
        const isSceneDetail = !scene;
 
        if (isSceneDetail) {
            selectedTabId = 'layout';
            selectedLayoutId = '1';
            scene = createEmptySceneFromLayouts(selectedLayoutId, orientation);
            hasFullContent = false;
        }
 
        this.state = {
            layout: scene,
            selectedLayoutId,
            selectedTabId,
            hasFullContent,
            selectedPanelId: '',
            draggedOverPanelId: '',
            alertVisibility: false,
            usedInOthers: false
        };
 
        this.selectLayout = this.selectLayout.bind(this);
        this.updatePanelWidths = this.updatePanelWidths.bind(this);
        this.selectTab = this.selectTab.bind(this);
        this.saveScene = this.saveScene.bind(this);
        this.checkContentsForPanel = this.checkContentsForPanel.bind(this);
        this.checkContentsForGroup = this.checkContentsForGroup.bind(this);
        this.isDraggedOverPanel = this.isDraggedOverPanel.bind(this);
        this.isSelectedPanel = this.isSelectedPanel.bind(this);
        this.setSelectedPanelId = this.setSelectedPanelId.bind(this);
        this.setDraggedOverPanelId = this.setDraggedOverPanelId.bind(this);
        this.addContentItem = this.addContentItem.bind(this);
        this.removeContentItem = this.removeContentItem.bind(this);
        this.getPanelGroupRects = this.getPanelGroupRects.bind(this);
        this.getPanelRects = this.getPanelRects.bind(this);
        this.getPanelSize = this.getPanelSize.bind(this);
        this.getWrapperSizeForResolution = this.getWrapperSizeForResolution.bind(this);
        this.moveToPreview = this.moveToPreview.bind(this);
        this.getCSSWrapperName = this.getCSSWrapperName.bind(this);
        this.getRelatedContents = this.getRelatedContents.bind(this);
        this.updateFitMode = this.updateFitMode.bind(this);
        this.isAddableContentsItem = this.isAddableContentsItem.bind(this);
        this.onCloseAlertMessage = this.onCloseAlertMessage.bind(this);
    }
 
    isDraggedOverPanel(id) {
        return id === this.state.draggedOverPanelId;
    }
 
    isSelectedPanel(id) {
        return id === this.state.selectedPanelId;
    }
 
    setSelectedPanelId(selectedPanelId) {
        this.setState({
            selectedPanelId
        });
    }
 
    setDraggedOverPanelId(draggedOverPanelId) {
        Eif (this.state.draggedOverPanelId !== draggedOverPanelId) {
            this.setState({
                draggedOverPanelId
            });
        }
    }
 
    selectLayout(layoutId) {
        this.setState({
            layout: createEmptySceneFromLayouts(layoutId, this.props.orientation),
            selectedLayoutId: layoutId,
            hasFullContent: false,
            selectedPanelId: '',
            draggedOverPanelId: '',
        });
    }
 
    addContentItem(panelId, contentItem) {
        const newScene = {...this.state.layout};
        let panels = _.flattenDeep(getAllPanel(newScene.panels));
 
        if (contentItem.type === 'video' && !this.isAddableContentsItem(panels)) {
            this.setState({
                alertVisibility: true,
            });
 
            return;
        }
 
        let panel = panels.find(panel => panel.id === panelId);
        Eif (!panel.contentItems) {
            panel.contentItems = [];
        }
 
        panel.contentItems.push(contentItem);
 
        this.setState({
            layout: newScene,
            hasFullContent: this.checkContentsForGroup(newScene.panels),
        });
    }
 
    isAddableContentsItem(panels) {
        let videoCount = 0;
 
        panels.forEach((panel) => {
            let {contentItems: [contentItem = {}] = []} = panel;
            if (contentItem.type === 'video') {
                videoCount++;
            }
        });
 
        return videoCount < MAX_PANEL_COUNT_WITH_VIDEO;
    }
 
    removeContentItem(panelId) {
        const newScene = {...this.state.layout};
        let panels = _.flattenDeep(getAllPanel(newScene.panels));
        let panel = panels.find(panel => panel.id === panelId);
 
        delete panel.contentItems;
 
        this.setState({
            layout: newScene,
            hasFullContent: this.checkContentsForGroup(newScene.panels),
        });
    }
 
    checkContentsForGroup(panels) {
        let resultList = panels.map((panel) => {
            if (panel.type === 'panel') {
                return this.checkContentsForPanel(panel);
            } else {
                return this.checkContentsForGroup(panel.panels);
            }
        });
 
        return resultList.reduce((accumulator, currentValue) => {
            return accumulator && currentValue
        }, true);
    }
 
    checkContentsForPanel(panel) {
        return !!panel.contentItems;
    }
 
    selectTab(tabId) {
        this.setState({
            selectedTabId: tabId,
        });
    }
 
    saveScene() {
        this.props.actions.unblockNavigation();
        const newScene = {...this.state.layout};
 
        const {sceneId, name, resolution, orientation, breadcrumb, actions: {saveSceneAsync, updateSceneAsync, openDialog}} = this.props;
        const relatedContents = this.getRelatedContents(newScene.panels);
        const currentFolderId = breadcrumb[breadcrumb.length - 1].id;
        const sceneElement = document.getElementById('scene-body');
 
        let sceneFormat = createScene(name, newScene.panels, currentFolderId, resolution, orientation, relatedContents);
        if (sceneId) {
            updateSceneAsync(sceneFormat, sceneId, sceneElement, resolution, orientation)
                .then((response) => {
                    Iif (response.occupied) {
                        openDialog('update-contents-item');
                    }
                });
        } else {
            saveSceneAsync(sceneFormat, sceneElement, resolution, orientation);
        }
    }
 
    getRelatedContents(panels) {
        let flatPanels = _.flattenDeep(getAllPanel(panels));
        let relatedContentsSet = new Set();
        flatPanels.forEach((panel) => {
            let contentItems = panel.contentItems;
            if (panel.type === 'panel' && contentItems) {
                contentItems.forEach((content) => relatedContentsSet.add(content.id));
            }
        });
 
        return Array.from(relatedContentsSet).map((id) => {
            return {id}
        });
    }
 
    updatePanelWidths() {
        const newScene = {...this.state.layout};
 
        let panels = _.flattenDeep(getAllPanel(newScene.panels)).reverse();
 
        panels.forEach((panel) => {
            if (panel.type === 'panel') {
                const domRect = document.getElementById(panel.id).getClientRects()[0];
                panel.domAttribute = {...panel.domAttribute, ...this.getPanelRects(panel, domRect)};
            } else {
                panel.domAttribute = {...panel.domAttribute, ...this.getPanelGroupRects(panel)};
            }
        });
 
        this.setState({
            layout: newScene,
            hasFullContent: this.checkContentsForGroup(newScene.panels),
        })
    }
 
    getWrapperSizeForResolution(orientation) {
        return orientation === 'horizontal' ? {width: 960, height: 540} : {width: 432, height: 768};
    }
 
    getPanelRects({parentDirection}, domRect) {
        const basicSize = this.getWrapperSizeForResolution(this.props.orientation);
        let width, height, pixelWidth, pixelHeight, size;
        width = (domRect.width / basicSize.width) * 100;
        height = (domRect.height / basicSize.height) * 100;
        pixelWidth = domRect.width;
        pixelHeight = domRect.height;
        size = this.getPanelSize(parentDirection, domRect);
 
        return {width, height, pixelWidth, pixelHeight, size};
    }
 
    getPanelSize(parentDirection, domRect) {
        let size = 0;
        if (parentDirection === 'row') {
            size = domRect.width;
        } else {
            size = domRect.height;
        }
        return size;
    }
 
    getPanelGroupRects({direction, parentDirection, panels}) {
        const basicSize = this.getWrapperSizeForResolution(this.props.orientation);
        let pixelHeight, pixelWidth, height, width, size;
 
        if (direction === 'row') {
            pixelHeight = panels[0].domAttribute.pixelHeight;
            pixelWidth = panels.reduce(
                (accum, current) => {
                    return accum + current.domAttribute.pixelWidth;
                }, 0);
        } else {
            pixelHeight = panels.reduce(
                (accum, current) => {
                    return accum + current.domAttribute.pixelHeight;
                }, 0);
            pixelWidth = panels[0].domAttribute.pixelWidth;
        }
 
        size = this.getPanelSize(parentDirection, {width: pixelWidth, height: pixelHeight});
 
        width = (pixelWidth / basicSize.width) * 100;
        height = (pixelHeight / basicSize.height) * 100;
 
        return {pixelHeight, pixelWidth, height, width, size};
    }
 
    moveToPreview() {
        this.props.actions.unblockNavigation();
        const newScene = {...this.state.layout};
        const {name, resolution, orientation, actions: {createScenePreview}} = this.props;
 
        const previewWindowRects = document.getElementsByClassName('scene')[0].getClientRects()[0];
        const previewWindowSize = {width: previewWindowRects.width, height: previewWindowRects.height};
 
        const sceneLayoutRects = document.getElementsByClassName('layout-wrapper' + this.getCSSWrapperName(orientation))[0].getClientRects()[0];
        const sceneLayoutSize = {width: sceneLayoutRects.width - 2, height: sceneLayoutRects.height - 2};
 
        createScenePreview(createScene(name, newScene.panels, resolution, orientation), previewWindowSize, sceneLayoutSize);
 
        setTimeout(() => {
            this.props.history.push('/contents/scene-preview');
        });
    }
 
    getCSSWrapperName(orientation) {
        return orientation === 'horizontal' ? '--horizontal' : '--vertical';
    }
 
    updateFitMode(fitMode, panelId, contentItemId) {
        const newScene = {...this.state.layout};
        let panels = _.flattenDeep(getAllPanel(newScene.panels));
        let panel = panels.find(panel => panel.id === panelId);
 
        let contentItem = panel.contentItems.find(contentItem => contentItem.id === contentItemId);
 
        contentItem.fitMode = fitMode;
 
        this.setState({
            layout: newScene,
        });
    }
 
    onCloseAlertMessage() {
        this.setState({
            selectedPanelId: '',
            draggedOverPanelId: '',
            alertVisibility: false,
        });
    }
 
    componentWillMount() {
        const {name, resolution, history} = this.props;
        if (!name || !resolution) {
            history.push('/contents/main');
        }
    }
 
    componentDidMount() {
        this.props.actions.blockNavigation('Your work has not been saved.');
    }
 
    componentWillUnmount() {
        this.props.actions.unblockNavigation();
    }
 
    render() {
        const {name = 'scene', orientation = 'vertical'} = this.props;
        const {layout, selectedLayoutId, selectedTabId, hasFullContent, alertVisibility} = this.state;
 
        return (
            <div className="scene">
                <SceneTitleView title={name}
                                saveScene={this.saveScene} hasFullContent={hasFullContent}
                                onPreviewClick={this.moveToPreview}
                />
                <div className="scene-body">
                    <SceneEditorView
                        layout={layout}
                        orientation={orientation}
                        updatePanelWidths={this.updatePanelWidths}
                        selectedTabId={selectedTabId}
                        isDraggedOverPanel={this.isDraggedOverPanel}
                        isSelectedPanel={this.isSelectedPanel}
                        setSelectedPanelId={this.setSelectedPanelId}
                        setDraggedOverPanelId={this.setDraggedOverPanelId}
                        addContentItem={this.addContentItem}
                        getCSSWrapperName={this.getCSSWrapperName}
                        removeContentItem={this.removeContentItem}
                        updateFitMode={this.updateFitMode}
                    />
                    <SceneResourceView
                        orientation={orientation}
                        getCSSWrapperName={this.getCSSWrapperName}
                        changeLayout={this.selectLayout}
                        selectTab={this.selectTab}
                        selectedTabId={selectedTabId}
                        selectedLayoutId={selectedLayoutId}
                    />
                    <DiscardRefresh/>
                    {
                        alertVisibility &&
                        <ConfirmView message={'You can add up to 2 video files.'}
                                     icon={'warning'} positiveButtonText={'OK'}
                                     onPositiveClick={this.onCloseAlertMessage}/>
                    }
                    <ConfirmUpdateContentsItem name={name}/>
                </div>
            </div>
        );
    }
}
 
const mapStateToProps = (state) => {
    let {scene: {scene, sceneId, name, resolution, orientation, usedIn}, contents: {breadcrumb}} = state;
 
    return {
        name,
        resolution,
        orientation,
        scene,
        sceneId,
        breadcrumb,
        usedIn,
    };
};
 
const mapDispatchToProps = (dispatch) => {
    const {saveSceneAsync, updateSceneAsync, createScenePreview} = sceneActions;
    const {blockNavigation, unblockNavigation} = alertActions;
    const {openDialog} = dialogActions;
 
    return {
        actions: bindActionCreators({
            saveSceneAsync, updateSceneAsync, createScenePreview, blockNavigation, unblockNavigation, openDialog
        }, dispatch)
    }
};
 
export default connect(mapStateToProps, mapDispatchToProps)(Scene);