call rfid_secure JSONP only if security changed
[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(t,val) {
31         if ( t.security.toUpperCase() == val.toUpperCase() ) return;
32         console.debug('rfid_secure', t.content, t.sid, val);
33         if ( barcode_on_screen(t.content) ) 
34                 $.getJSON( 'http://localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?' )
35 }
36
37 var rfid_reset_field = false;
38
39 function rfid_scan(data,textStatus) {
40         var rfid_refresh = 1500; // ms
41
42         console.debug( 'rfid_scan', data, textStatus );
43
44         var span = $('span#rfid');
45
46         if ( span.size() == 0 ) // insert last in language bar on bottom
47                 span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
48
49         if ( span.size() == 0 ) // or before login on top
50                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
51
52         span = $('span#rfid');
53
54
55         if ( data.tags ) {
56                 if ( data.tags.length === 1 ) {
57                         var t = data.tags[0];
58 //                      if ( span.text() != t.content ) {
59                         if ( 1 ) { // force update of security
60
61                                 var url = document.location.toString();
62                                 var circulation = url.substr(-14,14) == 'circulation.pl';
63                                 var returns = url.substr(-10,10) == 'returns.pl';
64
65                                 if ( t.content.length == 0 ) { // empty tag
66
67                                         span.text( t.sid + ' empty' ).css('color', 'red' );
68
69                                 } else if ( t.content.substr(0,3) == '130' ) { // books
70
71                                         if ( circulation ) rfid_secure( t, 'D7' );
72                                         if ( returns     ) rfid_secure( t, 'DA' );
73
74                                         var color = 'blue';
75                                         if ( t.security.toUpperCase() == 'DA' ) color = 'red';
76                                         if ( t.security.toUpperCase() == 'D7' ) color = 'green';
77                                         span.text( t.content ).css('color', color);
78
79                                         if ( ! barcode_on_screen( t.content ) ) {
80                                                 rfid_reset_field = 'barcode';
81                                                 var i = $('input[name=barcode]:last');
82                                                 if ( i.val() != t.content )  {
83                                                         rfid_refresh = 0;
84                                                         i.val( t.content )
85                                                         .closest('form').submit();
86                                                 }
87                                         }
88
89                                 } else {
90                                         span.text( t.content ).css('color', 'blue' );
91
92                                         if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
93                                                 console.debug('not submitting', t.contains);
94                                         } else {
95                                                 rfid_refresh = 0;
96                                                 rfid_reset_field = 'findborrower';
97                                                 $('input[name=findborrower]').val( t.content )
98                                                         .parent().submit();
99                                         }
100                                 }
101                         }
102                 } else {
103                         var error = data.tags.length + ' tags near reader: ';
104                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
105                         span.text( error ).css( 'color', 'red' );
106                 }
107
108         } else {
109                 span.text( 'no tags in range' ).css('color','gray');
110                 if ( rfid_reset_field ) {
111                         $('input[name='+rfid_reset_field+']').val( '' );
112                         rfid_reset_field = false;
113                 }
114         }
115
116         if (rfid_refresh > 1) {
117                 window.setTimeout( function() {
118                         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
119                 }, rfid_refresh );
120         } else {
121                 console.debug('rfid_refresh disabled',rfid_refresh);
122         }
123 }
124
125 $(document).ready( function() {
126         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
127 });