Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7923 from crdlc/bug-836023-2-v1-train
Browse files Browse the repository at this point in the history
Merge pull request #7863 from crdlc/bug-836023
  • Loading branch information
jhford committed Feb 1, 2013
2 parents ae2a054 + cb9f0a0 commit e3190c9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 40 deletions.
41 changes: 27 additions & 14 deletions apps/homescreen/js/dock.js
Expand Up @@ -12,21 +12,31 @@ const DockManager = (function() {
var duration = 300;

var initialOffsetLeft, initialOffsetRight, numApps, cellWidth;
var isPanning = false, startX, currentX, deltaX;
var isPanning = false, startEvent, currentX, deltaX;
var tapThreshold = Page.prototype.tapThreshold;

var isTouch = 'ontouchstart' in window;
var touchstart = isTouch ? 'touchstart' : 'mousedown';
var touchmove = isTouch ? 'touchmove' : 'mousemove';
var touchend = isTouch ? 'touchend' : 'mouseup';

var getX = (function getXWrapper() {
return isTouch ? function(e) { return e.touches[0].pageX } :
function(e) { return e.pageX };
})();

function handleEvent(evt) {
switch (evt.type) {
case 'touchstart':
case touchstart:
initialOffsetLeft = dock.getLeft();
initialOffsetRight = dock.getRight();
numApps = dock.getNumIcons();
startX = evt.touches[0].pageX;
startEvent = isTouch ? evt.touches[0] : evt;
attachEvents();
break;

case 'touchmove':
deltaX = evt.touches[0].pageX - startX;
case touchmove:
deltaX = getX(evt) - startEvent.pageX;
if (!isPanning) {
if (Math.abs(deltaX) < tapThreshold) {
return;
Expand Down Expand Up @@ -64,7 +74,7 @@ const DockManager = (function() {
dock.moveBy(initialOffsetLeft + deltaX);
break;

case 'touchend':
case touchend:
releaseEvents();

if (!isPanning) {
Expand All @@ -86,7 +96,10 @@ const DockManager = (function() {
Homescreen.setMode('edit');

if ('isIcon' in evt.target.dataset) {
DragDropManager.start(evt, {x: evt.clientX, y: evt.clientY});
DragDropManager.start(evt, {
'x': startEvent.pageX,
'y': startEvent.pageY
});
}
}
break;
Expand Down Expand Up @@ -126,14 +139,14 @@ const DockManager = (function() {

function releaseEvents() {
container.removeEventListener('contextmenu', handleEvent);
window.removeEventListener('touchmove', handleEvent);
window.removeEventListener('touchend', handleEvent);
window.removeEventListener(touchmove, handleEvent);
window.removeEventListener(touchend, handleEvent);
}

function attachEvents() {
container.addEventListener('contextmenu', handleEvent);
window.addEventListener('touchmove', handleEvent);
window.addEventListener('touchend', handleEvent);
window.addEventListener(touchmove, handleEvent);
window.addEventListener(touchend, handleEvent);
}

function placeAfterRemovingApp(numApps, centering) {
Expand Down Expand Up @@ -174,7 +187,7 @@ const DockManager = (function() {
*/
init: function dm_init(containerEl, page) {
container = containerEl;
container.addEventListener('touchstart', handleEvent);
container.addEventListener(touchstart, handleEvent);
dock = this.page = page;

var numIcons = dock.getNumIcons();
Expand All @@ -190,7 +203,7 @@ const DockManager = (function() {
},

onDragStop: function dm_onDragStop() {
container.addEventListener('touchstart', handleEvent);
container.addEventListener(touchstart, handleEvent);
var numApps = dock.getNumIcons();
calculateDimentions(numApps);

Expand All @@ -207,7 +220,7 @@ const DockManager = (function() {

onDragStart: function dm_onDragStart() {
releaseEvents();
container.removeEventListener('touchstart', handleEvent);
container.removeEventListener(touchstart, handleEvent);
numAppsBeforeDrag = dock.getNumIcons();
},

Expand Down
21 changes: 15 additions & 6 deletions apps/homescreen/js/dragdrop.js
Expand Up @@ -55,6 +55,15 @@ const DragDropManager = (function() {
}
}

var isTouch = 'ontouchstart' in window;
var touchmove = isTouch ? 'touchmove' : 'mousemove';
var touchend = isTouch ? 'touchend' : 'mouseup';

var getTouch = (function getTouchWrapper() {
return isTouch ? function(e) { return e.touches[0] } :
function(e) { return e };
})();

var transitioning = false;

function onNavigationEnd() {
Expand Down Expand Up @@ -213,8 +222,8 @@ const DragDropManager = (function() {
* @param {Object} DOMElement behind draggable icon
*/
function onMove(evt) {
var x = currentEvent.x = evt.touches[0].pageX;
var y = currentEvent.y = evt.touches[0].pageY;
var x = currentEvent.x = getTouch(evt).pageX;
var y = currentEvent.y = getTouch(evt).pageY;

draggableIcon.onDragMove(x, y);

Expand Down Expand Up @@ -279,8 +288,8 @@ const DragDropManager = (function() {
return;

clearTimeout(overlapingTimeout);
window.removeEventListener('touchmove', onMove);
window.removeEventListener('touchend', onEnd);
window.removeEventListener(touchmove, onMove);
window.removeEventListener(touchend, onEnd);
stop(function dg_stop() {
GridManager.onDragStop();
DockManager.onDragStop();
Expand Down Expand Up @@ -308,8 +317,8 @@ const DragDropManager = (function() {
* @param {Object} DOM event
*/
start: function ddm_start(evt, initCoords) {
window.addEventListener('touchmove', onMove);
window.addEventListener('touchend', onEnd);
window.addEventListener(touchmove, onMove);
window.addEventListener(touchend, onEnd);
GridManager.onDragStart();
DockManager.onDragStart();
startEvent = initCoords;
Expand Down
50 changes: 30 additions & 20 deletions apps/homescreen/js/grid.js
Expand Up @@ -35,21 +35,31 @@ const GridManager = (function() {
var startEvent, isPanning = false, deltaX, removePanHandler,
dummy = function() {};

var isTouch = 'ontouchstart' in window;
var touchstart = isTouch ? 'touchstart' : 'mousedown';
var touchmove = isTouch ? 'touchmove' : 'mousemove';
var touchend = isTouch ? 'touchend' : 'mouseup';

var getX = (function getXWrapper() {
return isTouch ? function(e) { return e.touches[0].pageX } :
function(e) { return e.pageX };
})();

function handleEvent(evt) {
switch (evt.type) {
case 'touchstart':
case touchstart:
touchStartTimestamp = evt.timeStamp;
startEvent = evt.touches[0];
startEvent = isTouch ? evt.touches[0] : evt;
deltaX = 0;
attachEvents();
removePanHandler = dummy;
isPanning = false;
break;

case 'touchmove':
case touchmove:
// Start panning immediately but only disable
// the tap when we've moved far enough.
deltaX = evt.touches[0].pageX - startEvent.pageX;
deltaX = getX(evt).pageX - startEvent.pageX;
if (deltaX === 0)
return;

Expand All @@ -59,7 +69,7 @@ const GridManager = (function() {
// method for panning only the 2 relevants pages based on the
// direction of the inputs. The code here is carefully written
// to avoid as much as possible allocations while panning.
window.removeEventListener('touchmove', handleEvent);
window.removeEventListener(touchmove, handleEvent);

// Before panning pages that are directly next to the current
// target are set visible.
Expand Down Expand Up @@ -128,15 +138,15 @@ const GridManager = (function() {
// Generate a function accordingly to the current page position.
if (Homescreen.isInEditMode() || currentPage > 2) {
var pan = function(e) {
deltaX = e.touches[0].pageX - startX;
deltaX = getX(e) - startX;
if (!isPanning && Math.abs(deltaX) >= tapThreshold) {
isPanning = true;
}
window.mozRequestAnimationFrame(refresh);
};
} else {
var pan = function(e) {
deltaX = e.touches[0].pageX - startX;
deltaX = getX(e) - startX;
if (!isPanning && Math.abs(deltaX) >= tapThreshold) {
isPanning = true;
}
Expand All @@ -148,25 +158,25 @@ const GridManager = (function() {
}

var container = pages[index].container;
container.addEventListener('touchmove', pan, true);
container.addEventListener(touchmove, pan, true);

removePanHandler = function removePanHandler(e) {
touchEndTimestamp = e ? e.timeStamp : Number.MAX_VALUE;
window.removeEventListener('touchend', removePanHandler, true);
window.removeEventListener(touchend, removePanHandler, true);

container.removeEventListener('touchmove', pan, true);
container.removeEventListener(touchmove, pan, true);

window.mozRequestAnimationFrame(function panTouchEnd() {
onTouchEnd(deltaX, e);
});
};

window.addEventListener('touchend', removePanHandler, true);
window.removeEventListener('touchend', handleEvent);
window.addEventListener(touchend, removePanHandler, true);
window.removeEventListener(touchend, handleEvent);

break;

case 'touchend':
case touchend:
releaseEvents();
pageHelper.getCurrent().tap(evt.target);
break;
Expand Down Expand Up @@ -228,13 +238,13 @@ const GridManager = (function() {
}

function attachEvents() {
window.addEventListener('touchmove', handleEvent);
window.addEventListener('touchend', handleEvent);
window.addEventListener(touchmove, handleEvent);
window.addEventListener(touchend, handleEvent);
}

function releaseEvents() {
window.removeEventListener('touchmove', handleEvent);
window.removeEventListener('touchend', handleEvent);
window.removeEventListener(touchmove, handleEvent);
window.removeEventListener(touchend, handleEvent);
}

function togglePagesVisibility(start, end) {
Expand Down Expand Up @@ -599,7 +609,7 @@ const GridManager = (function() {

container = document.querySelector(selector);
container.addEventListener('contextmenu', handleEvent);
container.addEventListener('touchstart', handleEvent, true);
container.addEventListener(touchstart, handleEvent, true);

limits.left = container.offsetWidth * 0.05;
limits.right = container.offsetWidth * 0.95;
Expand Down Expand Up @@ -906,15 +916,15 @@ const GridManager = (function() {

onDragStart: function gm_onDragSart() {
releaseEvents();
container.removeEventListener('touchstart', handleEvent, true);
container.removeEventListener(touchstart, handleEvent, true);
dragging = document.body.dataset.dragging = true;
},

onDragStop: function gm_onDragStop() {
delete document.body.dataset.dragging;
dragging = false;
delete document.body.dataset.transitioning;
container.addEventListener('touchstart', handleEvent, true);
container.addEventListener(touchstart, handleEvent, true);
ensurePagesOverflow();
removeEmptyPages();
},
Expand Down

0 comments on commit e3190c9

Please sign in to comment.