-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-photo-swiper.js
159 lines (133 loc) · 4.74 KB
/
react-photo-swiper.js
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
/* globals screen MouseEvent TouchEvent */
"use strict";
(function (root, factory) {
if (typeof module !== "undefined" && module.exports) {
module.exports = factory(
require("react/addons"),
require("swiper")
);
} else {
root.ReactPhotoSwiper = factory(
root.React,
root.Swiper
);
}
})(this, function (React, Swiper) {
var styles = {
container: {
},
wrapper: {
alignItems: "center"
},
child: {
}
};
var defaults = {
screenHeight: 1080,
threshold: 300,
minSpeed: 0.5
};
var ReactPhotoSwiper = React.createClass({
// http://www.idangero.us/swiper/api
propTypes: {
simulateTouch: React.PropTypes.bool,
initialSlide: React.PropTypes.number,
preloadImages: React.PropTypes.bool,
lazyLoading: React.PropTypes.bool,
lazyLoadingInPrevNext: React.PropTypes.bool,
lazyLoadingOnTransitionStart: React.PropTypes.bool
},
componentDidMount: function () {
if (this.isMounted()) {
var verticalSwiper = Swiper(this.getDOMNode(), { direction: "vertical" });
this.swiper = Swiper(".swiper-container-h", this.props);
this.bindSwiperEvents(verticalSwiper);
}
},
componentWillUnmount: function () {
this.swiper.destroy();
delete this.swiper;
},
shouldComponentUpdate: function (nextProps) {
return (typeof this.props.shouldUpdate !== "undefined") && this.props.shouldUpdate(nextProps);
},
render: function() {
return React.createElement("div", { className: "swiper-container swiper-container-v" },
React.createElement("div", { className: "swiper-wrapper" },
React.createElement("div", { className: "swiper-slide" },
React.createElement("div", React.__spread({}, this.props, {style: styles.container, className: "swiper-container swiper-container-h"}),
React.createElement("div", {style: styles.wrapper, className: "swiper-wrapper"},
React.Children.map(this.props.children, function (child) {
return React.addons.cloneWithProps(child, { style: styles.child, className: "swiper-slide" });
})
)
))));
},
bindSwiperEvents: function(swiperInstance) {
var startTouchPoint = null;
var isCloseMove = false;
var startTime = 0;
var touchStarted = false;
var touchInProgress = false;
swiperInstance.on("touchStart", function(swiper, event) {
startTouchPoint = this.getPointFromEvent(event);
startTime = event.timeStamp;
touchStarted = true;
}.bind(this));
swiperInstance.on("touchMove", function(swiper, event) {
if (!touchStarted) { return; }
var movePoint = this.getPointFromEvent(event);
if (!touchInProgress) {
isCloseMove = this.detectCloseMove(startTouchPoint, movePoint);
touchInProgress = true;
}
if (isCloseMove && this.props.onclosemove) {
var dy = Math.abs(movePoint.y - startTouchPoint.y);
var height = screen ? screen.height : defaults.screenHeight;
this.props.onclosemove(dy / height);
}
}.bind(this));
swiperInstance.on("touchEnd", function(swiper, event) {
if (this.props.onclosemove) {
this.props.onclosemove(0);
}
if (!this.props.onclose) { return; }
if (!isCloseMove) { return; }
var endPoint = this.getPointFromEvent(event);
var endTime = event.timeStamp;
if (this.detectCloseGesture(startTouchPoint, endPoint, startTime, endTime)) {
this.props.onclose();
}
isCloseMove = false;
touchStarted = false;
touchInProgress = false;
}.bind(this));
},
detectCloseGesture: function(startPoint, endPoint, startTime, endTime) {
var threshold = screen ? screen.height / 2 : defaults.threshold;
var dx = endPoint.x - startPoint.x;
var dy = endPoint.y - startPoint.y;
var dt = endTime - startTime;
var speed = Math.abs(dy) / dt;
return Math.abs(dy) > Math.abs(dx) && (Math.abs(dy) > threshold || speed > defaults.minSpeed);
},
detectCloseMove: function(startPoint, movePoint) {
var dx = movePoint.x - startPoint.x;
var dy = movePoint.y - startPoint.y;
return Math.abs(dy) > Math.abs(dx);
},
getPointFromEvent: function(event) {
var screenX = null;
var screenY = null;
if (event instanceof MouseEvent) {
screenX = event.screenX;
screenY = event.screenY;
} else if (event instanceof TouchEvent) {
screenX = event.changedTouches[0].screenX;
screenY = event.changedTouches[0].screenY;
}
return { x: screenX, y: screenY };
}
});
return ReactPhotoSwiper;
});