123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- (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) {
-
- var installedModules = {};
-
- function __webpack_require__(moduleId) {
-
- if(installedModules[moduleId])
- return installedModules[moduleId].exports;
-
- var module = installedModules[moduleId] = {
- exports: {},
- id: moduleId,
- loaded: false
- };
-
- modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
-
- module.loaded = true;
-
- return module.exports;
- }
-
- __webpack_require__.m = modules;
-
- __webpack_require__.c = installedModules;
-
- __webpack_require__.p = "";
-
- return __webpack_require__(0);
- })
- ([
- (function(module, exports) {
- 'use strict';
- module.exports = function () {
-
- if (typeof document === 'undefined' || typeof window === 'undefined') {
- return {
-
- ask: function ask() {
- return 'initial';
- },
-
- element: function element() {
- return null;
- },
-
- ignoreKeys: function ignoreKeys() {},
-
- specificKeys: function specificKeys() {},
-
- registerOnChange: function registerOnChange() {},
-
- unRegisterOnChange: function unRegisterOnChange() {}
- };
- }
-
-
- var docElem = document.documentElement;
-
- var currentElement = null;
-
- var currentInput = 'initial';
-
- var currentIntent = currentInput;
-
-
- try {
- if (window.sessionStorage.getItem('what-input')) {
- currentInput = window.sessionStorage.getItem('what-input');
- }
- if (window.sessionStorage.getItem('what-intent')) {
- currentIntent = window.sessionStorage.getItem('what-intent');
- }
- } catch (e) {}
-
- var eventTimer = null;
-
- var formInputs = ['input', 'select', 'textarea'];
-
- var functionList = [];
-
-
- var ignoreMap = [16,
- 17,
- 18,
- 91,
- 93
- ];
- var specificMap = [];
-
- var inputMap = {
- keydown: 'keyboard',
- keyup: 'keyboard',
- mousedown: 'mouse',
- mousemove: 'mouse',
- MSPointerDown: 'pointer',
- MSPointerMove: 'pointer',
- pointerdown: 'pointer',
- pointermove: 'pointer',
- touchstart: 'touch'
-
- };var isBuffering = false;
-
- var isScrolling = false;
-
- var mousePos = {
- x: null,
- y: null
-
- };var pointerMap = {
- 2: 'touch',
- 3: 'touch',
- 4: 'mouse'
-
- };var supportsPassive = false;
- try {
- var opts = Object.defineProperty({}, 'passive', {
- get: function get() {
- supportsPassive = true;
- }
- });
- window.addEventListener('test', null, opts);
- } catch (e) {}
-
- var setUp = function setUp() {
-
- inputMap[detectWheel()] = 'mouse';
- addListeners();
- doUpdate('input');
- doUpdate('intent');
- };
-
- var addListeners = function addListeners() {
-
-
-
- var options = supportsPassive ? { passive: true } : false;
-
- if (window.PointerEvent) {
- window.addEventListener('pointerdown', setInput);
- window.addEventListener('pointermove', setIntent);
- } else if (window.MSPointerEvent) {
- window.addEventListener('MSPointerDown', setInput);
- window.addEventListener('MSPointerMove', setIntent);
- } else {
-
- window.addEventListener('mousedown', setInput);
- window.addEventListener('mousemove', setIntent);
-
- if ('ontouchstart' in window) {
- window.addEventListener('touchstart', eventBuffer, options);
- window.addEventListener('touchend', setInput);
- }
- }
-
- window.addEventListener(detectWheel(), setIntent, options);
-
- window.addEventListener('keydown', eventBuffer);
- window.addEventListener('keyup', eventBuffer);
-
- window.addEventListener('focusin', setElement);
- window.addEventListener('focusout', clearElement);
- };
-
- var setInput = function setInput(event) {
-
- if (!isBuffering) {
- var eventKey = event.which;
- var value = inputMap[event.type];
- if (value === 'pointer') {
- value = pointerType(event);
- }
- var ignoreMatch = !specificMap.length && ignoreMap.indexOf(eventKey) === -1;
- var specificMatch = specificMap.length && specificMap.indexOf(eventKey) !== -1;
- var shouldUpdate = value === 'keyboard' && eventKey && (ignoreMatch || specificMatch) || value === 'mouse' || value === 'touch';
- if (currentInput !== value && shouldUpdate) {
- currentInput = value;
- try {
- window.sessionStorage.setItem('what-input', currentInput);
- } catch (e) {}
- doUpdate('input');
- }
- if (currentIntent !== value && shouldUpdate) {
-
- var activeElem = document.activeElement;
- var notFormInput = activeElem && activeElem.nodeName && formInputs.indexOf(activeElem.nodeName.toLowerCase()) === -1;
- if (notFormInput) {
- currentIntent = value;
- try {
- window.sessionStorage.setItem('what-intent', currentIntent);
- } catch (e) {}
- doUpdate('intent');
- }
- }
- }
- };
-
- var doUpdate = function doUpdate(which) {
- docElem.setAttribute('data-what' + which, which === 'input' ? currentInput : currentIntent);
- fireFunctions(which);
- };
-
- var setIntent = function setIntent(event) {
-
- detectScrolling(event);
-
-
- if (!isBuffering && !isScrolling) {
- var value = inputMap[event.type];
- if (value === 'pointer') {
- value = pointerType(event);
- }
- if (currentIntent !== value) {
- currentIntent = value;
- try {
- window.sessionStorage.setItem('what-intent', currentIntent);
- } catch (e) {}
- doUpdate('intent');
- }
- }
- };
- var setElement = function setElement(event) {
- if (!event.target.nodeName) {
-
-
- clearElement();
- return;
- }
- currentElement = event.target.nodeName.toLowerCase();
- docElem.setAttribute('data-whatelement', currentElement);
- if (event.target.classList && event.target.classList.length) {
- docElem.setAttribute('data-whatclasses', event.target.classList.toString().replace(' ', ','));
- }
- };
- var clearElement = function clearElement() {
- currentElement = null;
- docElem.removeAttribute('data-whatelement');
- docElem.removeAttribute('data-whatclasses');
- };
-
- var eventBuffer = function eventBuffer(event) {
-
- setInput(event);
-
- window.clearTimeout(eventTimer);
-
- isBuffering = true;
-
- eventTimer = window.setTimeout(function () {
-
- isBuffering = false;
- }, 100);
- };
-
- var pointerType = function pointerType(event) {
- if (typeof event.pointerType === 'number') {
- return pointerMap[event.pointerType];
- } else {
-
- return event.pointerType === 'pen' ? 'touch' : event.pointerType;
- }
- };
-
-
- var detectWheel = function detectWheel() {
- var wheelType = void 0;
-
- if ('onwheel' in document.createElement('div')) {
- wheelType = 'wheel';
- } else {
-
-
- wheelType = document.onmousewheel !== undefined ? 'mousewheel' : 'DOMMouseScroll';
- }
- return wheelType;
- };
-
- var fireFunctions = function fireFunctions(type) {
- for (var i = 0, len = functionList.length; i < len; i++) {
- if (functionList[i].type === type) {
- functionList[i].fn.call(undefined, type === 'input' ? currentInput : currentIntent);
- }
- }
- };
-
- var objPos = function objPos(match) {
- for (var i = 0, len = functionList.length; i < len; i++) {
- if (functionList[i].fn === match) {
- return i;
- }
- }
- };
- var detectScrolling = function detectScrolling(event) {
- if (mousePos['x'] !== event.screenX || mousePos['y'] !== event.screenY) {
- isScrolling = false;
- mousePos['x'] = event.screenX;
- mousePos['y'] = event.screenY;
- } else {
- isScrolling = true;
- }
- };
-
-
-
- if ('addEventListener' in window && Array.prototype.indexOf) {
- setUp();
- }
-
- return {
-
-
-
-
- ask: function ask(opt) {
- return opt === 'intent' ? currentIntent : currentInput;
- },
-
- element: function element() {
- return currentElement;
- },
-
- ignoreKeys: function ignoreKeys(arr) {
- ignoreMap = arr;
- },
-
- specificKeys: function specificKeys(arr) {
- specificMap = arr;
- },
-
-
-
- registerOnChange: function registerOnChange(fn, eventType) {
- functionList.push({
- fn: fn,
- type: eventType || 'input'
- });
- },
- unRegisterOnChange: function unRegisterOnChange(fn) {
- var position = objPos(fn);
- if (position || position === 0) {
- functionList.splice(position, 1);
- }
- }
- };
- }();
- })
- ])
- });
- ;
|