| 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 | 2×
2×
2×
2×
2×
2×
2×
| var GetPositionMixin, React, ScrollTracker, ScrollTrackerParentMixin, _;
React = require('react');
_ = require('underscore');
GetPositionMixin = require('openstax-react-components').GetPositionMixin;
ScrollTracker = {
mixins: [GetPositionMixin],
propTypes: {
setScrollPoint: React.PropTypes.func.isRequired,
unsetScrollPoint: React.PropTypes.func,
scrollState: React.PropTypes.object.isRequired
},
getInitialState: function() {
return {
scrollPoint: 0
};
},
setScrollPoint: function() {
var ref, scrollPoint, scrollState, setScrollPoint;
ref = this.props, setScrollPoint = ref.setScrollPoint, scrollState = ref.scrollState;
scrollPoint = this.getTopPosition(this.getDOMNode());
this.setState({
scrollPoint: scrollPoint
});
return setScrollPoint(scrollPoint, scrollState);
},
unsetScrollPoint: function() {
var unsetScrollPoint;
unsetScrollPoint = this.props.unsetScrollPoint;
return typeof unsetScrollPoint === "function" ? unsetScrollPoint(this.state.scrollPoint) : void 0;
},
componentDidMount: function() {
return this.setScrollPoint();
},
componentWillUnmount: function() {
return this.unsetScrollPoint();
}
};
ScrollTrackerParentMixin = {
getInitialState: function() {
return {
scrollPoints: [],
scrollState: {},
scrollTopBuffer: 0
};
},
setScrollTopBuffer: function() {
var scrollTopBuffer;
scrollTopBuffer = GetPositionMixin.getTopPosition(this.getDOMNode());
return this.setState({
scrollTopBuffer: scrollTopBuffer
});
},
setScrollPoint: function(scrollPoint, scrollState) {
var scrollPointData;
scrollPointData = _.extend({
scrollPoint: scrollPoint
}, scrollState);
this.state.scrollPoints.push(scrollPointData);
return this.sortScrollPoints();
},
unsetScrollPoint: function(unsetScrollPoint) {
this.state.scrollPoints = _.reject(this.state.scrollPoints, function(scrollPoint) {
return scrollPoint.scrollPoint === unsetScrollPoint;
});
return this.sortScrollPoints();
},
sortScrollPoints: function() {
var sortedDescScrollPoints;
sortedDescScrollPoints = _.sortBy(this.state.scrollPoints, function(scrollData) {
return -1 * scrollData.scrollPoint;
});
return this.setState({
scrollPoints: sortedDescScrollPoints
});
},
getScrollStateByScroll: function(scrollTop) {
var scrollState;
scrollState = _.find(this.state.scrollPoints, (function(_this) {
return function(scrollData) {
return scrollTop > (scrollData.scrollPoint - _this.state.scrollTopBuffer - 2);
};
})(this));
return scrollState || _.last(this.state.scrollPoints);
},
getScrollStateByKey: function(stepKey) {
var scrollStateIndex;
scrollStateIndex = _.findLastIndex(this.state.scrollPoints, function(scrollData) {
return scrollData.key === stepKey;
});
return this.state.scrollPoints[scrollStateIndex];
},
setScrollState: function() {
var scrollState;
scrollState = this.getScrollStateByScroll(this.state.scrollTop);
this.setState({
scrollState: scrollState
});
return this.props.setScrollState(scrollState);
},
componentDidMount: function() {
this.setScrollTopBuffer();
if (this.props.currentStep != null) {
return this.scrollToKey(this.props.currentStep);
}
},
componentWillUpdate: function(nextProps, nextState) {
var willScrollStateKeyChange;
willScrollStateKeyChange = !(nextState.scrollState.key === this.state.scrollState.key);
if (willScrollStateKeyChange) {
return this.props.goToStep(nextState.scrollState.key);
}
},
componentDidUpdate: function(prevProps, prevState) {
var didCurrentStepChange, doesScrollStateMatch, ref, ref1;
doesScrollStateMatch = prevState.scrollState.key === ((ref = this.getScrollStateByScroll(this.state.scrollTop)) != null ? ref.key : void 0);
didCurrentStepChange = !(this.props.currentStep === ((ref1 = prevState.scrollState) != null ? ref1.key : void 0));
if (!doesScrollStateMatch) {
return this.setScrollState();
} else if (didCurrentStepChange) {
return this.scrollToKey(this.props.currentStep);
}
},
scrollToKey: function(stepKey) {
var scrollState;
scrollState = this.getScrollStateByKey(stepKey);
return window.scrollTo(0, (scrollState != null ? scrollState.scrollPoint : void 0) - this.state.scrollTopBuffer);
}
};
module.exports = {
ScrollTracker: ScrollTracker,
ScrollTrackerParentMixin: ScrollTrackerParentMixin
};
|