-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScrollView.web.js
More file actions
275 lines (235 loc) · 8.98 KB
/
ScrollView.web.js
File metadata and controls
275 lines (235 loc) · 8.98 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* Copyright (c) 2015-present, Alibaba Group Holding Limited.
* All rights reserved.
*
* Copyright (c) 2015, Facebook, Inc. All rights reserved.
*
* @providesModule ReactScrollView
*/
'use strict';
import React, { PropTypes, Component} from 'react';
import ReactDOM from 'react-dom';
import ScrollResponder from 'ReactScrollResponder';
import StyleSheet from 'ReactStyleSheet';
import View from 'ReactView';
import throttle from 'domkit/throttle';
import mixin from 'react-mixin';
import autobind from 'autobind-decorator';
const SCROLLVIEW = 'ScrollView';
const INNERVIEW = 'InnerScrollView';
const CONTENT_EXT_STYLE = ['padding', 'paddingTop', 'paddingBottom', 'paddingLeft', 'paddingRight'];
/**
* Component that wraps platform ScrollView while providing
* integration with touch locking "responder" system.
*
* Keep in mind that ScrollViews must have a bounded height in order to work,
* since they contain unbounded-height children into a bounded container (via
* a scroll interaction). In order to bound the height of a ScrollView, either
* set the height of the view directly (discouraged) or make sure all parent
* views have bounded height. Forgetting to transfer `{flex: 1}` down the
* view stack can lead to errors here, which the element inspector makes
* easy to debug.
*
* Doesn't yet support other contained responders from blocking this scroll
* view from becoming the responder.
*/
class ScrollView extends Component {
state = this.scrollResponderMixinGetInitialState();
componentWillUnmount() {
if (this._aniFrame) {
window.cancelAnimationFrame(this._aniFrame);
this._aniFrame = null;
}
}
/**
* Returns a reference to the underlying scroll responder, which supports
* operations like `scrollTo`. All ScrollView-like components should
* implement this method so that they can be composed while providing access
* to the underlying scroll responder's methods.
*/
getScrollResponder() {
return this;
}
getInnerViewNode() {
return this.refs[INNERVIEW];
}
scrollTo(opts) {
// $FlowFixMe - Don't know how to pass Mixin correctly. Postpone for now
// this.getScrollResponder().scrollResponderScrollTo(destX || 0, destY || 0);
if (typeof opts === 'number') {
opts = { y: opts, x: arguments[1] };
}
this._scrollViewDom = this._scrollViewDom||ReactDOM.findDOMNode(this.refs[SCROLLVIEW]);
let srcX = this._scrollViewDom.scrollLeft;
let srcY = this._scrollViewDom.scrollTop;
let stepX = (opts.x||0)-this._scrollViewDom.scrollLeft;
let stepY = (opts.y||0)-this._scrollViewDom.scrollTop;
let now = Date.now();
const durtion = 100;
const stepFoo = (timestamp)=>{
let now2 = Date.now();
if (now2 - now < durtion && now2 - now > 0) {
let s = (now2 - now) / durtion;
this._scrollViewDom.scrollLeft = srcX + stepX*s;
this._scrollViewDom.scrollTop = srcY + stepY*s;
this._aniFrame = window.requestAnimationFrame(stepFoo);
} else {
this._scrollViewDom.scrollLeft = (opts.x||0);
this._scrollViewDom.scrollTop = (opts.y||0);
this._aniFrame = null;
}
};
this._aniFrame = window.requestAnimationFrame(stepFoo);
// this.state.__offset.setValue({
// y: this._scrollViewDom.scrollTop,
// x: this._scrollViewDom.scrollLeft
// });
// this.state.__scrolling = true;
// Animated.spring(this.state.__offset, {
// toValue: {x:-(opts.x||0),y:-(opts.y||0)},
// bounciness: 0,
// restSpeedThreshold: 1
// }).start(()=>{
// // this.setState({__scrolling:false}, ()=> {
// this._scrollViewDom = ReactDOM.findDOMNode(this.refs[SCROLLVIEW]);
// this._scrollViewDom.scrollTop = -(opts.x||0);
// this._scrollViewDom.scrollLeft = -(opts.y||0);
// // });
// });
//this.scrollWithoutAnimationTo(opts.y, opts.x);
}
scrollWithoutAnimationTo(destY?: number, destX?: number) {
// $FlowFixMe - Don't know how to pass Mixin correctly. Postpone for now
// this.getScrollResponder().scrollResponderScrollWithouthAnimationTo(
// destX || 0,
// destY || 0,
// );
this.state.__offset.setValue({x:-(destX || 0), y:-(destY || 0)});
// this._scrollViewDom = ReactDOM.findDOMNode(this.refs[SCROLLVIEW]);
// this._scrollViewDom.scrollTop = destY || 0;
// this._scrollViewDom.scrollLeft = destX || 0;
}
handleScroll(e: Event) {
// if (__DEV__) {
// if (this.props.onScroll && !this.props.scrollEventThrottle) {
// console.log(
// 'You specified `onScroll` on a <ScrollView> but not ' +
// '`scrollEventThrottle`. You will only receive one event. ' +
// 'Using `16` you get all the events but be aware that it may ' +
// 'cause frame drops, use a bigger number if you don\'t need as ' +
// 'much precision.'
// );
// }
// }
// if (Platform.OS === 'android') {
// if (this.props.keyboardDismissMode === 'on-drag') {
// dismissKeyboard();
// }
// }
if (!this._scrollViewDom)
this._scrollViewDom = ReactDOM.findDOMNode(this.refs[SCROLLVIEW]);
e.nativeEvent = e.nativeEvent || {};
e.nativeEvent.contentOffset = {x:this._scrollViewDom.scrollLeft, y:this._scrollViewDom.scrollTop};
this.props.onScroll && this.props.onScroll(e);
}
render() {
let {
style,
...otherProps
} = this.props;
let contentContainerExtStyle = {};
if (style) {
for (let i = 0; i < CONTENT_EXT_STYLE.length; i++) {
if (typeof style[CONTENT_EXT_STYLE[i]] === 'number') {
contentContainerExtStyle[CONTENT_EXT_STYLE[i]] = style[CONTENT_EXT_STYLE[i]];
}
}
}
let contentContainerStyle = [
styles.contentContainer,
this.props.horizontal && styles.contentContainerHorizontal,
this.props.contentContainerStyle,
contentContainerExtStyle,
];
// if (__DEV__ && this.props.style) {
// let style = flattenStyle(this.props.style);
// let childLayoutProps = ['alignItems', 'justifyContent']
// .filter((prop) => style && style[prop] !== undefined);
// invariant(
// childLayoutProps.length === 0,
// 'ScrollView child layout (' + JSON.stringify(childLayoutProps) +
// ') must by applied through the contentContainerStyle prop.'
// );
// }
let contentContainer =
<View
ref={INNERVIEW}
style={contentContainerStyle}
removeClippedSubviews={this.props.removeClippedSubviews}
collapsable={false}
>
{this.props.children}
</View>;
let alwaysBounceHorizontal =
this.props.alwaysBounceHorizontal !== undefined ?
this.props.alwaysBounceHorizontal :
this.props.horizontal;
let alwaysBounceVertical =
this.props.alwaysBounceVertical !== undefined ?
this.props.alwaysBounceVertical :
!this.props.horizontal;
let handleScroll = () => {};
if (this.props.scrollEventThrottle && this.props.onScroll) {
handleScroll = throttle(this.handleScroll, this.props.scrollEventThrottle ? 1000/this.props.scrollEventThrottle : 1000);
}
let props = {
...otherProps,
alwaysBounceHorizontal,
alwaysBounceVertical,
style: ([styles.base, this.props.style]: ?Array<any>),
onTouchStart: this.scrollResponderHandleTouchStart,
onTouchMove: this.scrollResponderHandleTouchMove,
onTouchEnd: this.scrollResponderHandleTouchEnd,
onScrollBeginDrag: this.scrollResponderHandleScrollBeginDrag,
onScrollEndDrag: this.scrollResponderHandleScrollEndDrag,
onMomentumScrollBegin: this.scrollResponderHandleMomentumScrollBegin,
onMomentumScrollEnd: this.scrollResponderHandleMomentumScrollEnd,
onStartShouldSetResponder: this.scrollResponderHandleStartShouldSetResponder,
onStartShouldSetResponderCapture: this.scrollResponderHandleStartShouldSetResponderCapture,
// onScrollShouldSetResponder: this.scrollResponderHandleScrollShouldSetResponder,
// onScroll: handleScroll,
onScrollShouldSetResponder: handleScroll,
// replace onScroll in the props
onScroll: () => {},
onResponderGrant: this.scrollResponderHandleResponderGrant,
onResponderTerminationRequest: this.scrollResponderHandleTerminationRequest,
onResponderTerminate: this.scrollResponderHandleTerminate,
onResponderRelease: this.scrollResponderHandleResponderRelease,
onResponderReject: this.scrollResponderHandleResponderReject,
};
return (
<View {...props} ref={SCROLLVIEW}>
{contentContainer}
</View>
);
}
};
let styles = StyleSheet.create({
base: {
overflow: 'scroll',
WebkitOverflowScrolling: 'touch',
flex: 1,
},
contentContainer: {
position: 'absolute',
minWidth: '100%',
},
contentContainerHorizontal: {
alignSelf: 'flex-start',
flexDirection: 'row',
},
});
mixin.onClass(ScrollView, ScrollResponder.Mixin);
autobind(ScrollView);
ScrollView.isReactNativeComponent = true;
export default ScrollView;