import React from 'react'; import { classes } from '/common/util'; import { Divider, Droppable } from '/workspace/components'; import { SectionContainer, TabContainer } from '/workspace/core'; import styles from './stylesheet.scss'; class WSSectionContainer extends React.Component { constructor(props) { super(props); const { core } = props; core.reference.component = this; this.core = core; } handleResize(index, targetElement, clientX, clientY) { const { offsetLeft, offsetTop, offsetWidth, offsetHeight } = targetElement.parentElement; const { horizontal } = this.core; const position = horizontal ? clientX - offsetLeft : clientY - offsetTop; const containerSize = horizontal ? offsetWidth : offsetHeight; this.core.resizeChild(index, position, containerSize); } handleDropTabToContainer(tab, index) { const tabContainer = new TabContainer(); this.handleDropSectionToContainer(tabContainer, index); tabContainer.addChild(tab); } handleDropSectionToContainer(section, index) { this.core.addChild(section, index); } handleDropTabToSection(tab, index, before = false) { const tabContainer = new TabContainer(); this.handleDropSectionToSection(tabContainer, index, before); tabContainer.addChild(tab); } handleDropSectionToSection(section, index, before = false) { const child = this.core.children[index]; const sectionContainer = new SectionContainer({ horizontal: !this.core.horizontal }); this.core.addChild(sectionContainer, index); sectionContainer.addChild(child); sectionContainer.addChild(section, before ? 0 : 1); } render() { const { className } = this.props; const { children, horizontal } = this.core; const visibleChildren = children.filter(child => child.visible); const totalWeight = visibleChildren.reduce((sumWeight, child) => sumWeight + (child.relative ? child.weight : 0), 0); const elements = []; visibleChildren.forEach((child, visibleIndex) => { const index = children.indexOf(child); elements.push( 0 && ((target, dx, dy) => this.handleResize(visibleIndex, target, dx, dy))} onDropTab={tab => this.handleDropTabToContainer(tab, index)} onDropSection={section => this.handleDropSectionToContainer(section, index)} /> ); const style = child.relative ? { flexGrow: child.weight / totalWeight, } : { flexGrow: 0, flexBasis: child.size, }; if (children.length === 1) { elements.push(
{child.element}
); } else { elements.push(
this.handleDropTabToSection(tab, index, true)} onDropSection={section => this.handleDropSectionToSection(section, index, true)} /> {child.element} this.handleDropTabToSection(tab, index)} onDropSection={section => this.handleDropSectionToSection(section, index)} />
); } if (visibleIndex === visibleChildren.length - 1) { elements.push( this.handleDropTabToContainer(tab, index + 1)} onDropSection={section => this.handleDropSectionToContainer(section, index + 1)} /> ); } }); return (
{ elements.length ? elements : ( this.handleDropTabToContainer(tab)} onDropSection={section => this.handleDropSectionToContainer(section)} /> ) }
); } } export default WSSectionContainer;