display empty tags and don't reload page
[Biblio-RFID.git] / examples / 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
16 <!-- this is basically remote script injection, doesn't work in Chrome with SSL -->
17 //]]></script>
18
19 <!-- invoke local RFID javascript -->
20 <script type="text/javascript"
21 src="http://localhost:9000/examples/koha-rfid.js" 
22 >
23
24 <script type="text/javascript">
25 //<![CDATA[
26
27  */
28
29 function barcode_on_screen(barcode) {
30         var found = 0;
31         $('table tr td a:contains(130)').each( function(i,o) {
32                 var possible = $(o).text();
33 console.debug(i,o,possible, barcode);
34                 if ( possible == barcode ) found++;
35         })
36         return found;
37 }
38
39 function rfid_secure(barcode,sid,val) {
40         console.debug('rfid_secure', barcode, sid, val);
41         if ( barcode_on_screen(barcode) ) 
42                 $.getJSON( 'http://localhost:9000/secure.js?' + sid + '=' + val + ';callback=?' )
43 }
44
45 var rfid_reset_field = false;
46
47 function rfid_scan(data,textStatus) {
48 //      console.debug( 'rfid_scan', data, textStatus );
49
50         var span = $('span#rfid');
51
52         if ( span.size() == 0 ) // insert last in language bar on bottom
53                 span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
54
55         if ( span.size() == 0 ) // or before login on top
56                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
57
58         span = $('span#rfid');
59
60
61         if ( data.tags ) {
62                 if ( data.tags.length === 1 ) {
63                         var t = data.tags[0];
64 //                      if ( span.text() != t.content ) {
65                         if ( 1 ) { // force update of security
66
67                                 var url = document.location.toString();
68                                 var circulation = url.substr(-14,14) == 'circulation.pl';
69                                 var returns = url.substr(-10,10) == 'returns.pl';
70
71                                 if ( t.content.length == 0 ) { // empty tag
72
73                                         span.text( t.sid + ' empty' ).css('color', 'red' );
74
75                                 } else if ( t.content.substr(0,3) == '130' ) { // books
76
77                                         if ( circulation )
78                                                  rfid_secure( t.content, t.sid, 'D7' );
79                                         if ( returns )
80                                                  rfid_secure( t.content, t.sid, 'DA' );
81
82                                         var color = 'blue';
83                                         if ( t.security.toUpperCase() == 'DA' ) color = 'red';
84                                         if ( t.security.toUpperCase() == 'D7' ) color = 'green';
85                                         span.text( t.content ).css('color', color);
86
87                                         if ( ! barcode_on_screen( t.content ) ) {
88                                                 rfid_reset_field = 'barcode';
89                                                 var i = $('input[name=barcode]:last');
90                                                 if ( i.val() != t.content ) 
91                                                         i.val( t.content )
92                                                         .closest('form').submit();
93                                         }
94
95                                 } else {
96                                         span.text( t.content ).css('color', 'blue' );
97
98                                         if ( url.substr(-14,14) != 'circulation.pl' || $('form[name=mainform]').size() == 0 ) {
99                                                 rfid_reset_field = 'findborrower';
100                                                 $('input[name=findborrower]').val( t.content )
101                                                         .parent().submit();
102                                         }
103                                 }
104                         }
105                 } else {
106                         var error = data.tags.length + ' tags near reader: ';
107                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
108                         span.text( error ).css( 'color', 'red' );
109                 }
110
111         } else {
112                 span.text( 'no tags in range' ).css('color','gray');
113                 if ( rfid_reset_field ) {
114                         $('input[name='+rfid_reset_field+']').val( '' );
115                         rfid_reset_field = false;
116                 }
117         }
118
119         window.setTimeout( function() {
120                 $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
121         }, 1000 ); // 1000ms
122 }
123
124 $(document).ready( function() {
125         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
126 });