make RFID window floating, better instructions
[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                 if ( possible == barcode ) found++;
25         })
26         return found;
27 }
28
29 var rfid_refresh = 1500; // ms
30
31 function rfid_secure_json(t,val, success) {
32         if ( t.security.toUpperCase() == val.toUpperCase() ) return;
33         rfid_refresh = 0; // disable rfid pull until secure call returns
34         console.log('rfid_secure_json', t, val);
35         $.getJSON( 'http://localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
36 }
37
38 function rfid_secure_check(t,val) {
39         if ( barcode_on_screen(t.content) ) {
40                 rfid_secure_json(t, val);
41         }
42 }
43
44
45 var rfid_reset_field = false;
46 var rfid_current_sid = false;
47 var rfid_blank_sid   = false;
48
49 function rfid_scan(data,textStatus) {
50
51         console.debug( 'rfid_scan', data, textStatus );
52         rfid_current_sid = false;
53         rfid_blank_sid = false;
54
55         var span = $('span#rfid');
56
57         if ( span.size() == 0 ) // insert last in language bar on bottom
58 //              span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
59                 span = $('#breadcrumbs').append('<div id="rfid_popup" style="position: fixed; bottom: 0; right: 0; background: #fff; border: 3px solid #ff0; padding: 1em; opacity: 0.7; z-index: 10;"><span id="rfid">RFID reader</span></div>');
60
61         if ( span.size() == 0 ) // or before login on top
62                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
63
64         span = $('span#rfid');
65
66
67         if ( data.tags ) {
68                 if ( data.tags.length === 1 ) {
69                         var t = data.tags[0];
70                         rfid_current_sid = t.sid;
71
72 //                      if ( span.text() != t.content ) {
73                         if ( 1 ) { // force update of security
74
75                                 var script_name = document.location.pathname.split(/\//).pop();
76                                 var tab_active  = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
77                                 console.debug('tab_active', tab_active);
78                                 var circulation = script_name == 'circulation.pl';
79                                 var returns     = script_name == 'returns.pl' || tab_active == 'checkin_search';
80
81                                 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
82
83                                         rfid_blank_sid = t.sid;
84                                         span.text( t.sid + ' blank' ).css('color', 'red' );
85
86                                 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
87
88                                         var color = 'blue';
89                                         if ( t.security.toUpperCase() == 'DA' ) color = 'red';
90                                         if ( t.security.toUpperCase() == 'D7' ) color = 'green';
91                                         span.text( t.content ).css('color', color);
92
93
94                                         if ( tab_active == 'catalog_search' && script_name != 'moredetail.pl' ) {
95                                                 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
96                                                         $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
97                                                 }
98                                         }
99
100                                         if ( ! barcode_on_screen( t.content ) || returns ) {
101                                                 rfid_reset_field = 'barcode';
102
103                                                 // return must be first to catch change of tab to check-in
104                                                 var afi_secure    = returns ? 'DA' : 'D7';
105                                                 var form_selector = returns ? 'first' : 'last';
106                                                 if ( returns || circulation ) {
107                                                         var i = $('input[name=barcode]:'+form_selector);
108                                                         if ( i.val() != t.content )  {
109                                                                 rfid_secure_json( t, afi_secure, function(data) {
110                                                                         console.log('secure', afi_secure, data);
111                                                                         i.val( t.content ).closest('form').submit();
112                                                                 });
113                                                         }
114                                                 } else {
115                                                         console.error('not in circulation or returns');
116                                                 }
117                                         }
118
119                                 } else if ( t.content.substr(0,3) == '130' ) {
120
121                                         span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
122
123                                 } else {
124                                         span.text( t.content ).css('color', 'blue' );
125
126                                         if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
127                                                 console.debug('not submitting', t.contains);
128                                         } else {
129                                                 rfid_refresh = 0; // stop rfid scan while submitting form
130                                                 rfid_reset_field = 'findborrower';
131                                                 $('input[name=findborrower]').val( t.content )
132                                                         .parent().submit();
133                                         }
134                                 }
135                         }
136                 } else {
137                         var error = data.tags.length + ' tags near reader: ';
138                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
139                         span.text( error ).css( 'color', 'red' );
140                 }
141
142         } else {
143                 span.text( 'no tags in range' ).css('color','gray');
144                 if ( rfid_reset_field ) {
145                         $('input[name='+rfid_reset_field+']').val( '' );
146                         rfid_reset_field = false;
147                 }
148         }
149
150         if (rfid_refresh > 1) {
151                 window.setTimeout( function() {
152                         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
153                 }, rfid_refresh );
154         } else {
155                 console.debug('rfid_refresh disabled',rfid_refresh);
156         }
157 }
158
159 $(document).ready( function() {
160         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
161
162         shortcut.add('F4', function() {
163                 // extract barcode from window title
164                 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
165                 if ( barcode ) {
166                         if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
167                                 rfid_blank_sid = rfid_current_sid;
168                         }
169
170                         console.debug('program barcode', barcode, 'to', rfid_blank_sid);
171                         $.getJSON( 'http://localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
172                                 console.info('programmed', rfid_blank_sid, barcode, data);
173                         });
174                 } else {
175                         console.error('no barcode in window title');
176                 }
177         });
178
179 });