1 | function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
2 | function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
3 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
4 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
5 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
6 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
7 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
8 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
9 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
10 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
11 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
12 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
13 | import React from 'react';
|
14 | import PropTypes from 'prop-types';
|
15 | import classNames from 'classnames';
|
16 | import CarouselItem from './CarouselItem';
|
17 | import { CarouselContext } from './CarouselContext';
|
18 | import { mapToCssModules, omit } from './utils';
|
19 | var SWIPE_THRESHOLD = 40;
|
20 | var propTypes = {
|
21 |
|
22 | activeIndex: PropTypes.number,
|
23 |
|
24 | next: PropTypes.func.isRequired,
|
25 |
|
26 | previous: PropTypes.func.isRequired,
|
27 |
|
28 | keyboard: PropTypes.bool,
|
29 | |
30 |
|
31 |
|
32 | pause: PropTypes.oneOf(['hover', false]),
|
33 |
|
34 | ride: PropTypes.oneOf(['carousel']),
|
35 |
|
36 | interval: PropTypes.oneOfType([PropTypes.number, PropTypes.string, PropTypes.bool]),
|
37 | children: PropTypes.array,
|
38 |
|
39 | mouseEnter: PropTypes.func,
|
40 |
|
41 | mouseLeave: PropTypes.func,
|
42 |
|
43 | slide: PropTypes.bool,
|
44 |
|
45 | dark: PropTypes.bool,
|
46 | fade: PropTypes.bool,
|
47 |
|
48 | cssModule: PropTypes.object,
|
49 |
|
50 | className: PropTypes.string,
|
51 |
|
52 | enableTouch: PropTypes.bool
|
53 | };
|
54 | var propsToOmit = Object.keys(propTypes);
|
55 | var defaultProps = {
|
56 | interval: 5000,
|
57 | pause: 'hover',
|
58 | keyboard: true,
|
59 | slide: true,
|
60 | enableTouch: true,
|
61 | fade: false
|
62 | };
|
63 | var Carousel = function (_React$Component) {
|
64 | _inherits(Carousel, _React$Component);
|
65 | var _super = _createSuper(Carousel);
|
66 | function Carousel(props) {
|
67 | var _this;
|
68 | _classCallCheck(this, Carousel);
|
69 | _this = _super.call(this, props);
|
70 | _this.handleKeyPress = _this.handleKeyPress.bind(_assertThisInitialized(_this));
|
71 | _this.renderItems = _this.renderItems.bind(_assertThisInitialized(_this));
|
72 | _this.hoverStart = _this.hoverStart.bind(_assertThisInitialized(_this));
|
73 | _this.hoverEnd = _this.hoverEnd.bind(_assertThisInitialized(_this));
|
74 | _this.handleTouchStart = _this.handleTouchStart.bind(_assertThisInitialized(_this));
|
75 | _this.handleTouchEnd = _this.handleTouchEnd.bind(_assertThisInitialized(_this));
|
76 | _this.touchStartX = 0;
|
77 | _this.touchStartY = 0;
|
78 | _this.state = {
|
79 | activeIndex: _this.props.activeIndex,
|
80 | direction: 'end',
|
81 | indicatorClicked: false
|
82 | };
|
83 | return _this;
|
84 | }
|
85 | _createClass(Carousel, [{
|
86 | key: "componentDidMount",
|
87 | value: function componentDidMount() {
|
88 |
|
89 | if (this.props.ride === 'carousel') {
|
90 | this.setInterval();
|
91 | }
|
92 |
|
93 |
|
94 | document.addEventListener('keyup', this.handleKeyPress);
|
95 | }
|
96 | }, {
|
97 | key: "componentDidUpdate",
|
98 | value: function componentDidUpdate(prevProps, prevState) {
|
99 | if (prevState.activeIndex === this.state.activeIndex) return;
|
100 | this.setInterval();
|
101 | }
|
102 | }, {
|
103 | key: "componentWillUnmount",
|
104 | value: function componentWillUnmount() {
|
105 | this.clearInterval();
|
106 | document.removeEventListener('keyup', this.handleKeyPress);
|
107 | }
|
108 | }, {
|
109 | key: "handleKeyPress",
|
110 | value: function handleKeyPress(evt) {
|
111 | if (this.props.keyboard) {
|
112 | if (evt.keyCode === 37) {
|
113 | this.props.previous();
|
114 | } else if (evt.keyCode === 39) {
|
115 | this.props.next();
|
116 | }
|
117 | }
|
118 | }
|
119 | }, {
|
120 | key: "handleTouchStart",
|
121 | value: function handleTouchStart(e) {
|
122 | if (!this.props.enableTouch) {
|
123 | return;
|
124 | }
|
125 | this.touchStartX = e.changedTouches[0].screenX;
|
126 | this.touchStartY = e.changedTouches[0].screenY;
|
127 | }
|
128 | }, {
|
129 | key: "handleTouchEnd",
|
130 | value: function handleTouchEnd(e) {
|
131 | if (!this.props.enableTouch) {
|
132 | return;
|
133 | }
|
134 | var currentX = e.changedTouches[0].screenX;
|
135 | var currentY = e.changedTouches[0].screenY;
|
136 | var diffX = Math.abs(this.touchStartX - currentX);
|
137 | var diffY = Math.abs(this.touchStartY - currentY);
|
138 |
|
139 |
|
140 | if (diffX < diffY) {
|
141 | return;
|
142 | }
|
143 | if (diffX < SWIPE_THRESHOLD) {
|
144 | return;
|
145 | }
|
146 | if (currentX < this.touchStartX) {
|
147 | this.props.next();
|
148 | } else {
|
149 | this.props.previous();
|
150 | }
|
151 | }
|
152 | }, {
|
153 | key: "getContextValue",
|
154 | value: function getContextValue() {
|
155 | return {
|
156 | direction: this.state.direction
|
157 | };
|
158 | }
|
159 | }, {
|
160 | key: "setInterval",
|
161 | value: function (_setInterval) {
|
162 | function setInterval() {
|
163 | return _setInterval.apply(this, arguments);
|
164 | }
|
165 | setInterval.toString = function () {
|
166 | return _setInterval.toString();
|
167 | };
|
168 | return setInterval;
|
169 | }(function () {
|
170 | var _this2 = this;
|
171 |
|
172 | this.clearInterval();
|
173 | if (this.props.interval) {
|
174 | this.cycleInterval = setInterval(function () {
|
175 | _this2.props.next();
|
176 | }, parseInt(this.props.interval, 10));
|
177 | }
|
178 | })
|
179 | }, {
|
180 | key: "clearInterval",
|
181 | value: function (_clearInterval) {
|
182 | function clearInterval() {
|
183 | return _clearInterval.apply(this, arguments);
|
184 | }
|
185 | clearInterval.toString = function () {
|
186 | return _clearInterval.toString();
|
187 | };
|
188 | return clearInterval;
|
189 | }(function () {
|
190 | clearInterval(this.cycleInterval);
|
191 | })
|
192 | }, {
|
193 | key: "hoverStart",
|
194 | value: function hoverStart() {
|
195 | if (this.props.pause === 'hover') {
|
196 | this.clearInterval();
|
197 | }
|
198 | if (this.props.mouseEnter) {
|
199 | var _this$props;
|
200 | (_this$props = this.props).mouseEnter.apply(_this$props, arguments);
|
201 | }
|
202 | }
|
203 | }, {
|
204 | key: "hoverEnd",
|
205 | value: function hoverEnd() {
|
206 | if (this.props.pause === 'hover') {
|
207 | this.setInterval();
|
208 | }
|
209 | if (this.props.mouseLeave) {
|
210 | var _this$props2;
|
211 | (_this$props2 = this.props).mouseLeave.apply(_this$props2, arguments);
|
212 | }
|
213 | }
|
214 | }, {
|
215 | key: "renderItems",
|
216 | value: function renderItems(carouselItems, className) {
|
217 | var _this3 = this;
|
218 | var slide = this.props.slide;
|
219 | return React.createElement("div", {
|
220 | className: className
|
221 | }, carouselItems.map(function (item, index) {
|
222 | var isIn = index === _this3.state.activeIndex;
|
223 | return React.cloneElement(item, {
|
224 | "in": isIn,
|
225 | slide: slide
|
226 | });
|
227 | }));
|
228 | }
|
229 | }, {
|
230 | key: "render",
|
231 | value: function render() {
|
232 | var _this4 = this;
|
233 | var _this$props3 = this.props,
|
234 | cssModule = _this$props3.cssModule,
|
235 | slide = _this$props3.slide,
|
236 | className = _this$props3.className,
|
237 | dark = _this$props3.dark,
|
238 | fade = _this$props3.fade;
|
239 | var attributes = omit(this.props, propsToOmit);
|
240 | var outerClasses = mapToCssModules(classNames(className, 'carousel', fade && 'carousel-fade', slide && 'slide', dark && 'carousel-dark'), cssModule);
|
241 | var innerClasses = mapToCssModules(classNames('carousel-inner'), cssModule);
|
242 |
|
243 |
|
244 | var children = this.props.children.filter(function (child) {
|
245 | return child !== null && child !== undefined && typeof child !== 'boolean';
|
246 | });
|
247 | var slidesOnly = children.every(function (child) {
|
248 | return child.type === CarouselItem;
|
249 | });
|
250 |
|
251 |
|
252 | if (slidesOnly) {
|
253 | return React.createElement("div", _extends({}, attributes, {
|
254 | className: outerClasses,
|
255 | onMouseEnter: this.hoverStart,
|
256 | onMouseLeave: this.hoverEnd
|
257 | }), React.createElement(CarouselContext.Provider, {
|
258 | value: this.getContextValue()
|
259 | }, this.renderItems(children, innerClasses)));
|
260 | }
|
261 |
|
262 |
|
263 | if (children[0] instanceof Array) {
|
264 | var _carouselItems = children[0];
|
265 | var _controlLeft = children[1];
|
266 | var _controlRight = children[2];
|
267 | return React.createElement("div", _extends({}, attributes, {
|
268 | className: outerClasses,
|
269 | onMouseEnter: this.hoverStart,
|
270 | onMouseLeave: this.hoverEnd
|
271 | }), React.createElement(CarouselContext.Provider, {
|
272 | value: this.getContextValue()
|
273 | }, this.renderItems(_carouselItems, innerClasses), _controlLeft, _controlRight));
|
274 | }
|
275 |
|
276 |
|
277 | var indicators = children[0];
|
278 | var wrappedOnClick = function wrappedOnClick(e) {
|
279 | if (typeof indicators.props.onClickHandler === 'function') {
|
280 | _this4.setState({
|
281 | indicatorClicked: true
|
282 | }, function () {
|
283 | return indicators.props.onClickHandler(e);
|
284 | });
|
285 | }
|
286 | };
|
287 | var wrappedIndicators = React.cloneElement(indicators, {
|
288 | onClickHandler: wrappedOnClick
|
289 | });
|
290 | var carouselItems = children[1];
|
291 | var controlLeft = children[2];
|
292 | var controlRight = children[3];
|
293 | return React.createElement("div", _extends({}, attributes, {
|
294 | className: outerClasses,
|
295 | onMouseEnter: this.hoverStart,
|
296 | onMouseLeave: this.hoverEnd,
|
297 | onTouchStart: this.handleTouchStart,
|
298 | onTouchEnd: this.handleTouchEnd
|
299 | }), React.createElement(CarouselContext.Provider, {
|
300 | value: this.getContextValue()
|
301 | }, wrappedIndicators, this.renderItems(carouselItems, innerClasses), controlLeft, controlRight));
|
302 | }
|
303 | }], [{
|
304 | key: "getDerivedStateFromProps",
|
305 | value: function getDerivedStateFromProps(nextProps, prevState) {
|
306 | var newState = null;
|
307 | var activeIndex = prevState.activeIndex,
|
308 | direction = prevState.direction,
|
309 | indicatorClicked = prevState.indicatorClicked;
|
310 | if (nextProps.activeIndex !== activeIndex) {
|
311 |
|
312 | if (nextProps.activeIndex === activeIndex + 1) {
|
313 | direction = 'end';
|
314 | } else if (nextProps.activeIndex === activeIndex - 1) {
|
315 | direction = 'start';
|
316 | } else if (nextProps.activeIndex < activeIndex) {
|
317 | direction = indicatorClicked ? 'start' : 'end';
|
318 | } else if (nextProps.activeIndex !== activeIndex) {
|
319 | direction = indicatorClicked ? 'end' : 'start';
|
320 | }
|
321 | newState = {
|
322 | activeIndex: nextProps.activeIndex,
|
323 | direction: direction,
|
324 | indicatorClicked: false
|
325 | };
|
326 | }
|
327 | return newState;
|
328 | }
|
329 | }]);
|
330 | return Carousel;
|
331 | }(React.Component);
|
332 | Carousel.propTypes = propTypes;
|
333 | Carousel.defaultProps = defaultProps;
|
334 | export default Carousel; |
\ | No newline at end of file |