Skip to content

Commit

Permalink
Merge pull request #4680 from sbatezat/patch-1
Browse files Browse the repository at this point in the history
Fix sliderview on Windows Phone devices
  • Loading branch information
mlynch committed Dec 6, 2015
2 parents 936252d + e9dfd5a commit cf4faf4
Showing 1 changed file with 54 additions and 31 deletions.
85 changes: 54 additions & 31 deletions js/views/sliderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ ionic.views.Slider = ionic.views.View.inherit({
initialize: function (options) {
var slider = this;

var touchStartEvent, touchMoveEvent, touchEndEvent;
if (window.navigator.pointerEnabled) {
touchStartEvent = 'pointerdown';
touchMoveEvent = 'pointermove';
touchEndEvent = 'pointerup';
} else if (window.navigator.msPointerEnabled) {
touchStartEvent = 'MSPointerDown';
touchMoveEvent = 'MSPointerMove';
touchEndEvent = 'MSPointerUp';
} else {
touchStartEvent = 'touchstart';
touchMoveEvent = 'touchmove';
touchEndEvent = 'touchend';
}

var mouseStartEvent = 'mousedown';
var mouseMoveEvent = 'mousemove';
var mouseEndEvent = 'mouseup';

// utilities
var noop = function() {}; // simple no operation function
var offloadFn = function(fn) { setTimeout(fn || noop, 0); }; // offload a functions execution

// check browser capabilities
var browser = {
addEventListener: !!window.addEventListener,
touch: ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch,
transitions: (function(temp) {
var props = ['transitionProperty', 'WebkitTransition', 'MozTransition', 'OTransition', 'msTransition'];
for ( var i in props ) if (temp.style[ props[i] ] !== undefined) return true;
Expand Down Expand Up @@ -256,20 +274,20 @@ ionic.views.Slider = ionic.views.View.inherit({
var events = {

handleEvent: function(event) {
if(event.type == 'mousedown' || event.type == 'mouseup' || event.type == 'mousemove') {
if(!event.touches && event.pageX && event.pageY) {
event.touches = [{
pageX: event.pageX,
pageY: event.pageY
}];
}

switch (event.type) {
case 'mousedown': this.start(event); break;
case 'touchstart': this.start(event); break;
case 'touchmove': this.touchmove(event); break;
case 'mousemove': this.touchmove(event); break;
case 'touchend': offloadFn(this.end(event)); break;
case 'mouseup': offloadFn(this.end(event)); break;
case touchStartEvent: this.start(event); break;
case mouseStartEvent: this.start(event); break;
case touchMoveEvent: this.touchmove(event); break;
case mouseMoveEvent: this.touchmove(event); break;
case touchEndEvent: offloadFn(this.end(event)); break;
case mouseEndEvent: offloadFn(this.end(event)); break;
case 'webkitTransitionEnd':
case 'msTransitionEnd':
case 'oTransitionEnd':
Expand All @@ -283,6 +301,11 @@ ionic.views.Slider = ionic.views.View.inherit({
},
start: function(event) {

// prevent to start if there is no valid event
if (!event.touches) {
return;
}

var touches = event.touches[0];

// measure start values
Expand All @@ -304,20 +327,22 @@ ionic.views.Slider = ionic.views.View.inherit({
delta = {};

// attach touchmove and touchend listeners
if(browser.touch) {
element.addEventListener('touchmove', this, false);
element.addEventListener('touchend', this, false);
} else {
element.addEventListener('mousemove', this, false);
element.addEventListener('mouseup', this, false);
document.addEventListener('mouseup', this, false);
}
element.addEventListener(touchMoveEvent, this, false);
element.addEventListener(mouseMoveEvent, this, false);

element.addEventListener(touchEndEvent, this, false);
element.addEventListener(mouseEndEvent, this, false);

document.addEventListener(touchEndEvent, this, false);
document.addEventListener(mouseEndEvent, this, false);
},
touchmove: function(event) {

// ensure there is a valid event
// ensure swiping with one touch and not pinching
// ensure sliding is enabled
if (event.touches.length > 1 ||
if (!event.touches ||
event.touches.length > 1 ||
event.scale && event.scale !== 1 ||
slider.slideIsDisabled) {
return;
Expand Down Expand Up @@ -453,14 +478,14 @@ ionic.views.Slider = ionic.views.View.inherit({
}

// kill touchmove and touchend event listeners until touchstart called again
if(browser.touch) {
element.removeEventListener('touchmove', events, false);
element.removeEventListener('touchend', events, false);
} else {
element.removeEventListener('mousemove', events, false);
element.removeEventListener('mouseup', events, false);
document.removeEventListener('mouseup', events, false);
}
element.removeEventListener(touchMoveEvent, events, false);
element.removeEventListener(mouseMoveEvent, events, false);

element.removeEventListener(touchEndEvent, events, false);
element.removeEventListener(mouseEndEvent, events, false);

document.removeEventListener(touchEndEvent, events, false);
document.removeEventListener(mouseEndEvent, events, false);

options.onDragEnd && options.onDragEnd();
},
Expand Down Expand Up @@ -562,7 +587,8 @@ ionic.views.Slider = ionic.views.View.inherit({
if (browser.addEventListener) {

// remove current event listeners
element.removeEventListener('touchstart', events, false);
element.removeEventListener(touchStartEvent, events, false);
element.removeEventListener(mouseStartEvent, events, false);
element.removeEventListener('webkitTransitionEnd', events, false);
element.removeEventListener('msTransitionEnd', events, false);
element.removeEventListener('oTransitionEnd', events, false);
Expand Down Expand Up @@ -590,11 +616,8 @@ ionic.views.Slider = ionic.views.View.inherit({
if (browser.addEventListener) {

// set touchstart event on element
if (browser.touch) {
element.addEventListener('touchstart', events, false);
} else {
element.addEventListener('mousedown', events, false);
}
element.addEventListener(touchStartEvent, events, false);
element.addEventListener(mouseStartEvent, events, false);

if (browser.transitions) {
element.addEventListener('webkitTransitionEnd', events, false);
Expand Down

0 comments on commit cf4faf4

Please sign in to comment.