PageSourceSearch

https://www.riflemagazine.com/WolfePublishing/Scripts/vendor/what-input.js

js riflemagazine.com collected 2026-10-02 11:29:43 UTC 10,433 bytes, 361 lines download raw bytes

1/**
2 * what-input - A global utility for tracking the current input method (mouse, keyboard or touch).
3 * @version v4.2.0
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	'use strict';
64
65	module.exports = function () {
66	  /*
67	   * variables
68	   */
69
70	  // last used input type
71	  var currentInput = 'initial';
72
73	  // last used input intent
74	  var currentIntent = null;
75
76	  // cache document.documentElement
77	  var doc = document.documentElement;
78
79	  // form input types
80	  var formInputs = ['input', 'select', 'textarea'];
81
82	  var functionList = [];
83
84	  // list of modifier keys commonly used with the mouse and
85	  // can be safely ignored to prevent false keyboard detection
86	  var ignoreMap = [16, // shift
87	  17, // control
88	  18, // alt
89	  91, // Windows key / left Apple cmd
90	  93 // Windows menu / right Apple cmd
91	  ];
92
93	  // list of keys for which we change intent even for form inputs
94	  var changeIntentMap = [9 // tab
95	  ];
96
97	  // mapping of events to input types
98	  var inputMap = {
99	    keydown: '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 is active
113	  var isBuffering = false;
114
115	  // boolean: true if the page is being scrolled
116	  var isScrolling = false;
117
118	  // store current mouse position
119	  var mousePos = {
120	    x: null,
121	    y: null
122	  };
123
124	  // map of IE 10 pointer events
125	  var pointerMap = {
126	    2: 'touch',
127	    3: 'touch', // treat pen like touch
128	    4: 'mouse'
129	  };
130
131	  var supportsPassive = false;
132
133	  try {
134	    var opts = Object.defineProperty({}, 'passive', {
135	      get: function get() {
136	        supportsPassive = true;
137	      }
138	    });
139
140	    window.addEventListener('test', null, opts);
141	  } catch (e) {}
142
143	  /*
144	   * set up
145	   */
146
147	  var setUp = function setUp() {
148	    // add correct mouse wheel event mapping to `inputMap`
149	    inputMap[detectWheel()] = 'mouse';
150
151	    addListeners();
152	    setInput();
153	  };
154
155	  /*
156	   * events
157	   */
158
159	  var addListeners = function addListeners() {
160	    // `pointermove`, `MSPointerMove`, `mousemove` and mouse wheel event binding
161	    // can only demonstrate potential, but not actual, interaction
162	    // and are treated separately
163
164	    // pointer events (mouse, pen, touch)
165	    if (window.PointerEvent) {
166	      doc.addEventListener('pointerdown', updateInput);
167	      doc.addEventListener('pointermove', setIntent);
168	    } else if (window.MSPointerEvent) {
169	      doc.addEventListener('MSPointerDown', updateInput);
170	      doc.addEventListener('MSPointerMove', setIntent);
171	    } else {
172	      // mouse events
173	      doc.addEventListener('mousedown', updateInput);
174	      doc.addEventListener('mousemove', setIntent);
175
176	      // touch events
177	      if ('ontouchstart' in window) {
178	        doc.addEventListener('touchstart', touchBuffer);
179	        doc.addEventListener('touchend', touchBuffer);
180	      }
181	    }
182
183	    // mouse wheel
184	    doc.addEventListener(detectWheel(), setIntent, supportsPassive ? { passive: true } : false);
185
186	    // keyboard events
187	    doc.addEventListener('keydown', updateInput);
188	  };
189
190	  // checks conditions before updating new input
191	  var updateInput = function updateInput(event) {
192	    // only execute if the touch buffer timer isn't running
193	    if (!isBuffering) {
194	      var eventKey = event.which;
195	      var value = inputMap[event.type];
196	      if (value === 'pointer') value = pointerType(event);
197
198	      if (currentInput !== value || currentIntent !== value) {
199	        var activeElem = document.activeElement;
200	        var activeInput = false;
201	        var notFormInput = activeElem && activeElem.nodeName && formInputs.indexOf(activeElem.nodeName.toLowerCase()) === -1;
202
203	        if (notFormInput || changeIntentMap.indexOf(eventKey) !== -1) {
204	          activeInput = true;
205	        }
206
207	        if (value === 'touch' ||
208	        // ignore mouse modifier keys
209	        value === 'mouse' ||
210	        // don't switch if the current element is a form input
211	        value === 'keyboard' && eventKey && activeInput && ignoreMap.indexOf(eventKey) === -1) {
212	          // set the current and catch-all variable
213	          currentInput = currentIntent = value;
214
215	          setInput();
216	        }
217	      }
218	    }
219	  };
220
221	  // updates the doc and `inputTypes` array with new input
222	  var setInput = function setInput() {
223	    doc.setAttribute('data-whatinput', currentInput);
224	    doc.setAttribute('data-whatintent', currentInput);
225
226	    if (inputTypes.indexOf(currentInput) === -1) {
227	      inputTypes.push(currentInput);
228	      doc.className += ' whatinput-types-' + currentInput;
229	    }
230
231	    fireFunctions('input');
232	  };
233
234	  // updates input intent for `mousemove` and `pointermove`
235	  var setIntent = function setIntent(event) {
236	    // test to see if `mousemove` happened relative to the screen
237	    // to detect scrolling versus mousemove
238	    if (mousePos['x'] !== event.screenX || mousePos['y'] !== event.screenY) {
239	      isScrolling = false;
240
241	      mousePos['x'] = event.screenX;
242	      mousePos['y'] = event.screenY;
243	    } else {
244	      isScrolling = true;
245	    }
246
247	    // only execute if the touch buffer timer isn't running
248	    // or scrolling isn't happening
249	    if (!isBuffering && !isScrolling) {
250	      var value = inputMap[event.type];
251	      if (value === 'pointer') value = pointerType(event);
252
253	      if (currentIntent !== value) {
254	        currentIntent = value;
255
256	        doc.setAttribute('data-whatintent', currentIntent);
257
258	        fireFunctions('intent');
259	      }
260	    }
261	  };
262
263	  // buffers touch events because they frequently also fire mouse events
264	  var touchBuffer = function touchBuffer(event) {
265	    if (event.type === 'touchstart') {
266	      isBuffering = false;
267
268	      // set the current input
269	      updateInput(event);
270	    } else {
271	      isBuffering = true;
272	    }
273	  };
274
275	  var fireFunctions = function fireFunctions(type) {
276	    for (var i = 0, len = functionList.length; i < len; i++) {
277	      if (functionList[i].type === type) {
278	        functionList[i].function.call(undefined, currentIntent);
279	      }
280	    }
281	  };
282
283	  /*
284	   * utilities
285	   */
286
287	  var pointerType = function pointerType(event) {
288	    if (typeof event.pointerType === 'number') {
289	      return pointerMap[event.pointerType];
290	    } else {
291	      // treat pen like touch
292	      return event.pointerType === 'pen' ? 'touch' : event.pointerType;
293	    }
294	  };
295
296	  // detect version of mouse wheel event to use
297	  // via https://developer.mozilla.org/en-US/docs/Web/Events/wheel
298	  var detectWheel = function detectWheel() {
299	    var wheelType = void 0;
300
301	    // Modern browsers support "wheel"
302	    if ('onwheel' in document.createElement('div')) {
303	      wheelType = 'wheel';
304	    } else {
305	      // Webkit and IE support at least "mousewheel"
306	      // or assume that remaining browsers are older Firefox
307	      wheelType = document.onmousewheel !== undefined ? 'mousewheel' : 'DOMMouseScroll';
308	    }
309
310	    return wheelType;
311	  };
312
313	  /*
314	   * init
315	   */
316
317	  // don't start script unless browser cuts the mustard
318	  // (also passes if polyfills are used)
319	  if ('addEventListener' in window && Array.prototype.indexOf) {
320	    setUp();
321	  }
322
323	  /*
324	   * api
325	   */
326
327	  return {
328	    // returns string: the current input type
329	    // opt: 'loose'|'strict'
330	    // 'strict' (default): returns the same value as the `data-whatinput` attribute
331	    // 'loose': includes `data-whatintent` value if it's more current than `data-whatinput`
332	    ask: function ask(opt) {
333	      return opt === 'loose' ? currentIntent : currentInput;
334	    },
335
336	    // returns array: all the detected input types
337	    types: function types() {
338	      return inputTypes;
339	    },
340
341	    // overwrites ignored keys with provided array
342	    ignoreKeys: function ignoreKeys(arr) {
343	      ignoreMap = arr;
344	    },
345
346	    // attach functions to input and intent "events"
347	    // funct: function to fire on change
348	    // eventType: 'input'|'intent'
349	    onChange: function onChange(funct, eventType) {
350	      functionList.push({
351	        function: funct,
352	        type: eventType
353	      });
354	    }
355	  };
356	}();
357
358/***/ })
359/******/ ])
360});
361;

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.