Skip to content

Commit 480eb31

Browse files
committed
improve slider
1 parent 8c12d4a commit 480eb31

File tree

1 file changed

+75
-19
lines changed

1 file changed

+75
-19
lines changed

src/components/Inputs/SliderInput/SliderInput.vue

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<template>
22
<div class="SliderInput ml-3 mr-3 pl-3 pr-3">
33
<vue-slider v-model="input" :lazy="true" :data="interval"
4-
:marks="true" :process="false"></vue-slider>
4+
:marks="getMarks" :process="false"></vue-slider>
55
<b-row class="mt-3 pt-3 pl-0 pr-0">
66
<div class="col text-left pr-0 pl-0">
77
<span v-if="getMinImageLabel">
88
<img class="imgLabel" :src="getMinImageLabel" />
99
</span>
1010
<p>{{ getMinLabel }}</p>
1111
</div>
12+
<div class="col text-center pr-0 pl-0" v-if="getMidLabel">
13+
<span v-if="getMidImageLabel">
14+
<img class="imgLabel" :src="getMidImageLabel" />
15+
</span>
16+
<p>{{ getMidLabel }}</p>
17+
</div>
1218
<div class="col text-right pr-0 pl-0">
1319
<span v-if="getMaxImageLabel">
1420
<img class="imgLabel" :src="getMaxImageLabel" />
@@ -47,43 +53,93 @@ export default {
4753
},
4854
},
4955
computed: {
56+
choices() {
57+
return this.constraints['http://schema.repronim.org/choices'] || [];
58+
},
59+
5060
interval() {
51-
return _.map(this.constraints['http://schema.repronim.org/choices'], (v) => {
52-
// console.log(52, v['http://schema.repronim.org/value'][0]['@value']);
53-
return v['http://schema.repronim.org/value'][0]['@value'];
54-
});
55-
// const diff = choices[1]-choices[0];
56-
// console.log(55, choices, diff, choices.unshift(choices[0] - diff));
57-
// return choices;
61+
const minValue = this.constraints['http://schema.org/minValue'];
62+
const maxValue = this.constraints['http://schema.org/maxValue'];
63+
64+
if (minValue && maxValue) {
65+
const min = minValue[0]['@value'];
66+
const max = maxValue[0]['@value'];
67+
return Array.from({length: max - min + 1}, (_, i) => min + i);
68+
}
69+
70+
return this.choices.map(v => v['http://schema.repronim.org/value'][0]['@value']);
5871
},
72+
5973
getMinLabel() {
60-
if (this.constraints['http://schema.repronim.org/choices']) {
61-
const activeMinLabel = _.filter(this.constraints['http://schema.repronim.org/choices'][0]['http://schema.org/name'], labels => labels['@language'] === this.selected_language);
62-
return activeMinLabel[0]['@value'];
63-
} return 'no min label';
74+
if (this.choices.length) {
75+
const activeLabel = _.find(this.choices[0]['http://schema.org/name'],
76+
label => label['@language'] === this.selected_language);
77+
return activeLabel ? activeLabel['@value'] : '';
78+
}
79+
return '';
6480
},
81+
6582
getMaxLabel() {
66-
if (this.constraints['http://schema.repronim.org/choices']) {
67-
const choicesLength = (this.constraints['http://schema.repronim.org/choices']).length;
68-
const activeMaxLabel = _.filter(this.constraints['http://schema.repronim.org/choices'][choicesLength-1]['http://schema.org/name'], labels => labels['@language'] === this.selected_language);
69-
return activeMaxLabel[0]['@value'];
70-
} return 'no max label';
83+
if (this.choices.length) {
84+
const activeLabel = _.find(this.choices[this.choices.length - 1]['http://schema.org/name'],
85+
label => label['@language'] === this.selected_language);
86+
return activeLabel ? activeLabel['@value'] : '';
87+
}
88+
return '';
7189
},
90+
7291
getMinImageLabel() {
73-
const vcList = this.constraints['http://schema.repronim.org/choices'];
92+
const vcList = this.choices;
7493
if (vcList[0]['http://schema.org/image']) {
7594
return vcList[0]['http://schema.org/image'][0]['@value'];
7695
}
7796
return false;
7897
},
98+
7999
getMaxImageLabel() {
80-
const vcList = this.constraints['http://schema.repronim.org/choices'];
100+
const vcList = this.choices;
81101
const N = vcList.length;
82102
if (vcList[N - 1]['http://schema.org/image']) {
83103
return vcList[N - 1]['http://schema.org/image'][0]['@value'];
84104
}
85105
return false;
86106
},
107+
108+
getMarks() {
109+
if (this.constraints['http://schema.org/minValue'] &&
110+
this.constraints['http://schema.org/maxValue']) {
111+
const min = this.constraints['http://schema.org/minValue'][0]['@value'];
112+
const max = this.constraints['http://schema.org/maxValue'][0]['@value'];
113+
const choices = this.choices;
114+
115+
if (choices) {
116+
const numChoices = choices.length;
117+
const step = (max - min) / (numChoices - 1);
118+
return Array.from({length: numChoices}, (_, i) => min + i * step);
119+
}
120+
}
121+
return true; // fallback to default behavior
122+
},
123+
124+
getMidLabel() {
125+
if (this.choices.length % 2 === 1) { // Check if odd number of choices
126+
const midIndex = Math.floor(this.choices.length / 2);
127+
const activeMidLabel = _.find(this.choices[midIndex]['http://schema.org/name'],
128+
label => label['@language'] === this.selected_language);
129+
return activeMidLabel ? activeMidLabel['@value'] : '';
130+
}
131+
return '';
132+
},
133+
134+
getMidImageLabel() {
135+
if (this.choices.length % 2 === 1) {
136+
const midIndex = Math.floor(this.choices.length / 2);
137+
if (this.choices[midIndex]['http://schema.org/image']) {
138+
return this.choices[midIndex]['http://schema.org/image'][0]['@value'];
139+
}
140+
}
141+
return false;
142+
},
87143
},
88144
data() {
89145
return {

0 commit comments

Comments
 (0)