RFID: improve interface, show reader checkbox and IP
[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( '///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         $.cookie('rfid', 'localhost'); // set cookie
52
53         console.debug( 'rfid_scan', data, textStatus, $.cookie('rfid') );
54         rfid_current_sid = false;
55         rfid_blank_sid = false;
56
57         var span = $('span#rfid');
58
59         if ( span.size() == 0 ) {
60                 // insert last in language bar on bottom
61 //              span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
62
63                 // alternative pop-up version
64                 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: 10;"><label for="rfid_active"><input type=checkbox id="rfid_active"> local_ip <span id="rfid">RFID reader</span></label></div>');
65                 $('input#rfid_active').attr('checked',true);
66                 $('input#rfid_active').click(scan_tags);
67         }
68
69
70         if ( span.size() == 0 ) // or before login on top
71                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
72
73         span = $('span#rfid');
74
75
76         if ( data.tags ) {
77                 if ( data.tags.length === 1 ) {
78                         var t = data.tags[0];
79                         rfid_current_sid = t.sid;
80
81 //                      if ( span.text() != t.content ) {
82                         if ( 1 ) { // force update of security
83
84                                 var script_name = document.location.pathname.split(/\//).pop();
85 //                              var tab_active  = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
86                                 var tab_active  = $("#header_search li[aria-selected=true]").attr('aria-controls');
87                                 console.debug('tab_active', tab_active);
88                                 var circulation = script_name == 'circulation.pl' || tab_active == 'circ_search' ;
89                                 var returns     = script_name == 'returns.pl' || tab_active == 'checkin_search';
90
91                                 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
92
93                                         rfid_blank_sid = t.sid;
94                                         span.text( t.sid + ' blank' ).css('color', 'red' );
95
96                                 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
97
98                                         var color = 'blue';
99                                         if ( t.security.toUpperCase() == 'DA' ) color = 'red';
100                                         if ( t.security.toUpperCase() == 'D7' ) color = 'green';
101                                         span.text( t.content ).css('color', color);
102
103
104                                         if ( tab_active == 'catalog_search' && script_name != 'moredetail.pl' ) {
105                                                 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
106                                                         $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
107                                                 }
108                                         }
109
110                                         if ( ! barcode_on_screen( t.content ) || returns ) {
111                                                 rfid_reset_field = 'barcode';
112
113                                                 // return must be first to catch change of tab to check-in
114                                                 var afi_secure    = returns ? 'DA' : 'D7';
115                                                 var form_selector = returns ? 'first' : 'last';
116                                                 if ( returns || circulation ) {
117                                                         var i = $('input[name=barcode]:'+form_selector);
118                                                         if ( i.val() != t.content )  {
119                                                                 rfid_secure_json( t, afi_secure, function(data) {
120                                                                         console.log('secure', afi_secure, data);
121                                                                         i.val( t.content ).closest('form').submit();
122                                                                 });
123                                                         }
124                                                 } else {
125                                                         console.error('not in circulation or returns');
126                                                 }
127                                         }
128
129                                 } else if ( t.content.substr(0,3) == '130' ) {
130
131                                         span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
132
133                                 } else {
134                                         span.text( t.content ).css('color', 'blue' );
135
136                                         if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
137                                                 console.debug('not submitting', t.contains);
138                                         } else {
139                                                 rfid_refresh = 0; // stop rfid scan while submitting form
140                                                 rfid_reset_field = 'findborrower';
141                                                 $('input[name=findborrower]').val( t.content )
142                                                         .parent().submit();
143                                         }
144                                 }
145                         }
146                 } else {
147                         var error = data.tags.length + ' tags near reader: ';
148                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
149                         span.text( error ).css( 'color', 'red' );
150                 }
151
152         } else {
153                 span.text( 'no tags in range' ).css('color','gray');
154                 if ( rfid_reset_field ) {
155                         $('input[name='+rfid_reset_field+']').val( '' );
156                         rfid_reset_field = false;
157                 }
158         }
159
160         if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') == 'checked' ) {
161                 window.setTimeout( function() {
162                         $('#rfid_popup').css('border','3px solid #fff');
163                         $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
164                 }, rfid_refresh );
165         } else {
166                 console.debug('rfid_refresh disabled',rfid_refresh);
167         }
168
169         $('#rfid_popup').css('border','3px solid #ff0');
170 }
171
172 function scan_tags() {
173         console.info('scan_tags');
174         $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
175 }
176
177 $(document).ready( function() {
178         console.log('rfid_active', $('input#rfid_active').attr('checked') );
179
180         scan_tags();
181
182         // intranet catalogingdd
183         shortcut.add('F4', function() {
184                 // extract barcode from window title
185                 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
186                 if ( barcode ) {
187                         if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
188                                 rfid_blank_sid = rfid_current_sid;
189                         }
190
191                         console.debug('program barcode', barcode, 'to', rfid_blank_sid);
192                         $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
193                                 console.info('programmed', rfid_blank_sid, barcode, data);
194                         });
195                 } else {
196                         console.error('no barcode in window title');
197                 }
198         });
199
200 });