-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhat-input.js
More file actions
335 lines (259 loc) · 8.46 KB
/
what-input.js
File metadata and controls
335 lines (259 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* what-input - A global utility for tracking the current input method (mouse, keyboard or touch).
* @version v4.0.4
* @link https://github.com/ten1seven/what-input
* @license MIT
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define("whatInput", [], factory);
else if(typeof exports === 'object')
exports["whatInput"] = factory();
else
root["whatInput"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {
module.exports = (function() {
/*
---------------
Variables
---------------
*/
// cache document.documentElement
var docElem = document.documentElement;
// last used input type
var currentInput = 'initial';
// last used input intent
var currentIntent = null;
// form input types
var formInputs = [
'input',
'select',
'textarea'
];
// list of modifier keys commonly used with the mouse and
// can be safely ignored to prevent false keyboard detection
var ignoreMap = [
16, // shift
17, // control
18, // alt
91, // Windows key / left Apple cmd
93 // Windows menu / right Apple cmd
];
// mapping of events to input types
var inputMap = {
'keyup': 'keyboard',
'mousedown': 'mouse',
'mousemove': 'mouse',
'MSPointerDown': 'pointer',
'MSPointerMove': 'pointer',
'pointerdown': 'pointer',
'pointermove': 'pointer',
'touchstart': 'touch'
};
// array of all used input types
var inputTypes = [];
// boolean: true if touch buffer timer is running
var isBuffering = false;
// map of IE 10 pointer events
var pointerMap = {
2: 'touch',
3: 'touch', // treat pen like touch
4: 'mouse'
};
// touch buffer timer
var touchTimer = null;
/*
---------------
Set up
---------------
*/
var setUp = function() {
// add correct mouse wheel event mapping to `inputMap`
inputMap[detectWheel()] = 'mouse';
addListeners();
setInput();
};
/*
---------------
Events
---------------
*/
var addListeners = function() {
// `pointermove`, `MSPointerMove`, `mousemove` and mouse wheel event binding
// can only demonstrate potential, but not actual, interaction
// and are treated separately
// pointer events (mouse, pen, touch)
if (window.PointerEvent) {
docElem.addEventListener('pointerdown', updateInput);
docElem.addEventListener('pointermove', setIntent);
} else if (window.MSPointerEvent) {
docElem.addEventListener('MSPointerDown', updateInput);
docElem.addEventListener('MSPointerMove', setIntent);
} else {
// mouse events
docElem.addEventListener('mousedown', updateInput);
docElem.addEventListener('mousemove', setIntent);
// touch events
if ('ontouchstart' in window) {
docElem.addEventListener('touchstart', touchBuffer);
}
}
// mouse wheel
docElem.addEventListener(detectWheel(), setIntent);
// keyboard events
docElem.addEventListener('keydown', updateInput);
docElem.addEventListener('keyup', updateInput);
};
// checks conditions before updating new input
var updateInput = function(event) {
// only execute if the touch buffer timer isn't running
if (!isBuffering) {
var eventKey = event.which;
var value = inputMap[event.type];
if (value === 'pointer') value = pointerType(event);
if (
currentInput !== value ||
currentIntent !== value
) {
var activeInput = (
document.activeElement &&
formInputs.indexOf(document.activeElement.nodeName.toLowerCase()) === -1
) ? true : false;
if (
value === 'touch' ||
// ignore mouse modifier keys
(value === 'mouse' && ignoreMap.indexOf(eventKey) === -1) ||
// don't switch if the current element is a form input
(value === 'keyboard' && activeInput)
) {
// set the current and catch-all variable
currentInput = currentIntent = value;
setInput();
}
}
}
};
// updates the doc and `inputTypes` array with new input
var setInput = function() {
docElem.setAttribute('data-whatinput', currentInput);
docElem.setAttribute('data-whatintent', currentInput);
if (inputTypes.indexOf(currentInput) === -1) {
inputTypes.push(currentInput);
docElem.className += ' whatinput-types-' + currentInput;
}
};
// updates input intent for `mousemove` and `pointermove`
var setIntent = function(event) {
// only execute if the touch buffer timer isn't running
if (!isBuffering) {
var value = inputMap[event.type];
if (value === 'pointer') value = pointerType(event);
if (currentIntent !== value) {
currentIntent = value;
docElem.setAttribute('data-whatintent', currentIntent);
}
}
};
// buffers touch events because they frequently also fire mouse events
var touchBuffer = function(event) {
// clear the timer if it happens to be running
window.clearTimeout(touchTimer);
// set the current input
updateInput(event);
// set the isBuffering to `true`
isBuffering = true;
// run the timer
touchTimer = window.setTimeout(function() {
// if the timer runs out, set isBuffering back to `false`
isBuffering = false;
}, 200);
};
/*
---------------
Utilities
---------------
*/
var pointerType = function(event) {
if (typeof event.pointerType === 'number') {
return pointerMap[event.pointerType];
} else {
return (event.pointerType === 'pen') ? 'touch' : event.pointerType; // treat pen like touch
}
};
// detect version of mouse wheel event to use
// via https://developer.mozilla.org/en-US/docs/Web/Events/wheel
var detectWheel = function() {
return 'onwheel' in document.createElement('div') ?
'wheel' : // Modern browsers support "wheel"
document.onmousewheel !== undefined ?
'mousewheel' : // Webkit and IE support at least "mousewheel"
'DOMMouseScroll'; // let's assume that remaining browsers are older Firefox
};
/*
---------------
Init
don't start script unless browser cuts the mustard
(also passes if polyfills are used)
---------------
*/
if (
'addEventListener' in window &&
Array.prototype.indexOf
) {
setUp();
}
/*
---------------
API
---------------
*/
return {
// returns string: the current input type
// opt: 'loose'|'strict'
// 'strict' (default): returns the same value as the `data-whatinput` attribute
// 'loose': includes `data-whatintent` value if it's more current than `data-whatinput`
ask: function(opt) { return (opt === 'loose') ? currentIntent : currentInput; },
// returns array: all the detected input types
types: function() { return inputTypes; }
};
}());
/***/ }
/******/ ])
});
;