All files / nexshop-web-contents/src/component/contents contents-item-view.js

97.37% Statements 37/38
96.55% Branches 28/29
100% Functions 13/13
97.37% Lines 37/38
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 1441x 1x 1x 1x 1x       44x   44x 44x 44x 44x 44x       13x       9x 7x 7x 2x       9x       1x           23x 12x 12x 12x         2x 2x           3x 3x 3x       3x 2x         1x 1x   1x       3x                 52x   52x                                           9x                                                   1x                      
import React, {Component} from 'react';
import moment from 'moment';
import PropTypes from 'prop-types';
import {findDOMNode} from '../../../src/wrapper/findDOMNodeWrapper';
import {convertBytesToKilobytes} from '../../helpers/utility';
 
export default class ContentsItemView extends Component {
    constructor(props) {
        super(props);
 
        this.componentDidUpdate = this.componentDidUpdate.bind(this);
        this.contentsItemNameInputBlurred = this.contentsItemNameInputBlurred.bind(this);
        this.onKeyUp = this.onKeyUp.bind(this);
        this.focusTitleInputAndSelect = this.focusTitleInputAndSelect.bind(this);
        this.getContentsItem = this.getContentsItem.bind(this);
    }
 
    componentDidMount() {
        this.focusTitleInputAndSelect();
    }
 
    componentDidUpdate() {
        this.timeOutID = setTimeout(() => {
            let myNode = findDOMNode(this);
            if (!myNode.querySelector(':hover')) {
                return;
            }
        }, 50);
 
        this.focusTitleInputAndSelect();
    }
 
    componentWillUnmount() {
        Iif (this.timeOutID !== undefined) {
            clearTimeout(this.timeOutID);
        }
    }
 
    focusTitleInputAndSelect() {
        if (this.titleInput) {
            this.titleInput.value = this.props.contentsItem.name;
            this.titleInput.focus();
            this.titleInput.setSelectionRange(0, this.props.contentsItem.name.lastIndexOf('.'));
        }
    }
 
    contentsItemNameInputBlurred() {
        let contentsItem = {...this.props.contentsItem};
        this.titleInput.value !== contentsItem.name ?
            this.props.updateContentsItemAsync(contentsItem, this.titleInput.value) :
            this.props.updateContentsItemData(this.getContentsItem(contentsItem));
    }
 
    getContentsItem(contentsItem) {
        let newContentsItem = {...contentsItem};
        newContentsItem.status === 'error' ? newContentsItem.status = 'new' : delete newContentsItem.status;
        return newContentsItem;
    }
 
    onKeyUp({key}) {
        if (key === 'Escape' || key === 'Enter') {
            this.titleInput.blur();
        }
    }
 
    getHighlightedContentsName() {
        const {contentsItem, searchKeyword} = this.props;
        const strParts = contentsItem.name.split(new RegExp(`(${searchKeyword})`, 'gi'));
 
        return (
            <span>
            {
                strParts.map((part, i) =>
                    <span key={i} className={part.toLowerCase() === searchKeyword.toLowerCase() ? 'contents-item__name-highlight' : '' }>
                        { part }
                    </span>)
            }
            </span>
        );
    }
 
    render() {
        const {contentsItem, onPreview, onDelete, onClick, onDoubleClick, onContextMenu, selectedContentsItemIds = [], searchKeyword} = this.props;
 
        return (
            <tr
                className={`contents-item${selectedContentsItemIds.includes(contentsItem.id) ? '--selected' : ''}`}
                onClick={onClick}
                onDoubleClick={onDoubleClick}
                onContextMenu={onContextMenu}
            >
                <td>
                    <div className="contents-item-preview" onClick={onPreview} >
                        <div className="contents-item-preview__play"/>
                    </div>
                    {contentsItem.thumbnailUrls.thumbnail ?
                        <div className="contents-item__thumbnail"
                             style={{backgroundImage: `url(${contentsItem.thumbnailUrls.thumbnail})`}}/> :
                        <div className="contents-item__thumbnail"
                             style={{backgroundColor: 'black'}}/>}
                </td>
                <td>
                    <div className={`contents-item__type--${contentsItem.type}`}/>
                    {
                        contentsItem.status === 'new' || contentsItem.status === 'error' ?
                            <input className="contents-item__name--new"
                                   ref={input => this.titleInput = input}
                                   onBlur={this.contentsItemNameInputBlurred}
                                   onKeyUp={this.onKeyUp}
                            /> :
                            <div className="contents-item__name">
                                {searchKeyword ? this.getHighlightedContentsName() : contentsItem.name}
                            </div>
                    }
                </td>
                <td/>
                <td/>
                <td/>
                <td>
                    {moment(contentsItem.lastModifiedTimestamp).format('MM-DD-YYYY')}
                </td>
                <td>
                    {contentsItem.fileSizeBytes && convertBytesToKilobytes(contentsItem.fileSizeBytes)}
                </td>
                <td>
                    <div className="contents-item__trash-can" onClick={onDelete}/>
                </td>
            </tr>
        )
    }
}
 
ContentsItemView.propTypes = {
    contentsItem: PropTypes.object.isRequired,
    onDelete: PropTypes.func,
    onPreview: PropTypes.func,
    onClick: PropTypes.func,
    onDoubleClick: PropTypes.func,
    onContextMenu: PropTypes.func,
    updateContentsItemAsync: PropTypes.func,
    updateContentsItemData: PropTypes.func,
    selectedContentsItemIds: PropTypes.array,
    searchKeyword: PropTypes.string,
};