Skip to content

Commit f2756b5

Browse files
committed
Building out more abstract adapters and added pointer adapter
1 parent 8d6dc66 commit f2756b5

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

License

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License
22

3-
Copyright (c) 2015 - 2016 Stuart Yamartino.
3+
Copyright (c) 2015 - 2017 Stuart Yamartino.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

src/adapters/adapter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Adapter{
5252
}
5353

5454
bindUnsupportedEvent(){
55-
this.add(isMobile ? 'touchstart' : 'mousedown', (event) => this.runClosure('unsupported', event));
55+
this.add(supportsTouch ? 'touchstart' : 'mousedown', (event) => this.runClosure('unsupported', event));
5656
}
5757

5858
_startPress(event){

src/adapters/adapter_pointer.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
This adapter is for devices that support pointer events.
3+
*/
4+
5+
class AdapterPointer extends Adapter{
6+
7+
constructor(el, block, options){
8+
super(el, block, options);
9+
}
10+
11+
bindEvents(){
12+
this.add('pointerdown', this.supportTest.bind(this, 0));
13+
this.add('pointermove', this.change.bind(this));
14+
this.add('pointerdown', this.supportTest.bind(this, 0));
15+
this.add('pointerup', this._endPress.bind(this));
16+
}
17+
18+
supportTest(iter, event, runKey = this.runKey){
19+
if(this.isPressed() === false){
20+
if(iter <= 6){
21+
iter++;
22+
setTimeout(this.supportTest.bind(this, iter, event, runKey), 10);
23+
} else {
24+
this.fail(event, runKey);
25+
}
26+
}
27+
}
28+
29+
change(event){
30+
if(this.isPressed() && event.pressure > 0){
31+
this._changePress(event.pressure, event);
32+
this.deepPress(event);
33+
}
34+
}
35+
36+
returnTouch(event){
37+
event.pressure >= 0.5 ? this._startDeepPress(event) : this._endDeepPress();
38+
}
39+
40+
}

src/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var Config = {
1010
// 'true' prevents the selecting of text and images via css properties
1111
preventSelect: true,
1212

13-
// 'mobile' or 'desktop' will make it run only on that type of device
13+
// 'touch', 'mouse', or 'pointer' will make it run only on that type of device
1414
only: null,
1515

1616
// this will get the correct config / option settings for the current pressure check

src/element.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ class Element{
77

88
routeEvents(el, block, options){
99
var type = Config.get('only', options);
10-
// if on desktop and requesting Force Touch or not requesting 3D Touch
11-
if(isDesktop && (type === 'desktop' || type !== 'mobile')){
10+
// for devices that support pointer events
11+
if(supportsPointer && (type === 'pointer' || type === null)){
12+
this.adapter = new AdapterPointer(el, block, options).bindEvents();
13+
}
14+
// for devices that support Force Touch
15+
else if(supportsMouse && (type === 'mouse' || type === null)){
1216
this.adapter = new AdapterForceTouch(el, block, options).bindEvents();
1317
}
14-
// if on mobile and requesting 3D Touch or not requestion Force Touch
15-
else if(isMobile && (type === 'mobile' || type !== 'desktop')){
18+
// for devices that support 3D Touch
19+
else if(supportsTouch && (type === 'touch' || type === null)){
1620
this.adapter = new Adapter3DTouch(el, block, options).bindEvents();
1721
}
1822
// unsupported if it is requesting a type and your browser is of other type

src/helpers.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ var map = function(x, in_min, in_max, out_min, out_max){
3333
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
3434
}
3535

36-
var isDesktop = false;
37-
var isMobile = false;
36+
var supportsMouse = false;
37+
var supportsTouch = false;
38+
var supportsPointer = false;
3839
var supportsTouchForceChange = false;
40+
3941
if (typeof window !== 'undefined') {
4042
// only attempt to assign these in a browser environment.
4143
// on the server, this is a no-op, like the rest of the library
42-
isMobile = 'ontouchstart' in window.document;
43-
isDesktop = !isMobile;
44+
supportsTouch = 'ontouchstart' in window.document;
45+
supportsMouse = 'onmousemove' in window.document && !supportsTouch;
46+
supportsPointer = 'onpointermove' in window.document;
4447
supportsTouchForceChange = 'ontouchforcechange' in window.document;
4548
}

0 commit comments

Comments
 (0)