forked from axenox/onscan.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onscan.min.js
5 lines (5 loc) · 5.71 KB
/
onscan.min.js
1
2
3
4
5
/*
* onScan.js - scan-events for hardware barcodes scanners in javascript
* Version: 1.4.2
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t()):e.onScan=t()}(this,function(){var d={attachTo:function(e,t){if(void 0!==e.scannerDetectionData)throw new Error("onScan.js is already initialized for DOM element "+e);var n={onScan:function(e,t){},onScanError:function(e){},onKeyProcess:function(e,t){},onKeyDetect:function(e,t){},onPaste:function(e,t){},keyCodeMapper:function(e){return d.decodeKeyEvent(e)},onScanButtonLongPress:function(){},scanButtonKeyCode:!1,scanButtonLongPressTime:500,timeBeforeScanTest:100,avgTimeByChar:30,minLength:6,suffixKeyCodes:[9,13],prefixKeyCodes:[],ignoreIfFocusOn:!1,stopPropagation:!1,preventDefault:!1,reactToKeydown:!0,reactToPaste:!1,singleScanQty:1};return t=this._mergeOptions(n,t),e.scannerDetectionData={options:t,vars:{firstCharTime:0,lastCharTime:0,stringWriting:"",callIsScanner:!1,testTimer:!1,longPressTimeStart:0,longPressed:!1}},!0===t.reactToPaste&&e.addEventListener("paste",this._handlePaste),!1!==t.scanButtonKeyCode&&e.addEventListener("keyup",this._handleKeyUp),!0!==t.reactToKeydown&&!1===t.scanButtonKeyCode||e.addEventListener("keydown",this._handleKeyDown),this},detachFrom:function(e){e.scannerDetectionData.options.reactToPaste&&e.removeEventListener("paste",this._handlePaste),!1!==e.scannerDetectionData.options.scanButtonKeyCode&&e.removeEventListener("keyup",this._handleKeyUp),e.removeEventListener("keydown",this._handleKeyDown),e.scannerDetectionData=void 0},getOptions:function(e){return e.scannerDetectionData.options},setOptions:function(e,t){switch(e.scannerDetectionData.options.reactToPaste){case!0:!1===t.reactToPaste&&e.removeEventListener("paste",this._handlePaste);break;case!1:!0===t.reactToPaste&&e.addEventListener("paste",this._handlePaste)}switch(e.scannerDetectionData.options.scanButtonKeyCode){case!1:!1!==t.scanButtonKeyCode&&e.addEventListener("keyup",this._handleKeyUp);break;default:!1===t.scanButtonKeyCode&&e.removeEventListener("keyup",this._handleKeyUp)}return e.scannerDetectionData.options=this._mergeOptions(e.scannerDetectionData.options,t),this._reinitialize(e),this},decodeKeyEvent:function(e){switch(iCode=e.which,!0){case 48<=iCode&&iCode<=90:case 106<=iCode&&iCode<=111:if(void 0!==e.key)return e.key;var t=String.fromCharCode(iCode);switch(e.shiftKey){case!1:t.toLowerCase(t);break;case!0:t.toUpperCase(t)}return t;case 96<=iCode&&iCode<=105:return iCode-96}return""},simulate:function(e,t){var n=e.scannerDetectionData.vars;return n.firstCharTime=0,n.lastCharTime=0,n.stringWriting=t,this._validateScanCode(e),this},_reinitialize:function(e){var t=e.scannerDetectionData.vars;t.firstCharTime=0,t.stringWriting=""},_isFocusOnIgnoredElement:function(e){var t=e.scannerDetectionData.options.ignoreIfFocusOn;if(!t)return!1;var n=document.activeElement;if(Array.isArray(t)){for(var a=0;a<t.length;a++)if(!0===n.matches(t[a]))return!0}else if(n.matches(t))return!0;return!1},_validateScanCode:function(e){var t,n=e.scannerDetectionData,a=n.options,i=n.options.singleScanQty,o=n.vars.stringWriting,r=n.vars.firstCharTime,s=n.vars.lastCharTime,c={};switch(!0){case o.length<a.minLength:c={message:"Receieved code is shorter then minimal length"};break;case s-r>o.length*a.avgTimeByChar:c={message:"Receieved code was not entered in time"};break;default:return a.onScan.call(e,o,i),t=new CustomEvent("scan",{detail:{scanCode:o,qty:i}}),e.dispatchEvent(t),d._reinitialize(e),!0}return c.scanCode=o,c.scanDuration=s-r,c.avgTimeByChar=a.avgTimeByChar,c.minLength=a.minLength,a.onScanError.call(e,c),t=new CustomEvent("scanError",{detail:c}),e.dispatchEvent(t),d._reinitialize(e),!1},_mergeOptions:function(e,t){var n,a={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&(a[n]=e[n]);for(n in t)Object.prototype.hasOwnProperty.call(t,n)&&(a[n]=t[n]);return a},_getNormalizedKeyNum:function(e){var t;return window.event?t=e.keyCode:e.which&&(t=e.which),t},_handleKeyDown:function(e){var t=d._getNormalizedKeyNum(e);if(e.which!==t)try{e.which=t}catch(e){console.warn("Cannot normalize KeyboardEvent.which: ",e)}var n=this.scannerDetectionData.options,a=this.scannerDetectionData.vars;if(n.onKeyDetect.call(this,e.which,e),!d._isFocusOnIgnoredElement(this))if(!1===n.scanButtonKeyCode||t!=n.scanButtonKeyCode){switch(n.stopPropagation&&e.stopImmediatePropagation(),n.preventDefault&&e.preventDefault(),!0){case a.firstCharTime&&-1!==n.suffixKeyCodes.indexOf(t):e.preventDefault(),e.stopImmediatePropagation(),this.scannerDetectionData.vars.callIsScanner=!0;break;case!a.firstCharTime&&-1!==n.prefixKeyCodes.indexOf(t):e.preventDefault(),e.stopImmediatePropagation(),a.callIsScanner=!1;break;default:var i=n.keyCodeMapper.call(this,e);if(null===i)return;a.stringWriting+=i,a.callIsScanner=!1}a.firstCharTime||(a.firstCharTime=Date.now()),a.lastCharTime=Date.now(),a.testTimer&&clearTimeout(a.testTimer),a.callIsScanner?(d._validateScanCode(this),a.testTimer=!1):a.testTimer=setTimeout(d._validateScanCode,n.timeBeforeScanTest,this),n.onKeyProcess.call(this,i,e)}else a.longPressed||(a.longPressTimer=setTimeout(n.onScanButtonLongPress,n.scanButtonLongPressTime,this),a.longPressed=!0)},_handlePaste:function(e){if(!d._isFocusOnIgnoredElement(this)){e.preventDefault();var t=(event.clipboardData||window.clipboardData).getData("text");this.scannerDetectionData.options.onPaste.call(this,t,event);var n=this.scannerDetectionData.vars;n.firstCharTime=0,n.lastCharTime=0,n.stringWriting=t,d._validateScanCode(this)}},_handleKeyUp:function(e){d._isFocusOnIgnoredElement(this)||d._getNormalizedKeyNum(e)==this.scannerDetectionData.options.scanButtonKeyCode&&(clearTimeout(this.scannerDetectionData.vars.longPressTimer),this.scannerDetectionData.vars.longPressed=!1)}};return d});