Skip to content

Commit

Permalink
馃悶 check for browser window #49 #85 #86
Browse files Browse the repository at this point in the history
Addresses `this = window` mismatch with Webpack, Parcel, Gatsby.
  • Loading branch information
desandro committed Oct 23, 2019
1 parent 75e4f4b commit 3566dcb
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions js/dragger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
// module definition
if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory( root );
module.exports = factory();
} else {
// browser global
root.Zdog.Dragger = factory( root );
root.Zdog.Dragger = factory();
}
}( this, function factory( window ) {
}( this, function factory() {

// quick & dirty drag event stuff
// messes up if multiple pointers/touches

// check for browser window #85
var hasWindow = typeof window != 'undefined';
// event support, default to mouse events
var downEvent = 'mousedown';
var moveEvent = 'mousemove';
var upEvent = 'mouseup';
if ( window.PointerEvent ) {
// PointerEvent, Chrome
downEvent = 'pointerdown';
moveEvent = 'pointermove';
upEvent = 'pointerup';
} else if ( 'ontouchstart' in window ) {
// Touch Events, iOS Safari
downEvent = 'touchstart';
moveEvent = 'touchmove';
upEvent = 'touchend';
if ( hasWindow ) {
if ( window.PointerEvent ) {
// PointerEvent, Chrome
downEvent = 'pointerdown';
moveEvent = 'pointermove';
upEvent = 'pointerup';
} else if ( 'ontouchstart' in window ) {
// Touch Events, iOS Safari
downEvent = 'touchstart';
moveEvent = 'touchmove';
upEvent = 'touchend';
}
}

function noop() {}
Expand Down Expand Up @@ -84,8 +88,10 @@ Dragger.prototype.dragStart = function( event, pointer ) {
event.preventDefault();
this.dragStartX = pointer.pageX;
this.dragStartY = pointer.pageY;
window.addEventListener( moveEvent, this );
window.addEventListener( upEvent, this );
if ( hasWindow ) {
window.addEventListener( moveEvent, this );
window.addEventListener( upEvent, this );
}
this.onDragStart( pointer );
};

Expand Down

0 comments on commit 3566dcb

Please sign in to comment.