You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
6.3 KiB
201 lines
6.3 KiB
3 years ago
|
import json2mq from '../_util/json2mq';
|
||
|
import BaseMixin from '../_util/BaseMixin';
|
||
|
import { cloneElement } from '../_util/vnode';
|
||
6 years ago
|
import InnerSlider from './inner-slider';
|
||
|
import defaultProps from './default-props';
|
||
|
import { canUseDOM } from './utils/innerSliderUtils';
|
||
3 years ago
|
import { getSlot } from '../_util/props-util';
|
||
4 years ago
|
import { defineComponent } from 'vue';
|
||
6 years ago
|
|
||
4 years ago
|
export default defineComponent({
|
||
4 years ago
|
name: 'Slider',
|
||
4 years ago
|
mixins: [BaseMixin],
|
||
|
inheritAttrs: false,
|
||
6 years ago
|
props: {
|
||
|
...defaultProps,
|
||
|
},
|
||
6 years ago
|
data() {
|
||
|
this._responsiveMediaHandlers = [];
|
||
6 years ago
|
return {
|
||
|
breakpoint: null,
|
||
6 years ago
|
};
|
||
6 years ago
|
},
|
||
|
// handles responsive breakpoints
|
||
3 years ago
|
mounted() {
|
||
6 years ago
|
if (this.responsive) {
|
||
6 years ago
|
const breakpoints = this.responsive.map(breakpt => breakpt.breakpoint);
|
||
6 years ago
|
// sort them in increasing order of their numerical value
|
||
6 years ago
|
breakpoints.sort((x, y) => x - y);
|
||
6 years ago
|
|
||
|
breakpoints.forEach((breakpoint, index) => {
|
||
|
// media query for each breakpoint
|
||
6 years ago
|
let bQuery;
|
||
6 years ago
|
if (index === 0) {
|
||
6 years ago
|
bQuery = json2mq({ minWidth: 0, maxWidth: breakpoint });
|
||
6 years ago
|
} else {
|
||
|
bQuery = json2mq({
|
||
|
minWidth: breakpoints[index - 1] + 1,
|
||
|
maxWidth: breakpoint,
|
||
6 years ago
|
});
|
||
6 years ago
|
}
|
||
|
// when not using server side rendering
|
||
|
canUseDOM() &&
|
||
|
this.media(bQuery, () => {
|
||
5 years ago
|
this.setState({ breakpoint });
|
||
6 years ago
|
});
|
||
|
});
|
||
6 years ago
|
|
||
|
// Register media query for full screen. Need to support resize from small to large
|
||
|
// convert javascript object to media query string
|
||
6 years ago
|
const query = json2mq({ minWidth: breakpoints.slice(-1)[0] });
|
||
6 years ago
|
|
||
|
canUseDOM() &&
|
||
|
this.media(query, () => {
|
||
6 years ago
|
this.setState({ breakpoint: null });
|
||
|
});
|
||
6 years ago
|
}
|
||
|
},
|
||
5 years ago
|
beforeUnmount() {
|
||
3 years ago
|
this._responsiveMediaHandlers.forEach(function (obj) {
|
||
4 years ago
|
obj.mql.removeListener(obj.listener);
|
||
6 years ago
|
});
|
||
6 years ago
|
},
|
||
4 years ago
|
methods: {
|
||
|
innerSliderRefHandler(ref) {
|
||
|
this.innerSlider = ref;
|
||
|
},
|
||
|
media(query, handler) {
|
||
|
// javascript handler for css media query
|
||
|
const mql = window.matchMedia(query);
|
||
|
const listener = ({ matches }) => {
|
||
|
if (matches) {
|
||
|
handler();
|
||
|
}
|
||
|
};
|
||
|
mql.addListener(listener);
|
||
|
listener(mql);
|
||
|
this._responsiveMediaHandlers.push({ mql, query, listener });
|
||
|
},
|
||
|
slickPrev() {
|
||
3 years ago
|
this.innerSlider?.slickPrev();
|
||
4 years ago
|
},
|
||
|
slickNext() {
|
||
3 years ago
|
this.innerSlider?.slickNext();
|
||
4 years ago
|
},
|
||
|
slickGoTo(slide, dontAnimate = false) {
|
||
3 years ago
|
this.innerSlider?.slickGoTo(slide, dontAnimate);
|
||
4 years ago
|
},
|
||
|
slickPause() {
|
||
3 years ago
|
this.innerSlider?.pause('paused');
|
||
4 years ago
|
},
|
||
|
slickPlay() {
|
||
3 years ago
|
this.innerSlider?.handleAutoPlay('play');
|
||
4 years ago
|
},
|
||
|
},
|
||
6 years ago
|
|
||
6 years ago
|
render() {
|
||
|
let settings;
|
||
|
let newProps;
|
||
6 years ago
|
if (this.breakpoint) {
|
||
6 years ago
|
newProps = this.responsive.filter(resp => resp.breakpoint === this.breakpoint);
|
||
6 years ago
|
settings =
|
||
|
newProps[0].settings === 'unslick'
|
||
|
? 'unslick'
|
||
6 years ago
|
: { ...this.$props, ...newProps[0].settings };
|
||
6 years ago
|
} else {
|
||
6 years ago
|
settings = { ...this.$props };
|
||
6 years ago
|
}
|
||
|
|
||
|
// force scrolling by one if centerMode is on
|
||
|
if (settings.centerMode) {
|
||
6 years ago
|
if (settings.slidesToScroll > 1 && process.env.NODE_ENV !== 'production') {
|
||
6 years ago
|
console.warn(
|
||
5 years ago
|
`slidesToScroll should be equal to 1 in centerMode, you are using ${settings.slidesToScroll}`,
|
||
6 years ago
|
);
|
||
6 years ago
|
}
|
||
6 years ago
|
settings.slidesToScroll = 1;
|
||
6 years ago
|
}
|
||
|
// force showing one slide and scrolling by one if the fade mode is on
|
||
|
if (settings.fade) {
|
||
|
if (settings.slidesToShow > 1 && process.env.NODE_ENV !== 'production') {
|
||
|
console.warn(
|
||
5 years ago
|
`slidesToShow should be equal to 1 when fade is true, you're using ${settings.slidesToShow}`,
|
||
6 years ago
|
);
|
||
6 years ago
|
}
|
||
6 years ago
|
if (settings.slidesToScroll > 1 && process.env.NODE_ENV !== 'production') {
|
||
6 years ago
|
console.warn(
|
||
5 years ago
|
`slidesToScroll should be equal to 1 when fade is true, you're using ${settings.slidesToScroll}`,
|
||
6 years ago
|
);
|
||
6 years ago
|
}
|
||
6 years ago
|
settings.slidesToShow = 1;
|
||
|
settings.slidesToScroll = 1;
|
||
6 years ago
|
}
|
||
|
|
||
|
// makes sure that children is an array, even when there is only 1 child
|
||
4 years ago
|
let children = getSlot(this) || [];
|
||
6 years ago
|
|
||
|
// Children may contain false or null, so we should filter them
|
||
|
// children may also contain string filled with spaces (in certain cases where we use jsx strings)
|
||
|
children = children.filter(child => {
|
||
|
if (typeof child === 'string') {
|
||
6 years ago
|
return !!child.trim();
|
||
6 years ago
|
}
|
||
6 years ago
|
return !!child;
|
||
|
});
|
||
6 years ago
|
|
||
|
// rows and slidesPerRow logic is handled here
|
||
6 years ago
|
if (settings.variableWidth && (settings.rows > 1 || settings.slidesPerRow > 1)) {
|
||
|
console.warn(`variableWidth is not supported in case of rows > 1 or slidesPerRow > 1`);
|
||
|
settings.variableWidth = false;
|
||
6 years ago
|
}
|
||
6 years ago
|
const newChildren = [];
|
||
|
let currentWidth = null;
|
||
|
for (let i = 0; i < children.length; i += settings.rows * settings.slidesPerRow) {
|
||
|
const newSlide = [];
|
||
|
for (let j = i; j < i + settings.rows * settings.slidesPerRow; j += settings.slidesPerRow) {
|
||
|
const row = [];
|
||
6 years ago
|
for (let k = j; k < j + settings.slidesPerRow; k += 1) {
|
||
4 years ago
|
if (settings.variableWidth && children[k].props?.style) {
|
||
4 years ago
|
currentWidth = children[k].props.style.width;
|
||
6 years ago
|
}
|
||
6 years ago
|
if (k >= children.length) break;
|
||
6 years ago
|
row.push(
|
||
|
cloneElement(children[k], {
|
||
|
key: 100 * i + 10 * j + k,
|
||
4 years ago
|
tabindex: -1,
|
||
6 years ago
|
style: {
|
||
|
width: `${100 / settings.slidesPerRow}%`,
|
||
|
display: 'inline-block',
|
||
|
},
|
||
6 years ago
|
}),
|
||
|
);
|
||
6 years ago
|
}
|
||
6 years ago
|
newSlide.push(<div key={10 * i + j}>{row}</div>);
|
||
6 years ago
|
}
|
||
|
if (settings.variableWidth) {
|
||
|
newChildren.push(
|
||
|
<div key={i} style={{ width: currentWidth }}>
|
||
|
{newSlide}
|
||
6 years ago
|
</div>,
|
||
|
);
|
||
6 years ago
|
} else {
|
||
6 years ago
|
newChildren.push(<div key={i}>{newSlide}</div>);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (settings === 'unslick') {
|
||
6 years ago
|
const className = 'regular slider ' + (this.className || '');
|
||
3 years ago
|
return <div class={className}>{children}</div>;
|
||
6 years ago
|
} else if (newChildren.length <= settings.slidesToShow) {
|
||
6 years ago
|
settings.unslick = true;
|
||
6 years ago
|
}
|
||
|
const sliderProps = {
|
||
4 years ago
|
...this.$attrs,
|
||
|
...settings,
|
||
|
children: newChildren,
|
||
|
ref: this.innerSliderRefHandler,
|
||
6 years ago
|
};
|
||
4 years ago
|
return <InnerSlider {...sliderProps} v-slots={this.$slots} __propsSymbol__={[]} />;
|
||
6 years ago
|
},
|
||
4 years ago
|
});
|