| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', { value: true });
- var vue = require('vue');
- var lodashUnified = require('lodash-unified');
- var core = require('@vueuse/core');
- var constants = require('../constants.js');
- var aria = require('../../../../constants/aria.js');
- var event = require('../../../../constants/event.js');
- const useTooltip = (props, formatTooltip, showTooltip) => {
- const tooltip = vue.ref();
- const tooltipVisible = vue.ref(false);
- const enableFormat = vue.computed(() => {
- return formatTooltip.value instanceof Function;
- });
- const formatValue = vue.computed(() => {
- return enableFormat.value && formatTooltip.value(props.modelValue) || props.modelValue;
- });
- const displayTooltip = lodashUnified.debounce(() => {
- showTooltip.value && (tooltipVisible.value = true);
- }, 50);
- const hideTooltip = lodashUnified.debounce(() => {
- showTooltip.value && (tooltipVisible.value = false);
- }, 50);
- return {
- tooltip,
- tooltipVisible,
- formatValue,
- displayTooltip,
- hideTooltip
- };
- };
- const useSliderButton = (props, initData, emit) => {
- const {
- disabled,
- min,
- max,
- step,
- showTooltip,
- persistent,
- precision,
- sliderSize,
- formatTooltip,
- emitChange,
- resetSize,
- updateDragging
- } = vue.inject(constants.sliderContextKey);
- const { tooltip, tooltipVisible, formatValue, displayTooltip, hideTooltip } = useTooltip(props, formatTooltip, showTooltip);
- const button = vue.ref();
- const currentPosition = vue.computed(() => {
- return `${(props.modelValue - min.value) / (max.value - min.value) * 100}%`;
- });
- const wrapperStyle = vue.computed(() => {
- return props.vertical ? { bottom: currentPosition.value } : { left: currentPosition.value };
- });
- const handleMouseEnter = () => {
- initData.hovering = true;
- displayTooltip();
- };
- const handleMouseLeave = () => {
- initData.hovering = false;
- if (!initData.dragging) {
- hideTooltip();
- }
- };
- const onButtonDown = (event) => {
- if (disabled.value)
- return;
- event.preventDefault();
- onDragStart(event);
- window.addEventListener("mousemove", onDragging);
- window.addEventListener("touchmove", onDragging);
- window.addEventListener("mouseup", onDragEnd);
- window.addEventListener("touchend", onDragEnd);
- window.addEventListener("contextmenu", onDragEnd);
- button.value.focus();
- };
- const incrementPosition = (amount) => {
- if (disabled.value)
- return;
- initData.newPosition = Number.parseFloat(currentPosition.value) + amount / (max.value - min.value) * 100;
- setPosition(initData.newPosition);
- emitChange();
- };
- const onLeftKeyDown = () => {
- incrementPosition(-step.value);
- };
- const onRightKeyDown = () => {
- incrementPosition(step.value);
- };
- const onPageDownKeyDown = () => {
- incrementPosition(-step.value * 4);
- };
- const onPageUpKeyDown = () => {
- incrementPosition(step.value * 4);
- };
- const onHomeKeyDown = () => {
- if (disabled.value)
- return;
- setPosition(0);
- emitChange();
- };
- const onEndKeyDown = () => {
- if (disabled.value)
- return;
- setPosition(100);
- emitChange();
- };
- const onKeyDown = (event) => {
- let isPreventDefault = true;
- switch (event.code) {
- case aria.EVENT_CODE.left:
- case aria.EVENT_CODE.down:
- onLeftKeyDown();
- break;
- case aria.EVENT_CODE.right:
- case aria.EVENT_CODE.up:
- onRightKeyDown();
- break;
- case aria.EVENT_CODE.home:
- onHomeKeyDown();
- break;
- case aria.EVENT_CODE.end:
- onEndKeyDown();
- break;
- case aria.EVENT_CODE.pageDown:
- onPageDownKeyDown();
- break;
- case aria.EVENT_CODE.pageUp:
- onPageUpKeyDown();
- break;
- default:
- isPreventDefault = false;
- break;
- }
- isPreventDefault && event.preventDefault();
- };
- const getClientXY = (event) => {
- let clientX;
- let clientY;
- if (event.type.startsWith("touch")) {
- clientY = event.touches[0].clientY;
- clientX = event.touches[0].clientX;
- } else {
- clientY = event.clientY;
- clientX = event.clientX;
- }
- return {
- clientX,
- clientY
- };
- };
- const onDragStart = (event) => {
- initData.dragging = true;
- initData.isClick = true;
- const { clientX, clientY } = getClientXY(event);
- if (props.vertical) {
- initData.startY = clientY;
- } else {
- initData.startX = clientX;
- }
- initData.startPosition = Number.parseFloat(currentPosition.value);
- initData.newPosition = initData.startPosition;
- };
- const onDragging = (event) => {
- if (initData.dragging) {
- initData.isClick = false;
- displayTooltip();
- resetSize();
- let diff;
- const { clientX, clientY } = getClientXY(event);
- if (props.vertical) {
- initData.currentY = clientY;
- diff = (initData.startY - initData.currentY) / sliderSize.value * 100;
- } else {
- initData.currentX = clientX;
- diff = (initData.currentX - initData.startX) / sliderSize.value * 100;
- }
- initData.newPosition = initData.startPosition + diff;
- setPosition(initData.newPosition);
- }
- };
- const onDragEnd = () => {
- if (initData.dragging) {
- setTimeout(() => {
- initData.dragging = false;
- if (!initData.hovering) {
- hideTooltip();
- }
- if (!initData.isClick) {
- setPosition(initData.newPosition);
- }
- emitChange();
- }, 0);
- window.removeEventListener("mousemove", onDragging);
- window.removeEventListener("touchmove", onDragging);
- window.removeEventListener("mouseup", onDragEnd);
- window.removeEventListener("touchend", onDragEnd);
- window.removeEventListener("contextmenu", onDragEnd);
- }
- };
- const setPosition = async (newPosition) => {
- if (newPosition === null || Number.isNaN(+newPosition))
- return;
- if (newPosition < 0) {
- newPosition = 0;
- } else if (newPosition > 100) {
- newPosition = 100;
- }
- const lengthPerStep = 100 / ((max.value - min.value) / step.value);
- const steps = Math.round(newPosition / lengthPerStep);
- let value = steps * lengthPerStep * (max.value - min.value) * 0.01 + min.value;
- value = Number.parseFloat(value.toFixed(precision.value));
- if (value !== props.modelValue) {
- emit(event.UPDATE_MODEL_EVENT, value);
- }
- if (!initData.dragging && props.modelValue !== initData.oldValue) {
- initData.oldValue = props.modelValue;
- }
- await vue.nextTick();
- initData.dragging && displayTooltip();
- tooltip.value.updatePopper();
- };
- vue.watch(() => initData.dragging, (val) => {
- updateDragging(val);
- });
- core.useEventListener(button, "touchstart", onButtonDown, { passive: false });
- return {
- disabled,
- button,
- tooltip,
- tooltipVisible,
- showTooltip,
- persistent,
- wrapperStyle,
- formatValue,
- handleMouseEnter,
- handleMouseLeave,
- onButtonDown,
- onKeyDown,
- setPosition
- };
- };
- exports.useSliderButton = useSliderButton;
- //# sourceMappingURL=use-slider-button.js.map
|