forked from ElemeFE/element-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBar.jsx
87 lines (69 loc) · 2.53 KB
/
Bar.jsx
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
29
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
82
83
84
85
86
87
import React from 'react';
import { PropTypes, Component } from '../../libs';
import { BAR_MAP, renderThumbStyle} from './util';
import {on, off} from '../../libs/utils/dom';
export class Bar extends Component {
constructor(props) {
super(props);
}
get bar() {
return BAR_MAP[this.props.vertical ? 'vertical' : 'horizontal'];
}
get wrap() {
return this.props.getParentWrap();
}
clickThumbHandler(e) {
this.startDrag(e);
this[this.bar.axis] = (e.currentTarget[this.bar.offset] - (e[this.bar.client] - e.currentTarget.getBoundingClientRect()[this.bar.direction]));
}
clickTrackHandler(e) {
const offset = Math.abs(e.target.getBoundingClientRect()[this.bar.direction] - e[this.bar.client]);
const thumbHalf = (this.refs.thumb[this.bar.offset] / 2);
const thumbPositionPercentage = ((offset - thumbHalf) * 100 / this.root[this.bar.offset]);
this.wrap[this.bar.scroll] = (thumbPositionPercentage * this.wrap[this.bar.scrollSize] / 100);
}
startDrag(e) {
e.stopImmediatePropagation();
this.cursorDown = true;
on(document, 'mousemove', this.mouseMoveDocumentHandler);
on(document, 'mouseup', this.mouseUpDocumentHandler);
document.onselectstart = () => false;
}
mouseMoveDocumentHandler(e) {
if (this.cursorDown === false) return;
const prevPage = this[this.bar.axis];
if (!prevPage) return;
const offset = (e[this.bar.client] - this.root.getBoundingClientRect()[this.bar.direction]);
const thumbClickPosition = (this.refs.thumb[this.bar.offset] - prevPage);
const thumbPositionPercentage = ((offset - thumbClickPosition) * 100 / this.root[this.bar.offset]);
this.wrap[this.bar.scroll] = (thumbPositionPercentage * this.wrap[this.bar.scrollSize] / 100);
}
mouseUpDocumentHandler() {
this.cursorDown = false;
this[this.bar.axis] = 0;
off(document, 'mousemove', this.mouseMoveDocumentHandler);
document.onselectstart = null;
}
render() {
const { size, move } = this.props;
return (
<div
ref="root"
className={this.classNames('el-scrollbar__bar', `is-${this.bar.key}`)}
onMouseDown={ this.clickTrackHandler.bind(this) } >
<div
ref="thumb"
className="el-scrollbar__thumb"
onMouseDown={ this.clickThumbHandler.bind(this) }
style={ renderThumbStyle({ size, move, bar: this.bar }) }>
</div>
</div>
);
}
}
Bar.propTypes = {
vertical: PropTypes.bool,
size: PropTypes.string,
move: PropTypes.number,
getParentWrap: PropTypes.func.isRequired
}