UNPKG

8.12 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
10
11var _react = require('react');
12
13var _react2 = _interopRequireDefault(_react);
14
15var _server = require('react-dom/server');
16
17var _server2 = _interopRequireDefault(_server);
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
24
25function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
26
27var NO_OP = function NO_OP() {};
28
29// in order to make react-slidy compatible with server-rendering
30// by default slidy and imagesLoaded are empty functions
31var slidy = NO_OP;
32var imagesLoaded = NO_OP;
33
34// if window is present, then we get the needed library
35if (typeof window !== 'undefined' && window.document) {
36 slidy = require('./slidy/slidy.js').slidy;
37 imagesLoaded = require('imagesloaded');
38}
39
40var ReactSlidySlider = function (_Component) {
41 _inherits(ReactSlidySlider, _Component);
42
43 function ReactSlidySlider() {
44 var _ref;
45
46 _classCallCheck(this, ReactSlidySlider);
47
48 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
49 args[_key] = arguments[_key];
50 }
51
52 var _this = _possibleConstructorReturn(this, (_ref = ReactSlidySlider.__proto__ || Object.getPrototypeOf(ReactSlidySlider)).call.apply(_ref, [this].concat(args)));
53
54 _this.getFrameNode = _this.getFrameNode.bind(_this);
55 _this.getSliderNode = _this.getSliderNode.bind(_this);
56 _this.handleAfterSlide = _this.handleAfterSlide.bind(_this);
57 _this.nextSlider = _this.nextSlider.bind(_this);
58 _this.prevSlider = _this.prevSlider.bind(_this);
59 _this.slidyInstance = null;
60 _this.DOM = {};
61
62 _this.classes = {
63 classNameItem: _this.getClassName('item'),
64 classNameFrame: _this.getClassName('frame'),
65 classNameSlideContainer: _this.getClassName('slides'),
66 classNamePrevCtrl: _this.getClassName('prev'),
67 classNameNextCtrl: _this.getClassName('next')
68 };
69
70 var children = _this.props.children;
71
72 _this.listItems = Array.isArray(children) ? children : [children];
73
74 var sliderItems = _react2.default.Children.map(_this.listItems, function (child) {
75 return _server2.default.renderToStaticMarkup(child);
76 });
77
78 _this.sliderOptions = _extends({}, _this.classes, {
79 items: sliderItems,
80 doAfterSlide: _this.handleAfterSlide,
81 // fix if the user try to use a `true` value for infinite
82 infinite: _this.props.infinite === true ? 1 : _this.props.infinite,
83 // if infinite, rewindOnResize is always true
84 rewindOnResize: _this.props.rewindOnResize || _this.props.infinite
85 });
86 return _this;
87 }
88
89 _createClass(ReactSlidySlider, [{
90 key: 'componentDidMount',
91 value: function componentDidMount() {
92 var _this2 = this;
93
94 // wait to load the images in order to start some stuff only when needed
95 imagesLoaded(this.DOM['slider'], function () {
96 var slidyOptions = _extends({}, _this2.props, _this2.sliderOptions, {
97 frameDOMEl: _this2.DOM['frame']
98 });
99 // start slidy slider instance
100 _this2.slidyInstance = slidy(_this2.DOM['slider'], slidyOptions);
101 });
102 }
103 }, {
104 key: 'componentWillUnmount',
105 value: function componentWillUnmount() {
106 this.slidyInstance && this.slidyInstance.destroy();
107 }
108 }, {
109 key: 'shouldComponentUpdate',
110 value: function shouldComponentUpdate(nextProps) {
111 return false;
112 }
113 }, {
114 key: 'getClassName',
115 value: function getClassName(element) {
116 return this.props.classNameBase + '-' + element;
117 }
118 }, {
119 key: 'getFrameNode',
120 value: function getFrameNode(node) {
121 this.DOM['frame'] = node;
122 }
123 }, {
124 key: 'getSliderNode',
125 value: function getSliderNode(node) {
126 this.DOM['slider'] = node;
127 }
128 }, {
129 key: 'handleAfterSlide',
130 value: function handleAfterSlide(_ref2) {
131 var currentSlide = _ref2.currentSlide;
132
133 this.props.doAfterSlide({ currentSlide: currentSlide });
134 }
135 }, {
136 key: 'nextSlider',
137 value: function nextSlider(e) {
138 e.preventDefault();
139 this.slidyInstance.next();
140 }
141 }, {
142 key: 'prevSlider',
143 value: function prevSlider(e) {
144 e.preventDefault();
145 this.slidyInstance.prev();
146 }
147 }, {
148 key: 'renderItems',
149 value: function renderItems() {
150 var _this3 = this;
151
152 return this.listItems.slice(0, 2).map(function (item, index) {
153 return _react2.default.createElement(
154 'li',
155 { key: index, className: _this3.sliderOptions.classNameItem },
156 item
157 );
158 });
159 }
160 }, {
161 key: 'render',
162 value: function render() {
163 var showArrows = this.props.showArrows;
164
165
166 return _react2.default.createElement(
167 'div',
168 { ref: this.getSliderNode },
169 _react2.default.createElement(
170 'div',
171 { ref: this.getFrameNode, className: this.sliderOptions.classNameFrame },
172 showArrows && _react2.default.createElement('span', { className: this.classes.classNamePrevCtrl, onClick: this.prevSlider }),
173 showArrows && _react2.default.createElement('span', { className: this.classes.classNameNextCtrl, onClick: this.nextSlider }),
174 _react2.default.createElement(
175 'ul',
176 { className: this.sliderOptions.classNameSlideContainer },
177 this.renderItems()
178 )
179 )
180 );
181 }
182 }]);
183
184 return ReactSlidySlider;
185}(_react.Component);
186
187exports.default = ReactSlidySlider;
188
189
190ReactSlidySlider.propTypes = {
191 children: _react.PropTypes.oneOfType([_react.PropTypes.array, _react.PropTypes.object]).isRequired,
192 classNameBase: _react.PropTypes.string,
193 doAfterSlide: _react.PropTypes.func,
194 ease: _react.PropTypes.string,
195 enableMouseEvents: _react.PropTypes.bool,
196 infinite: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.number]),
197 onReady: _react.PropTypes.func,
198 rewind: _react.PropTypes.bool,
199 rewindOnResize: _react.PropTypes.bool,
200 rewindSpeed: _react.PropTypes.number,
201 showArrows: _react.PropTypes.bool,
202 slideSpeed: _react.PropTypes.number,
203 slidesToScroll: _react.PropTypes.number,
204 snapBackSpeed: _react.PropTypes.number
205};
206
207ReactSlidySlider.defaultProps = {
208 doAfterSlide: NO_OP,
209 ease: 'ease',
210 enableMouseEvents: true,
211 infinite: 1,
212 onReady: NO_OP,
213 rewind: false,
214 rewindOnResize: false,
215 rewindSpeed: 500,
216 showArrows: true,
217 slideSpeed: 500,
218 slidesToScroll: 1,
219 snapBackSpeed: 500
220};
\No newline at end of file