import React, {Component, PropTypes} from 'react' import ReactDOM from 'react-dom' import {connect} from 'react-redux' import {bindActionCreators} from 'redux' import Prompts from './Prompts' import EnvironmentEditModal from '../components/EnvironmentEditModal' import SettingsModal from '../components/SettingsModal' import RequestPane from '../components/RequestPane' import ResponsePane from '../components/ResponsePane' import Sidebar from '../components/Sidebar' import {PREVIEW_MODE_FRIENDLY} from '../lib/previewModes' import {MAX_PANE_WIDTH, MIN_PANE_WIDTH, MAX_SIDEBAR_REMS, MIN_SIDEBAR_REMS} from '../lib/constants' import * as GlobalActions from '../redux/modules/global' import * as RequestGroupActions from '../redux/modules/requestGroups' import * as RequestActions from '../redux/modules/requests' import * as ModalActions from '../redux/modules/modals' import * as db from '../database' class App extends Component { constructor (props) { super(props); this.state = { activeResponse: null, activeRequest: null, draggingSidebar: false, draggingPane: false, paneWidth: 0.5, // % (fr) sidebarWidth: 19 // rem } } _generateSidebarTree (parentId, entities) { const children = entities.filter(e => e.parentId === parentId); if (children.length > 0) { return children.map(c => ({ doc: c, children: this._generateSidebarTree(c._id, entities) })); } else { return children; } } _startDragSidebar () { console.log('-- Start Sidebar Drag --'); this.setState({ draggingSidebar: true }) } _startDragPane () { console.log('-- Start Pane Drag --'); this.setState({ draggingPane: true }) } componentDidMount () { document.addEventListener('mouseup', () => { if (this.state.draggingSidebar || this.state.draggingPane) { console.log('-- End Pane or Sidebar Drag --'); this.setState({ draggingSidebar: false, draggingPane: false }) } }); document.addEventListener('mousemove', e => { if (this.state.draggingPane) { const requestPane = ReactDOM.findDOMNode(this.refs.requestPane); const responsePane = ReactDOM.findDOMNode(this.refs.responsePane); const requestPaneWidth = requestPane.offsetWidth; const responsePaneWidth = responsePane.offsetWidth; const pixelOffset = e.clientX - requestPane.offsetLeft; let paneWidth = pixelOffset / (requestPaneWidth + responsePaneWidth); paneWidth = Math.min(Math.max(paneWidth, MIN_PANE_WIDTH), MAX_PANE_WIDTH); this.setState({paneWidth}); } else if (this.state.draggingSidebar) { const currentPixelWidth = ReactDOM.findDOMNode(this.refs.sidebar).offsetWidth; const ratio = e.clientX / currentPixelWidth; const width = this.state.sidebarWidth * ratio; const sidebarWidth = Math.max(Math.min(width, MAX_SIDEBAR_REMS), MIN_SIDEBAR_REMS); this.setState({sidebarWidth}) } }) } componentWillUnmount () { console.log('hello'); } render () { const {actions, modals, workspaces, requests, entities} = this.props; // TODO: Factor this out into a selector let workspace = entities.workspaces[workspaces.activeId]; if (!workspace) { workspace = entities.workspaces[Object.keys(entities.workspaces)[0]]; } let activeRequestId = workspace.activeRequestId; const activeRequest = activeRequestId ? entities.requests[activeRequestId] : null; // Request doesn't actually exist anymore :( if (!activeRequest) { activeRequestId = null; } const responses = Object.keys(entities.responses).map(id => entities.responses[id]); const allRequests = Object.keys(entities.requests).map(id => entities.requests[id]); const allRequestGroups = Object.keys(entities.requestGroups).map(id => entities.requestGroups[id]); const activeResponse = responses.sort( (a, b) => a._id > b._id ? -1 : 1 ).find(r => r.parentId === activeRequestId); const children = this._generateSidebarTree( workspace._id, allRequests.concat(allRequestGroups) ); const {sidebarWidth, paneWidth} = this.state; const gridTemplateColumns = `${sidebarWidth}rem 0 ${paneWidth}fr 0 ${1 - paneWidth}fr`; return (