Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (96 loc) · 3.3 KB
/
Copy pathindex.js
File metadata and controls
112 lines (96 loc) · 3.3 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Module dependencies
*/
import React, {Component} from 'react';
/**
* Container styles
*/
const styles = {position: 'relative'};
/**
* Expose Rotation
*
* @param {Array} data
* @param {Boolean} cycle
* @param {Boolean} vertical
* @api public
*/
export default class Rotation extends Component {
state = {current: 0};
constructor(props) {
super(props);
this.handleWheel = ::this.handleWheel;
this.handleTouchStart = ::this.handleTouchStart;
this.handleTouchMove = ::this.handleTouchMove;
this.handleTouchEnd = ::this.handleTouchEnd;
}
componentDidMount() {
const el = React.findDOMNode(this.refs.container);
el.addEventListener('wheel', this.handleWheel, false);
el.addEventListener('touchstart', this.handleTouchStart, false);
el.addEventListener('touchmove', this.handleTouchMove, false);
el.addEventListener('touchend', this.handleTouchEnd, false);
el.addEventListener('mousedown', this.handleTouchStart, false);
el.addEventListener('mousemove', this.handleTouchMove, false);
document.addEventListener('mouseup', this.handleTouchEnd, false);
}
componentWillUnmount() {
const el = React.findDOMNode(this.refs.container);
el.removeEventListener('wheel', this.handleWheel, false);
el.removeEventListener('touchstart', this.handleTouchStart, false);
el.removeEventListener('touchmove', this.handleTouchMove, false);
el.removeEventListener('touchend', this.handleTouchEnd, false);
el.removeEventListener('mousedown', this.handleTouchStart, false);
el.removeEventListener('mousemove', this.handleTouchMove, false);
document.removeEventListener('mouseup', this.handleTouchEnd, false);
}
handleWheel(e) {
e.preventDefault();
const delta = e.deltaY / Math.abs(e.deltaY);
this.show(this.state.current + delta);
}
handleTouchStart(e) {
e.preventDefault();
const pointer = this.pointerPosition(e);
const touched = this.state.current;
this.setState({pointer, touched});
}
handleTouchMove(e) {
e.preventDefault();
if (typeof this.state.pointer !== 'number') return;
const el = React.findDOMNode(this.refs.container);
const pointer = this.pointerPosition(e);
const max = this.props.vertical ? el.offsetHeight : el.offsetWidth;
const offset = pointer - this.state.pointer;
const delta = Math.floor(offset / max * this.props.data.length);
this.show(this.state.touched + delta);
}
handleTouchEnd(e) {
e.preventDefault();
this.setState({pointer: null, touched: null});
}
pointerPosition(e) {
e = e.type.indexOf('touch') === 0 ? e.changedTouches[0] : e;
const el = React.findDOMNode(this.refs.container);
const pos = this.props.vertical ?
e.clientY - el.offsetTop :
e.clientX - el.offsetLeft;
return pos;
}
show(n) {
const len = this.props.data.length;
if (n < 0) n = this.props.cycle ? n + len : 0;
if (n > len - 1) n = this.props.cycle ? n - len : len - 1;
this.setState({current: n});
}
render() {
return (
<div ref='container' style={styles}>
{this.props.data.map((src, i) => {
const display = this.state.current === i ? 'block' : 'none';
const imgStyles = {display, width: '100%'};
return <img src={src} alt='' key={i} style={imgStyles} />;
})}
</div>
);
}
}