index.jsx 4.3 KB
Newer Older
J
Jason Park 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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;
20 21
    const containerSize = horizontal ? offsetWidth : offsetHeight;
    this.core.resizeChild(index, position, containerSize);
J
Jason Park 已提交
22 23 24 25
  }

  handleDropTabToContainer(tab, index) {
    const tabContainer = new TabContainer();
J
Jason Park 已提交
26
    this.handleDropSectionToContainer(tabContainer, index);
J
Jason Park 已提交
27 28 29
    tabContainer.addChild(tab);
  }

J
Jason Park 已提交
30 31 32 33
  handleDropSectionToContainer(section, index) {
    this.core.addChild(section, index);
  }

J
Jason Park 已提交
34 35
  handleDropTabToSection(tab, index, before = false) {
    const tabContainer = new TabContainer();
J
Jason Park 已提交
36 37 38 39 40 41
    this.handleDropSectionToSection(tabContainer, index, before);
    tabContainer.addChild(tab);
  }

  handleDropSectionToSection(section, index, before = false) {
    const child = this.core.children[index];
J
Jason Park 已提交
42 43 44
    const sectionContainer = new SectionContainer({ horizontal: !this.core.horizontal });
    this.core.addChild(sectionContainer, index);
    sectionContainer.addChild(child);
J
Jason Park 已提交
45
    sectionContainer.addChild(section, before ? 0 : 1);
J
Jason Park 已提交
46 47 48 49 50 51 52
  }

  render() {
    const { className } = this.props;
    const { children, horizontal } = this.core;

    const visibleChildren = children.filter(child => child.visible);
53
    const totalWeight = visibleChildren.reduce((sumWeight, child) => sumWeight + (child.relative ? child.weight : 0), 0);
J
Jason Park 已提交
54
    const elements = [];
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    visibleChildren.forEach((child, visibleIndex) => {
      const index = children.indexOf(child);
      elements.push(
        <Divider key={`divider-before-${child.key}`} horizontal={horizontal}
                 onResize={visibleIndex > 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(
          <div key={child.key} className={classes(styles.wrapper)} style={style}>
            {child.element}
          </div>
        );
      } else {
J
Jason Park 已提交
76
        elements.push(
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
          <div key={child.key} className={classes(styles.wrapper, !horizontal && styles.horizontal)} style={style}>
            <Divider horizontal={!horizontal}
                     onDropTab={tab => this.handleDropTabToSection(tab, index, true)}
                     onDropSection={section => this.handleDropSectionToSection(section, index, true)} />
            {child.element}
            <Divider horizontal={!horizontal}
                     onDropTab={tab => this.handleDropTabToSection(tab, index)}
                     onDropSection={section => this.handleDropSectionToSection(section, index)} />
          </div>
        );
      }
      if (visibleIndex === visibleChildren.length - 1) {
        elements.push(
          <Divider key={`divider-after-${child.key}`} horizontal={horizontal}
                   onDropTab={tab => this.handleDropTabToContainer(tab, index + 1)}
                   onDropSection={section => this.handleDropSectionToContainer(section, index + 1)} />
J
Jason Park 已提交
93 94 95 96 97 98
        );
      }
    });

    return (
      <div className={classes(styles.container, horizontal && styles.horizontal, className)}>
99 100 101 102 103 104 105 106
        {
          elements.length ?
            elements : (
              <Droppable key="empty" className={styles.wrapper} droppingClassName={styles.dropping}
                         onDropTab={tab => this.handleDropTabToContainer(tab)}
                         onDropSection={section => this.handleDropSectionToContainer(section)} />
            )
        }
J
Jason Park 已提交
107 108 109 110 111 112
      </div>
    );
  }
}

export default WSSectionContainer;