PageSourceSearch

https://cdn.hellodd.com/script/what-input.js

js hellodd.com collected 2026-10-01 11:59:46 UTC 8,663 bytes, 335 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.