call rfid_secure JSONP only if security changed
[koha.git] / ffzg / rfid / koha-rfid.js
index 9ad8bb0..8f9ce3c 100644 (file)
@@ -27,16 +27,19 @@ console.debug(i,o,possible, barcode);
        return found;
 }
 
-function rfid_secure(barcode,sid,val) {
-       console.debug('rfid_secure', barcode, sid, val);
-       if ( barcode_on_screen(barcode) )
-               $.getJSON( 'http://localhost:9000/secure.js?' + sid + '=' + val + ';callback=?' )
+function rfid_secure(t,val) {
+       if ( t.security.toUpperCase() == val.toUpperCase() ) return;
+       console.debug('rfid_secure', t.content, t.sid, val);
+       if ( barcode_on_screen(t.content) )
+               $.getJSON( 'http://localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?' )
 }
 
 var rfid_reset_field = false;
 
 function rfid_scan(data,textStatus) {
-//     console.debug( 'rfid_scan', data, textStatus );
+       var rfid_refresh = 1500; // ms
+
+       console.debug( 'rfid_scan', data, textStatus );
 
        var span = $('span#rfid');
 
@@ -65,10 +68,8 @@ function rfid_scan(data,textStatus) {
 
                                } else if ( t.content.substr(0,3) == '130' ) { // books
 
-                                       if ( circulation )
-                                                rfid_secure( t.content, t.sid, 'D7' );
-                                       if ( returns )
-                                                rfid_secure( t.content, t.sid, 'DA' );
+                                       if ( circulation ) rfid_secure( t, 'D7' );
+                                       if ( returns     ) rfid_secure( t, 'DA' );
 
                                        var color = 'blue';
                                        if ( t.security.toUpperCase() == 'DA' ) color = 'red';
@@ -78,15 +79,20 @@ function rfid_scan(data,textStatus) {
                                        if ( ! barcode_on_screen( t.content ) ) {
                                                rfid_reset_field = 'barcode';
                                                var i = $('input[name=barcode]:last');
-                                               if ( i.val() != t.content )
+                                               if ( i.val() != t.content )  {
+                                                       rfid_refresh = 0;
                                                        i.val( t.content )
                                                        .closest('form').submit();
+                                               }
                                        }
 
                                } else {
                                        span.text( t.content ).css('color', 'blue' );
 
-                                       if ( url.substr(-14,14) != 'circulation.pl' || $('form[name=mainform]').size() == 0 ) {
+                                       if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
+                                               console.debug('not submitting', t.contains);
+                                       } else {
+                                               rfid_refresh = 0;
                                                rfid_reset_field = 'findborrower';
                                                $('input[name=findborrower]').val( t.content )
                                                        .parent().submit();
@@ -107,9 +113,13 @@ function rfid_scan(data,textStatus) {
                }
        }
 
-       window.setTimeout( function() {
-               $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
-       }, 1000 ); // 1000ms
+       if (rfid_refresh > 1) {
+               window.setTimeout( function() {
+                       $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
+               }, rfid_refresh );
+       } else {
+               console.debug('rfid_refresh disabled',rfid_refresh);
+       }
 }
 
 $(document).ready( function() {