-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathwhatInput.js
7 lines (7 loc) · 4.08 KB
/
whatInput.js
1
2
3
4
5
6
7
/**
* what-input - A global utility for tracking the current input method (mouse, keyboard or touch).
* @version v5.1.0
* @link https://github.com/ten1seven/what-input
* @license MIT
*/
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("whatInput",[],t):"object"==typeof exports?exports.whatInput=t():e.whatInput=t()}(this,function(){return function(e){function t(o){if(n[o])return n[o].exports;var i=n[o]={exports:{},id:o,loaded:!1};return e[o].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t){"use strict";e.exports=function(){if("undefined"==typeof document||"undefined"==typeof window)return{ask:function(){return"initial"},element:function(){return null},ignoreKeys:function(){},registerOnChange:function(){},unRegisterOnChange:function(){}};var e=document.documentElement,t=null,n="initial",o=n;window.sessionStorage&&(window.sessionStorage.getItem("what-input")&&(n=window.sessionStorage.getItem("what-input")),window.sessionStorage.getItem("what-intent")&&(o=window.sessionStorage.getItem("what-intent")));var i=null,r=["input","select","textarea"],s=[],u=[16,17,18,91,93],a={keydown:"keyboard",keyup:"keyboard",mousedown:"mouse",mousemove:"mouse",MSPointerDown:"pointer",MSPointerMove:"pointer",pointerdown:"pointer",pointermove:"pointer",touchstart:"touch"},d=!1,w=!1,c={x:null,y:null},p={2:"touch",3:"touch",4:"mouse"},f=!1;try{var l=Object.defineProperty({},"passive",{get:function(){f=!0}});window.addEventListener("test",null,l)}catch(e){}var m=function(){var e=!!f&&{passive:!0};window.PointerEvent?(window.addEventListener("pointerdown",v),window.addEventListener("pointermove",g)):window.MSPointerEvent?(window.addEventListener("MSPointerDown",v),window.addEventListener("MSPointerMove",g)):(window.addEventListener("mousedown",v),window.addEventListener("mousemove",g),"ontouchstart"in window&&(window.addEventListener("touchstart",L,e),window.addEventListener("touchend",v))),window.addEventListener(x(),g,e),window.addEventListener("keydown",L),window.addEventListener("keyup",L),window.addEventListener("focusin",y),window.addEventListener("focusout",E)},v=function(e){if(!d){var t=e.which,i=a[e.type];"pointer"===i&&(i=S(e));var s="keyboard"===i&&t&&-1===u.indexOf(t)||"mouse"===i||"touch"===i;if(n!==i&&s&&(n=i,window.sessionStorage&&window.sessionStorage.setItem("what-input",n),h("input")),o!==i&&s){var w=document.activeElement;w&&w.nodeName&&-1===r.indexOf(w.nodeName.toLowerCase())&&(o=i,window.sessionStorage&&window.sessionStorage.setItem("what-intent",o),h("intent"))}}},h=function(t){e.setAttribute("data-what"+t,"input"===t?n:o),b(t)},g=function(e){if(k(e),!d&&!w){var t=a[e.type];"pointer"===t&&(t=S(e)),o!==t&&(o=t,window.sessionStorage&&window.sessionStorage.setItem("what-intent",o),h("intent"))}},y=function(n){if(!n.target.nodeName)return void E();t=n.target.nodeName.toLowerCase(),e.setAttribute("data-whatelement",t),n.target.classList&&n.target.classList.length&&e.setAttribute("data-whatclasses",n.target.classList.toString().replace(" ",","))},E=function(){t=null,e.removeAttribute("data-whatelement"),e.removeAttribute("data-whatclasses")},L=function(e){v(e),window.clearTimeout(i),d=!0,i=window.setTimeout(function(){d=!1},100)},S=function(e){return"number"==typeof e.pointerType?p[e.pointerType]:"pen"===e.pointerType?"touch":e.pointerType},x=function(){return"onwheel"in document.createElement("div")?"wheel":void 0!==document.onmousewheel?"mousewheel":"DOMMouseScroll"},b=function(e){for(var t=0,i=s.length;t<i;t++)s[t].type===e&&s[t].fn.call(void 0,"input"===e?n:o)},I=function(e){for(var t=0,n=s.length;t<n;t++)if(s[t].fn===e)return t},k=function(e){c.x!==e.screenX||c.y!==e.screenY?(w=!1,c.x=e.screenX,c.y=e.screenY):w=!0};return"addEventListener"in window&&Array.prototype.indexOf&&function(){a[x()]="mouse",m(),h("input"),h("intent")}(),{ask:function(e){return"intent"===e?o:n},element:function(){return t},ignoreKeys:function(e){u=e},registerOnChange:function(e,t){s.push({fn:e,type:t||"input"})},unRegisterOnChange:function(e){var t=I(e);(t||0===t)&&s.splice(t,1)}}}()}])});