UNPKG

@txdfe/at

Version:

一个设计体系组件库

141 lines (140 loc) 7.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTrackLeft = exports.getTrackCSS = exports.getTrackAnimateCSS = void 0; var _reactDom = _interopRequireDefault(require("react-dom")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var checkSpecKeys = function checkSpecKeys(spec, keysArray) { return keysArray.reduce(function (value, key) { return value && spec.hasOwnProperty(key); }, true) ? null : console.error('Keys Missing', spec); /*eslint no-console:0*/ }; var getTrackCSS = exports.getTrackCSS = function getTrackCSS(spec) { checkSpecKeys(spec, ['left', 'variableWidth', 'slideCount', 'slidesToShow', 'slideWidth']); var trackWidth; var trackHeight; var trackChildren = spec.slideCount + 2 * spec.slidesToShow; if (!spec.vertical) { if (spec.variableWidth) { trackWidth = (spec.slideCount + 2 * spec.slidesToShow) * spec.slideWidth; } else if (spec.centerMode) { trackWidth = (spec.slideCount + 2 * (spec.slidesToShow + 1)) * spec.slideWidth; } else { trackWidth = (spec.slideCount + 2 * spec.slidesToShow) * spec.slideWidth; } } else { trackHeight = trackChildren * spec.slideHeight; } var style = { opacity: 1 }; var transform = { WebkitTransform: !spec.vertical ? "translate3d(".concat(spec.left, "px, 0px, 0px)") : "translate3d(0px, ".concat(spec.left, "px, 0px)"), transform: !spec.vertical ? "translate3d(".concat(spec.left, "px, 0px, 0px)") : "translate3d(0px, ".concat(spec.left, "px, 0px)"), transition: '', WebkitTransition: '', msTransform: !spec.vertical ? "translateX(".concat(spec.left, "px)") : "translateY(".concat(spec.left, "px)") }; if (trackWidth) { style = _objectSpread(_objectSpread({}, style), {}, { width: trackWidth }); } if (trackHeight) { style = _objectSpread(_objectSpread({}, style), {}, { height: trackHeight }); } if (spec.animation !== 'fade') { style = _objectSpread(_objectSpread({}, style), transform); } return style; }; var getTrackAnimateCSS = exports.getTrackAnimateCSS = function getTrackAnimateCSS(spec) { checkSpecKeys(spec, ['left', 'variableWidth', 'slideCount', 'slidesToShow', 'slideWidth', 'speed', 'cssEase']); var style = getTrackCSS(spec); // useCSS is true by default so it can be undefined style.WebkitTransition = "-webkit-transform ".concat(spec.speed, "ms ").concat(spec.cssEase); style.transition = "transform ".concat(spec.speed, "ms ").concat(spec.cssEase); return style; }; var getTrackLeft = exports.getTrackLeft = function getTrackLeft(spec) { checkSpecKeys(spec, ['slideIndex', 'trackRef', 'infinite', 'centerMode', 'slideCount', 'slidesToShow', 'slidesToScroll', 'slideWidth', 'listWidth', 'variableWidth', 'slideHeight']); var slideOffset = 0; var targetLeft; var targetSlide; var verticalOffset = 0; if (spec.animation === 'fade') { return 0; } if (spec.infinite) { if (spec.slideCount > spec.slidesToShow) { slideOffset = spec.slideWidth * spec.slidesToShow * -1; verticalOffset = spec.slideHeight * spec.slidesToShow * -1; } if (spec.slideCount % spec.slidesToScroll !== 0) { var condition = spec.slideIndex + spec.slidesToScroll > spec.slideCount && spec.slideCount > spec.slidesToShow; if (spec.rtl) { var slideIndex = spec.slideIndex >= spec.slideCount ? spec.slideCount - spec.slideIndex : spec.slideIndex; condition = slideIndex + spec.slidesToScroll > spec.slideCount && spec.slideCount > spec.slidesToShow; } if (condition) { if (spec.slideIndex > spec.slideCount) { slideOffset = (spec.slidesToShow - (spec.slideIndex - spec.slideCount)) * spec.slideWidth * -1; verticalOffset = (spec.slidesToShow - (spec.slideIndex - spec.slideCount)) * spec.slideHeight * -1; } else { slideOffset = spec.slideCount % spec.slidesToScroll * spec.slideWidth * -1; verticalOffset = spec.slideCount % spec.slidesToScroll * spec.slideHeight * -1; } } } } else { /* eslint-disable no-lonely-if */ if (spec.slideCount % spec.slidesToScroll !== 0) { if (spec.slideIndex + spec.slidesToScroll > spec.slideCount && spec.slideCount > spec.slidesToShow) { var slidesToOffset = spec.slidesToShow - spec.slideCount % spec.slidesToScroll; slideOffset = slidesToOffset * spec.slideWidth; } } } if (spec.centerMode) { if (spec.infinite) { slideOffset += spec.slideWidth * Math.floor(spec.slidesToShow / 2); } else { slideOffset = spec.slideWidth * Math.floor(spec.slidesToShow / 2); } } if (!spec.vertical) { targetLeft = spec.slideIndex * spec.slideWidth * -1 + slideOffset; } else { targetLeft = spec.slideIndex * spec.slideHeight * -1 + verticalOffset; } if (spec.variableWidth === true) { var targetSlideIndex; if (spec.slideCount <= spec.slidesToShow || spec.infinite === false) { targetSlide = _reactDom["default"].findDOMNode(spec.trackRef).childNodes[spec.slideIndex]; } else { targetSlideIndex = spec.slideIndex + spec.slidesToShow; targetSlide = _reactDom["default"].findDOMNode(spec.trackRef).childNodes[targetSlideIndex]; } targetLeft = targetSlide ? targetSlide.offsetLeft * -1 : 0; if (spec.centerMode === true) { if (spec.infinite === false) { targetSlide = _reactDom["default"].findDOMNode(spec.trackRef).children[spec.slideIndex]; } else { targetSlide = _reactDom["default"].findDOMNode(spec.trackRef).children[spec.slideIndex + spec.slidesToShow + 1]; } if (targetSlide) { targetLeft = targetSlide.offsetLeft * -1 + (spec.listWidth - targetSlide.offsetWidth) / 2; } } } return targetLeft; };