| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { use } from '../utils';
- import { TouchMixin } from '../mixins/touch';
- import { preventDefault } from '../utils/event';
- var _use = use('slider'),
- sfc = _use[0],
- bem = _use[1];
- export default sfc({
- mixins: [TouchMixin],
- props: {
- min: Number,
- value: Number,
- disabled: Boolean,
- vertical: Boolean,
- activeColor: String,
- inactiveColor: String,
- max: {
- type: Number,
- "default": 100
- },
- step: {
- type: Number,
- "default": 1
- },
- barHeight: {
- type: String,
- "default": '2px'
- }
- },
- methods: {
- onTouchStart: function onTouchStart(event) {
- if (this.disabled) return;
- this.touchStart(event);
- this.startValue = this.format(this.value);
- },
- onTouchMove: function onTouchMove(event) {
- preventDefault(event, true);
- if (this.disabled) return;
- this.touchMove(event);
- var rect = this.$el.getBoundingClientRect();
- var delta = this.vertical ? this.deltaY : this.deltaX;
- var total = this.vertical ? rect.height : rect.width;
- var diff = delta / total * 100;
- this.newValue = this.startValue + diff;
- this.updateValue(this.newValue);
- },
- onTouchEnd: function onTouchEnd() {
- if (this.disabled) return;
- this.updateValue(this.newValue, true);
- },
- onClick: function onClick(event) {
- event.stopPropagation();
- if (this.disabled) return;
- var rect = this.$el.getBoundingClientRect();
- var delta = this.vertical ? event.clientY - rect.top : event.clientX - rect.left;
- var total = this.vertical ? rect.height : rect.width;
- var value = delta / total * 100;
- this.updateValue(value, true);
- },
- updateValue: function updateValue(value, end) {
- value = this.format(value);
- this.$emit('input', value);
- if (end) {
- this.$emit('change', value);
- }
- },
- format: function format(value) {
- return Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step;
- }
- },
- render: function render(h) {
- var _barStyle;
- var vertical = this.vertical;
- var style = {
- background: this.inactiveColor
- };
- var mainAxis = vertical ? 'height' : 'width';
- var crossAxis = vertical ? 'width' : 'height';
- var barStyle = (_barStyle = {}, _barStyle[mainAxis] = this.format(this.value) + "%", _barStyle[crossAxis] = this.barHeight, _barStyle.background = this.activeColor, _barStyle);
- return h("div", {
- "style": style,
- "class": bem({
- disabled: this.disabled,
- vertical: vertical
- }),
- "on": {
- "click": this.onClick
- }
- }, [h("div", {
- "class": bem('bar'),
- "style": barStyle
- }, [h("div", {
- "class": bem('button-wrapper'),
- "on": {
- "touchstart": this.onTouchStart,
- "touchmove": this.onTouchMove,
- "touchend": this.onTouchEnd,
- "touchcancel": this.onTouchEnd
- }
- }, [this.slots('button') || h("div", {
- "class": bem('button')
- })])])]);
- }
- });
|