Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
  6. var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
  7. var _react = _interopRequireDefault(require("react"));
  8. var _propTypes = _interopRequireDefault(require("prop-types"));
  9. var _classnames = _interopRequireDefault(require("classnames"));
  10. var _CarouselItem = _interopRequireDefault(require("./CarouselItem"));
  11. var _utils = require("./utils");
  12. var Carousel =
  13. /*#__PURE__*/
  14. function (_React$Component) {
  15. (0, _inheritsLoose2.default)(Carousel, _React$Component);
  16. function Carousel(props) {
  17. var _this;
  18. _this = _React$Component.call(this, props) || this;
  19. _this.handleKeyPress = _this.handleKeyPress.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
  20. _this.renderItems = _this.renderItems.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
  21. _this.hoverStart = _this.hoverStart.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
  22. _this.hoverEnd = _this.hoverEnd.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
  23. _this.state = {
  24. direction: 'right',
  25. indicatorClicked: false
  26. };
  27. return _this;
  28. }
  29. var _proto = Carousel.prototype;
  30. _proto.getChildContext = function getChildContext() {
  31. return {
  32. direction: this.state.direction
  33. };
  34. };
  35. _proto.componentDidMount = function componentDidMount() {
  36. // Set up the cycle
  37. if (this.props.ride === 'carousel') {
  38. this.setInterval();
  39. } // TODO: move this to the specific carousel like bootstrap. Currently it will trigger ALL carousels on the page.
  40. document.addEventListener('keyup', this.handleKeyPress);
  41. };
  42. _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  43. this.setInterval(nextProps); // Calculate the direction to turn
  44. if (this.props.activeIndex + 1 === nextProps.activeIndex) {
  45. this.setState({
  46. direction: 'right'
  47. });
  48. } else if (this.props.activeIndex - 1 === nextProps.activeIndex) {
  49. this.setState({
  50. direction: 'left'
  51. });
  52. } else if (this.props.activeIndex > nextProps.activeIndex) {
  53. this.setState({
  54. direction: this.state.indicatorClicked ? 'left' : 'right'
  55. });
  56. } else if (this.props.activeIndex !== nextProps.activeIndex) {
  57. this.setState({
  58. direction: this.state.indicatorClicked ? 'right' : 'left'
  59. });
  60. }
  61. this.setState({
  62. indicatorClicked: false
  63. });
  64. };
  65. _proto.componentWillUnmount = function componentWillUnmount() {
  66. this.clearInterval();
  67. document.removeEventListener('keyup', this.handleKeyPress);
  68. };
  69. _proto.setInterval = function (_setInterval) {
  70. function setInterval() {
  71. return _setInterval.apply(this, arguments);
  72. }
  73. setInterval.toString = function () {
  74. return _setInterval.toString();
  75. };
  76. return setInterval;
  77. }(function (props) {
  78. if (props === void 0) {
  79. props = this.props;
  80. }
  81. // make sure not to have multiple intervals going...
  82. this.clearInterval();
  83. if (props.interval) {
  84. this.cycleInterval = setInterval(function () {
  85. props.next();
  86. }, parseInt(props.interval, 10));
  87. }
  88. });
  89. _proto.clearInterval = function (_clearInterval) {
  90. function clearInterval() {
  91. return _clearInterval.apply(this, arguments);
  92. }
  93. clearInterval.toString = function () {
  94. return _clearInterval.toString();
  95. };
  96. return clearInterval;
  97. }(function () {
  98. clearInterval(this.cycleInterval);
  99. });
  100. _proto.hoverStart = function hoverStart() {
  101. if (this.props.pause === 'hover') {
  102. this.clearInterval();
  103. }
  104. if (this.props.mouseEnter) {
  105. var _this$props;
  106. (_this$props = this.props).mouseEnter.apply(_this$props, arguments);
  107. }
  108. };
  109. _proto.hoverEnd = function hoverEnd() {
  110. if (this.props.pause === 'hover') {
  111. this.setInterval();
  112. }
  113. if (this.props.mouseLeave) {
  114. var _this$props2;
  115. (_this$props2 = this.props).mouseLeave.apply(_this$props2, arguments);
  116. }
  117. };
  118. _proto.handleKeyPress = function handleKeyPress(evt) {
  119. if (this.props.keyboard) {
  120. if (evt.keyCode === 37) {
  121. this.props.previous();
  122. } else if (evt.keyCode === 39) {
  123. this.props.next();
  124. }
  125. }
  126. };
  127. _proto.renderItems = function renderItems(carouselItems, className) {
  128. var _this2 = this;
  129. var slide = this.props.slide;
  130. return _react.default.createElement("div", {
  131. role: "listbox",
  132. className: className
  133. }, carouselItems.map(function (item, index) {
  134. var isIn = index === _this2.props.activeIndex;
  135. return _react.default.cloneElement(item, {
  136. in: isIn,
  137. slide: slide
  138. });
  139. }));
  140. };
  141. _proto.render = function render() {
  142. var _this3 = this;
  143. var _this$props3 = this.props,
  144. cssModule = _this$props3.cssModule,
  145. slide = _this$props3.slide,
  146. className = _this$props3.className;
  147. var outerClasses = (0, _utils.mapToCssModules)((0, _classnames.default)(className, 'carousel', slide && 'slide'), cssModule);
  148. var innerClasses = (0, _utils.mapToCssModules)((0, _classnames.default)('carousel-inner'), cssModule); // filter out booleans, null, or undefined
  149. var children = this.props.children.filter(function (child) {
  150. return child !== null && child !== undefined && typeof child !== 'boolean';
  151. });
  152. var slidesOnly = children.every(function (child) {
  153. return child.type === _CarouselItem.default;
  154. }); // Rendering only slides
  155. if (slidesOnly) {
  156. return _react.default.createElement("div", {
  157. className: outerClasses,
  158. onMouseEnter: this.hoverStart,
  159. onMouseLeave: this.hoverEnd
  160. }, this.renderItems(children, innerClasses));
  161. } // Rendering slides and controls
  162. if (children[0] instanceof Array) {
  163. var _carouselItems = children[0];
  164. var _controlLeft = children[1];
  165. var _controlRight = children[2];
  166. return _react.default.createElement("div", {
  167. className: outerClasses,
  168. onMouseEnter: this.hoverStart,
  169. onMouseLeave: this.hoverEnd
  170. }, this.renderItems(_carouselItems, innerClasses), _controlLeft, _controlRight);
  171. } // Rendering indicators, slides and controls
  172. var indicators = children[0];
  173. var wrappedOnClick = function wrappedOnClick(e) {
  174. if (typeof indicators.props.onClickHandler === 'function') {
  175. _this3.setState({
  176. indicatorClicked: true
  177. }, function () {
  178. return indicators.props.onClickHandler(e);
  179. });
  180. }
  181. };
  182. var wrappedIndicators = _react.default.cloneElement(indicators, {
  183. onClickHandler: wrappedOnClick
  184. });
  185. var carouselItems = children[1];
  186. var controlLeft = children[2];
  187. var controlRight = children[3];
  188. return _react.default.createElement("div", {
  189. className: outerClasses,
  190. onMouseEnter: this.hoverStart,
  191. onMouseLeave: this.hoverEnd
  192. }, wrappedIndicators, this.renderItems(carouselItems, innerClasses), controlLeft, controlRight);
  193. };
  194. return Carousel;
  195. }(_react.default.Component);
  196. Carousel.propTypes = {
  197. // the current active slide of the carousel
  198. activeIndex: _propTypes.default.number,
  199. // a function which should advance the carousel to the next slide (via activeIndex)
  200. next: _propTypes.default.func.isRequired,
  201. // a function which should advance the carousel to the previous slide (via activeIndex)
  202. previous: _propTypes.default.func.isRequired,
  203. // controls if the left and right arrow keys should control the carousel
  204. keyboard: _propTypes.default.bool,
  205. /* If set to "hover", pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on
  206. * mouseleave. If set to false, hovering over the carousel won't pause it. (default: "hover")
  207. */
  208. pause: _propTypes.default.oneOf(['hover', false]),
  209. // Autoplays the carousel after the user manually cycles the first item. If "carousel", autoplays the carousel on load.
  210. // This is how bootstrap defines it... I would prefer a bool named autoplay or something...
  211. ride: _propTypes.default.oneOf(['carousel']),
  212. // the interval at which the carousel automatically cycles (default: 5000)
  213. // eslint-disable-next-line react/no-unused-prop-types
  214. interval: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string, _propTypes.default.bool]),
  215. children: _propTypes.default.array,
  216. // called when the mouse enters the Carousel
  217. mouseEnter: _propTypes.default.func,
  218. // called when the mouse exits the Carousel
  219. mouseLeave: _propTypes.default.func,
  220. // controls whether the slide animation on the Carousel works or not
  221. slide: _propTypes.default.bool,
  222. cssModule: _propTypes.default.object,
  223. className: _propTypes.default.string
  224. };
  225. Carousel.defaultProps = {
  226. interval: 5000,
  227. pause: 'hover',
  228. keyboard: true,
  229. slide: true
  230. };
  231. Carousel.childContextTypes = {
  232. direction: _propTypes.default.string
  233. };
  234. var _default = Carousel;
  235. exports.default = _default;