RFID add renew shortcut
[koha.git] / ffzg / rfid / koha-rfid.js
index a66084a..4c6a00a 100644 (file)
@@ -26,24 +26,53 @@ function barcode_on_screen(barcode) {
        return found;
 }
 
-function rfid_secure(t,val) {
+var rfid_refresh = 1500; // ms
+var rfid_count_timeout = 10; // number of times to scan reader before turning off
+
+function rfid_secure_json(t,val, success) {
        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=?' )
+       rfid_refresh = 0; // disable rfid pull until secure call returns
+       console.log('rfid_secure_json', t, val);
+       $.getJSON( '///localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
 }
 
+function rfid_secure_check(t,val) {
+       if ( barcode_on_screen(t.content) ) {
+               rfid_secure_json(t, val);
+       }
+}
+
+
 var rfid_reset_field = false;
+var rfid_current_sid = false;
+var rfid_blank_sid   = false;
 
 function rfid_scan(data,textStatus) {
-       var rfid_refresh = 1500; // ms
 
-       console.debug( 'rfid_scan', data, textStatus );
+       var rfid_count = $.cookie('rfid_count');
+       if ( rfid_count === undefined ) {
+               rfid_count = rfid_count_timeout;
+       }
+       console.log('rfid_count', rfid_count);
+
+       $.cookie('rfid', 'localhost'); // set cookie
+
+       console.debug( 'rfid_scan', data, textStatus, $.cookie('rfid') );
+       rfid_current_sid = false;
+       rfid_blank_sid = false;
 
        var span = $('span#rfid');
 
-       if ( span.size() == 0 ) // insert last in language bar on bottom
-               span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
+       if ( span.size() == 0 ) {
+               // insert last in language bar on bottom
+//             span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
+
+               // alternative pop-up version
+               span = $('#breadcrumbs').append('<div id="rfid_popup" style="position: fixed; bottom: 3em; right: 1em; background: #fff; border: 3px solid #ff0; padding: 1em; opacity: 0.7; z-index: 1040;"><label for="rfid_active"><input type=checkbox id="rfid_active"> local_ip <span id="rfid">RFID reader</span></label></div>');
+               if ( rfid_count ) $('input#rfid_active').attr('checked',true);
+               $('input#rfid_active').click(activate_scan_tags);
+       }
+
 
        if ( span.size() == 0 ) // or before login on top
                span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
@@ -54,44 +83,76 @@ function rfid_scan(data,textStatus) {
        if ( data.tags ) {
                if ( data.tags.length === 1 ) {
                        var t = data.tags[0];
+                       rfid_current_sid = t.sid;
+
 //                     if ( span.text() != t.content ) {
                        if ( 1 ) { // force update of security
 
-                               var url = document.location.toString();
-                               var circulation = url.substr(-14,14) == 'circulation.pl';
-                               var returns = url.substr(-10,10) == 'returns.pl';
-
-                               if ( t.content.length == 0 ) { // empty tag
+                               var script_name = document.location.pathname.split(/\//).pop();
+//                             var tab_active  = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
+                               var tab_active  = $("#header_search li[aria-selected=true]").attr('aria-controls');
+                               console.debug('tab_active', tab_active);
+                               var circulation = script_name == 'circulation.pl' || tab_active == 'circ_search' ;
+                               var returns     = script_name == 'returns.pl' || tab_active == 'checkin_search';
+                               console.debug('script_name', script_name, 'circulation', circulation, 'returns', returns);
 
-                                       span.text( t.sid + ' empty' ).css('color', 'red' );
+                               if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
 
-                               } else if ( t.content.substr(0,3) == '130' ) { // books
+                                       rfid_blank_sid = t.sid;
+                                       span.text( t.sid + ' blank' ).css('color', 'red' );
 
-                                       if ( circulation ) rfid_secure( t, 'D7' );
-                                       if ( returns     ) rfid_secure( t, 'DA' );
+                               } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
 
                                        var color = 'blue';
-                                       if ( t.security.toUpperCase() == 'DA' ) color = 'red';
-                                       if ( t.security.toUpperCase() == 'D7' ) color = 'green';
-                                       span.text( t.content ).css('color', color);
+                                       var icon  = '?';
+                                       if ( t.security.toUpperCase() == 'DA' ) { color = 'red'; icon = '&timesb;' }
+                                       if ( t.security.toUpperCase() == 'D7' ) { color = 'green'; icon = '&rarr;' }
+                                       span.html( t.content + '&nbsp;' + icon ).css('color', color);
 
-                                       if ( ! barcode_on_screen( t.content ) ) {
+
+                                       if ( tab_active == 'catalog_search' && script_name != 'moredetail.pl' && $('input#rfid_active').attr('checked') ) {
+                                               if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
+                                                       $.cookie('rfid_count', rfid_count_timeout);
+                                                       $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
+                                               }
+                                       }
+
+                                       if ( ! barcode_on_screen( t.content ) || returns ) {
                                                rfid_reset_field = 'barcode';
-                                               var i = $('input[name=barcode]:last');
-                                               if ( i.val() != t.content )  {
-                                                       rfid_refresh = 0;
-                                                       i.val( t.content )
-                                                       .closest('form').submit();
+
+                                               // return must be first to catch change of tab to check-in
+                                               var afi_secure    = returns ? 'DA' : 'D7';
+                                               var form_selector = returns ? 'first' : 'last';
+                                               if ( returns || circulation ) {
+
+                                                       if ( circulation && $('#circ_needsconfirmation').length > 0 ) {
+                                                               console.log("in circulation, but needs confirmation");
+                                                       } else {
+                                                                       var i = $('input[name=barcode]:'+form_selector);
+                                                                       if ( i.val() != t.content )  {
+                                                                               rfid_secure_json( t, afi_secure, function(data) {
+                                                                                       console.log('secure', afi_secure, data);
+                                                                                       $.cookie('rfid_count', rfid_count_timeout);
+                                                                                       i.val( t.content ).closest('form').submit();
+                                                                               });
+                                                                       }
+                                                       }
+                                               } else {
+                                                       console.error('not in circulation or returns');
                                                }
                                        }
 
+                               } else if ( t.content.substr(0,3) == '130' ) {
+
+                                       span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
+
                                } else {
-                                       span.text( t.content ).css('color', 'blue' );
+                                       span.html( t.content + '&nbsp;&sstarf;' ).css('color', 'blue' );
 
                                        if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
                                                console.debug('not submitting', t.contains);
                                        } else {
-                                               rfid_refresh = 0;
+                                               rfid_refresh = 0; // stop rfid scan while submitting form
                                                rfid_reset_field = 'findborrower';
                                                $('input[name=findborrower]').val( t.content )
                                                        .parent().submit();
@@ -112,15 +173,78 @@ function rfid_scan(data,textStatus) {
                }
        }
 
-       if (rfid_refresh > 1) {
+
+       if (rfid_count > 0) rfid_count--;
+       if (rfid_count == 0) {
+               span.text('RFID reader disabled').css('color','black');
+               $('input#rfid_active').attr('checked', false)
+       }
+       $.cookie('rfid_count', rfid_count);
+
+
+       if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
                window.setTimeout( function() {
-                       $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
+                       $('#rfid_popup').css('border','3px solid #ff0');
+                       $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
                }, rfid_refresh );
        } else {
                console.debug('rfid_refresh disabled',rfid_refresh);
        }
+
+       $('#rfid_popup').css('border','3px solid #fff');
+}
+
+function scan_tags() {
+       console.info('scan_tags');
+       $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
+}
+
+function set_rfid_active(active) {
+       var input_active = $('input#rfid_active').attr('checked');
+       console.info('set_rfid_active', active);
+       if ( active ) {
+               $.cookie('rfid_count', rfid_count_timeout);
+               scan_tags();
+               if ( ! input_active ) $('input#rfid_active').attr('checked', true);
+       } else {
+               if ( input_active ) $('input#rfid_active').attr('checked', false);
+               $.cookie('rfid_count', 0);
+       }
+}
+
+function activate_scan_tags() {
+       var active = $('input#rfid_active').attr('checked');
+       console.info('activate_scan_tags', active);
+       set_rfid_active(active);
 }
 
 $(document).ready( function() {
-       $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
+       console.log('rfid_active', $('input#rfid_active').attr('checked') );
+
+       scan_tags();
+
+       // circulation keyboard shortcuts (FFZG specific!)
+       shortcut.add('Alt+r', function() { set_rfid_active(true); } );
+       shortcut.add('Alt+z', function() { set_rfid_active(true); } );
+       shortcut.add('Alt+k', function() { set_rfid_active(false) } );
+       shortcut.add('Alt+y', function() { set_rfid_active(true); } ); // renew
+
+       // intranet cataloging
+       shortcut.add('F4', function() {
+               // extract barcode from window title
+               var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
+               if ( barcode ) {
+                       if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
+                               rfid_blank_sid = rfid_current_sid;
+                       }
+
+                       console.debug('program barcode', barcode, 'to', rfid_blank_sid);
+                       $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
+                               console.info('programmed', rfid_blank_sid, barcode, data);
+                       });
+               } else {
+                       console.error('no barcode in window title');
+               }
+       });
+
 });