<template>
    <view :class="'custom-class ' + utils.bem('slider', { disabled })" :style="inactiveColor ? 'background:' + inactiveColor : ''" @tap="onClick">
        <view class="van-slider__bar" :style="barStyle + ';' + computed.barStyle(barHeight, activeColor)">
            <view class="van-slider__button-wrapper" @touchstart="onTouchStart" @touchmove.stop.prevent="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
                <slot v-if="useButtonSlot" name="button" />
                <view v-else class="van-slider__button" />
            </view>
        </view>
    </view>
</template>
<script module="utils" lang="wxs" src="@/miniprogram_npm/@vant/weapp/wxs/utils.wxs"></script>
<script module="computed" lang="wxs" src="@/miniprogram_npm/@vant/weapp/slider/index.wxs"></script>
<script>
'use strict';
Object.defineProperty(exports, '__esModule', {
    value: true
});
var component_1 = require('../common/component');
var touch_1 = require('../mixins/touch');
var version_1 = require('../common/version');
component_1.VantComponent({
    mixins: [touch_1.touch],
    props: {
        disabled: Boolean,
        useButtonSlot: Boolean,
        activeColor: String,
        inactiveColor: String,
        max: {
            type: Number,
            value: 100
        },
        min: {
            type: Number,
            value: 0
        },
        step: {
            type: Number,
            value: 1
        },
        value: {
            type: Number,
            value: 0,
            observer: 'updateValue'
        },
        barHeight: {
            type: null,
            value: '2px'
        }
    },
    created: function () {
        this.updateValue(this.value);
    },
    methods: {
        onTouchStart: function (event) {
            if (this.disabled) {
                return;
            }
            this.touchStart(event);
            this.startValue = this.format(this.value);
            this.dragStatus = 'start';
        },
        onTouchMove: function (event) {
            var that = this;
            if (this.disabled) {
                return;
            }
            if (this.dragStatus === 'start') {
                this.$emit('drag-start');
            }
            this.touchMove(event);
            this.dragStatus = 'draging';
            this.getRect('.van-slider').then(function (rect) {
                var diff = (that.deltaX / rect.width) * 100;
                that.newValue = that.startValue + diff;
                that.updateValue(that.newValue, false, true);
            });
        },
        onTouchEnd: function () {
            if (this.disabled) {
                return;
            }
            if (this.dragStatus === 'draging') {
                this.updateValue(this.newValue, true);
                this.$emit('drag-end');
            }
        },
        onClick: function (event) {
            var that = this;
            if (this.disabled) {
                return;
            }
            var min = this.min;
            this.getRect('.van-slider').then(function (rect) {
                var value = ((event.detail.x - rect.left) / rect.width) * that.getRange() + min;
                that.updateValue(value, true);
            });
        },
        updateValue: function (value, end, drag) {
            value = this.format(value);
            var min = this.min;
            var width = ((value - min) * 100) / this.getRange() + '%';
            this.setData({
                value: value,
                barStyle: '\n          width: ' + width + ';\n          ' + (drag ? 'transition: none;' : '') + '\n        '
            });
            if (drag) {
                this.$emit('drag', {
                    value: value
                });
            }
            if (end) {
                this.$emit('change', value);
            }
            if ((drag || end) && version_1.canIUseModel()) {
                this.setData({
                    value: value
                });
            }
        },
        getRange: function () {
            var _a = this;
            var max = _a.max;
            var min = _a.min;
            return max - min;
        },
        format: function (value) {
            var _a = this;
            var max = _a.max;
            var min = _a.min;
            var step = _a.step;
            return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
        }
    }
});
</script>
<style>
@import './index.css';
</style>
