forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfieldNoUiSlider.vue
116 lines (104 loc) · 2.6 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
<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 {
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 (this.schema.noUiSliderOptions && (typeof this.schema.noUiSliderOptions.pips) !== "undefined");
},
containTooltip(){
return (this.schema.noUiSliderOptions && this.schema.noUiSliderOptions.tooltips);
}
},
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.schema.noUiSliderOptions !== "undefined" && this.schema.noUiSliderOptions.double) {
return [this.schema.min, this.schema.min];
}else{
return this.schema.min;
}
}
}
},
mounted() {
this.$nextTick(function () {
if (window.noUiSlider) {
this.slider = this.$el;
window.noUiSlider.create(this.slider, defaults(this.schema.noUiSliderOptions || {}, {
start: this.getStartValue(),
range: {
"min": this.schema.min,
"max": this.schema.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="sass">
.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>