Skip to content

Commit 72cdb0b

Browse files
author
Sebastien Pereira
committed
Leverage has() for feature detection. Ref #4.
1 parent cf8cb76 commit 72cdb0b

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

bower.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "dpointer",
33
"version": "0.1.2",
4+
"dependencies": {
5+
"requirejs-dplugins": "0.2.x"
6+
},
47
"devDependencies": {
58
"requirejs": "2.1.x",
69
"requirejs-domready": "2.0.x"

events.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@
22
* Pointer Events shim
33
*/
44
define([
5+
"./handlers/features",
56
"./handlers/utils",
67
"./handlers/touch",
78
"./handlers/mouse",
89
"./handlers/mspointer"
9-
], function (utils, touch, mouse, mspointer) {
10+
], function (has, utils, touch, mouse, mspointer) {
1011
"use strict";
1112

12-
var pointerEvents = {_targetElement: null},
13-
feature = {
14-
//todo: should use has() module instead and
15-
//consider loading touch and mspointer modules conditionally.
16-
touch: ("ontouchstart" in document),
17-
pointer: ("onpointerdown" in document),
18-
mspointer: ("onmspointerdown" in document),
19-
chrome: /chrome/i.test(navigator.userAgent),
20-
mobile: /(mobile)|(android)/i.test(navigator.userAgent)
21-
};
13+
var pointerEvents = {_targetElement: null};
2214

2315
/**
2416
* Enable Pointer events. Register native event handlers. Importing this module automatically register native
@@ -32,14 +24,14 @@ define([
3224
if (this._targetElement) {
3325
return;// already initialized
3426
}
35-
if (!feature.pointer) {
36-
if (feature.mspointer) {
27+
if (!has("pointer")) {
28+
if (has("mspointer")) {
3729
mspointer.registerHandlers(targetElement);
3830
} else {
39-
if (feature.touch) {
40-
if (!feature.mobile) {
31+
if (has("touch")) {
32+
if (!has("mobile")) {
4133
mouse.registerHandlers(targetElement);
42-
if (feature.chrome) {
34+
if (has("chrome")) {
4335
touch.registerHandlers(targetElement);
4436
}
4537
} else {
@@ -91,10 +83,10 @@ define([
9183
if (!this._targetElement) {
9284
return false;// not initialized
9385
}
94-
if (feature.pointer) {
86+
if (has("pointer")) {
9587
return targetElement.setPointerCapture(pointerId);// use native Pointer Events method
9688
} else {
97-
if (feature.mspointer) {
89+
if (has("mspointer")) {
9890
return targetElement.msSetPointerCapture(pointerId);// use native Pointer Events method
9991
} else {
10092
if (pointerId === 1) { // mouse always gets ID = 1
@@ -116,10 +108,10 @@ define([
116108
if (!this._targetElement) {
117109
return false;
118110
}
119-
if (feature.pointer) {
111+
if (has("pointer")) {
120112
return targetElement.releasePointerCapture(pointerId);
121113
} else {
122-
if (feature.mspointer) {
114+
if (has("mspointer")) {
123115
return targetElement.msReleasePointerCapture(pointerId);
124116
} else {
125117
if (pointerId === 1) {
@@ -149,12 +141,12 @@ define([
149141
}
150142

151143
// CSS rule when user agent implements W3C Pointer Events or when a polyfill is in place.
152-
if (feature.pointer) {
144+
if (has("pointer")) {
153145
insertTouchActionCSSRule("touch-action");
154146
}
155147

156148
// CSS rule for IE10 and IE11 preview
157-
if (feature.mspointer) {
149+
if (has("mspointer")) {
158150
insertTouchActionCSSRule("-ms-touch-action");
159151
}
160152

@@ -178,7 +170,7 @@ define([
178170
* @param e click event
179171
*/
180172
function clickHandler(e) {
181-
if (feature.touch) {
173+
if (has("touch")) {
182174
// (7) Android 4.1.1 generates a click after touchend even when touchstart is prevented.
183175
// if we receive a native click at an element with touch action disabled we just have to absorb it.
184176
// (fixed in Android 4.1.2+)

handlers/features.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
*
3+
*/
4+
define([
5+
"../../requirejs-dplugins/has"
6+
], function (has) {
7+
has.add("touch", "ontouchstart" in document); // UA supports Touch Events
8+
has.add("pointer", "onpointerdown" in document); // UA supports Pointer Events
9+
has.add("mspointer", "onmspointerdown" in document); // UA supports Pointer Events (IE10+IE11 preview)
10+
has.add("chrome", /chrome/i.test(navigator.userAgent)); // UA is chrome.
11+
has.add("mobile", /(mobile)|(android)/i.test(navigator.userAgent)); // mobile device
12+
return has;
13+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "dpointer",
33
"version": "0.1.2",
44
"dependencies": {
5+
"requirejs-dplugins": "0.2.x"
56
},
67
"devDependencies": {
78
"intern": "1.6.x",

0 commit comments

Comments
 (0)