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

198 lines
6.3 KiB
Vue
Raw Normal View History

2020-07-20 08:29:46 +00:00
import { createVNode } from 'vue';
import classnames from '../_util/classNames';
import { flattenChildren } from '../_util/props-util';
2019-01-12 03:33:27 +00:00
import { lazyStartIndex, lazyEndIndex, getPreClones } from './utils/innerSliderUtils';
import { deepCloneElement } from '../_util/vnode';
2018-07-20 08:13:21 +00:00
// given specifications/props for a slide, fetch all the classes that need to be applied to the slide
const getSlideClasses = spec => {
2019-01-12 03:33:27 +00:00
let slickActive, slickCenter;
let centerOffset, index;
2018-07-20 08:13:21 +00:00
if (spec.rtl) {
2019-01-12 03:33:27 +00:00
index = spec.slideCount - 1 - spec.index;
2018-07-20 08:13:21 +00:00
} else {
2019-01-12 03:33:27 +00:00
index = spec.index;
2018-07-20 08:13:21 +00:00
}
2019-01-12 03:33:27 +00:00
const slickCloned = index < 0 || index >= spec.slideCount;
2018-07-20 08:13:21 +00:00
if (spec.centerMode) {
2019-01-12 03:33:27 +00:00
centerOffset = Math.floor(spec.slidesToShow / 2);
slickCenter = (index - spec.currentSlide) % spec.slideCount === 0;
if (index > spec.currentSlide - centerOffset - 1 && index <= spec.currentSlide + centerOffset) {
slickActive = true;
2018-07-20 08:13:21 +00:00
}
} else {
2019-01-12 03:33:27 +00:00
slickActive = spec.currentSlide <= index && index < spec.currentSlide + spec.slidesToShow;
2018-07-20 08:13:21 +00:00
}
let focusedSlide;
if (spec.targetSlide < 0) {
focusedSlide = spec.targetSlide + spec.slideCount;
} else if (spec.targetSlide >= spec.slideCount) {
focusedSlide = spec.targetSlide - spec.slideCount;
} else {
focusedSlide = spec.targetSlide;
}
let slickCurrent = index === focusedSlide;
2018-07-20 08:13:21 +00:00
return {
'slick-slide': true,
'slick-active': slickActive,
'slick-center': slickCenter,
'slick-cloned': slickCloned,
'slick-current': slickCurrent, // dubious in case of RTL
2019-01-12 03:33:27 +00:00
};
};
2018-07-20 08:13:21 +00:00
2021-06-23 15:08:16 +00:00
const getSlideStyle = function (spec) {
2019-01-12 03:33:27 +00:00
const style = {};
2018-07-20 08:13:21 +00:00
if (spec.variableWidth === undefined || spec.variableWidth === false) {
2019-01-12 03:33:27 +00:00
style.width = spec.slideWidth + (typeof spec.slideWidth === 'number' ? 'px' : '');
2018-07-20 08:13:21 +00:00
}
if (spec.fade) {
2019-01-12 03:33:27 +00:00
style.position = 'relative';
2018-07-20 08:13:21 +00:00
if (spec.vertical) {
2019-01-12 03:33:27 +00:00
style.top = -spec.index * parseInt(spec.slideHeight) + 'px';
2018-07-20 08:13:21 +00:00
} else {
2019-01-12 03:33:27 +00:00
style.left = -spec.index * parseInt(spec.slideWidth) + 'px';
2018-07-20 08:13:21 +00:00
}
2019-01-12 03:33:27 +00:00
style.opacity = spec.currentSlide === spec.index ? 1 : 0;
if (spec.useCSS) {
style.transition =
'opacity ' +
spec.speed +
'ms ' +
spec.cssEase +
', ' +
'visibility ' +
spec.speed +
'ms ' +
spec.cssEase;
}
2018-07-20 08:13:21 +00:00
}
2019-01-12 03:33:27 +00:00
return style;
};
2018-07-20 08:13:21 +00:00
const getKey = (child, fallbackKey) => child.key + '-' + fallbackKey;
2018-07-20 08:13:21 +00:00
2021-06-23 15:08:16 +00:00
const renderSlides = function (spec, children) {
2019-01-12 03:33:27 +00:00
let key;
const slides = [];
const preCloneSlides = [];
const postCloneSlides = [];
const childrenCount = children.length;
const startIndex = lazyStartIndex(spec);
const endIndex = lazyEndIndex(spec);
2018-07-20 08:13:21 +00:00
children.forEach((elem, index) => {
2019-01-12 03:33:27 +00:00
let child;
2018-07-20 08:13:21 +00:00
const childOnClickOptions = {
message: 'children',
index,
2018-07-20 08:13:21 +00:00
slidesToScroll: spec.slidesToScroll,
currentSlide: spec.currentSlide,
2019-01-12 03:33:27 +00:00
};
2018-07-20 08:13:21 +00:00
// in case of lazyLoad, whether or not we want to fetch the slide
2019-01-12 03:33:27 +00:00
if (!spec.lazyLoad || (spec.lazyLoad && spec.lazyLoadedList.indexOf(index) >= 0)) {
child = elem;
2018-07-20 08:13:21 +00:00
} else {
2020-07-20 08:29:46 +00:00
child = createVNode('div');
2018-07-20 08:13:21 +00:00
}
2019-01-12 03:33:27 +00:00
const childStyle = getSlideStyle({ ...spec, index });
2020-07-20 08:29:46 +00:00
const slideClass = child.props.class || '';
2019-01-12 03:33:27 +00:00
let slideClasses = getSlideClasses({ ...spec, index });
2018-07-20 08:13:21 +00:00
// push a cloned element of the desired slide
slides.push(
deepCloneElement(child, {
2020-07-20 08:29:46 +00:00
key: 'original' + getKey(child, index),
tabindex: '-1',
'data-index': index,
'aria-hidden': !slideClasses['slick-active'],
class: classnames(slideClasses, slideClass),
style: { outline: 'none', ...(child.props.style || {}), ...childStyle },
onClick: () => {
// child.props && child.props.onClick && child.props.onClick(e)
if (spec.focusOnSelect) {
spec.focusOnSelect(childOnClickOptions);
}
2018-07-20 08:13:21 +00:00
},
2020-07-20 08:29:46 +00:00
}),
2019-01-12 03:33:27 +00:00
);
2018-07-20 08:13:21 +00:00
// if slide needs to be precloned or postcloned
if (spec.infinite && spec.fade === false) {
2019-01-12 03:33:27 +00:00
const preCloneNo = childrenCount - index;
if (preCloneNo <= getPreClones(spec) && childrenCount !== spec.slidesToShow) {
key = -preCloneNo;
2018-07-20 08:13:21 +00:00
if (key >= startIndex) {
2019-01-12 03:33:27 +00:00
child = elem;
2018-07-20 08:13:21 +00:00
}
2019-01-12 03:33:27 +00:00
slideClasses = getSlideClasses({ ...spec, index: key });
2018-07-20 08:13:21 +00:00
preCloneSlides.push(
deepCloneElement(child, {
2018-07-20 08:13:21 +00:00
key: 'precloned' + getKey(child, key),
class: classnames(slideClasses, slideClass),
2020-07-20 08:29:46 +00:00
tabindex: '-1',
'data-index': key,
'aria-hidden': !slideClasses['slick-active'],
style: { ...(child.props.style || {}), ...childStyle },
onClick: () => {
// child.props && child.props.onClick && child.props.onClick(e)
if (spec.focusOnSelect) {
spec.focusOnSelect(childOnClickOptions);
}
2018-07-20 08:13:21 +00:00
},
2019-01-12 03:33:27 +00:00
}),
);
2018-07-20 08:13:21 +00:00
}
if (childrenCount !== spec.slidesToShow) {
2019-01-12 03:33:27 +00:00
key = childrenCount + index;
2018-07-20 08:13:21 +00:00
if (key < endIndex) {
2019-01-12 03:33:27 +00:00
child = elem;
2018-07-20 08:13:21 +00:00
}
2019-01-12 03:33:27 +00:00
slideClasses = getSlideClasses({ ...spec, index: key });
2018-07-20 08:13:21 +00:00
postCloneSlides.push(
deepCloneElement(child, {
2018-07-20 08:13:21 +00:00
key: 'postcloned' + getKey(child, key),
2020-07-20 08:29:46 +00:00
tabindex: '-1',
'data-index': key,
'aria-hidden': !slideClasses['slick-active'],
2018-07-20 08:13:21 +00:00
class: classnames(slideClasses, slideClass),
2020-07-20 08:29:46 +00:00
style: { ...(child.props.style || {}), ...childStyle },
onClick: () => {
// child.props && child.props.onClick && child.props.onClick(e)
if (spec.focusOnSelect) {
spec.focusOnSelect(childOnClickOptions);
}
2018-07-20 08:13:21 +00:00
},
2019-01-12 03:33:27 +00:00
}),
);
2018-07-20 08:13:21 +00:00
}
}
2019-01-12 03:33:27 +00:00
});
2018-07-20 08:13:21 +00:00
if (spec.rtl) {
2019-01-12 03:33:27 +00:00
return preCloneSlides.concat(slides, postCloneSlides).reverse();
2018-07-20 08:13:21 +00:00
} else {
2019-01-12 03:33:27 +00:00
return preCloneSlides.concat(slides, postCloneSlides);
2018-07-20 08:13:21 +00:00
}
2019-01-12 03:33:27 +00:00
};
2018-07-20 08:13:21 +00:00
2020-07-20 08:29:46 +00:00
const Track = (_, { attrs, slots }) => {
const slides = renderSlides(attrs, flattenChildren(slots?.default()));
// const slides = renderSlides(attrs, slots?.default);
2020-07-20 08:29:46 +00:00
const { onMouseenter, onMouseover, onMouseleave } = attrs;
const mouseEvents = { onMouseenter, onMouseover, onMouseleave };
const trackProps = {
class: 'slick-track',
style: attrs.trackStyle,
...mouseEvents,
};
return <div {...trackProps}>{slides}</div>;
2019-01-12 03:33:27 +00:00
};
2020-07-20 08:29:46 +00:00
Track.inheritAttrs = false;
export default Track;