import PropTypes from 'prop-types' import React from 'react' import CSSModules from 'browser/lib/CSSModules' import ReactDOM from 'react-dom' import styles from './FolderItem.styl' import dataApi from 'browser/main/lib/dataApi' import store from 'browser/main/store' import { SketchPicker } from 'react-color' import { SortableElement, SortableHandle } from 'react-sortable-hoc' class FolderItem extends React.Component { constructor (props) { super(props) this.state = { status: 'IDLE', folder: { showColumnPicker: false, colorPickerPos: { left: 0, top: 0 }, color: props.color, name: props.name } } } handleEditChange (e) { const { folder } = this.state folder.name = this.refs.nameInput.value this.setState({ folder }) } handleConfirmButtonClick (e) { this.confirm() } confirm () { const { storage, folder } = this.props dataApi .updateFolder(storage.key, folder.key, { color: this.state.folder.color, name: this.state.folder.name }) .then((data) => { store.dispatch({ type: 'UPDATE_FOLDER', storage: data.storage }) this.setState({ status: 'IDLE' }) }) } handleColorButtonClick (e) { const folder = Object.assign({}, this.state.folder, { showColumnPicker: true, colorPickerPos: { left: 0, top: 0 } }) this.setState({ folder }, function () { // After the color picker has been painted, re-calculate its position // by comparing its dimensions to the host dimensions. const { hostBoundingBox } = this.props const colorPickerNode = ReactDOM.findDOMNode(this.refs.colorPicker) const colorPickerBox = colorPickerNode.getBoundingClientRect() const offsetTop = hostBoundingBox.bottom - colorPickerBox.bottom const folder = Object.assign({}, this.state.folder, { colorPickerPos: { left: 25, top: offsetTop < 0 ? offsetTop - 5 : 0 // subtract 5px for aestetics } }) this.setState({ folder }) }) } handleColorChange (color) { const folder = Object.assign({}, this.state.folder, { color: color.hex }) this.setState({ folder }) } handleColorPickerClose (event) { const folder = Object.assign({}, this.state.folder, { showColumnPicker: false }) this.setState({ folder }) } handleCancelButtonClick (e) { this.setState({ status: 'IDLE' }) } handleFolderItemBlur (e) { let el = e.relatedTarget while (el != null) { if (el === this.refs.root) { return false } el = el.parentNode } this.confirm() } renderEdit (e) { const popover = { position: 'absolute', zIndex: 2 } const cover = { position: 'fixed', top: 0, right: 0, bottom: 0, left: 0 } const pickerStyle = Object.assign({}, { position: 'absolute' }, this.state.folder.colorPickerPos) return (