Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
amend(gestures): only hijack click events on mobile devices
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Jan 29, 2015
1 parent 79196c3 commit ade65b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
46 changes: 28 additions & 18 deletions src/core/services/gesture/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ document.contains || (document.contains = function(node) {
return document.body.contains(node);
});

document.addEventListener('click', function(ev) {
// Space/enter on a button, and submit events, can send clicks
var isKeyClick = ev.clientX === 0 && ev.clientY === 0;
if (isKeyClick || ev.$material) return;

// Prevent clicks unless they're sent by material
ev.preventDefault();
ev.stopPropagation();
}, true);
// TODO add windows phone to this
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var isIos = userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i);
var isAndroid = userAgent.match(/Android/i);
var shouldHijackClicks = isIos || isAndroid;

if (shouldHijackClicks) {
document.addEventListener('click', function(ev) {
// Space/enter on a button, and submit events, can send clicks
var isKeyClick = ev.clientX === 0 && ev.clientY === 0;
if (isKeyClick || ev.$material) return;

// Prevent clicks unless they're sent by material
ev.preventDefault();
ev.stopPropagation();
}, true);
}

angular.element(document)
.on(START_EVENTS, gestureStart)
Expand Down Expand Up @@ -134,16 +142,18 @@ angular.module('material.core')
.factory('$mdGesture', function($$MdGestureHandler, $$rAF, $timeout) {
HANDLERS = {};

addHandler('click', {
options: {
maxDistance: 6
},
onEnd: function(ev, pointer) {
if (pointer.distance < this.state.options.maxDistance) {
this.dispatchEvent(ev, 'click', null, ev);
if (shouldHijackClicks) {
addHandler('click', {
options: {
maxDistance: 6
},
onEnd: function(ev, pointer) {
if (pointer.distance < this.state.options.maxDistance) {
this.dispatchEvent(ev, 'click', null, ev);
}
}
}
});
});
}

addHandler('press', {
onStart: function(ev, pointer) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/services/gesture/gesture.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('$mdGesture', function() {

});

describe('click', function() {
xdescribe('click', function() {

This comment has been minimized.

Copy link
@ThomasBurleson

ThomasBurleson Jan 29, 2015

Contributor

Let's activate this test plz.


it('should click if distance < options.maxDistance', inject(function($document) {
var spy = jasmine.createSpy('click');
Expand Down

0 comments on commit ade65b6

Please sign in to comment.