/*
* jQuery Scanner Detection
*
* Copyright (c) 2013 Julien Maurel
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://github.com/julien-maurel/jQuery-Scanner-Detection
*
* Version: 1.2.1
*
*/
!function(h){h.fn.scannerDetection=function(u){if("string"==typeof u)return this.each(function(){this.scannerDetectionTest(u)}),this;if(!1===u)return this.each(function(){this.scannerDetectionOff()}),this;var e={onComplete:!1,onError:!1,onReceive:!1,onKeyDetect:!1,timeBeforeScanTest:100,avgTimeByChar:30,minLength:6,endChar:[9,13],startChar:[],ignoreIfFocusOn:!1,scanButtonKeyCode:!1,scanButtonLongPressThreshold:3,onScanButtonLongPressed:!1,stopPropagation:!1,preventDefault:!1};return"function"==typeof u&&(u={onComplete:u}),u="object"!=typeof u?h.extend({},e):h.extend({},e,u),this.each(function(){function n(){c="",f=r=0}var t=this,o=h(t),r=0,i=0,c="",s=!1,a=!1,f=0;t.scannerDetectionOff=function(){o.unbind("keydown.scannerDetection"),o.unbind("keypress.scannerDetection")},t.isFocusOnIgnoredElement=function(){if(!u.ignoreIfFocusOn)return!1;if("string"==typeof u.ignoreIfFocusOn)return h(":focus").is(u.ignoreIfFocusOn);if("object"==typeof u.ignoreIfFocusOn&&u.ignoreIfFocusOn.length)for(var e=h(":focus"),n=0;n<u.ignoreIfFocusOn.length;n++)if(e.is(u.ignoreIfFocusOn[n]))return!0;return!1},t.scannerDetectionTest=function(e){return e&&(r=i=0,c=e),f=f||1,c.length>=u.minLength&&i-r<c.length*u.avgTimeByChar?(u.onScanButtonLongPressed&&f>u.scanButtonLongPressThreshold?u.onScanButtonLongPressed.call(t,c,f):u.onComplete&&u.onComplete.call(t,c,f),o.trigger("scannerDetectionComplete",{string:c}),n(),!0):(u.onError&&u.onError.call(t,c),o.trigger("scannerDetectionError",{string:c}),n(),!1)},o.data("scannerDetection",{options:u}).unbind(".scannerDetection").bind("keydown.scannerDetection",function(e){if(!1!==u.scanButtonKeyCode&&e.which==u.scanButtonKeyCode)f++,e.preventDefault(),e.stopImmediatePropagation();else if(r&&-1!==u.endChar.indexOf(e.which)||!r&&-1!==u.startChar.indexOf(e.which)){var n=jQuery.Event("keypress",e);n.type="keypress.scannerDetection",o.triggerHandler(n),e.preventDefault(),e.stopImmediatePropagation()}u.onKeyDetect&&u.onKeyDetect.call(t,e),o.trigger("scannerDetectionKeyDetect",{evt:e})}).bind("keypress.scannerDetection",function(e){this.isFocusOnIgnoredElement()||(u.stopPropagation&&e.stopImmediatePropagation(),u.preventDefault&&e.preventDefault(),s=r&&-1!==u.endChar.indexOf(e.which)?(e.preventDefault(),e.stopImmediatePropagation(),!0):(r||-1===u.startChar.indexOf(e.which)?void 0!==e.which&&(c+=String.fromCharCode(e.which)):(e.preventDefault(),e.stopImmediatePropagation()),!1),r=r||Date.now(),i=Date.now(),a&&clearTimeout(a),a=s?(t.scannerDetectionTest(),!1):setTimeout(t.scannerDetectionTest,u.timeBeforeScanTest),u.onReceive&&u.onReceive.call(t,e),o.trigger("scannerDetectionReceive",{evt:e}))})}),this}}(jQuery);
$(document).scannerDetection
({
onComplete: function(barcode, qty)
{
var barcode = barcode.split(''),
barcode_output = '';
$.each(barcode, function(key, val)
{
if('!' == val)
{
val = 1;
}
else if('@' == val)
{
val = 2;
}
else if('#' == val)
{
val = 3;
}
else if('$' == val)
{
val = 4;
}
else if('%' == val)
{
val = 5;
}
else if('^' == val)
{
val = 6;
}
else if('&' == val)
{
val = 7;
}
else if('*' == val)
{
val = 8;
}
else if('(' == val)
{
val = 9;
}
else if(')' == val)
{
val = 0;
}
barcode_output = barcode_output + val;
});
if($('.--barcode-scanner-input').length)
{
$('.--barcode-scanner-input').focus().val('').val(barcode_output)
}
else if($('[data-barcode=' + barcode_output + ']').length)
{
$('[data-barcode=' + barcode_output + ']').trigger('click')
}
else
{
return false;
}
}
});
|