diff --git a/CHANGELOG.txt b/CHANGELOG.txt index efbd31f..d6354cb 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,2 +1,5 @@ +1.1.0 + Add events scannerDetectionComplete, scannerDetectionError, scannerDetectionReceive + 1.0.0 Initial version diff --git a/README.md b/README.md index 8f7dfea..7fd2aa3 100644 --- a/README.md +++ b/README.md @@ -8,24 +8,26 @@ How use it ---------- To initialize the detection, 2 ways: + $(selector).scannerDetection(); // Initiliaze with default options $(selector).scannerDetection({...}); // Initiliaze with an object that contains options $(selector).scannerDetection(function(){...}); // Initiliaze with a function that is onComplete callback To simulate a scanning after initialisation: - $(selector).scannerDetection('string scanned'); + $(selector).scannerDetection('scanned string'); Options ------- ###onComplete -Callback (mandatory) after detection of a successfull scanning +Default: false +Callback after detection of a successfull scanning ###onError Default: false Callback after detection of a unsuccessfull scanning (scanned string in parameter) ###onReceive Default: false -Callback after receive a char (scanned char in parameter) +Callback after receive a char (original keypress event in parameter) ###timeBeforeScanTest Default: 100 Wait duration (ms) after keypress event to check if scanning is finished @@ -36,7 +38,7 @@ Average time (ms) between 2 chars. Used to do difference between keyboard typing Default: :6 Minimum length for a scanning ###endChar -Default: [9,13] +Default: [9,13] Chars to remove and means end of scanning ###stopPropagation Default: false @@ -44,3 +46,25 @@ Stop immediate propagation on keypress event ###preventDefault Default: false Prevent default action on keypress event + + +Events +------ +All callbacks are of type function and can also be bound as event listeners. +Like this, you can add multiple callbacks for each event. +Of course, events exist only if plugin is initialized on selector. + + $(selector) + .bind('scannerDetectionComplete',function(e,data){...}) + .bind('scannerDetectionError',function(e,data){...}) + .bind('scannerDetectionReceive',function(e,data){...}) + +###scannerDetectionComplete +Callback after detection of a successfull scanning +Event data: {string: "scanned string"} +###scannerDetectionError +Callback after detection of a unsuccessfull scanning +Event data: {string: "scanned string"} +###scannerDetectionReceive +Callback after receive a char +Event data: {evt: {original keypress event}} diff --git a/jquery.scannerdetection.js b/jquery.scannerdetection.js index 8c07172..0c23eb5 100644 --- a/jquery.scannerdetection.js +++ b/jquery.scannerdetection.js @@ -9,7 +9,7 @@ * Project home: * https://github.com/julien-maurel/jQuery-Scanner-Detection * - * Version: 1.0.0 + * Version: 1.1.0 * */ (function($){ @@ -24,7 +24,7 @@ } var defaults={ - onComplete:function(){}, // Callback after detection of a successfull scanning (scanned string in parameter) + onComplete:false, // Callback after detection of a successfull scanning (scanned string in parameter) onError:false, // Callback after detection of a unsuccessfull scanning (scanned string in parameter) onReceive:false, // Callback after receive a char (scanned char in parameter) timeBeforeScanTest:100, // Wait duration (ms) after keypress event to check if scanning is finished @@ -32,13 +32,17 @@ minLength:6, // Minimum length for a scanning endChar:[9,13], // Chars to remove and means end of scanning stopPropagation:false, // Stop immediate propagation on keypress event - preventDefault:false, // Prevent default action on keypress event + preventDefault:false // Prevent default action on keypress event }; if(typeof options==="function"){ options={onComplete:options} } - options=$.extend({},defaults,options); - + if(typeof options!=="object"){ + options=$.extend({},defaults); + }else{ + options=$.extend({},defaults,options); + } + this.each(function(){ var self=this, $self=$(self), firstCharTime=0, lastCharTime=0, stringWriting='', callIsScanner=false, testTimer=false; var initScannerDetection=function(){ @@ -49,11 +53,13 @@ // If all condition are good (length, time...), call the callback and re-initialize the plugin for next scanning // Else, just re-initialize if(stringWriting.length>=options.minLength && lastCharTime-firstCharTime