All files / src/components/shared DoubleSliderSelector.vue

100% Statements 52/52
100% Branches 14/14
100% Functions 15/15
100% Lines 49/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                      45x                         4x                           4x                                 2x                                                                       53x                   53x   53x   53x 53x 53x 53x 53x   53x 53x   53x   92x     10x 10x       53x 82x   29x 29x       96x 29x 29x 29x 3x         33x       33x 30x 30x 3x 1x 1x   2x     33x 29x 29x 4x 1x 1x   3x         4x 4x 4x       4x 4x 4x     53x                                                  
<template>
  <v-row
    class="flex-column"
    no-gutters
  >
    <v-row
      v-if="label"
      no-gutters
      justify="start"
    >
      <v-col>
        <span class="pl-0 pb-1 sh-label">{{ label }}</span>
      </v-col>
    </v-row>
    <v-row
      class="py-2 new-style"
      no-gutters
      v-if="!hideInput"
    >
      <v-col cols="6">
        <v-text-field
          class="mr-1"
          data-testid="lower"
          :model-value="sliderState[0]"
          @update:model-value="input[0] = parseInt($event)"
          @blur="onUpdateLower"
          @keyup.enter="onUpdateLower"
          density="compact"
          hide-details
          type="number"
          variant="outlined"
        />
      </v-col>
      <v-col cols="6">
        <v-text-field
          class="ml-1"
          data-testid="upper"
          :model-value="sliderState[1]"
          @update:model-value="input[1] = parseInt($event)"
          @blur="onUpdateHigher"
          @keyup.enter="onUpdateHigher"
          density="compact"
          hide-details
          type="number"
          variant="outlined"
        />
      </v-col>
    </v-row>
    <v-row
      class="pb-0"
      no-gutters
    >
      <v-range-slider
        ref="slider"
        :style="`--thumbBackgroundColor: ${bgColor}`"
        v-model="sliderState"
        data-testid="slider"
        :loading="loading"
        :thumb-label="showThumb"
        thumb-size="16"
        thumb-color="primary"
        :min="min"
        :max="max"
        :step="10"
        hide-details
        :strict="true"
      >
      </v-range-slider>
    </v-row>
  </v-row>
</template>
 
<script setup lang="ts">
import { clamp } from '@/functions/clamp';
import { computed, ref, toRefs, unref, watch } from 'vue';
import { VRangeSlider } from 'vuetify/components';
 
interface Props {
  modelValue?: [number, number];
  loading?: boolean;
  label?: string;
  min?: number;
  max?: number;
  bgColor?: string;
  hideInput?: boolean;
}
 
type Emits = {
  'update:modelValue': [[number, number]];
};
 
const props = withDefaults(defineProps<Props>(), {
  modelValue: () => [0, 0],
  loading: false,
  label: '',
  min: 0,
  max: 100,
  bgColor: 'rgb(var(--v-theme-background))',
  hideInput: false,
});
 
const { label } = toRefs(props);
 
const emit = defineEmits<Emits>();
 
const _modelValue = ref<[number, number]>(props.modelValue);
const input = ref<[number, number]>(props.modelValue);
const showThumb = ref<'always' | boolean>(false);
const showThumbTimeout = ref<number>();
const slider = ref<VRangeSlider>();
 
const rightOffset = ref<string>('');
const leftOffset = ref<string>('');
 
const sliderState = computed({
  get(): [number, number] {
    return _modelValue.value;
  },
  set(value: [number, number]): void {
    _modelValue.value = value;
    emit('update:modelValue', _modelValue.value);
  },
});
 
watch(
  () => props.modelValue,
  (value: [number, number]) => {
    _modelValue.value = value;
    applyRangeSliderLabelBoundaries();
  },
);
 
watch([() => sliderState.value[0], () => sliderState.value[1]], () => {
  clearTimeout(unref(showThumbTimeout));
  showThumb.value = 'always';
  showThumbTimeout.value = setTimeout(() => {
    showThumb.value = false;
  }, 1000);
});
 
function applyRangeSliderLabelBoundaries(): void {
  const thumbs = slider.value?.$el.getElementsByClassName(
    'v-slider-thumb',
  ) as any;
 
  if (thumbs[0]['offsetLeft'] < 10) {
    const offset = -50 + (30 - thumbs[0]['offsetLeft']);
    leftOffset.value = 'translateX(' + offset + '%)';
  } else if (thumbs[0]['offsetLeft'] > 220) {
    const offset = -50 - (40 - (240 - thumbs[0]['offsetLeft']));
    leftOffset.value = 'translateX(' + offset + '%)';
  } else {
    leftOffset.value = 'translateX(-50%)';
  }
 
  if (thumbs[1]['offsetLeft'] < 10) {
    const offset = -50 + (30 - thumbs[0]['offsetLeft']);
    rightOffset.value = 'translateX(' + offset + '%)';
  } else if (thumbs[1]['offsetLeft'] > 220) {
    const offset = -50 - (40 - (240 - thumbs[1]['offsetLeft']));
    rightOffset.value = 'translateX(' + offset + '%)';
  } else {
    rightOffset.value = 'translateX(-50%)';
  }
}
 
function onUpdateLower(): void {
  const higher = sliderState.value[1];
  const lower = clamp(input.value[0], props.min, higher);
  sliderState.value = [lower, higher];
}
 
function onUpdateHigher(): void {
  const lower = sliderState.value[0];
  const higher = clamp(input.value[1], lower, props.max);
  sliderState.value = [lower, higher];
}
 
defineExpose({
  sliderState,
  applyRangeSliderLabelBoundaries,
  leftOffset,
  rightOffset,
});
</script>
 
<style scoped lang="scss">
:deep(.v-slider-thumb) {
  .v-slider-thumb__label {
    transition: none !important;
    height: fit-content;
    transform: v-bind(leftOffset) !important;
  }
 
  ~ .v-slider-thumb {
    .v-slider-thumb__label {
      transition: none !important;
      height: fit-content;
      transform: v-bind(rightOffset) !important;
    }
  }
}
</style>