react-slideshow-image 3.7.0 → 3.7.3

Sign up to get free protection for your applications and to get access to all the features.
package/lib/slide.js DELETED
@@ -1,567 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
-
8
- var _react = _interopRequireWildcard(require("react"));
9
-
10
- var _tween = _interopRequireDefault(require("@tweenjs/tween.js"));
11
-
12
- var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));
13
-
14
- var _props = require("./props");
15
-
16
- var _helpers = require("./helpers.js");
17
-
18
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
19
-
20
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
21
-
22
- function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
23
-
24
- function _extends() { _extends = Object.assign || 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); }
25
-
26
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27
-
28
- 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); } }
29
-
30
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
31
-
32
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
33
-
34
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
35
-
36
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
37
-
38
- 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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }
39
-
40
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
41
-
42
- var Slideshow =
43
- /*#__PURE__*/
44
- function (_Component) {
45
- _inherits(Slideshow, _Component);
46
-
47
- function Slideshow(props) {
48
- var _this;
49
-
50
- _classCallCheck(this, Slideshow);
51
-
52
- _this = _possibleConstructorReturn(this, _getPrototypeOf(Slideshow).call(this));
53
- _this.state = {
54
- slidesToShow: props.slidesToShow || 1,
55
- index: props.defaultIndex && props.defaultIndex < props.children.length ? props.defaultIndex : 0
56
- };
57
- _this.width = 0;
58
- _this.dragging = false;
59
- _this.imageContainer = null;
60
- _this.wrapper = null;
61
- _this.timeout = null;
62
- _this.moveSlides = _this.moveSlides.bind(_assertThisInitialized(_this));
63
- _this.pauseSlides = _this.pauseSlides.bind(_assertThisInitialized(_this));
64
- _this.startSlides = _this.startSlides.bind(_assertThisInitialized(_this));
65
- _this.handleResize = _this.handleResize.bind(_assertThisInitialized(_this));
66
- _this.initResizeObserver = _this.initResizeObserver.bind(_assertThisInitialized(_this));
67
- _this.reactSlideshowWrapper = (0, _react.createRef)();
68
- _this.goToSlide = _this.goToSlide.bind(_assertThisInitialized(_this));
69
- _this.tweenGroup = new _tween["default"].Group();
70
- _this.startSwipe = _this.startSwipe.bind(_assertThisInitialized(_this));
71
- _this.endSwipe = _this.endSwipe.bind(_assertThisInitialized(_this));
72
- _this.swipe = _this.swipe.bind(_assertThisInitialized(_this));
73
- _this.distanceSwiped = 0;
74
- return _this;
75
- }
76
-
77
- _createClass(Slideshow, [{
78
- key: "componentDidMount",
79
- value: function componentDidMount() {
80
- var _this2 = this;
81
-
82
- this.setWidth();
83
- this.initResizeObserver();
84
- (0, _props.validatePropTypes)(this.props);
85
-
86
- var _getProps = (0, _props.getProps)(this.props),
87
- autoplay = _getProps.autoplay,
88
- duration = _getProps.duration;
89
-
90
- if (autoplay) {
91
- this.timeout = setTimeout(function () {
92
- return _this2.goNext();
93
- }, duration);
94
- }
95
- }
96
- }, {
97
- key: "initResizeObserver",
98
- value: function initResizeObserver() {
99
- var _this3 = this;
100
-
101
- this.resizeObserver = new _resizeObserverPolyfill["default"](function (entries) {
102
- if (!entries) return;
103
-
104
- _this3.handleResize();
105
- });
106
-
107
- if (this.reactSlideshowWrapper.current) {
108
- this.resizeObserver.observe(this.reactSlideshowWrapper.current);
109
- }
110
- }
111
- }, {
112
- key: "componentWillUnmount",
113
- value: function componentWillUnmount() {
114
- this.willUnmount = true;
115
- clearTimeout(this.timeout);
116
- this.removeResizeObserver();
117
- }
118
- }, {
119
- key: "startSwipe",
120
- value: function startSwipe(e) {
121
- var _getProps2 = (0, _props.getProps)(this.props),
122
- canSwipe = _getProps2.canSwipe;
123
-
124
- if (canSwipe) {
125
- this.startingClientX = e.touches ? e.touches[0].pageX : e.clientX;
126
- clearTimeout(this.timeout);
127
- this.dragging = true;
128
- }
129
- }
130
- }, {
131
- key: "endSwipe",
132
- value: function endSwipe() {
133
- var _getProps3 = (0, _props.getProps)(this.props),
134
- canSwipe = _getProps3.canSwipe;
135
-
136
- if (canSwipe) {
137
- this.dragging = false;
138
-
139
- if (Math.abs(this.distanceSwiped) / this.width > 0.2) {
140
- if (this.distanceSwiped < 0) {
141
- this.goNext();
142
- } else {
143
- this.goBack();
144
- }
145
- } else {
146
- if (Math.abs(this.distanceSwiped) > 0) {
147
- this.slideImages(this.state.index, 300);
148
- }
149
- }
150
- }
151
- }
152
- }, {
153
- key: "swipe",
154
- value: function swipe(e) {
155
- var _getProps4 = (0, _props.getProps)(this.props),
156
- canSwipe = _getProps4.canSwipe,
157
- slidesToShow = _getProps4.slidesToShow,
158
- children = _getProps4.children,
159
- infinite = _getProps4.infinite,
160
- slidesToScroll = _getProps4.slidesToScroll;
161
-
162
- if (canSwipe) {
163
- var clientX = e.touches ? e.touches[0].pageX : e.clientX;
164
-
165
- if (this.dragging) {
166
- var translateValue = this.width * (this.state.index + this.getOffset(infinite, slidesToShow));
167
- var distance = clientX - this.startingClientX;
168
-
169
- if (!infinite && this.state.index === children.length - slidesToScroll && distance < 0) {
170
- // if it is the last and infinite is false and you're swiping left
171
- // then nothing happens
172
- return;
173
- }
174
-
175
- if (!infinite && this.state.index === 0 && distance > 0) {
176
- // if it is the first and infinite is false and you're swiping right
177
- // then nothing happens
178
- return;
179
- }
180
-
181
- this.distanceSwiped = distance;
182
- translateValue -= this.distanceSwiped;
183
- this.imageContainer.style.transform = "translate(-".concat(translateValue, "px)");
184
- }
185
- }
186
- }
187
- }, {
188
- key: "removeResizeObserver",
189
- value: function removeResizeObserver() {
190
- if (this.resizeObserver && this.reactSlideshowWrapper && this.reactSlideshowWrapper.current) {
191
- this.resizeObserver.unobserve(this.reactSlideshowWrapper.current);
192
- }
193
- }
194
- }, {
195
- key: "setWidth",
196
- value: function setWidth() {
197
- // the .slice.call was needed to support ie11
198
- this.allImages = this.wrapper && Array.prototype.slice.call(this.wrapper.querySelectorAll(".images-wrap > div"), 0) || [];
199
-
200
- var _getProps5 = (0, _props.getProps)(this.props),
201
- slidesToShow = _getProps5.slidesToShow,
202
- infinite = _getProps5.infinite;
203
-
204
- if (this.state.slidesToShow !== slidesToShow) {
205
- this.setState({
206
- slidesToShow: slidesToShow,
207
- index: 0
208
- });
209
- }
210
-
211
- this.width = (this.wrapper && this.wrapper.clientWidth || 0) / slidesToShow;
212
-
213
- var numberOfSlides = _react["default"].Children.count(this.props.children);
214
-
215
- var fullwidth = this.width * (numberOfSlides + slidesToShow * 2);
216
-
217
- if (this.imageContainer) {
218
- this.imageContainer.style.width = "".concat(fullwidth, "px");
219
- this.imageContainer.style.transform = "translate(-".concat(this.width * (this.state.index + this.getOffset(infinite, slidesToShow)), "px)");
220
- }
221
-
222
- this.applySlideStyle();
223
- }
224
- }, {
225
- key: "componentDidUpdate",
226
- value: function componentDidUpdate(props) {
227
- var _this4 = this;
228
-
229
- var _getProps6 = (0, _props.getProps)(this.props),
230
- autoplay = _getProps6.autoplay,
231
- duration = _getProps6.duration,
232
- children = _getProps6.children;
233
-
234
- var newProps = (0, _props.getProps)(props);
235
-
236
- if (autoplay !== newProps.autoplay) {
237
- if (autoplay) {
238
- this.timeout = setTimeout(function () {
239
- return _this4.goNext();
240
- }, duration);
241
- } else {
242
- clearTimeout(this.timeout);
243
- }
244
- }
245
-
246
- if (children.length != newProps.children.length) {
247
- this.setWidth();
248
- clearTimeout(this.timeout);
249
- this.timeout = setTimeout(function () {
250
- return _this4.goNext();
251
- }, duration);
252
- }
253
- }
254
- }, {
255
- key: "handleResize",
256
- value: function handleResize() {
257
- this.setWidth();
258
- }
259
- }, {
260
- key: "applySlideStyle",
261
- value: function applySlideStyle() {
262
- var _this5 = this;
263
-
264
- this.allImages.forEach(function (eachImage, index) {
265
- eachImage.style.width = "".concat(_this5.width, "px");
266
- });
267
- }
268
- }, {
269
- key: "pauseSlides",
270
- value: function pauseSlides() {
271
- if ((0, _props.getProps)(this.props).pauseOnHover) {
272
- clearTimeout(this.timeout);
273
- }
274
- }
275
- }, {
276
- key: "startSlides",
277
- value: function startSlides() {
278
- var _this6 = this;
279
-
280
- var _getProps7 = (0, _props.getProps)(this.props),
281
- pauseOnHover = _getProps7.pauseOnHover,
282
- autoplay = _getProps7.autoplay,
283
- duration = _getProps7.duration;
284
-
285
- if (this.dragging) {
286
- this.endSwipe();
287
- } else {
288
- if (pauseOnHover && autoplay) {
289
- this.timeout = setTimeout(function () {
290
- return _this6.goNext();
291
- }, duration);
292
- }
293
- }
294
- }
295
- }, {
296
- key: "moveSlides",
297
- value: function moveSlides(_ref) {
298
- var dataset = _ref.currentTarget.dataset;
299
-
300
- if (dataset.type === 'next') {
301
- this.goNext();
302
- } else {
303
- this.goBack();
304
- }
305
- }
306
- }, {
307
- key: "goToSlide",
308
- value: function goToSlide(_ref2) {
309
- var currentTarget = _ref2.currentTarget;
310
-
311
- var _getProps8 = (0, _props.getProps)(this.props),
312
- slidesToScroll = _getProps8.slidesToScroll;
313
-
314
- this.goTo(parseInt(currentTarget.dataset.key * slidesToScroll));
315
- }
316
- }, {
317
- key: "goTo",
318
- value: function goTo(index) {
319
- this.slideImages(this.calculateIndex(index));
320
- }
321
- }, {
322
- key: "calculateIndex",
323
- value: function calculateIndex(nextIndex) {
324
- var _getProps9 = (0, _props.getProps)(this.props),
325
- children = _getProps9.children,
326
- slidesToScroll = _getProps9.slidesToScroll;
327
-
328
- if (nextIndex < children.length && nextIndex + slidesToScroll > children.length) {
329
- if ((children.length - slidesToScroll) % slidesToScroll) {
330
- return children.length - slidesToScroll;
331
- }
332
-
333
- return nextIndex;
334
- }
335
-
336
- return nextIndex;
337
- }
338
- }, {
339
- key: "goNext",
340
- value: function goNext() {
341
- var index = this.state.index;
342
-
343
- var _getProps10 = (0, _props.getProps)(this.props),
344
- children = _getProps10.children,
345
- infinite = _getProps10.infinite,
346
- slidesToScroll = _getProps10.slidesToScroll;
347
-
348
- if (!infinite && index === children.length - slidesToScroll) {
349
- return;
350
- }
351
-
352
- var nextIndex = this.calculateIndex(index + slidesToScroll);
353
- this.slideImages(nextIndex);
354
- }
355
- }, {
356
- key: "goBack",
357
- value: function goBack() {
358
- var index = this.state.index;
359
-
360
- var _getProps11 = (0, _props.getProps)(this.props),
361
- infinite = _getProps11.infinite,
362
- slidesToScroll = _getProps11.slidesToScroll;
363
-
364
- if (!infinite && index === 0) {
365
- return;
366
- }
367
-
368
- var previousIndex = index - slidesToScroll;
369
-
370
- if (previousIndex % slidesToScroll) {
371
- previousIndex = Math.ceil(previousIndex / slidesToScroll) * slidesToScroll;
372
- }
373
-
374
- this.slideImages(previousIndex);
375
- }
376
- }, {
377
- key: "isSlideActive",
378
- value: function isSlideActive(key) {
379
- var _getProps12 = (0, _props.getProps)(this.props),
380
- slidesToShow = _getProps12.slidesToShow;
381
-
382
- return key < this.state.index + slidesToShow && key >= this.state.index;
383
- }
384
- }, {
385
- key: "renderPreceedingSlides",
386
- value: function renderPreceedingSlides(children, slidesToShow) {
387
- return children.slice(-slidesToShow).map(function (each, index) {
388
- return _react["default"].createElement("div", {
389
- "data-index": index - slidesToShow,
390
- "aria-roledescription": "slide",
391
- "aria-hidden": "true",
392
- key: index - slidesToShow
393
- }, each);
394
- });
395
- }
396
- }, {
397
- key: "renderTrailingSlides",
398
- value: function renderTrailingSlides() {
399
- var _getProps13 = (0, _props.getProps)(this.props),
400
- children = _getProps13.children,
401
- slidesToShow = _getProps13.slidesToShow,
402
- slidesToScroll = _getProps13.slidesToScroll,
403
- infinite = _getProps13.infinite;
404
-
405
- if (!infinite && slidesToShow === slidesToScroll) {
406
- return;
407
- }
408
-
409
- return children.slice(0, slidesToShow).map(function (each, index) {
410
- return _react["default"].createElement("div", {
411
- "data-index": children.length + index,
412
- "aria-roledescription": "slide",
413
- "aria-hidden": "true",
414
- key: children.length + index
415
- }, each);
416
- });
417
- }
418
- }, {
419
- key: "getOffset",
420
- value: function getOffset(infinite, slidesToShow) {
421
- if (!infinite) {
422
- return 0;
423
- }
424
-
425
- return slidesToShow;
426
- }
427
- }, {
428
- key: "render",
429
- value: function render() {
430
- var _this7 = this;
431
-
432
- var _getProps14 = (0, _props.getProps)(this.props),
433
- children = _getProps14.children,
434
- indicators = _getProps14.indicators,
435
- arrows = _getProps14.arrows,
436
- cssClass = _getProps14.cssClass,
437
- slidesToShow = _getProps14.slidesToShow,
438
- infinite = _getProps14.infinite;
439
-
440
- var unhandledProps = (0, _helpers.getUnhandledProps)(_props.propTypes, this.props);
441
- var index = this.state.index;
442
- var style = {
443
- transform: "translate(-".concat((index + this.getOffset(infinite, slidesToShow)) * this.width, "px)")
444
- };
445
- return _react["default"].createElement("div", _extends({
446
- dir: "ltr",
447
- "aria-roledescription": "carousel"
448
- }, unhandledProps), _react["default"].createElement("div", {
449
- className: "react-slideshow-container",
450
- onMouseEnter: this.pauseSlides,
451
- onMouseOver: this.pauseSlides,
452
- onMouseLeave: this.startSlides,
453
- onMouseDown: this.startSwipe,
454
- onMouseUp: this.endSwipe,
455
- onMouseMove: this.swipe,
456
- onTouchStart: this.startSwipe,
457
- onTouchEnd: this.endSwipe,
458
- onTouchCancel: this.endSwipe,
459
- onTouchMove: this.swipe,
460
- ref: this.reactSlideshowWrapper
461
- }, arrows && (0, _helpers.showPreviousArrow)((0, _props.getProps)(this.props), this.state.index, this.moveSlides), _react["default"].createElement("div", {
462
- className: "react-slideshow-wrapper slide ".concat(cssClass),
463
- ref: function ref(_ref4) {
464
- return _this7.wrapper = _ref4;
465
- }
466
- }, _react["default"].createElement("div", {
467
- className: "images-wrap",
468
- style: style,
469
- ref: function ref(_ref3) {
470
- return _this7.imageContainer = _ref3;
471
- }
472
- }, infinite ? this.renderPreceedingSlides(children, slidesToShow) : '', children.map(function (each, key) {
473
- var isSlideActive = _this7.isSlideActive(key);
474
-
475
- return _react["default"].createElement("div", {
476
- "data-index": key,
477
- key: key,
478
- className: isSlideActive ? 'active' : '',
479
- "aria-roledescription": "slide",
480
- "aria-hidden": isSlideActive ? 'false' : 'true'
481
- }, each);
482
- }), this.renderTrailingSlides())), arrows && (0, _helpers.showNextArrow)((0, _props.getProps)(this.props), this.state.index, this.moveSlides)), indicators && (0, _helpers.showIndicators)((0, _props.getProps)(this.props), this.state.index, this.goToSlide));
483
- }
484
- }, {
485
- key: "slideImages",
486
- value: function slideImages(index, animationDuration) {
487
- var _this8 = this;
488
-
489
- var _getProps15 = (0, _props.getProps)(this.props),
490
- children = _getProps15.children,
491
- transitionDuration = _getProps15.transitionDuration,
492
- autoplay = _getProps15.autoplay,
493
- infinite = _getProps15.infinite,
494
- duration = _getProps15.duration,
495
- onChange = _getProps15.onChange,
496
- easing = _getProps15.easing,
497
- slidesToShow = _getProps15.slidesToShow,
498
- slidesToScroll = _getProps15.slidesToScroll;
499
-
500
- transitionDuration = animationDuration || transitionDuration;
501
- var existingTweens = this.tweenGroup.getAll();
502
-
503
- if (!existingTweens.length) {
504
- clearTimeout(this.timeout);
505
- var value = {
506
- margin: -this.width * (this.state.index + this.getOffset(infinite, slidesToShow)) + this.distanceSwiped
507
- };
508
- var tween = new _tween["default"].Tween(value, this.tweenGroup).to({
509
- margin: -this.width * (index + this.getOffset(infinite, slidesToShow))
510
- }, transitionDuration).onUpdate(function (value) {
511
- if (_this8.imageContainer) {
512
- _this8.imageContainer.style.transform = "translate(".concat(value.margin, "px)");
513
- }
514
- }).start();
515
- tween.easing((0, _helpers.getEasing)(easing));
516
-
517
- var animate = function animate() {
518
- if (_this8.willUnmount) {
519
- _this8.tweenGroup.removeAll();
520
-
521
- return;
522
- }
523
-
524
- requestAnimationFrame(animate);
525
-
526
- _this8.tweenGroup.update();
527
- };
528
-
529
- animate();
530
- tween.onComplete(function () {
531
- if (_this8.willUnmount) {
532
- return;
533
- }
534
-
535
- _this8.distanceSwiped = 0;
536
- var newIndex = index;
537
-
538
- if (newIndex < 0) {
539
- newIndex = children.length - slidesToScroll;
540
- } else if (newIndex >= children.length) {
541
- newIndex = 0;
542
- }
543
-
544
- if (typeof onChange === 'function') {
545
- onChange(_this8.state.index, newIndex);
546
- }
547
-
548
- _this8.setState({
549
- index: newIndex
550
- }, function () {
551
- if (autoplay && (infinite || _this8.state.index < children.length)) {
552
- clearTimeout(_this8.timeout);
553
- _this8.timeout = setTimeout(function () {
554
- return _this8.goNext();
555
- }, duration);
556
- }
557
- });
558
- });
559
- }
560
- }
561
- }]);
562
-
563
- return Slideshow;
564
- }(_react.Component);
565
-
566
- var _default = Slideshow;
567
- exports["default"] = _default;