ant-design-vue/components/vc-slick/dots.jsx

87 lines
2.1 KiB
Vue
Raw Normal View History

import classnames from '../_util/classNames';
import { cloneElement } from '../_util/vnode';
import { clamp } from './utils/innerSliderUtils';
2018-07-20 08:13:21 +00:00
2021-06-23 15:08:16 +00:00
const getDotCount = function (spec) {
2019-01-12 03:33:27 +00:00
let dots;
2018-07-20 08:13:21 +00:00
if (spec.infinite) {
2019-01-12 03:33:27 +00:00
dots = Math.ceil(spec.slideCount / spec.slidesToScroll);
2018-07-20 08:13:21 +00:00
} else {
2019-01-12 03:33:27 +00:00
dots = Math.ceil((spec.slideCount - spec.slidesToShow) / spec.slidesToScroll) + 1;
2018-07-20 08:13:21 +00:00
}
2019-01-12 03:33:27 +00:00
return dots;
};
2018-07-20 08:13:21 +00:00
2020-07-20 08:29:46 +00:00
const Dots = (_, { attrs }) => {
const {
slideCount,
slidesToScroll,
slidesToShow,
infinite,
currentSlide,
appendDots,
customPaging,
clickHandler,
dotsClass,
onMouseenter,
onMouseover,
onMouseleave,
} = attrs;
const dotCount = getDotCount({
slideCount,
slidesToScroll,
slidesToShow,
infinite,
});
2018-07-20 08:13:21 +00:00
2020-07-20 08:29:46 +00:00
// Apply join & split to Array to pre-fill it for IE8
//
// Credit: http://stackoverflow.com/a/13735425/1849458
const mouseEvents = { onMouseenter, onMouseover, onMouseleave };
let dots = [];
for (let i = 0; i < dotCount; i++) {
let _rightBound = (i + 1) * slidesToScroll - 1;
let rightBound = infinite ? _rightBound : clamp(_rightBound, 0, slideCount - 1);
let _leftBound = rightBound - (slidesToScroll - 1);
let leftBound = infinite ? _leftBound : clamp(_leftBound, 0, slideCount - 1);
let className = classnames({
'slick-active': infinite
? currentSlide >= leftBound && currentSlide <= rightBound
: currentSlide === leftBound,
2020-07-20 08:29:46 +00:00
});
2018-07-20 08:13:21 +00:00
let dotOptions = {
2020-07-20 08:29:46 +00:00
message: 'dots',
index: i,
slidesToScroll,
currentSlide,
};
2020-07-20 08:29:46 +00:00
function onClick(e) {
// In Autoplay the focus stays on clicked button even after transition
// to next slide. That only goes away by click somewhere outside
if (e) {
e.preventDefault();
2018-07-20 08:13:21 +00:00
}
2020-07-20 08:29:46 +00:00
clickHandler(dotOptions);
}
dots = dots.concat(
2020-07-20 08:29:46 +00:00
<li key={i} class={className}>
{cloneElement(customPaging({ i }), { onClick })}
</li>,
2020-07-20 08:29:46 +00:00
);
}
2018-07-20 08:13:21 +00:00
2020-07-20 08:29:46 +00:00
return cloneElement(appendDots({ dots }), {
class: dotsClass,
...mouseEvents,
});
2019-01-12 03:33:27 +00:00
};
2020-07-20 08:29:46 +00:00
Dots.inheritAttrs = false;
export default Dots;