forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfieldNoUiSlider.vue
118 lines (109 loc) · 2.45 KB
/
fieldNoUiSlider.vue
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
<template>
<div class="slider" :disabled="disabled" :class="{ 'contain-pips': containPips, 'contain-tooltip': containTooltip }"></div>
</template>
<script>
import abstractField from "../abstractField";
import { isArray, defaults } from "lodash";
export default {
name: "field-noUiSlider",
mixins: [abstractField],
data() {
return {
slider: null
};
},
watch: {
model: function() {
if (window.noUiSlider && this.slider && this.slider.noUiSlider) {
this.slider.noUiSlider.set(this.value);
}
}
},
computed: {
containPips() {
return typeof this.fieldOptions.pips !== "undefined";
},
containTooltip() {
return typeof this.fieldOptions.tooltips !== "undefined";
}
},
methods: {
onChange(value) {
if (isArray(value)) {
// Array (range)
this.value = [parseFloat(value[0]), parseFloat(value[1])];
} else {
// Single value
this.value = parseFloat(value);
}
},
formatValueToField(value) {
if (this.slider !== null && typeof this.slider.noUiSlider !== "undefined") {
this.slider.noUiSlider.set(value);
}
},
formatValueToModel(val) {
if (typeof this.slider.noUiSlider !== "undefined") {
if (val instanceof Array) {
return [Number(val[0]), Number(val[1])];
} else {
return Number(val);
}
}
},
getStartValue() {
if (this.value != null) {
return this.value;
} else {
if (typeof this.fieldOptions.double !== "undefined") {
return [this.fieldOptions.min, this.fieldOptions.min];
} else {
return this.fieldOptions.min;
}
}
}
},
mounted() {
this.$nextTick(() => {
if (window.noUiSlider) {
this.slider = this.$el;
window.noUiSlider.create(
this.slider,
defaults(this.fieldOptions || {}, {
start: this.getStartValue(),
range: {
min: this.fieldOptions.min,
max: this.fieldOptions.max
}
})
);
this.slider.noUiSlider.on("change", this.onChange.bind(this));
} else {
console.warn(
"noUiSlider is missing. Please download from https://github.com/leongersen/noUiSlider and load the script and CSS in the HTML head section!"
);
}
});
},
beforeDestroy() {
if (this.slider) this.slider.noUiSlider.off("change");
}
};
</script>
<style lang="scss">
.vue-form-generator .field-noUiSlider {
.field-wrap {
display: block;
}
.contain-pips {
margin-bottom: 30px;
}
.contain-tooltip {
margin-top: 30px;
}
.noUi-vertical {
height: 200px;
margin: 10px 0;
}
}
</style>