Fresh living in Canada

.How to survival and make greate achievement in Canada

My Links

Blog Stats

News


CNY to 1 CAD

Google
Web bcchinese
american spaces@msn

文章 分类

存档

日记 分类

BizLink

Daily Links

Education

Job finding

Living in Canada

渤海帆船

CardReader - jQuery based

						
/**
* jQuery based magnetic stripe card reader "driver" to be used in web
* applications.
*
* Copyright @ 2009-2010 Jonathan Stoppani (http://garetjax.info/)
* Licensed under the MIT license (see LICENSE for details)
*/

var CardReader = function ( error_start , track_start , track_end , timeout ) {
this . error_start = error_start || "é" ;
this . track_start = track_start || "%" ;
this . track_end = track_end || "_" ;
this . timeout = timeout || 100 ;

this . error_start = this . error_start . charCodeAt ( 0 );
this . track_start = this . track_start . charCodeAt ( 0 );
this . track_end = this . track_end . charCodeAt ( 0 );

this . started = false ;
this . finished = false ;
this . isError = false ;
this . input = "" ;
this . timer = undefined ;
this . callbacks = [];
this . errbacks = [];
this . validators = [];
this . isDispatching = false ;
};

CardReader . prototype = {
dispatch : function ( data , isError ) {
if ( ! isError ) {
for ( var cb in this . validators ) {
if ( ! this . validators [ cb ]( data )) {
isError = true ;
break ;
}
}
}

if ( this . isDispatching ) {
if ( isError ) {
console . log ( "Immediate error!" );
return ;
} else {
clearTimeout ( this . isDispatching );
}
}

reader = this ;

this . isDispatching = setTimeout ( function () {
console . log ( "Error timeout cleared" );
reader . isDispatching = false ;
}, 200 );

if ( isError ) {
for ( var cb in this . errbacks ) {
this . errbacks [ cb ]( this . input );
}
} else {
for ( var cb in this . callbacks ) {
this . callbacks [ cb ]( this . input );
}
}
},

readObserver : function ( e ) {
var ob = this ;

if ( ! this . started && ( e . which === this . track_start || e . which === this . error_start )) {
e . stopImmediatePropagation ();
e . preventDefault ();

this . started = true ;
this . isError = e . which === this . error_start ;

this . timer = setTimeout ( function () {
ob . started = false ;
ob . finished = false ;
ob . isError = false ;
ob . input = "" ;
}, this . timeout );
} else if ( this . started && e . which === this . track_end ) {
e . stopImmediatePropagation ();
e . preventDefault ();

this . finished = true ;

clearTimeout ( this . timer );
this . timer = setTimeout ( function () {
ob . started = false ;
ob . finished = false ;
ob . isError = false ;
ob . input = "" ;
}, this . timeout );
} else if ( this . started && this . finished && e . which === 13 ) {
e . stopImmediatePropagation ();
e . preventDefault ();

this . dispatch ( this . input , this . isError );

this . started = false ;
this . finished = false ;
this . isError = false ;
this . input = "" ;

clearTimeout ( this . timer );

} else if ( this . started ) {
e . stopImmediatePropagation ();
e . preventDefault ();

this . input += String . fromCharCode ( e . which );

clearTimeout ( this . timer );
this . timer = setTimeout ( function () {
ob . started = false ;
ob . finished = false ;
ob . isError = false ;
ob . input = "" ;
}, this . timeout );
}
},

observe : function ( element ) {
var func = this ;

$ ( element ). keypress ( function ( e ) {
CardReader . prototype . readObserver . apply ( func , arguments );
});
},

validate : function ( validator ) {
this . validators . push ( validator );
},

cardRead : function ( callback ) {
this . callbacks . push ( callback );
},

cardError : function ( errback ) {
this . errbacks . push ( errback );
},
}


Card Reader
===========

A simple jQuery based driver to detect and read the input of a magnetic stripe
card reader or similar device identified by the OS as a Human Interface Device.

For further information see the blog post at:
http://garetjax.info/articles/2010/01/magnetic-card-jquery/

posted on Wednesday, November 14, 2012 6:05 PM #Education|教育学习

Feedback

No comments posted yet.
Title  
Name  
Url
Comments   
Protected by Clearscreen.SharpHIPEnter the code you see: