Skip to content

Commit e2cd409

Browse files
committed
Added support for touches #2
1 parent f60dd33 commit e2cd409

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

dist/index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,14 @@
409409
};
410410
},
411411
dragStart: function(event) {
412+
var e = event.touches ? event.touches[0] : event;
412413
this.zIndex = 2, this.shiftX = this.shiftStartX = this.left, this.shiftY = this.shiftStartY = this.top,
413-
this.mouseMoveStartX = event.pageX, this.mouseMoveStartY = event.pageY, this.animate = !1,
414+
this.mouseMoveStartX = e.pageX, this.mouseMoveStartY = e.pageY, this.animate = !1,
414415
this.dragging = !0, document.addEventListener("mousemove", this.documentMouseMove),
415-
this.$emit("dragstart", this.wrapEvent(event));
416+
document.addEventListener("touchmove", this.documentMouseMove), this.$emit("dragstart", this.wrapEvent(event));
416417
},
417418
drag: function(event) {
418-
var distanceX = event.pageX - this.mouseMoveStartX, distanceY = event.pageY - this.mouseMoveStartY;
419+
var e = event.touches ? event.touches[0] : event, distanceX = e.pageX - this.mouseMoveStartX, distanceY = e.pageY - this.mouseMoveStartY;
419420
this.shiftX = distanceX + this.shiftStartX, this.shiftY = distanceY + this.shiftStartY;
420421
var gridX = Math.round(this.shiftX / this.cellWidth), gridY = Math.round(this.shiftY / this.cellHeight);
421422
gridX = Math.min(gridX, this.rowCount - 1), gridY = Math.max(gridY, 0);
@@ -436,7 +437,8 @@
436437
var _this2 = this;
437438
this.draggable && (this.timer = setTimeout(function() {
438439
_this2.dragStart(event);
439-
}, this.dragDelay), document.addEventListener("mouseup", this.documentMouseUp));
440+
}, this.dragDelay), document.addEventListener("mouseup", this.documentMouseUp),
441+
document.addEventListener("touchend", this.documentMouseUp));
440442
},
441443
documentMouseMove: function(event) {
442444
this.draggable && this.dragging && this.drag(event);
@@ -446,9 +448,10 @@
446448
var dx = this.shiftStartX - this.shiftX, dy = this.shiftStartY - this.shiftY, distance = Math.sqrt(dx * dx + dy * dy);
447449
this.animate = !0, this.dragging = !1, this.mouseMoveStartX = 0, this.mouseMoveStartY = 0,
448450
this.shiftStartX = 0, this.shiftStartY = 0, document.removeEventListener("mousemove", this.documentMouseMove),
449-
document.removeEventListener("mouseup", this.documentMouseUp);
451+
document.removeEventListener("touchmove", this.documentMouseMove), document.removeEventListener("mouseup", this.documentMouseUp),
452+
document.removeEventListener("touchend", this.documentMouseUp);
450453
var $event = this.wrapEvent(event);
451-
distance < 5 && this.$emit("click", $event), this.$emit("dragend", $event);
454+
distance < 4 && this.$emit("click", $event), this.$emit("dragend", $event);
452455
}
453456
}
454457
};
@@ -546,7 +549,8 @@
546549
class: _vm.className,
547550
style: _vm.style,
548551
on: {
549-
mousedown: _vm.mousedown
552+
mousedown: _vm.mousedown,
553+
touchstart: _vm.mousedown
550554
}
551555
}, [ _vm._t("default") ], 2);
552556
},

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-js-grid",
33
"description": "Vue.js responsive grid plugin",
4-
"version": "1.0.0-rc2",
4+
"version": "1.0.1",
55
"author": "euvl <[email protected]>",
66
"private": false,
77
"scripts": {

src/GridItem.vue

+16-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
<div ref="self"
33
:class="className"
44
:style="style"
5-
@mousedown="mousedown">
5+
@mousedown="mousedown"
6+
@touchstart="mousedown">
67
<slot/>
78
</div>
89
</template>
910

1011
<script>
11-
const CLICK_PIXEL_DISTANCE = 5
12+
const CLICK_PIXEL_DISTANCE = 4
1213
1314
export default {
1415
name: 'GridItem',
@@ -109,26 +110,30 @@ export default {
109110
},
110111
111112
dragStart (event) {
113+
let e = event.touches ? event.touches[0] : event
114+
112115
this.zIndex = 2
113116
114117
this.shiftX = this.shiftStartX = this.left
115118
this.shiftY = this.shiftStartY = this.top
116119
117-
this.mouseMoveStartX = event.pageX
118-
this.mouseMoveStartY = event.pageY
120+
this.mouseMoveStartX = e.pageX
121+
this.mouseMoveStartY = e.pageY
119122
120123
this.animate = false
121124
this.dragging = true
122125
123126
document.addEventListener('mousemove', this.documentMouseMove)
124-
127+
document.addEventListener('touchmove', this.documentMouseMove)
125128
126129
this.$emit('dragstart', this.wrapEvent(event))
127130
},
128131
129132
drag (event) {
130-
let distanceX = event.pageX - this.mouseMoveStartX
131-
let distanceY = event.pageY - this.mouseMoveStartY
133+
let e = event.touches ? event.touches[0] : event
134+
135+
let distanceX = e.pageX - this.mouseMoveStartX
136+
let distanceY = e.pageY - this.mouseMoveStartY
132137
133138
this.shiftX = distanceX + this.shiftStartX
134139
this.shiftY = distanceY + this.shiftStartY
@@ -163,6 +168,7 @@ export default {
163168
}, this.dragDelay)
164169
165170
document.addEventListener('mouseup', this.documentMouseUp)
171+
document.addEventListener('touchend', this.documentMouseUp)
166172
}
167173
},
168174
@@ -191,7 +197,10 @@ export default {
191197
this.shiftStartY = 0
192198
193199
document.removeEventListener('mousemove', this.documentMouseMove)
200+
document.removeEventListener('touchmove', this.documentMouseMove)
201+
194202
document.removeEventListener('mouseup', this.documentMouseUp)
203+
document.removeEventListener('touchend', this.documentMouseUp)
195204
196205
let $event = this.wrapEvent(event)
197206

0 commit comments

Comments
 (0)