In this repo i store all my websites, each in a different branch
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

334 行
8.5 KiB

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