Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pointer events with Drag and Drag.Move #1325 #1326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 60 additions & 29 deletions Source/Drag/Drag.js
Original file line number Diff line number Diff line change
@@ -72,7 +72,32 @@ var Drag = this.Drag = new Class({
var isBody = !offsetParent || (/^(?:body|html)$/i).test(offsetParent.tagName);
return isBody ? window : document.id(offsetParent);
})(this.element);
this.selection = 'selectstart' in document ? 'selectstart' : 'mousedown';
if (window.PointerEvent){
this.eventTypes = {
down: 'pointerdown',
move: 'pointermove',
up: 'pointerup'
};
} else if (window.MSPointerEvent && !('onpointerdown' in window)){
// For IE10 support
this.eventTypes = {
down: 'MSPointerDown',
move: 'MSPointerMove',
up: 'MSPointerUp'
};
} else {
this.eventTypes = {
down: 'mousedown',
move: 'mousemove',
up: 'mouseup'
};
this.touchTypes = {
down: 'touchstart',
move: 'touchmove',
up: 'touchend'
};
}
this.selection = 'selectstart' in document ? 'selectstart' : this.eventTypes.down;

this.compensateScroll = {start: {}, diff: {}, last: {}};

@@ -94,15 +119,15 @@ var Drag = this.Drag = new Class({
},

attach: function(){
this.handles.addEvent('mousedown', this.bound.start);
this.handles.addEvent('touchstart', this.bound.start);
this.handles.addEvent(this.eventTypes.down, this.bound.start);
if (this.touchTypes) this.handles.addEvent(this.touchTypes.down, this.bound.start);
if (this.options.compensateScroll) this.offsetParent.addEvent('scroll', this.bound.scrollListener);
return this;
},

detach: function(){
this.handles.removeEvent('mousedown', this.bound.start);
this.handles.removeEvent('touchstart', this.bound.start);
this.handles.removeEvent(this.eventTypes.down, this.bound.start);
if (this.touchTypes) this.handles.removeEvent(this.touchTypes.down, this.bound.start);
if (this.options.compensateScroll) this.offsetParent.removeEvent('scroll', this.bound.scrollListener);
return this;
},
@@ -180,12 +205,13 @@ var Drag = this.Drag = new Class({
y: this.options.grid
};

var events = {
mousemove: this.bound.check,
mouseup: this.bound.cancel,
touchmove: this.bound.check,
touchend: this.bound.cancel
};
var events = {};
events[this.eventTypes.move] = this.bound.check;
events[this.eventTypes.up] = this.bound.cancel;
if (this.touchTypes) {
events[this.touchTypes.move] = this.bound.check;
events[this.touchTypes.up] = this.bound.cancel;
}
events[this.selection] = this.bound.eventStop;
this.document.addEvents(events);
},
@@ -195,12 +221,14 @@ var Drag = this.Drag = new Class({
var distance = Math.round(Math.sqrt(Math.pow(event.page.x - this.mouse.start.x, 2) + Math.pow(event.page.y - this.mouse.start.y, 2)));
if (distance > this.options.snap){
this.cancel();
this.document.addEvents({
mousemove: this.bound.drag,
mouseup: this.bound.stop,
touchmove: this.bound.drag,
touchend: this.bound.stop
});
var events = {};
events[this.eventTypes.move] = this.bound.drag;
events[this.eventTypes.up] = this.bound.stop;
if (this.touchTypes) {
events[this.touchTypes.move] = this.bound.drag;
events[this.touchTypes.up] = this.bound.stop;
}
this.document.addEvents(events);
this.fireEvent('start', [this.element, event]).fireEvent('snap', this.element);
}
},
@@ -234,25 +262,28 @@ var Drag = this.Drag = new Class({
},

cancel: function(event){
this.document.removeEvents({
mousemove: this.bound.check,
mouseup: this.bound.cancel,
touchmove: this.bound.check,
touchend: this.bound.cancel
});
var events = {};
events[this.eventTypes.move] = this.bound.check;
events[this.eventTypes.up] = this.bound.cancel;
if (this.touchTypes) {
events[this.touchTypes.move] = this.bound.check;
events[this.touchTypes.up] = this.bound.cancel;
}
this.document.removeEvents(events);
if (event){
this.document.removeEvent(this.selection, this.bound.eventStop);
this.fireEvent('cancel', this.element);
}
},

stop: function(event){
var events = {
mousemove: this.bound.drag,
mouseup: this.bound.stop,
touchmove: this.bound.drag,
touchend: this.bound.stop
};
var events = {};
events[this.eventTypes.move] = this.bound.drag;
events[this.eventTypes.up] = this.bound.stop;
if (this.touchTypes){
events[this.touchTypes.move] = this.bound.drag;
events[this.touchTypes.up] = this.bound.stop;
}
events[this.selection] = this.bound.eventStop;
this.document.removeEvents(events);
this.mouse.start = null;