disable rfid refresh before page load
[koha.git] / ffzg / rfid / koha-rfid.js
1 /*
2  * RFID support for Koha
3  *
4  * Writtern by Dobrica Pavlinusic <dpavlin@rot13.org> under GPL v2 or later
5  *
6  * This provides example how to intergrate JSONP interface from
7  *
8  * scripts/RFID-JSONP-server.pl
9  *
10  * to provide overlay for tags in range and emulate form fill for Koha Library System
11  * which allows check-in and checkout-operations without touching html interface
12  *
13  * You will have to inject remote javascript in Koha intranetuserjs using:
14
15 // inject JavaScript RFID support
16 $.getScript('http://localhost:9000/examples/koha-rfid.js');
17
18  */
19
20 function barcode_on_screen(barcode) {
21         var found = 0;
22         $('table tr td a:contains(130)').each( function(i,o) {
23                 var possible = $(o).text();
24 console.debug(i,o,possible, barcode);
25                 if ( possible == barcode ) found++;
26         })
27         return found;
28 }
29
30 function rfid_secure(barcode,sid,val) {
31         console.debug('rfid_secure', barcode, sid, val);
32         if ( barcode_on_screen(barcode) )
33                 $.getJSON( 'http://localhost:9000/secure.js?' + sid + '=' + val + ';callback=?' )
34 }
35
36 var rfid_reset_field = false;
37
38 function rfid_scan(data,textStatus) {
39         var rfid_refresh = 1500; // ms
40
41         console.debug( 'rfid_scan', data, textStatus );
42
43         var span = $('span#rfid');
44
45         if ( span.size() == 0 ) // insert last in language bar on bottom
46                 span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
47
48         if ( span.size() == 0 ) // or before login on top
49                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
50
51         span = $('span#rfid');
52
53
54         if ( data.tags ) {
55                 if ( data.tags.length === 1 ) {
56                         var t = data.tags[0];
57 //                      if ( span.text() != t.content ) {
58                         if ( 1 ) { // force update of security
59
60                                 var url = document.location.toString();
61                                 var circulation = url.substr(-14,14) == 'circulation.pl';
62                                 var returns = url.substr(-10,10) == 'returns.pl';
63
64                                 if ( t.content.length == 0 ) { // empty tag
65
66                                         span.text( t.sid + ' empty' ).css('color', 'red' );
67
68                                 } else if ( t.content.substr(0,3) == '130' ) { // books
69
70                                         if ( circulation )
71                                                  rfid_secure( t.content, t.sid, 'D7' );
72                                         if ( returns )
73                                                  rfid_secure( t.content, t.sid, 'DA' );
74
75                                         var color = 'blue';
76                                         if ( t.security.toUpperCase() == 'DA' ) color = 'red';
77                                         if ( t.security.toUpperCase() == 'D7' ) color = 'green';
78                                         span.text( t.content ).css('color', color);
79
80                                         if ( ! barcode_on_screen( t.content ) ) {
81                                                 rfid_reset_field = 'barcode';
82                                                 var i = $('input[name=barcode]:last');
83                                                 if ( i.val() != t.content )  {
84                                                         rfid_refresh = 0;
85                                                         i.val( t.content )
86                                                         .closest('form').submit();
87                                                 }
88                                         }
89
90                                 } else {
91                                         span.text( t.content ).css('color', 'blue' );
92
93                                         if ( url.substr(-14,14) != 'circulation.pl' || $('form[name=mainform]').size() == 0 ) {
94                                                 rfid_refresh = 0;
95                                                 rfid_reset_field = 'findborrower';
96                                                 $('input[name=findborrower]').val( t.content )
97                                                         .parent().submit();
98                                         }
99                                 }
100                         }
101                 } else {
102                         var error = data.tags.length + ' tags near reader: ';
103                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
104                         span.text( error ).css( 'color', 'red' );
105                 }
106
107         } else {
108                 span.text( 'no tags in range' ).css('color','gray');
109                 if ( rfid_reset_field ) {
110                         $('input[name='+rfid_reset_field+']').val( '' );
111                         rfid_reset_field = false;
112                 }
113         }
114
115         if (rfid_refresh > 1) {
116                 window.setTimeout( function() {
117                         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
118                 }, rfid_refresh );
119         } else {
120                 console.debug('rfid_refresh disabled',rfid_refresh);
121         }
122 }
123
124 $(document).ready( function() {
125         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
126 });