2021-03-03 01:18:36 +00:00
|
|
|
<template>
|
|
|
|
<div class="range-container">
|
|
|
|
<div
|
|
|
|
class="label label-start"
|
|
|
|
:class="{ disabled }"
|
|
|
|
@click="setValue('valueA', min)"
|
|
|
|
>
|
|
|
|
<slot name="start" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div
|
|
|
|
class="range"
|
|
|
|
:class="{ disabled }"
|
|
|
|
:style="{ background: `linear-gradient(90deg, var(--slider-track) ${minPercentage}%, var(--slider-range) ${minPercentage}%, var(--slider-range) ${maxPercentage}%, var(--slider-track) ${maxPercentage}%)` }"
|
2021-03-03 13:48:04 +00:00
|
|
|
@click="setNearest"
|
2021-03-03 01:18:36 +00:00
|
|
|
>
|
|
|
|
<input
|
2021-03-03 19:27:45 +00:00
|
|
|
v-model.number="valueA"
|
2021-03-03 01:18:36 +00:00
|
|
|
:min="min"
|
|
|
|
:max="max"
|
|
|
|
:data-value="valueA"
|
|
|
|
:disabled="disabled"
|
|
|
|
type="range"
|
|
|
|
class="slider"
|
2021-03-03 15:47:57 +00:00
|
|
|
@input="emit('input')"
|
|
|
|
@change="emit('change')"
|
2021-03-03 13:54:51 +00:00
|
|
|
@click.stop
|
2021-03-03 01:18:36 +00:00
|
|
|
>
|
|
|
|
|
|
|
|
<input
|
2021-03-03 19:27:45 +00:00
|
|
|
v-model.number="valueB"
|
2021-03-03 01:18:36 +00:00
|
|
|
:min="min"
|
|
|
|
:max="max"
|
|
|
|
:data-value="valueB"
|
|
|
|
:disabled="disabled"
|
|
|
|
type="range"
|
|
|
|
class="slider"
|
2021-03-03 15:47:57 +00:00
|
|
|
@input="emit('input')"
|
|
|
|
@change="emit('change')"
|
2021-03-03 13:54:51 +00:00
|
|
|
@click.stop
|
2021-03-03 01:18:36 +00:00
|
|
|
>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div
|
|
|
|
class="label label-end"
|
|
|
|
:class="{ disabled }"
|
|
|
|
@click="setValue('valueB', max)"
|
|
|
|
>
|
|
|
|
<slot name="end" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-03-03 18:13:09 +00:00
|
|
|
import { nextTick } from 'vue';
|
|
|
|
|
2021-03-03 01:18:36 +00:00
|
|
|
function minValue() {
|
|
|
|
return Math.min(this.valueA, this.valueB);
|
|
|
|
}
|
|
|
|
|
|
|
|
function maxValue() {
|
|
|
|
return Math.max(this.valueA, this.valueB);
|
|
|
|
}
|
|
|
|
|
|
|
|
function minPercentage() {
|
2021-03-03 15:47:57 +00:00
|
|
|
return ((this.minValue - this.min) / (this.max - this.min)) * 100;
|
2021-03-03 01:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function maxPercentage() {
|
2021-03-03 15:47:57 +00:00
|
|
|
return ((this.maxValue - this.min) / (this.max - this.min)) * 100;
|
2021-03-03 01:18:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 15:47:57 +00:00
|
|
|
function emit(type = 'change') {
|
2021-03-03 12:47:27 +00:00
|
|
|
if (this.values) {
|
2021-03-03 15:47:57 +00:00
|
|
|
this.$emit(type, [this.values[this.minValue], this.values[this.maxValue]]);
|
2021-03-03 12:47:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:47:57 +00:00
|
|
|
this.$emit(type, [this.minValue, this.maxValue]);
|
2021-03-03 01:18:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 13:48:04 +00:00
|
|
|
function setNearest(event) {
|
2021-03-03 18:13:09 +00:00
|
|
|
if (this.allowEnable) {
|
|
|
|
this.emit('enable');
|
2021-03-03 13:48:04 +00:00
|
|
|
}
|
2021-03-03 18:13:09 +00:00
|
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
if (!this.disabled) {
|
2021-03-03 18:29:40 +00:00
|
|
|
const closestValue = Math.round((event.offsetX / event.target.getBoundingClientRect().width) * (this.max - this.min)) + this.min;
|
2021-03-03 18:13:09 +00:00
|
|
|
const closestSlider = Math.abs(this.valueA - closestValue) < Math.abs(this.valueB - closestValue) ? 'valueA' : 'valueB';
|
|
|
|
|
|
|
|
this[closestSlider] = closestValue;
|
|
|
|
this.emit();
|
|
|
|
}
|
|
|
|
});
|
2021-03-03 13:48:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 01:18:36 +00:00
|
|
|
function setValue(prop, value) {
|
2021-03-03 18:13:09 +00:00
|
|
|
if (this.allowEnable) {
|
|
|
|
this.emit('enable');
|
2021-03-03 01:18:36 +00:00
|
|
|
}
|
2021-03-03 18:13:09 +00:00
|
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
if (!this.disabled) {
|
|
|
|
this[prop] = value;
|
|
|
|
this.emit();
|
|
|
|
}
|
|
|
|
});
|
2021-03-03 01:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
min: {
|
|
|
|
type: Number,
|
|
|
|
default: 0,
|
|
|
|
},
|
|
|
|
max: {
|
|
|
|
type: Number,
|
|
|
|
default: 10,
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
type: Array,
|
|
|
|
default: () => [3, 7],
|
|
|
|
},
|
2021-03-03 12:47:27 +00:00
|
|
|
values: {
|
|
|
|
type: Array,
|
|
|
|
default: null,
|
|
|
|
},
|
2021-03-03 01:18:36 +00:00
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2021-03-03 18:13:09 +00:00
|
|
|
allowEnable: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2021-03-03 01:18:36 +00:00
|
|
|
},
|
2021-03-03 18:13:09 +00:00
|
|
|
emits: ['change', 'input', 'enable'],
|
2021-03-03 01:18:36 +00:00
|
|
|
data() {
|
2021-03-03 12:47:27 +00:00
|
|
|
if (this.values) {
|
|
|
|
return {
|
|
|
|
valueA: this.values.indexOf(this.value[0]),
|
|
|
|
valueB: this.values.indexOf(this.value[1]),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-03 01:18:36 +00:00
|
|
|
return {
|
|
|
|
valueA: this.value[0],
|
|
|
|
valueB: this.value[1],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
minValue,
|
|
|
|
maxValue,
|
|
|
|
minPercentage,
|
|
|
|
maxPercentage,
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
emit,
|
2021-03-03 13:48:04 +00:00
|
|
|
setNearest,
|
2021-03-03 01:18:36 +00:00
|
|
|
setValue,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
@mixin thumb {
|
|
|
|
appearance: none;
|
|
|
|
display: block;
|
|
|
|
width: 1.25rem;
|
|
|
|
height: 1.25rem;
|
|
|
|
border: none;
|
|
|
|
border-radius: 50%;
|
|
|
|
background: var(--slider-thumb);
|
|
|
|
pointer-events: visible;
|
|
|
|
cursor: pointer;
|
2021-03-03 03:05:27 +00:00
|
|
|
box-shadow: 0 0 3px var(--darken-weak);
|
2021-03-03 01:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.range-container {
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
}
|
|
|
|
|
|
|
|
.range {
|
|
|
|
--slider-track: var(--shadow-hint);
|
|
|
|
--slider-range: var(--primary-faded);
|
|
|
|
--slider-thumb: var(--primary);
|
|
|
|
|
|
|
|
position: relative;
|
|
|
|
height: 1.25rem;
|
|
|
|
flex-grow: 1;
|
|
|
|
border-radius: .625rem;
|
|
|
|
|
|
|
|
&.disabled {
|
|
|
|
--slider-range: var(--shadow-weak);
|
|
|
|
--slider-thumb: var(--disabled-handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.slider {
|
|
|
|
width: 100%;
|
|
|
|
top: 0;
|
|
|
|
margin: 0;
|
|
|
|
appearance: none;
|
|
|
|
position: absolute;
|
|
|
|
background: none;
|
|
|
|
outline: none;
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.slider::-webkit-slider-thumb {
|
|
|
|
@include thumb;
|
|
|
|
}
|
|
|
|
|
|
|
|
.slider::-moz-range-thumb {
|
|
|
|
@include thumb;
|
|
|
|
transform: translateY(2px);
|
|
|
|
}
|
|
|
|
|
|
|
|
.label {
|
|
|
|
padding: 0 .5rem;
|
|
|
|
|
|
|
|
&:hover:not(.disabled) {
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
::v-deep(.icon) {
|
|
|
|
fill: var(--primary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::v-deep(.icon) {
|
2021-03-03 18:13:09 +00:00
|
|
|
width: 1.25rem;
|
|
|
|
height: 1.25rem;
|
2021-03-03 01:18:36 +00:00
|
|
|
flex-shrink: 0;
|
|
|
|
fill: var(--shadow);
|
|
|
|
}
|
|
|
|
</style>
|