2021-06-26 09:35:40 +08:00
|
|
|
import { defineComponent, reactive } from 'vue';
|
2022-03-26 22:52:54 +08:00
|
|
|
import type { PropType } from 'vue';
|
2020-09-28 19:14:00 +08:00
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import createRef from '../_util/createRef';
|
|
|
|
import raf from '../_util/raf';
|
2020-12-10 17:40:46 +08:00
|
|
|
import supportsPassive from '../_util/supportsPassive';
|
2020-09-28 19:14:00 +08:00
|
|
|
|
|
|
|
const MIN_SIZE = 20;
|
|
|
|
|
2020-10-01 17:20:10 +08:00
|
|
|
interface ScrollBarState {
|
|
|
|
dragging: boolean;
|
|
|
|
pageY: number | null;
|
|
|
|
startTop: number | null;
|
|
|
|
visible: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPageY(e: MouseEvent | TouchEvent) {
|
2020-09-28 19:14:00 +08:00
|
|
|
return 'touches' in e ? e.touches[0].pageY : e.pageY;
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:20:10 +08:00
|
|
|
export default defineComponent({
|
2022-09-26 21:33:41 +08:00
|
|
|
compatConfig: { MODE: 3 },
|
2020-09-28 19:14:00 +08:00
|
|
|
name: 'ScrollBar',
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: {
|
2022-03-26 22:52:54 +08:00
|
|
|
prefixCls: String,
|
|
|
|
scrollTop: Number,
|
|
|
|
scrollHeight: Number,
|
|
|
|
height: Number,
|
|
|
|
count: Number,
|
2020-10-01 17:20:10 +08:00
|
|
|
onScroll: {
|
|
|
|
type: Function as PropType<(scrollTop: number) => void>,
|
|
|
|
},
|
|
|
|
onStartMove: {
|
|
|
|
type: Function as PropType<() => void>,
|
|
|
|
},
|
|
|
|
onStopMove: {
|
|
|
|
type: Function as PropType<() => void>,
|
|
|
|
},
|
2020-09-28 19:14:00 +08:00
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
moveRaf: null,
|
|
|
|
scrollbarRef: createRef(),
|
|
|
|
thumbRef: createRef(),
|
|
|
|
visibleTimeout: null,
|
2020-10-01 17:20:10 +08:00
|
|
|
state: reactive<ScrollBarState>({
|
2020-09-28 19:14:00 +08:00
|
|
|
dragging: false,
|
|
|
|
pageY: null,
|
|
|
|
startTop: null,
|
|
|
|
visible: false,
|
2020-09-29 16:11:05 +08:00
|
|
|
}),
|
2020-09-28 19:14:00 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
scrollTop: {
|
|
|
|
handler() {
|
|
|
|
this.delayHidden();
|
|
|
|
},
|
|
|
|
flush: 'post',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
2021-12-11 14:59:52 +08:00
|
|
|
this.scrollbarRef.current?.addEventListener(
|
2020-12-10 17:40:46 +08:00
|
|
|
'touchstart',
|
|
|
|
this.onScrollbarTouchStart,
|
2020-12-18 18:54:18 +08:00
|
|
|
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
|
2020-12-10 17:40:46 +08:00
|
|
|
);
|
2021-12-11 14:59:52 +08:00
|
|
|
this.thumbRef.current?.addEventListener(
|
2020-12-10 17:40:46 +08:00
|
|
|
'touchstart',
|
|
|
|
this.onMouseDown,
|
2020-12-18 18:54:18 +08:00
|
|
|
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
|
2020-12-10 17:40:46 +08:00
|
|
|
);
|
2020-09-28 19:14:00 +08:00
|
|
|
},
|
|
|
|
|
2020-09-28 21:21:43 +08:00
|
|
|
beforeUnmount() {
|
2020-09-28 19:14:00 +08:00
|
|
|
this.removeEvents();
|
|
|
|
clearTimeout(this.visibleTimeout);
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
delayHidden() {
|
|
|
|
clearTimeout(this.visibleTimeout);
|
|
|
|
this.state.visible = true;
|
2020-09-29 15:16:56 +08:00
|
|
|
|
2020-09-28 19:14:00 +08:00
|
|
|
this.visibleTimeout = setTimeout(() => {
|
|
|
|
this.state.visible = false;
|
|
|
|
}, 2000);
|
|
|
|
},
|
|
|
|
|
2020-10-01 17:20:10 +08:00
|
|
|
onScrollbarTouchStart(e: TouchEvent) {
|
2020-09-28 19:14:00 +08:00
|
|
|
e.preventDefault();
|
|
|
|
},
|
|
|
|
|
2020-10-01 17:20:10 +08:00
|
|
|
onContainerMouseDown(e: MouseEvent) {
|
2020-09-28 19:14:00 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
|
|
|
|
|
|
|
// ======================= Clean =======================
|
|
|
|
patchEvents() {
|
|
|
|
window.addEventListener('mousemove', this.onMouseMove);
|
|
|
|
window.addEventListener('mouseup', this.onMouseUp);
|
|
|
|
|
2020-12-10 17:40:46 +08:00
|
|
|
this.thumbRef.current.addEventListener(
|
|
|
|
'touchmove',
|
|
|
|
this.onMouseMove,
|
2020-12-18 18:54:18 +08:00
|
|
|
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
|
2020-12-10 17:40:46 +08:00
|
|
|
);
|
2020-09-28 19:14:00 +08:00
|
|
|
this.thumbRef.current.addEventListener('touchend', this.onMouseUp);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeEvents() {
|
|
|
|
window.removeEventListener('mousemove', this.onMouseMove);
|
|
|
|
window.removeEventListener('mouseup', this.onMouseUp);
|
|
|
|
|
2020-12-10 17:40:46 +08:00
|
|
|
this.scrollbarRef.current.removeEventListener(
|
|
|
|
'touchstart',
|
|
|
|
this.onScrollbarTouchStart,
|
2020-12-18 18:54:18 +08:00
|
|
|
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
|
2020-12-10 17:40:46 +08:00
|
|
|
);
|
|
|
|
this.thumbRef.current.removeEventListener(
|
|
|
|
'touchstart',
|
|
|
|
this.onMouseDown,
|
2020-12-18 18:54:18 +08:00
|
|
|
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
|
2020-12-10 17:40:46 +08:00
|
|
|
);
|
|
|
|
this.thumbRef.current.removeEventListener(
|
|
|
|
'touchmove',
|
|
|
|
this.onMouseMove,
|
2020-12-18 18:54:18 +08:00
|
|
|
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
|
2020-12-10 17:40:46 +08:00
|
|
|
);
|
2020-09-28 19:14:00 +08:00
|
|
|
this.thumbRef.current.removeEventListener('touchend', this.onMouseUp);
|
|
|
|
|
|
|
|
raf.cancel(this.moveRaf);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ======================= Thumb =======================
|
2020-10-01 17:20:10 +08:00
|
|
|
onMouseDown(e: MouseEvent | TouchEvent) {
|
2020-09-28 19:14:00 +08:00
|
|
|
const { onStartMove } = this.$props;
|
|
|
|
|
|
|
|
Object.assign(this.state, {
|
|
|
|
dragging: true,
|
|
|
|
pageY: getPageY(e),
|
|
|
|
startTop: this.getTop(),
|
|
|
|
});
|
|
|
|
|
|
|
|
onStartMove();
|
|
|
|
this.patchEvents();
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
|
|
|
|
2020-10-01 17:20:10 +08:00
|
|
|
onMouseMove(e: MouseEvent | TouchEvent) {
|
2020-09-28 19:14:00 +08:00
|
|
|
const { dragging, pageY, startTop } = this.state;
|
|
|
|
const { onScroll } = this.$props;
|
|
|
|
|
|
|
|
raf.cancel(this.moveRaf);
|
|
|
|
|
|
|
|
if (dragging) {
|
|
|
|
const offsetY = getPageY(e) - pageY;
|
|
|
|
const newTop = startTop + offsetY;
|
|
|
|
|
|
|
|
const enableScrollRange = this.getEnableScrollRange();
|
|
|
|
const enableHeightRange = this.getEnableHeightRange();
|
|
|
|
|
2020-11-04 18:59:08 +08:00
|
|
|
const ptg = enableHeightRange ? newTop / enableHeightRange : 0;
|
2020-09-28 19:14:00 +08:00
|
|
|
const newScrollTop = Math.ceil(ptg * enableScrollRange);
|
|
|
|
this.moveRaf = raf(() => {
|
|
|
|
onScroll(newScrollTop);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onMouseUp() {
|
|
|
|
const { onStopMove } = this.$props;
|
|
|
|
this.state.dragging = false;
|
|
|
|
|
|
|
|
onStopMove();
|
|
|
|
this.removeEvents();
|
|
|
|
},
|
|
|
|
|
|
|
|
// ===================== Calculate =====================
|
|
|
|
getSpinHeight() {
|
|
|
|
const { height, count } = this.$props;
|
|
|
|
let baseHeight = (height / count) * 10;
|
|
|
|
baseHeight = Math.max(baseHeight, MIN_SIZE);
|
|
|
|
baseHeight = Math.min(baseHeight, height / 2);
|
|
|
|
return Math.floor(baseHeight);
|
|
|
|
},
|
|
|
|
|
|
|
|
getEnableScrollRange() {
|
|
|
|
const { scrollHeight, height } = this.$props;
|
2020-11-04 18:59:08 +08:00
|
|
|
return scrollHeight - height || 0;
|
2020-09-28 19:14:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getEnableHeightRange() {
|
|
|
|
const { height } = this.$props;
|
|
|
|
const spinHeight = this.getSpinHeight();
|
2020-11-04 18:59:08 +08:00
|
|
|
return height - spinHeight || 0;
|
2020-09-28 19:14:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getTop() {
|
|
|
|
const { scrollTop } = this.$props;
|
|
|
|
const enableScrollRange = this.getEnableScrollRange();
|
|
|
|
const enableHeightRange = this.getEnableHeightRange();
|
2020-11-04 18:59:08 +08:00
|
|
|
if (scrollTop === 0 || enableScrollRange === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-09-28 19:14:00 +08:00
|
|
|
const ptg = scrollTop / enableScrollRange;
|
|
|
|
return ptg * enableHeightRange;
|
|
|
|
},
|
2021-06-22 15:33:11 +08:00
|
|
|
// Not show scrollbar when height is large than scrollHeight
|
|
|
|
showScroll() {
|
2020-11-04 18:59:08 +08:00
|
|
|
const { height, scrollHeight } = this.$props;
|
2021-06-22 15:33:11 +08:00
|
|
|
return scrollHeight > height;
|
2020-11-04 18:59:08 +08:00
|
|
|
},
|
2020-09-28 19:14:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2021-06-22 15:33:11 +08:00
|
|
|
const { dragging, visible } = this.state;
|
2020-09-28 19:14:00 +08:00
|
|
|
const { prefixCls } = this.$props;
|
|
|
|
const spinHeight = this.getSpinHeight() + 'px';
|
|
|
|
const top = this.getTop() + 'px';
|
2021-06-22 15:33:11 +08:00
|
|
|
const canScroll = this.showScroll();
|
|
|
|
const mergedVisible = canScroll && visible;
|
2020-09-28 19:14:00 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
ref={this.scrollbarRef}
|
2021-06-22 15:33:11 +08:00
|
|
|
class={classNames(`${prefixCls}-scrollbar`, {
|
|
|
|
[`${prefixCls}-scrollbar-show`]: canScroll,
|
|
|
|
})}
|
2020-09-28 19:14:00 +08:00
|
|
|
style={{
|
|
|
|
width: '8px',
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
right: 0,
|
|
|
|
position: 'absolute',
|
2021-06-22 15:33:11 +08:00
|
|
|
display: mergedVisible ? undefined : 'none',
|
2020-09-28 19:14:00 +08:00
|
|
|
}}
|
|
|
|
onMousedown={this.onContainerMouseDown}
|
|
|
|
onMousemove={this.delayHidden}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
ref={this.thumbRef}
|
|
|
|
class={classNames(`${prefixCls}-scrollbar-thumb`, {
|
|
|
|
[`${prefixCls}-scrollbar-thumb-moving`]: dragging,
|
|
|
|
})}
|
|
|
|
style={{
|
|
|
|
width: '100%',
|
|
|
|
height: spinHeight,
|
|
|
|
top,
|
|
|
|
left: 0,
|
|
|
|
position: 'absolute',
|
|
|
|
background: 'rgba(0, 0, 0, 0.5)',
|
|
|
|
borderRadius: '99px',
|
|
|
|
cursor: 'pointer',
|
|
|
|
userSelect: 'none',
|
|
|
|
}}
|
|
|
|
onMousedown={this.onMouseDown}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2020-10-01 17:20:10 +08:00
|
|
|
});
|