Fixed #980 - Touch support for ColorPicker

pull/1021/head
Cagatay Civici 2021-02-17 16:57:54 +03:00
parent bfeb790f77
commit c20bd23e14
1 changed files with 49 additions and 24 deletions

View File

@ -5,12 +5,14 @@
<transition name="p-connected-overlay" @enter="onOverlayEnter" @leave="onOverlayLeave"> <transition name="p-connected-overlay" @enter="onOverlayEnter" @leave="onOverlayLeave">
<div :ref="pickerRef" :class="pickerClass" v-if="inline ? true : overlayVisible"> <div :ref="pickerRef" :class="pickerClass" v-if="inline ? true : overlayVisible">
<div class="p-colorpicker-content"> <div class="p-colorpicker-content">
<div :ref="colorSelectorRef" class="p-colorpicker-color-selector" @mousedown="onColorMousedown"> <div :ref="colorSelectorRef" class="p-colorpicker-color-selector" @mousedown="onColorMousedown($event)"
@touchstart="onColorDragStart($event)" @touchmove="onDrag($event)" @touchend="onDragEnd()">
<div class="p-colorpicker-color"> <div class="p-colorpicker-color">
<div :ref="colorHandleRef" class="p-colorpicker-color-handle"></div> <div :ref="colorHandleRef" class="p-colorpicker-color-handle"></div>
</div> </div>
</div> </div>
<div :ref="hueViewRef" class="p-colorpicker-hue" @mousedown="onHueMousedown"> <div :ref="hueViewRef" class="p-colorpicker-hue" @mousedown="onHueMousedown($event)"
@touchstart="onHueDragStart($event)" @touchmove="onDrag($event)" @touchend="onDragEnd()">
<div :ref="hueHandleRef" class="p-colorpicker-hue-handle"></div> <div :ref="hueHandleRef" class="p-colorpicker-hue-handle"></div>
</div> </div>
</div> </div>
@ -84,8 +86,7 @@ export default {
hueHandle: null, hueHandle: null,
beforeUnmount() { beforeUnmount() {
this.unbindOutsideClickListener(); this.unbindOutsideClickListener();
this.unbindDocumentMouseMoveListener(); this.unbindDragListeners();
this.unbindDocumentMouseUpListener();
this.unbindResizeListener(); this.unbindResizeListener();
if (this.scrollHandler) { if (this.scrollHandler) {
@ -381,26 +382,64 @@ export default {
return; return;
} }
this.bindDragListeners();
this.onColorDragStart(event);
},
onColorDragStart(event) {
if (this.disabled) {
return;
}
this.colorDragging = true; this.colorDragging = true;
this.bindDocumentMouseMoveListener();
this.bindDocumentMouseUpListener();
this.pickColor(event); this.pickColor(event);
DomHandler.addClass(this.$el, 'p-colorpicker-dragging'); DomHandler.addClass(this.$el, 'p-colorpicker-dragging');
event.preventDefault();
},
onDrag(event) {
if (this.colorDragging) {
this.pickColor(event);
event.preventDefault();
}
if (this.hueDragging) {
this.pickHue(event);
event.preventDefault();
}
},
onDragEnd() {
this.colorDragging = false;
this.hueDragging = false;
DomHandler.removeClass(this.$el, 'p-colorpicker-dragging');
this.unbindDragListeners();
}, },
onHueMousedown(event) { onHueMousedown(event) {
if (this.disabled) { if (this.disabled) {
return; return;
} }
this.bindDragListeners();
this.onHueDragStart(event);
},
onHueDragStart(event) {
if (this.disabled) {
return;
}
this.hueDragging = true; this.hueDragging = true;
this.bindDocumentMouseMoveListener();
this.bindDocumentMouseUpListener();
this.pickHue(event); this.pickHue(event);
DomHandler.addClass(this.$el, 'p-colorpicker-dragging'); DomHandler.addClass(this.$el, 'p-colorpicker-dragging');
}, },
isInputClicked(event) { isInputClicked(event) {
return this.$refs.input && this.$refs.input.isSameNode(event.target); return this.$refs.input && this.$refs.input.isSameNode(event.target);
}, },
bindDragListeners() {
this.bindDocumentMouseMoveListener();
this.bindDocumentMouseUpListener();
},
unbindDragListeners() {
this.unbindDocumentMouseMoveListener();
this.unbindDocumentMouseUpListener();
},
bindOutsideClickListener() { bindOutsideClickListener() {
if (!this.outsideClickListener) { if (!this.outsideClickListener) {
this.outsideClickListener = (event) => { this.outsideClickListener = (event) => {
@ -451,15 +490,7 @@ export default {
}, },
bindDocumentMouseMoveListener() { bindDocumentMouseMoveListener() {
if (!this.documentMouseMoveListener) { if (!this.documentMouseMoveListener) {
this.documentMouseMoveListener = (event) => { this.documentMouseMoveListener = this.onDrag.bind(this);
if (this.colorDragging) {
this.pickColor(event);
}
if (this.hueDragging) {
this.pickHue(event);
}
};
document.addEventListener('mousemove', this.documentMouseMoveListener); document.addEventListener('mousemove', this.documentMouseMoveListener);
} }
}, },
@ -471,13 +502,7 @@ export default {
}, },
bindDocumentMouseUpListener() { bindDocumentMouseUpListener() {
if (!this.documentMouseUpListener) { if (!this.documentMouseUpListener) {
this.documentMouseUpListener = () => { this.documentMouseUpListener = this.onDragEnd.bind(this);
this.colorDragging = false;
this.hueDragging = false;
DomHandler.removeClass(this.$el, 'p-colorpicker-dragging');
this.unbindDocumentMouseMoveListener();
this.unbindDocumentMouseUpListener();
};
document.addEventListener('mouseup', this.documentMouseUpListener); document.addEventListener('mouseup', this.documentMouseUpListener);
} }
}, },