index.jsx 2.1 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 20 21 22 23 24 25 26 27 28
import React from 'react';
import { classes } from '/common/util';
import { Button } from '/components';
import { draggingData } from '/workspace/core';
import { Droppable } from '/workspace/components';
import styles from './stylesheet.scss';

class WSTabContainer extends React.Component {
  constructor(props) {
    super(props);

    const { core } = props;
    core.reference.component = this;
    this.core = core;
  }

  setDraggingTab(e, tab) {
    e.stopPropagation();
    draggingData.set(e, {
      type: 'tab',
      tab,
    });
  }

  setDraggingSection(e) {
    e.stopPropagation();
    draggingData.set(e, {
      type: 'section',
J
Jason Park 已提交
29
      section: this.core,
J
Jason Park 已提交
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
    })
  }


  handleOnClickTab(tabIndex) {
    this.core.setTabIndex(tabIndex);
  }

  handleDropTab(tab) {
    this.core.addChild(tab);
  }

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

    return (
      <Droppable className={classes(styles.tab_container, className)}
                 droppingClassName={styles.dropping}
                 onDropTab={tab => this.handleDropTab(tab)}>
        <div className={styles.tab_bar}
             draggable={true}
             onDragStart={e => this.setDraggingSection(e)}>
          <div className={classes(styles.title, styles.fake)} />
          {
            children.map((tab, i) => {
              const { title } = tab;
              const selected = i === tabIndex;
              return (
                <Button className={classes(styles.title, selected && styles.selected)}
                        onClick={() => this.handleOnClickTab(i)}
                        draggable={true} key={i}
                        onDragStart={e => this.setDraggingTab(e, tab)}>
                  {title}
                </Button>
              );
            })
          }
          <div className={classes(styles.title, styles.fake)} />
        </div>
        <div className={styles.content}>
          {
            ~tabIndex ? children[tabIndex].element : null
          }
        </div>
      </Droppable>
    );
  }
}

export default WSTabContainer;