'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _CarouselItem = require('./CarouselItem'); var _CarouselItem2 = _interopRequireDefault(_CarouselItem); var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _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; } function _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; } var Carousel = function (_React$Component) { _inherits(Carousel, _React$Component); function Carousel(props) { _classCallCheck(this, Carousel); var _this = _possibleConstructorReturn(this, (Carousel.__proto__ || Object.getPrototypeOf(Carousel)).call(this, props)); _this.handleKeyPress = _this.handleKeyPress.bind(_this); _this.renderItems = _this.renderItems.bind(_this); _this.hoverStart = _this.hoverStart.bind(_this); _this.hoverEnd = _this.hoverEnd.bind(_this); _this.state = { direction: 'right' }; return _this; } _createClass(Carousel, [{ key: 'getChildContext', value: function getChildContext() { return { direction: this.state.direction }; } }, { key: 'componentDidMount', value: function componentDidMount() { // Set up the cycle if (this.props.ride === 'carousel') { this.setInterval(); } // TODO: move this to the specific carousel like bootstrap. Currently it will trigger ALL carousels on the page. document.addEventListener('keyup', this.handleKeyPress); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { this.setInterval(nextProps); // Calculate the direction to turn if (this.props.activeIndex + 1 === nextProps.activeIndex) { this.setState({ direction: 'right' }); } else if (this.props.activeIndex - 1 === nextProps.activeIndex) { this.setState({ direction: 'left' }); } else if (this.props.activeIndex > nextProps.activeIndex) { this.setState({ direction: 'right' }); } else if (this.props.activeIndex !== nextProps.activeIndex) { this.setState({ direction: 'left' }); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.clearInterval(); document.removeEventListener('keyup', this.handleKeyPress); } }, { key: 'setInterval', value: function (_setInterval) { function setInterval() { return _setInterval.apply(this, arguments); } setInterval.toString = function () { return _setInterval.toString(); }; return setInterval; }(function () { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props; // make sure not to have multiple intervals going... this.clearInterval(); if (props.interval) { this.cycleInterval = setInterval(function () { props.next(); }, parseInt(props.interval, 10)); } }) }, { key: 'clearInterval', value: function (_clearInterval) { function clearInterval() { return _clearInterval.apply(this, arguments); } clearInterval.toString = function () { return _clearInterval.toString(); }; return clearInterval; }(function () { clearInterval(this.cycleInterval); }) }, { key: 'hoverStart', value: function hoverStart() { if (this.props.pause === 'hover') { this.clearInterval(); } if (this.props.mouseEnter) { var _props; (_props = this.props).mouseEnter.apply(_props, arguments); } } }, { key: 'hoverEnd', value: function hoverEnd() { if (this.props.pause === 'hover') { this.setInterval(); } if (this.props.mouseLeave) { var _props2; (_props2 = this.props).mouseLeave.apply(_props2, arguments); } } }, { key: 'handleKeyPress', value: function handleKeyPress(evt) { if (this.props.keyboard) { if (evt.keyCode === 37) { this.props.previous(); } else if (evt.keyCode === 39) { this.props.next(); } } } }, { key: 'renderItems', value: function renderItems(carouselItems, className) { var _this2 = this; var slide = this.props.slide; return _react2.default.createElement( 'div', { role: 'listbox', className: className }, carouselItems.map(function (item, index) { var isIn = index === _this2.props.activeIndex; return _react2.default.cloneElement(item, { in: isIn, slide: slide }); }) ); } }, { key: 'render', value: function render() { var _props3 = this.props, children = _props3.children, cssModule = _props3.cssModule, slide = _props3.slide, className = _props3.className; var outerClasses = (0, _utils.mapToCssModules)((0, _classnames2.default)(className, 'carousel', slide && 'slide'), cssModule); var innerClasses = (0, _utils.mapToCssModules)((0, _classnames2.default)('carousel-inner'), cssModule); var slidesOnly = children.every(function (child) { return child.type === _CarouselItem2.default; }); // Rendering only slides if (slidesOnly) { return _react2.default.createElement( 'div', { className: outerClasses, onMouseEnter: this.hoverStart, onMouseLeave: this.hoverEnd }, this.renderItems(children, innerClasses) ); } // Rendering slides and controls if (children[0] instanceof Array) { var _carouselItems = children[0]; var _controlLeft = children[1]; var _controlRight = children[2]; return _react2.default.createElement( 'div', { className: outerClasses, onMouseEnter: this.hoverStart, onMouseLeave: this.hoverEnd }, this.renderItems(_carouselItems, innerClasses), _controlLeft, _controlRight ); } // Rendering indicators, slides and controls var indicators = children[0]; var carouselItems = children[1]; var controlLeft = children[2]; var controlRight = children[3]; return _react2.default.createElement( 'div', { className: outerClasses, onMouseEnter: this.hoverStart, onMouseLeave: this.hoverEnd }, indicators, this.renderItems(carouselItems, innerClasses), controlLeft, controlRight ); } }]); return Carousel; }(_react2.default.Component); Carousel.propTypes = { // the current active slide of the carousel activeIndex: _propTypes2.default.number, // a function which should advance the carousel to the next slide (via activeIndex) next: _propTypes2.default.func.isRequired, // a function which should advance the carousel to the previous slide (via activeIndex) previous: _propTypes2.default.func.isRequired, // controls if the left and right arrow keys should control the carousel keyboard: _propTypes2.default.bool, /* If set to "hover", pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on * mouseleave. If set to false, hovering over the carousel won't pause it. (default: "hover") */ pause: _propTypes2.default.oneOf(['hover', false]), // Autoplays the carousel after the user manually cycles the first item. If "carousel", autoplays the carousel on load. // This is how bootstrap defines it... I would prefer a bool named autoplay or something... ride: _propTypes2.default.oneOf(['carousel']), // the interval at which the carousel automatically cycles (default: 5000) // eslint-disable-next-line react/no-unused-prop-types interval: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string, _propTypes2.default.bool]), children: _propTypes2.default.array, // called when the mouse enters the Carousel mouseEnter: _propTypes2.default.func, // called when the mouse exits the Carousel mouseLeave: _propTypes2.default.func, // controls whether the slide animation on the Carousel works or not slide: _propTypes2.default.bool, cssModule: _propTypes2.default.object, className: _propTypes2.default.string }; Carousel.defaultProps = { interval: 5000, pause: 'hover', keyboard: true, slide: true }; Carousel.childContextTypes = { direction: _propTypes2.default.string }; exports.default = Carousel;