-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathItemSelector.jsx
More file actions
202 lines (144 loc) · 5.11 KB
/
ItemSelector.jsx
File metadata and controls
202 lines (144 loc) · 5.11 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { PropTypes } from 'react';
import { PanoramaDispatcher, PanoramaEventTypes } from '../PanoramaDispatcher.js';
import './style.scss';
export default class ItemSelector extends React.Component {
static propTypes = {
title: PropTypes.string,
items: PropTypes.array.isRequired,
selectedIndex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
selectedItem: PropTypes.object,
onItemSelected: PropTypes.func
}
static defaultProps = {
title: '',
items: [],
selectedIndex: '',
selectedItem: null,
onItemSelected: null
}
constructor (props) {
super(props);
// manually bind event handlers,
// since React ES6 doesn't do this automatically
this.onItemClick = this.onItemClick.bind(this);
this.onArrowMouseDown = this.onArrowMouseDown.bind(this);
this.onArrowMouseUp = this.onArrowMouseUp.bind(this);
this.animateScrollPosition = this.animateScrollPosition.bind(this);
}
componentWillMount () {
//
}
componentDidMount () {
this.scrollToSelectedItem();
}
componentWillUnmount () {
//
}
componentDidUpdate () {
this.scrollToSelectedItem();
}
onItemClick (event) {
// Defense.
if (!event.currentTarget || !event.currentTarget.dataset) { return; }
// Direct communication: call callback if it was passed in.
if (this.props.onItemSelected) {
this.props.onItemSelected(this.props.items[event.currentTarget.dataset.index], event.currentTarget.dataset.index);
}
// Indirect communication: Notify any subscribers of item selection.
PanoramaDispatcher.ItemSelector.selected(this.props.items[event.currentTarget.dataset.index], event.currentTarget.dataset.index);
}
onArrowMouseDown (event) {
let dir;
if (event.target.classList.contains('up-arrow')) {
dir = -1;
} else if (event.target.classList.contains('down-arrow')) {
dir = 1;
}
if (!dir) { return; }
let itemList = this.refs['item-list'],
nextAccelCounter = 16,
accelCounter = 0,
itemEl = itemList.querySelector('li'),
itemMetrics = window.getComputedStyle(itemEl),
speed = itemEl.offsetHeight + (itemMetrics ? parseFloat(itemMetrics['margin-bottom'].replace('px', '')) : 0);
this.arrowMouseUp = false;
let onArrowMouseHold = function () {
if (accelCounter-- <= 1) {
this.scrollToPosition(itemList.scrollTop + dir * speed);
accelCounter = nextAccelCounter = Math.max(1, Math.floor(nextAccelCounter * 0.75));
}
if (!this.arrowMouseUp) {
window.requestAnimationFrame(onArrowMouseHold);
}
}.bind(this);
window.requestAnimationFrame(onArrowMouseHold);
}
onArrowMouseUp (event) {
this.arrowMouseUp = true;
}
scrollToPosition (position) {
if (typeof this.targetScrollPosition === 'undefined') {
// Not currently animating, so start
this.targetScrollPosition = position;
this.animateScrollPosition();
} else {
// Already animating; just update target
this.targetScrollPosition = position;
}
}
scrollToSelectedItem () {
let itemList = this.refs['item-list'],
selectedItem = itemList.querySelector('.selected');
if (selectedItem) {
this.scrollToPosition(selectedItem.offsetTop - itemList.offsetHeight);
}
}
animateScrollPosition () {
let itemList = this.refs['item-list'],
delta;
if (typeof this.scrollPosition === 'undefined') {
this.scrollPosition = itemList.scrollTop;
}
delta = this.targetScrollPosition - this.scrollPosition;
if (Math.abs(delta) > 1) {
this.scrollPosition += 0.25 * delta;
itemList.scrollTop = this.scrollPosition; // scrollTop rounds to the nearest int
window.requestAnimationFrame(this.animateScrollPosition);
} else {
itemList.scrollTop = this.targetScrollPosition;
this.targetScrollPosition = undefined;
this.scrollPosition = undefined;
}
}
getDefaultState () {
return {};
}
render () {
let isSelected;
return (
<div className='panorama item-selector'>
<h3>{ this.props.title }</h3>
<div className='scroll-arrow up-arrow' onMouseDown={ this.onArrowMouseDown } onMouseUp={ this.onArrowMouseUp } />
<ul ref='item-list'>
{ this.props.items.map((item, i) => {
isSelected =
this.props.selectedItem.id == item.id || // selectedItem with items as Objects
this.props.selectedItem === item || // selectedItem with items as Strings
this.props.selectedIndex === i; // selectedIndex
return (
<li
className = { 'item' + (isSelected ? ' selected' : '') + (item.className ? ' ' + item.className : '') }
data-index = { i }
key = { i }
onClick = { this.onItemClick }
>
<span>{ item.name }</span>
</li>
);
}) }
</ul>
<div className='scroll-arrow down-arrow' onMouseDown={ this.onArrowMouseDown } onMouseUp={ this.onArrowMouseUp } />
</div>
);
}
}