Skip to content

Commit e5930d6

Browse files
committed
fix(QSlider/QRange): emitting @input/@change
1 parent 98febd5 commit e5930d6

File tree

4 files changed

+79
-78
lines changed

4 files changed

+79
-78
lines changed

src/components/knob/QKnob.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export default {
215215
}, [
216216
h('div', {
217217
on: {
218-
click: e => !this.dragging && this.__onInput(e, undefined, true)
218+
click: e => !this.dragging && this.__onInput(e, void 0, true)
219219
},
220220
directives: [{
221221
name: 'touch-pan',

src/components/range/QRange.js

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ export default {
118118
}
119119
},
120120
methods: {
121-
__setActive (event) {
121+
__getDragging (event) {
122122
let
123123
container = this.$refs.handle,
124124
width = container.offsetWidth,
125125
sensitivity = (this.dragOnlyRange ? -1 : 1) * this.$refs.handleMin.offsetWidth / (2 * width)
126126

127-
this.dragging = {
127+
let dragging = {
128128
left: container.getBoundingClientRect().left,
129129
width,
130130
valueMin: this.model.min,
@@ -134,7 +134,7 @@ export default {
134134
}
135135

136136
let
137-
percentage = getPercentage(event, this.dragging, this.$q.i18n.rtl),
137+
percentage = getPercentage(event, dragging, this.$q.i18n.rtl),
138138
type
139139

140140
if (percentage < this.currentMinPercentage + sensitivity) {
@@ -143,10 +143,10 @@ export default {
143143
else if (percentage < this.currentMaxPercentage - sensitivity) {
144144
if (this.dragRange || this.dragOnlyRange) {
145145
type = dragType.RANGE
146-
extend(this.dragging, {
146+
extend(dragging, {
147147
offsetPercentage: percentage,
148148
offsetModel: getModel(percentage, this.min, this.max, this.step, this.computedDecimals),
149-
rangeValue: this.dragging.valueMax - this.dragging.valueMin,
149+
rangeValue: dragging.valueMax - dragging.valueMin,
150150
rangePercentage: this.currentMaxPercentage - this.currentMinPercentage
151151
})
152152
}
@@ -161,89 +161,84 @@ export default {
161161
}
162162

163163
if (this.dragOnlyRange && type !== dragType.RANGE) {
164-
setTimeout(() => {
165-
this.dragging = false
166-
}, 100)
167-
return
164+
return false
168165
}
169166

170-
this.dragging.type = type
171-
this.__update(event)
167+
dragging.type = type
168+
return dragging
172169
},
173-
__update (event) {
170+
__move (event, dragging = this.dragging) {
174171
let
175-
percentage = getPercentage(event, this.dragging, this.$q.i18n.rtl),
172+
percentage = getPercentage(event, dragging, this.$q.i18n.rtl),
176173
model = getModel(percentage, this.min, this.max, this.step, this.computedDecimals),
177174
pos
178175

179-
switch (this.dragging.type) {
176+
switch (dragging.type) {
180177
case dragType.MIN:
181-
if (percentage <= this.dragging.percentageMax) {
178+
if (percentage <= dragging.percentageMax) {
182179
pos = {
183180
minP: percentage,
184-
maxP: this.dragging.percentageMax,
181+
maxP: dragging.percentageMax,
185182
min: model,
186-
max: this.dragging.valueMax
183+
max: dragging.valueMax
187184
}
188185
}
189186
else {
190187
pos = {
191-
minP: this.dragging.percentageMax,
188+
minP: dragging.percentageMax,
192189
maxP: percentage,
193-
min: this.dragging.valueMax,
190+
min: dragging.valueMax,
194191
max: model
195192
}
196193
}
197194
break
198195

199196
case dragType.MAX:
200-
if (percentage >= this.dragging.percentageMin) {
197+
if (percentage >= dragging.percentageMin) {
201198
pos = {
202-
minP: this.dragging.percentageMin,
199+
minP: dragging.percentageMin,
203200
maxP: percentage,
204-
min: this.dragging.valueMin,
201+
min: dragging.valueMin,
205202
max: model
206203
}
207204
}
208205
else {
209206
pos = {
210207
minP: percentage,
211-
maxP: this.dragging.percentageMin,
208+
maxP: dragging.percentageMin,
212209
min: model,
213-
max: this.dragging.valueMin
210+
max: dragging.valueMin
214211
}
215212
}
216213
break
217214

218215
case dragType.RANGE:
219216
let
220-
percentageDelta = percentage - this.dragging.offsetPercentage,
221-
minP = between(this.dragging.percentageMin + percentageDelta, 0, 1 - this.dragging.rangePercentage),
222-
modelDelta = model - this.dragging.offsetModel,
223-
min = between(this.dragging.valueMin + modelDelta, this.min, this.max - this.dragging.rangeValue)
217+
percentageDelta = percentage - dragging.offsetPercentage,
218+
minP = between(dragging.percentageMin + percentageDelta, 0, 1 - dragging.rangePercentage),
219+
modelDelta = model - dragging.offsetModel,
220+
min = between(dragging.valueMin + modelDelta, this.min, this.max - dragging.rangeValue)
224221

225222
pos = {
226223
minP,
227-
maxP: minP + this.dragging.rangePercentage,
224+
maxP: minP + dragging.rangePercentage,
228225
min: parseFloat(min.toFixed(this.computedDecimals)),
229-
max: parseFloat((min + this.dragging.rangeValue).toFixed(this.computedDecimals))
226+
max: parseFloat((min + dragging.rangeValue).toFixed(this.computedDecimals))
230227
}
231228
break
232229
}
233230

234231
this.currentMinPercentage = pos.minP
235232
this.currentMaxPercentage = pos.maxP
236-
this.__updateInput(pos)
233+
this.model = {
234+
min: pos.min,
235+
max: pos.max
236+
}
237237
},
238-
__end () {
238+
__end (event, dragging = this.dragging) {
239+
this.__move(event, dragging)
239240
this.currentMinPercentage = (this.model.min - this.min) / (this.max - this.min)
240241
this.currentMaxPercentage = (this.model.max - this.min) / (this.max - this.min)
241-
this.__endEmit()
242-
},
243-
__updateInput ({min = this.model.min, max = this.model.max}) {
244-
const model = {min, max}
245-
this.model = model
246-
this.$emit('input', model)
247242
},
248243
__validateProps () {
249244
if (this.min >= this.max) {

src/components/slider/QSlider.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,31 @@ export default {
6868
}
6969
},
7070
methods: {
71-
__setActive (event) {
72-
let container = this.$refs.handle
73-
74-
this.dragging = {
71+
__getDragging (evt) {
72+
const container = this.$refs.handle
73+
return {
7574
left: container.getBoundingClientRect().left,
7675
width: container.offsetWidth
7776
}
78-
this.__update(event)
7977
},
80-
__update (event) {
81-
let
82-
percentage = getPercentage(event, this.dragging, this.$q.i18n.rtl),
83-
model = getModel(percentage, this.min, this.max, this.step, this.computedDecimals)
78+
__move (event) {
79+
const percentage = getPercentage(
80+
event,
81+
this.dragging,
82+
this.$q.i18n.rtl
83+
)
8484

8585
this.currentPercentage = percentage
86-
this.model = model
87-
this.$emit('input', model)
86+
this.model = getModel(percentage, this.min, this.max, this.step, this.computedDecimals)
8887
},
89-
__end () {
88+
__end (event, dragging = this.dragging) {
89+
const percentage = getPercentage(
90+
event,
91+
dragging,
92+
this.$q.i18n.rtl
93+
)
94+
this.model = getModel(percentage, this.min, this.max, this.step, this.computedDecimals)
9095
this.currentPercentage = (this.model - this.min) / (this.max - this.min)
91-
this.__endEmit()
9296
},
9397
__validateProps () {
9498
if (this.min >= this.max) {

src/components/slider/slider-utils.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ export let SliderMixin = {
5959
readonly: Boolean,
6060
disable: Boolean
6161
},
62-
data () {
63-
return {
64-
clickDisabled: false
65-
}
66-
},
6762
computed: {
6863
editable () {
6964
return !this.disable && !this.readonly
@@ -98,37 +93,44 @@ export let SliderMixin = {
9893
methods: {
9994
__pan (event) {
10095
if (event.isFinal) {
101-
this.clickDisabled = true
102-
this.$nextTick(() => {
103-
this.clickDisabled = false
104-
})
105-
this.__end(event.evt)
96+
if (this.dragging) {
97+
this.dragTimer = setTimeout(() => {
98+
this.dragging = false
99+
}, 100)
100+
this.__end(event.evt)
101+
this.__update(true)
102+
}
106103
}
107104
else if (event.isFirst) {
108-
this.__setActive(event.evt)
105+
clearTimeout(this.dragTimer)
106+
this.dragging = this.__getDragging(event.evt)
109107
}
110108
else if (this.dragging) {
111-
this.__update(event.evt)
109+
this.__move(event.evt)
110+
this.__update()
112111
}
113112
},
114-
__endEmit () {
115-
setTimeout(() => {
116-
this.dragging = false
117-
}, 100)
113+
__update (change) {
114+
if (JSON.stringify(this.model) === JSON.stringify(this.value)) {
115+
return
116+
}
118117
this.$emit('input', this.model)
119-
this.$nextTick(() => {
120-
this.$emit('dragend', this.model)
121-
if (JSON.stringify(this.model) !== JSON.stringify(this.value)) {
122-
this.$emit('change', this.model)
123-
}
124-
})
118+
if (change) {
119+
this.$nextTick(() => {
120+
if (JSON.stringify(this.model) !== JSON.stringify(this.value)) {
121+
this.$emit('change', this.model)
122+
}
123+
})
124+
}
125125
},
126126
__click (event) {
127-
if (this.clickDisabled || this.dragging) {
128-
return
127+
if (!this.dragging) {
128+
const dragging = this.__getDragging(event)
129+
if (dragging) {
130+
this.__end(event, dragging)
131+
this.__update(true)
132+
}
129133
}
130-
this.__setActive(event)
131-
this.__end(event)
132134
},
133135
__getMarkers (h) {
134136
if (!this.markers) {

0 commit comments

Comments
 (0)