RFID: submit pages again and continue scanning reader in some cases
[koha.git] / ffzg / rfid / koha-rfid.js
index 2627a72..35839a1 100644 (file)
@@ -18,11 +18,25 @@ function barcode_on_screen(barcode) {
                var possible = $(o).text();
                if ( possible == barcode ) found++;
        })
+       var lastchecked = $('div.lastchecked').text();
+       if ( lastchecked ) {
+               console.info('found lastchecked div', lastchecked);
+               var checked_out_barcode = lastchecked.split(/\(/)[1].split(/\)/)[0];
+               if ( checked_out_barcode == barcode ) found++;
+       }
+       // Not checked out message in returns.pl
+       var alert_dialog = $('div.alert p a:contains(130)').text();
+       if ( alert_dialog ) {
+               console.info('found alert dialog', alert_dialog);
+               var alert_barcode = alert_dialog.split(/:/)[0];
+               if ( alert_barcode == barcode ) found++;
+       }
+       console.debug('barcode_on_screen', barcode, found);
        return found;
 }
 
 var rfid_refresh = 200; // ms
-var rfid_count_timeout = 1; // number of times to scan reader before turning off
+var rfid_count_timeout = 50; // number of times to scan reader before turning off
 
 
 function rfid_secure_json(t,val, success) {
@@ -38,9 +52,12 @@ var rfid_reset_field = false;
 var rfid_current_sid = false;
 var rfid_blank_sid   = false;
 var rfid_action = undefined;
+var rfid_scan_busy = false;
 
 function rfid_scan(data,textStatus) {
 
+       rfid_scan_busy = false;
+
        var rfid_count = $.cookie('rfid_count');
        if ( rfid_count === undefined ) {
                rfid_count = rfid_count_timeout;
@@ -74,11 +91,13 @@ function rfid_scan(data,textStatus) {
                if ( data.tags.length === 1 ) {
                        var t = data.tags[0];
                        rfid_current_sid = t.sid;
+                       var rfid_last_tag = $.cookie('rfid_last_tag');
 
 //                     if ( span.text() != t.content ) {
                        if ( 1 ) { // force update of security
 
                                var script_name = document.location.pathname.split(/\//).pop();
+                               var referrer_name = document.referrer.split(/\//).pop();
                                var tab_active  = $("#header_search li[aria-selected=true]").attr('aria-controls');
                                var focused_form = $('input:focus').first().name;
                                var action =
@@ -87,9 +106,15 @@ function rfid_scan(data,textStatus) {
                                        ( script_name == 'circulation.pl' || tab_active == 'circ_search' )   ? 'circulation' :
                     'scan';
                                rfid_action = undefined; // one-shot
-                               console.debug('script_name', script_name, 'tab_active', tab_active, 'action', action, 'focused_form', focused_form );
+                               console.debug('script_name', script_name, 'referrer_name', referrer_name, 'tab_active', tab_active, 'action', action, 'focused_form', focused_form, 'rfid_last_tag' , rfid_last_tag );
                                info.text(action);
 
+                               // keep refreshing rfid reader
+                               if ( referrer_name == 'circulation.pl' ) {
+                                       rfid_count = rfid_count_timeout;
+                               }
+
+
                                if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
 
                                        rfid_blank_sid = t.sid;
@@ -115,11 +140,15 @@ function rfid_scan(data,textStatus) {
                                                        $('input[name=q]')
                                                                .css('background', '#ff0')
                                                                .val( 'bc:' + t.content )
-                                                               ;//.closest('form').submit();
+                                                               .closest('form').submit();
                                                }
                                        }
 
-                                       if ( action == 'returns' || action == 'checkin' || action == 'circulation' ) {
+                                       if (
+                                               ( action == 'returns' || action == 'checkin' || action == 'circulation' )
+                                               && ! barcode_on_screen( t.content )
+                                               //&& t.content != rfid_last_tag
+                                       ) {
                                                rfid_reset_field = 'barcode';
 
                                                // return must be first to catch change of tab to check-in
@@ -149,10 +178,10 @@ function rfid_scan(data,textStatus) {
 
                                                        if ( i.val() != t.content ) { // && i.name == 'barcode' )  {
                                                                i.css('background', '#0ff' );
+                                                               rfid_refresh = 0;
                                                                rfid_secure_json( t, afi_secure, function(data) {
                                                                        console.log('secure', afi_secure, data);
                                                                        $.cookie('rfid_count', 0); // FIXME once? to see change rfid_count_timeout);
-                                                                       rfid_refresh = 0;
                                                                        i.css('background',
                                                                                        afi_secure == 'DA' ? '#f00' :
                                                                                        afi_secure == 'D7' ? '#0f0' :
@@ -187,7 +216,7 @@ function rfid_scan(data,textStatus) {
                                                $('input[name=findborrower]')
                                                        .css('background', '#00f')
                                                        .val( t.content )
-                                                       ;//.parent().submit();
+                                                       .parent().submit();
                                        }
                                }
                        }
@@ -217,7 +246,7 @@ function rfid_scan(data,textStatus) {
                window.setTimeout( function() {
                        if ( rfid_refresh ) {
                                $('#rfid_popup').css('border','3px solid #ff0');
-                               $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
+                               scan_tags();
                        } else {
                                console.error('got setTimeout but rfid_refresh', rfid_refresh, ' is not set');
                        }
@@ -227,10 +256,16 @@ function rfid_scan(data,textStatus) {
        }
 
        $('#rfid_popup').css('border','3px solid #fff');
+       $.cookie('rfid_last_tag', t ? t.content : '--none--');
 
 }
 
 function scan_tags() {
+       if ( rfid_scan_busy ) {
+               console.error('rfid_scan_busy');
+               return;
+       }
+       rfid_scan_busy = true;
        console.info('scan_tags');
        $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
 }
@@ -239,7 +274,9 @@ function set_rfid_active(active,action) {
        rfid_action = action;
        var input_active = $('input#rfid_active').attr('checked');
        if ( active && input_active ) {
+               $.cookie('rfid_count', rfid_count_timeout);
                console.info('ignored set_rfid_active ', active, action);
+               scan_tags();
                return;
        }
        console.info('set_rfid_active', active);
@@ -267,10 +304,13 @@ $(document).ready( function() {
        scan_tags();    // FIXME should we trigger this on page load even if rfid is not active
 
        // circulation keyboard shortcuts (FFZG specific!)
-       shortcut.add('Alt+1', function() { set_rfid_active(true,'checkin'    )});
-       shortcut.add('Alt+2', function() { set_rfid_active(true,'circulation')});
+       shortcut.add('Alt+r', function() { set_rfid_active(true,'checkin'    )});
+       shortcut.add('Alt+z', function() { set_rfid_active(true,'circulation')});
+       shortcut.add('Alt+y', function() { set_rfid_active(true,'circulation')});
+/*
        shortcut.add('Alt+3', function() { set_rfid_active(true,'search?'    )});
        shortcut.add('Alt+4', function() { set_rfid_active(true,'renew'      )}); // renew
+*/
 
        // send RFID tag to currently focused field on screen
        shortcut.add('Alt+s', function() {
@@ -283,7 +323,8 @@ $(document).ready( function() {
 
        shortcut.add('F9', function() {
                console.log('F9');
-               set_rfid_active(true,'F9');
+               //set_rfid_active(true,'F9');
+               scan_tags();
        });
 
        // intranet cataloging