Skip to content

Commit ed3ee1d

Browse files
author
Adam Bradley
committed
fix(scroll): Scrolling using pointer events
Scroller only worked with touch or mouse events, but not with MSPointer or standard pointer events.
1 parent addf75a commit ed3ee1d

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

Diff for: js/views/scrollView.js

+38-30
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,12 @@ ionic.views.Scroll = ionic.views.View.inherit({
678678
ionic.scroll.isScrolling = false;
679679
});
680680

681+
function getEventTouches(e) {
682+
return e.touches && e.touches.length ? e.touches : [{
683+
pageX: e.pageX,
684+
pageY: e.pageY
685+
}];
686+
}
681687

682688
self.touchStart = function(e) {
683689
self.startCoordinates = getPointerCoordinates(e);
@@ -686,6 +692,8 @@ ionic.views.Scroll = ionic.views.View.inherit({
686692
return;
687693
}
688694

695+
self.__isDown = true;
696+
689697
if( ionic.tap.containsOrIsTextInput(e.target) || e.target.tagName === 'SELECT' ) {
690698
// do not start if the target is a text input
691699
// if there is a touchmove on this input, then we can start the scroll
@@ -696,12 +704,13 @@ ionic.views.Scroll = ionic.views.View.inherit({
696704
self.__isSelectable = true;
697705
self.__enableScrollY = true;
698706
self.__hasStarted = true;
699-
self.doTouchStart(e.touches, e.timeStamp);
707+
self.doTouchStart(getEventTouches(e), e.timeStamp);
700708
e.preventDefault();
701709
};
702710

703711
self.touchMove = function(e) {
704-
if(e.defaultPrevented ||
712+
if(!self.__isDown ||
713+
e.defaultPrevented ||
705714
(e.target.tagName === 'TEXTAREA' && e.target.parentElement.querySelector(':focus')) ) {
706715
return;
707716
}
@@ -710,7 +719,7 @@ ionic.views.Scroll = ionic.views.View.inherit({
710719
// the target is a text input and scroll has started
711720
// since the text input doesn't start on touchStart, do it here
712721
self.__hasStarted = true;
713-
self.doTouchStart(e.touches, e.timeStamp);
722+
self.doTouchStart(getEventTouches(e), e.timeStamp);
714723
e.preventDefault();
715724
return;
716725
}
@@ -736,11 +745,15 @@ ionic.views.Scroll = ionic.views.View.inherit({
736745
}
737746
}
738747

739-
self.doTouchMove(e.touches, e.timeStamp, e.scale);
748+
self.doTouchMove(getEventTouches(e), e.timeStamp, e.scale);
749+
self.__isDown = true;
740750
};
741751

742752
self.touchEnd = function(e) {
753+
if(!self.__isDown) return;
754+
743755
self.doTouchEnd(e.timeStamp);
756+
self.__isDown = false;
744757
self.__hasStarted = false;
745758
self.__isSelectable = true;
746759
self.__enableScrollY = true;
@@ -756,25 +769,36 @@ ionic.views.Scroll = ionic.views.View.inherit({
756769
self.options.orgScrollingComplete();
757770
};
758771

759-
760772
if ('ontouchstart' in window) {
773+
// Touch Events
761774
container.addEventListener("touchstart", self.touchStart, false);
762775
document.addEventListener("touchmove", self.touchMove, false);
763776
document.addEventListener("touchend", self.touchEnd, false);
764777
document.addEventListener("touchcancel", self.touchEnd, false);
765778

766-
} else {
779+
} else if (window.navigator.pointerEnabled) {
780+
// Pointer Events
781+
container.addEventListener("pointerdown", self.touchStart, false);
782+
document.addEventListener("pointermove", self.touchMove, false);
783+
document.addEventListener("pointerup", self.touchEnd, false);
784+
document.addEventListener("pointercancel", self.touchEnd, false);
785+
786+
} else if (window.navigator.msPointerEnabled) {
787+
// IE10, WP8 (Pointer Events)
788+
container.addEventListener("MSPointerDown", self.touchStart, false);
789+
document.addEventListener("MSPointerMove", self.touchMove, false);
790+
document.addEventListener("MSPointerUp", self.touchEnd, false);
791+
document.addEventListener("MSPointerCancel", self.touchEnd, false);
767792

793+
} else {
794+
// Mouse Events
768795
var mousedown = false;
769796

770797
container.addEventListener("mousedown", function(e) {
771798
if ( ionic.tap.ignoreScrollStart(e) || e.target.tagName === 'SELECT' ) {
772799
return;
773800
}
774-
self.doTouchStart([{
775-
pageX: e.pageX,
776-
pageY: e.pageY
777-
}], e.timeStamp);
801+
self.doTouchStart(getEventTouches(e), e.timeStamp);
778802

779803
e.preventDefault();
780804
mousedown = true;
@@ -785,10 +809,7 @@ ionic.views.Scroll = ionic.views.View.inherit({
785809
return;
786810
}
787811

788-
self.doTouchMove([{
789-
pageX: e.pageX,
790-
pageY: e.pageY
791-
}], e.timeStamp);
812+
self.doTouchMove(getEventTouches(e), e.timeStamp);
792813

793814
mousedown = true;
794815
}, false);
@@ -1476,16 +1497,11 @@ ionic.views.Scroll = ionic.views.View.inherit({
14761497
doTouchStart: function(touches, timeStamp) {
14771498
this.hintResize();
14781499

1479-
// Array-like check is enough here
1480-
if (touches.length == null) {
1481-
throw new Error("Invalid touch list: " + touches);
1482-
}
1483-
14841500
if (timeStamp instanceof Date) {
14851501
timeStamp = timeStamp.valueOf();
14861502
}
14871503
if (typeof timeStamp !== "number") {
1488-
throw new Error("Invalid timestamp value: " + timeStamp);
1504+
timeStamp = Date.now();
14891505
}
14901506

14911507
var self = this;
@@ -1561,17 +1577,11 @@ ionic.views.Scroll = ionic.views.View.inherit({
15611577
* Touch move handler for scrolling support
15621578
*/
15631579
doTouchMove: function(touches, timeStamp, scale) {
1564-
1565-
// Array-like check is enough here
1566-
if (touches.length == null) {
1567-
throw new Error("Invalid touch list: " + touches);
1568-
}
1569-
15701580
if (timeStamp instanceof Date) {
15711581
timeStamp = timeStamp.valueOf();
15721582
}
15731583
if (typeof timeStamp !== "number") {
1574-
throw new Error("Invalid timestamp value: " + timeStamp);
1584+
timeStamp = Date.now();
15751585
}
15761586

15771587
var self = this;
@@ -1581,7 +1591,6 @@ ionic.views.Scroll = ionic.views.View.inherit({
15811591
return;
15821592
}
15831593

1584-
15851594
var currentTouchLeft, currentTouchTop;
15861595

15871596
// Compute move based around of center of fingers
@@ -1750,12 +1759,11 @@ ionic.views.Scroll = ionic.views.View.inherit({
17501759
* Touch end handler for scrolling support
17511760
*/
17521761
doTouchEnd: function(timeStamp) {
1753-
17541762
if (timeStamp instanceof Date) {
17551763
timeStamp = timeStamp.valueOf();
17561764
}
17571765
if (typeof timeStamp !== "number") {
1758-
throw new Error("Invalid timestamp value: " + timeStamp);
1766+
timeStamp = Date.now();
17591767
}
17601768

17611769
var self = this;

0 commit comments

Comments
 (0)