Skip to content

Commit

Permalink
Add event triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Maurel committed Aug 4, 2013
1 parent 6e3b361 commit bd9d48e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
@@ -1,2 +1,5 @@
1.1.0
Add events scannerDetectionComplete, scannerDetectionError, scannerDetectionReceive

1.0.0
Initial version
32 changes: 28 additions & 4 deletions README.md
Expand Up @@ -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
Expand All @@ -36,11 +38,33 @@ 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
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}}
21 changes: 14 additions & 7 deletions jquery.scannerdetection.js
Expand Up @@ -9,7 +9,7 @@
* Project home:
* https://github.com/julien-maurel/jQuery-Scanner-Detection
*
* Version: 1.0.0
* Version: 1.1.0
*
*/
(function($){
Expand All @@ -24,21 +24,25 @@
}

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
avgTimeByChar:30, // Average time (ms) between 2 chars. Used to do difference between keyboard typing and scanning
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(){
Expand All @@ -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<stringWriting.length*options.avgTimeByChar){
options.onComplete.call(self,stringWriting);
if(options.onComplete) options.onComplete.call(self,stringWriting);
$self.trigger('scannerDetectionComplete',{string:stringWriting});
initScannerDetection();
return true;
}else{
if(options.onError) options.onError.call(self,stringWriting);
$self.trigger('scannerDetectionError',{string:stringWriting});
initScannerDetection();
return false;
}
Expand Down Expand Up @@ -84,7 +90,8 @@
testTimer=setTimeout(isScanner,options.timeBeforeScanTest);
}

if(options.onReceive) options.onReceive.call(self,e.which);
if(options.onReceive) options.onReceive.call(self,e);
$self.trigger('scannerDetectionReceive',{evt:e});
});
});
return this;
Expand Down
2 changes: 1 addition & 1 deletion scannerdetection.jquery.json
@@ -1,6 +1,6 @@
{
"name": "scannerdetection",
"version": "1.0.0",
"version": "1.1.0",
"title": "jQuery Scanner Detection",
"author": {
"name": "Julien Maurel",
Expand Down

0 comments on commit bd9d48e

Please sign in to comment.