UNPKG

ukelli-ui

Version:

[![Build Status](https://travis-ci.org/ukelli/ukelli-ui.svg?branch=master)](https://travis-ci.org/ukelli/ukelli-ui) [![install size](https://packagephobia.now.sh/badge?p=ukelli-ui)](https://packagephobia.now.sh/result?p=ukelli-ui)

310 lines (267 loc) 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _basicHelper = require("basic-helper"); var _icon = require("../icon"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } } 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); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var TRANSTION_TIME = 2000; var defaultRotateX = window.$UKE.isMobile ? '200px' : '1600px'; var delayExec = new _basicHelper.DebounceClass(); /** * 3D 效果的轮播控件,暂时没有用 * * @export * @class Carousel3D * @extends {Component} */ var Carousel3D = /*#__PURE__*/ function (_Component) { _inherits(Carousel3D, _Component); function Carousel3D(props) { var _this; _classCallCheck(this, Carousel3D); _this = _possibleConstructorReturn(this, _getPrototypeOf(Carousel3D).call(this, props)); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTouchStart", function (e) { var touches = e.changedTouches || e; _this.startPageX = touches[0] ? touches[0].pageX : touches.pageX; }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTouchEnd", function (e) { var activeIdx = _this.state.activeIdx; var touches = e.changedTouches || e; _this.endPageX = touches[0] ? touches[0].pageX : touches.pageX; var touchOffset = _this.endPageX - _this.startPageX; if (Math.abs(touchOffset) < 50) { return _this.showDetail(); } var toLeft = touchOffset < 0; _this.roll(activeIdx + (toLeft ? -1 : 1)); }); var config = props.config; _this.state = { activeIdx: 0, bannerTotalWidth: 0, rotateIdx: 0, carouselRotateY: 0 }; _this.timer = null; _this.freq = 5000; _this.isStarted = false; _this._loopConfig = config; var configIdx = 0; // for (var i = 0; i < 6; i++) { // this._loopConfig.push(config[configIdx]); // configIdx++; // configIdx = config[configIdx] ? configIdx : 0; // } _this.animateDuration = 1000; _this.bannerItemWidth = 800; _this.configLen = config.length; _this.eachItemRotate = 360 / _this.configLen; return _this; } _createClass(Carousel3D, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if (this.props.config.length == 0 && nextProps.config.length > 0) { this.startLoop(); } } }, { key: "componentDidMount", value: function componentDidMount() { this.setState({ bannerTotalWidth: this.bannerItemWidth * this.configLen }); this.setActiveIdx(this.props.config.length - 1); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.stopLoop(); } }, { key: "startLoop", value: function startLoop() { var _this2 = this; var self = this; if (this.timer) this.stopLoop(); this.timer = setInterval(function () { var activeIdx = _this2.state.activeIdx; /** * 如果不在当前的标签,就不渲染 */ if (!document.hidden) self.roll(activeIdx - 1); }, this.freq); } }, { key: "stopLoop", value: function stopLoop() { clearInterval(this.timer); this.timer = null; } }, { key: "setActiveIdx", value: function setActiveIdx(idx) { this.startLoop(); } }, { key: "roll", value: function roll(type, callback) { var _this3 = this; var self = this; this.isAnimating = true; this.stopLoop(); var _this$state = this.state, carouselRotateY = _this$state.carouselRotateY, activeIdx = _this$state.activeIdx, rotateIdx = _this$state.rotateIdx; var nextRotateY = carouselRotateY; var nextIdx = activeIdx; var nextRotateIdx = activeIdx; var currGroupLen = this.configLen - 1; if (!!+type || type == 0) { nextIdx = type; nextRotateIdx = rotateIdx - activeIdx + type; } nextRotateY = nextRotateIdx * this.eachItemRotate; //判断索引 if (nextIdx < 0) nextIdx = currGroupLen; if (nextIdx > currGroupLen) nextIdx = 0; this.setState({ carouselRotateY: nextRotateY, activeIdx: nextIdx, rotateIdx: nextRotateIdx }, function () { (0, _basicHelper.Call)(callback, nextIdx); setTimeout(function () { self.isAnimating = false; }, _this3.animateDuration); }); } }, { key: "rollAndEmitChangeEvent", value: function rollAndEmitChangeEvent(type) { var _this$props = this.props, onClickItem = _this$props.onClickItem, onChange = _this$props.onChange; if (this.isAnimating || !type && type != 0) return; this.roll(type, function (nextIdx) { (0, _basicHelper.Call)(onChange, nextIdx); }); } }, { key: "showDetail", value: function showDetail() { var activeArea = document.querySelector('.card-render-group .card-item.active .action-area'); if (activeArea) activeArea.click(); } }, { key: "handleWheel", value: function handleWheel(e) { var _this4 = this; var self = this; this.oneWheel += e.deltaY; //返回鼠标滚轮的垂直滚动量 //防抖 delayExec.exec(function () { // this.queryOrderDetail(); var rollTyoe; if (self.oneWheel > 50) rollTyoe = '+'; if (self.oneWheel < -50) rollTyoe = '-'; delayExec.exec(function () { if (!rollTyoe) return; _this4.rollAndEmitChangeEvent(rollTyoe); }, 20); self.oneWheel = 0; }, 10); } }, { key: "render", value: function render() { var _this5 = this; var _this$state2 = this.state, carouselRotateY = _this$state2.carouselRotateY, activeIdx = _this$state2.activeIdx, bannerTotalWidth = _this$state2.bannerTotalWidth; var _this$props2 = this.props, config = _this$props2.config, _this$props2$isMobile = _this$props2.isMobile, isMobile = _this$props2$isMobile === void 0 ? false : _this$props2$isMobile, onClickItem = _this$props2.onClickItem; return _react.default.createElement("div", { onWheel: function onWheel(e) { return _this5.handleWheel(e); }, className: "card-render-group" }, _react.default.createElement("div", { className: "fill-h-w", style: {// 'transform': `translateZ(-${defaultRotateX})`, } }, _react.default.createElement("div", { className: "carousel", style: { 'transform': "rotateY(".concat(carouselRotateY, "deg)") } }, _react.default.createElement("div", null, this._loopConfig.map(function (item, idx) { var isActive = idx == activeIdx; return _react.default.createElement("div", { key: idx, style: { transform: "rotateY(".concat(idx * _this5.eachItemRotate, "deg) translateZ(").concat(defaultRotateX, ")") }, className: "card-item item idx-" + idx + (isActive ? ' active' : '') }, _react.default.createElement("div", { className: "card-img" }, item)); })))), isMobile ? _react.default.createElement("div", { className: "section-mark", onMouseDown: this.handleTouchStart, onMouseUp: this.handleTouchEnd, onTouchStart: this.handleTouchStart, onTouchEnd: this.handleTouchEnd }) : [_react.default.createElement("div", { className: "func-btn prev-btn", key: "prev", onClick: function onClick(e) { return _this5.rollAndEmitChangeEvent(activeIdx + 1); } }, _react.default.createElement(_icon.Icon, { n: "arrow" })), _react.default.createElement("div", { className: "func-btn next-btn", key: "next", onClick: function onClick(e) { return _this5.rollAndEmitChangeEvent(activeIdx - 1); } }, _react.default.createElement(_icon.Icon, { n: "arrow" }))]); } }]); return Carousel3D; }(_react.Component); exports.default = Carousel3D; _defineProperty(Carousel3D, "propTypes", { /** * config: [ * ...components * ] * @type {[type]} */ config: _propTypes.default.array.isRequired, isMobile: _propTypes.default.bool, onClickItem: _propTypes.default.func });