2017-02-17 11:09:59 +00:00
|
|
|
|
<template>
|
|
|
|
|
<div
|
|
|
|
|
class="el-slider__button-wrapper"
|
|
|
|
|
@mouseenter="handleMouseEnter"
|
|
|
|
|
@mouseleave="handleMouseLeave"
|
|
|
|
|
@mousedown="onButtonDown"
|
|
|
|
|
:class="{ 'hover': hovering, 'dragging': dragging }"
|
2017-04-27 00:28:38 +00:00
|
|
|
|
:style="wrapperStyle"
|
2017-02-17 11:09:59 +00:00
|
|
|
|
ref="button">
|
2017-03-10 14:24:04 +00:00
|
|
|
|
<el-tooltip placement="top" ref="tooltip" :disabled="!showTooltip">
|
2017-04-04 07:40:39 +00:00
|
|
|
|
<span slot="content">{{ formatValue }}</span>
|
2017-02-17 11:09:59 +00:00
|
|
|
|
<div class="el-slider__button" :class="{ 'hover': hovering, 'dragging': dragging }"></div>
|
|
|
|
|
</el-tooltip>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import ElTooltip from 'element-ui/packages/tooltip';
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
name: 'ElSliderButton',
|
|
|
|
|
|
|
|
|
|
components: {
|
|
|
|
|
ElTooltip
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
props: {
|
|
|
|
|
value: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 0
|
2017-04-11 05:44:54 +00:00
|
|
|
|
},
|
|
|
|
|
vertical: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false
|
2017-02-17 11:09:59 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
hovering: false,
|
|
|
|
|
dragging: false,
|
|
|
|
|
startX: 0,
|
|
|
|
|
currentX: 0,
|
2017-04-11 05:44:54 +00:00
|
|
|
|
startY: 0,
|
|
|
|
|
currentY: 0,
|
2017-02-17 11:09:59 +00:00
|
|
|
|
startPosition: 0,
|
|
|
|
|
newPosition: null,
|
|
|
|
|
oldValue: this.value
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
|
disabled() {
|
|
|
|
|
return this.$parent.disabled;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
max() {
|
|
|
|
|
return this.$parent.max;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
min() {
|
|
|
|
|
return this.$parent.min;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
step() {
|
|
|
|
|
return this.$parent.step;
|
|
|
|
|
},
|
|
|
|
|
|
2017-03-10 14:24:04 +00:00
|
|
|
|
showTooltip() {
|
|
|
|
|
return this.$parent.showTooltip;
|
|
|
|
|
},
|
|
|
|
|
|
2017-02-17 11:09:59 +00:00
|
|
|
|
precision() {
|
|
|
|
|
return this.$parent.precision;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
currentPosition() {
|
|
|
|
|
return `${ (this.value - this.min) / (this.max - this.min) * 100 }%`;
|
2017-04-04 07:40:39 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
enableFormat() {
|
|
|
|
|
return this.$parent.formatTooltip instanceof Function;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
formatValue() {
|
|
|
|
|
return this.enableFormat && this.$parent.formatTooltip(this.value) || this.value;
|
2017-04-27 00:28:38 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
wrapperStyle() {
|
|
|
|
|
return this.vertical ? { bottom: this.currentPosition } : { left: this.currentPosition };
|
2017-02-17 11:09:59 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
|
dragging(val) {
|
|
|
|
|
this.$parent.dragging = val;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
methods: {
|
2017-03-10 14:24:04 +00:00
|
|
|
|
displayTooltip() {
|
2017-02-17 11:09:59 +00:00
|
|
|
|
this.$refs.tooltip && (this.$refs.tooltip.showPopper = true);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
hideTooltip() {
|
|
|
|
|
this.$refs.tooltip && (this.$refs.tooltip.showPopper = false);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleMouseEnter() {
|
|
|
|
|
this.hovering = true;
|
2017-03-10 14:24:04 +00:00
|
|
|
|
this.displayTooltip();
|
2017-02-17 11:09:59 +00:00
|
|
|
|
},
|
2017-03-10 14:24:04 +00:00
|
|
|
|
|
2017-02-17 11:09:59 +00:00
|
|
|
|
handleMouseLeave() {
|
|
|
|
|
this.hovering = false;
|
|
|
|
|
this.hideTooltip();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onButtonDown(event) {
|
|
|
|
|
if (this.disabled) return;
|
2017-02-26 10:57:44 +00:00
|
|
|
|
event.preventDefault();
|
2017-02-17 11:09:59 +00:00
|
|
|
|
this.onDragStart(event);
|
|
|
|
|
window.addEventListener('mousemove', this.onDragging);
|
|
|
|
|
window.addEventListener('mouseup', this.onDragEnd);
|
|
|
|
|
window.addEventListener('contextmenu', this.onDragEnd);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onDragStart(event) {
|
|
|
|
|
this.dragging = true;
|
2017-04-11 05:44:54 +00:00
|
|
|
|
if (this.vertical) {
|
|
|
|
|
this.startY = event.clientY;
|
|
|
|
|
} else {
|
|
|
|
|
this.startX = event.clientX;
|
|
|
|
|
}
|
2017-03-21 17:50:57 +00:00
|
|
|
|
this.startPosition = parseFloat(this.currentPosition);
|
2017-02-17 11:09:59 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onDragging(event) {
|
|
|
|
|
if (this.dragging) {
|
2017-03-10 14:24:04 +00:00
|
|
|
|
this.displayTooltip();
|
2017-08-20 14:07:03 +00:00
|
|
|
|
this.$parent.resetSize();
|
2017-04-11 05:44:54 +00:00
|
|
|
|
let diff = 0;
|
|
|
|
|
if (this.vertical) {
|
|
|
|
|
this.currentY = event.clientY;
|
2017-08-03 06:08:08 +00:00
|
|
|
|
diff = (this.startY - this.currentY) / this.$parent.sliderSize * 100;
|
2017-04-11 05:44:54 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.currentX = event.clientX;
|
2017-08-03 06:08:08 +00:00
|
|
|
|
diff = (this.currentX - this.startX) / this.$parent.sliderSize * 100;
|
2017-04-11 05:44:54 +00:00
|
|
|
|
}
|
2017-02-17 11:09:59 +00:00
|
|
|
|
this.newPosition = this.startPosition + diff;
|
|
|
|
|
this.setPosition(this.newPosition);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onDragEnd() {
|
|
|
|
|
if (this.dragging) {
|
|
|
|
|
/*
|
|
|
|
|
* 防止在 mouseup 后立即触发 click,导致滑块有几率产生一小段位移
|
|
|
|
|
* 不使用 preventDefault 是因为 mouseup 和 click 没有注册在同一个 DOM 上
|
|
|
|
|
*/
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.dragging = false;
|
|
|
|
|
this.hideTooltip();
|
|
|
|
|
this.setPosition(this.newPosition);
|
|
|
|
|
}, 0);
|
|
|
|
|
window.removeEventListener('mousemove', this.onDragging);
|
|
|
|
|
window.removeEventListener('mouseup', this.onDragEnd);
|
|
|
|
|
window.removeEventListener('contextmenu', this.onDragEnd);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setPosition(newPosition) {
|
2017-08-08 10:17:40 +00:00
|
|
|
|
if (newPosition === null) return;
|
2017-02-17 11:09:59 +00:00
|
|
|
|
if (newPosition < 0) {
|
|
|
|
|
newPosition = 0;
|
|
|
|
|
} else if (newPosition > 100) {
|
|
|
|
|
newPosition = 100;
|
|
|
|
|
}
|
|
|
|
|
const lengthPerStep = 100 / ((this.max - this.min) / this.step);
|
|
|
|
|
const steps = Math.round(newPosition / lengthPerStep);
|
|
|
|
|
let value = steps * lengthPerStep * (this.max - this.min) * 0.01 + this.min;
|
|
|
|
|
value = parseFloat(value.toFixed(this.precision));
|
|
|
|
|
this.$emit('input', value);
|
|
|
|
|
this.$refs.tooltip && this.$refs.tooltip.updatePopper();
|
|
|
|
|
if (!this.dragging && this.value !== this.oldValue) {
|
|
|
|
|
this.oldValue = this.value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-04-01 06:06:23 +00:00
|
|
|
|
</script>
|