Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @flow
  2. import React, {Component, PropTypes} from 'react';
  3. import '../helpers/number';
  4.  
  5.  
  6. export class ResizeblePanel extends Component {
  7.  
  8.   constructor(props) {
  9.     super(props);
  10.     this.handleResize = this.handleResize.bind(this);
  11.     this.handleResizeStart = this.handleResizeStart.bind(this);
  12.     this.handleResizeEnd = this.handleResizeEnd.bind(this);
  13.     this.type = 'horizontal';
  14.     this.resizeRange = [20, 60];
  15.     this.state = {
  16.       size: 20
  17.     }
  18.   }
  19.  
  20.   handleResize(event) {
  21.     event.preventDefault();
  22.  
  23.     if (this.props.type === 'horizontal') {
  24.       this.setState({size: (100 * event.clientX / document.body.clientWidth).between(this.resizeRange)});
  25.     } else if (this.props.type === 'vertical') {
  26.       this.setState({size: (100 * event.clientY / document.body.clientHeight).between(this.resizeRange)});
  27.     }
  28.   }
  29.  
  30.   handleResizeStart(event) {
  31.     if (event.button === 0) {
  32.       event.preventDefault();
  33.  
  34.       document.addEventListener('mousemove', this.handleResize);
  35.       document.addEventListener('mouseup', this.handleResizeEnd);
  36.     }
  37.   }
  38.  
  39.   handleResizeEnd(event) {
  40.     if (event.button === 0) {
  41.       event.preventDefault();
  42.  
  43.       document.removeEventListener('mousemove', this.handleResize);
  44.       document.removeEventListener('mouseup', this.handleResizeEnd);
  45.     }
  46.   }
  47.  
  48.   render() {
  49.     let delimiterPos;
  50.     let sizeFirst;
  51.     let sizeSecond;
  52.  
  53.     if (this.props.type === 'horizontal') {
  54.       delimiterPos = {left: this.state.size + '%'};
  55.       sizeFirst = {width: this.state.size + '%'};
  56.       sizeSecond = {width: (100 - this.state.size) + '%'};
  57.     } else if (this.props.type === 'vertical') {
  58.       delimiterPos = {top: this.state.size + '%'};
  59.       sizeFirst = {height: this.state.size + '%'};
  60.       sizeSecond = {height: (100 - this.state.size) + '%'};
  61.     }
  62.  
  63.     return (
  64.       <div className={"resizeble-panel " + this.props.type}>
  65.         {React.cloneElement(this.props.children[0], {size: sizeFirst})}
  66.         {React.cloneElement(this.props.children[1], {size: sizeSecond})}
  67.         <div onMouseDown={this.handleResizeStart} style={delimiterPos}></div>
  68.       </div>
  69.     );
  70.   }
  71. }
  72.  
  73. ResizeblePanel.propTypes = {
  74.   type: PropTypes.string,
  75.   resizeRange: PropTypes.array
  76. };
  77.  
  78. export class Panel extends Component {
  79.   constructor(props) {
  80.     super(props);
  81.   }
  82.  
  83.   render() {
  84.     return (
  85.       <div style={this.props.size}>{this.props.children}</div>
  86.     );
  87.   };
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement