| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 | import { createVNode as _createVNode } from "vue";import { ref, computed, defineComponent } from "vue";import { clamp, addUnit, addNumber, numericProp, getSizeStyle, preventDefault, stopPropagation, createNamespace, makeNumericProp } from "../utils/index.mjs";import { useRect, useCustomFieldValue, useEventListener } from "@vant/use";import { useTouch } from "../composables/use-touch.mjs";const [name, bem] = createNamespace("slider");const sliderProps = {  min: makeNumericProp(0),  max: makeNumericProp(100),  step: makeNumericProp(1),  range: Boolean,  reverse: Boolean,  disabled: Boolean,  readonly: Boolean,  vertical: Boolean,  barHeight: numericProp,  buttonSize: numericProp,  activeColor: String,  inactiveColor: String,  modelValue: {    type: [Number, Array],    default: 0  }};var stdin_default = defineComponent({  name,  props: sliderProps,  emits: ["change", "drag-end", "drag-start", "update:modelValue"],  setup(props, {    emit,    slots  }) {    let buttonIndex;    let current;    let startValue;    const root = ref();    const slider = ref();    const dragStatus = ref();    const touch = useTouch();    const scope = computed(() => Number(props.max) - Number(props.min));    const wrapperStyle = computed(() => {      const crossAxis = props.vertical ? "width" : "height";      return {        background: props.inactiveColor,        [crossAxis]: addUnit(props.barHeight)      };    });    const isRange = (val) => props.range && Array.isArray(val);    const calcMainAxis = () => {      const {        modelValue,        min      } = props;      if (isRange(modelValue)) {        return `${(modelValue[1] - modelValue[0]) * 100 / scope.value}%`;      }      return `${(modelValue - Number(min)) * 100 / scope.value}%`;    };    const calcOffset = () => {      const {        modelValue,        min      } = props;      if (isRange(modelValue)) {        return `${(modelValue[0] - Number(min)) * 100 / scope.value}%`;      }      return "0%";    };    const barStyle = computed(() => {      const mainAxis = props.vertical ? "height" : "width";      const style = {        [mainAxis]: calcMainAxis(),        background: props.activeColor      };      if (dragStatus.value) {        style.transition = "none";      }      const getPositionKey = () => {        if (props.vertical) {          return props.reverse ? "bottom" : "top";        }        return props.reverse ? "right" : "left";      };      style[getPositionKey()] = calcOffset();      return style;    });    const format = (value) => {      const min = +props.min;      const max = +props.max;      const step = +props.step;      value = clamp(value, min, max);      const diff = Math.round((value - min) / step) * step;      return addNumber(min, diff);    };    const isSameValue = (newValue, oldValue) => JSON.stringify(newValue) === JSON.stringify(oldValue);    const handleRangeValue = (value) => {      var _a, _b;      const left = (_a = value[0]) != null ? _a : Number(props.min);      const right = (_b = value[1]) != null ? _b : Number(props.max);      return left > right ? [right, left] : [left, right];    };    const updateValue = (value, end) => {      if (isRange(value)) {        value = handleRangeValue(value).map(format);      } else {        value = format(value);      }      if (!isSameValue(value, props.modelValue)) {        emit("update:modelValue", value);      }      if (end && !isSameValue(value, startValue)) {        emit("change", value);      }    };    const onClick = (event) => {      event.stopPropagation();      if (props.disabled || props.readonly) {        return;      }      const {        min,        reverse,        vertical,        modelValue      } = props;      const rect = useRect(root);      const getDelta = () => {        if (vertical) {          if (reverse) {            return rect.bottom - event.clientY;          }          return event.clientY - rect.top;        }        if (reverse) {          return rect.right - event.clientX;        }        return event.clientX - rect.left;      };      const total = vertical ? rect.height : rect.width;      const value = Number(min) + getDelta() / total * scope.value;      if (isRange(modelValue)) {        const [left, right] = modelValue;        const middle = (left + right) / 2;        if (value <= middle) {          updateValue([value, right], true);        } else {          updateValue([left, value], true);        }      } else {        updateValue(value, true);      }    };    const onTouchStart = (event) => {      if (props.disabled || props.readonly) {        return;      }      touch.start(event);      current = props.modelValue;      if (isRange(current)) {        startValue = current.map(format);      } else {        startValue = format(current);      }      dragStatus.value = "start";    };    const onTouchMove = (event) => {      if (props.disabled || props.readonly) {        return;      }      if (dragStatus.value === "start") {        emit("drag-start", event);      }      preventDefault(event, true);      touch.move(event);      dragStatus.value = "dragging";      const rect = useRect(root);      const delta = props.vertical ? touch.deltaY.value : touch.deltaX.value;      const total = props.vertical ? rect.height : rect.width;      let diff = delta / total * scope.value;      if (props.reverse) {        diff = -diff;      }      if (isRange(startValue)) {        const index = props.reverse ? 1 - buttonIndex : buttonIndex;        current[index] = startValue[index] + diff;      } else {        current = startValue + diff;      }      updateValue(current);    };    const onTouchEnd = (event) => {      if (props.disabled || props.readonly) {        return;      }      if (dragStatus.value === "dragging") {        updateValue(current, true);        emit("drag-end", event);      }      dragStatus.value = "";    };    const getButtonClassName = (index) => {      if (typeof index === "number") {        const position = ["left", "right"];        return bem(`button-wrapper`, position[index]);      }      return bem("button-wrapper", props.reverse ? "left" : "right");    };    const renderButtonContent = (value, index) => {      if (typeof index === "number") {        const slot = slots[index === 0 ? "left-button" : "right-button"];        if (slot) {          return slot({            value          });        }      }      if (slots.button) {        return slots.button({          value        });      }      return _createVNode("div", {        "class": bem("button"),        "style": getSizeStyle(props.buttonSize)      }, null);    };    const renderButton = (index) => {      const current2 = typeof index === "number" ? props.modelValue[index] : props.modelValue;      return _createVNode("div", {        "ref": slider,        "role": "slider",        "class": getButtonClassName(index),        "tabindex": props.disabled ? void 0 : 0,        "aria-valuemin": props.min,        "aria-valuenow": current2,        "aria-valuemax": props.max,        "aria-disabled": props.disabled || void 0,        "aria-readonly": props.readonly || void 0,        "aria-orientation": props.vertical ? "vertical" : "horizontal",        "onTouchstartPassive": (event) => {          if (typeof index === "number") {            buttonIndex = index;          }          onTouchStart(event);        },        "onTouchend": onTouchEnd,        "onTouchcancel": onTouchEnd,        "onClick": stopPropagation      }, [renderButtonContent(current2, index)]);    };    updateValue(props.modelValue);    useCustomFieldValue(() => props.modelValue);    useEventListener("touchmove", onTouchMove, {      target: slider    });    return () => _createVNode("div", {      "ref": root,      "style": wrapperStyle.value,      "class": bem({        vertical: props.vertical,        disabled: props.disabled      }),      "onClick": onClick    }, [_createVNode("div", {      "class": bem("bar"),      "style": barStyle.value    }, [props.range ? [renderButton(0), renderButton(1)] : renderButton()])]);  }});export {  stdin_default as default};
 |