f1ee7654a8473c2ac17a4dab7425c2a98c2eb56e
[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
14 function barcode_on_screen(barcode) {
15         var found = 0;
16         $('table tr td a:contains(130)').each( function(i,o) {
17                 var possible = $(o).text();
18                 if ( possible == barcode ) found++;
19         })
20         return found;
21 }
22
23 var rfid_refresh = 1000; // ms
24 var rfid_count_timeout = 30; // number of times to scan reader before turning off
25
26 rfid_count_timeout = 0; // FIXME disable
27
28 function rfid_secure_json(t,val, success) {
29         if ( t.security.toUpperCase() == val.toUpperCase() ) return success();
30         rfid_refresh = 0; // disable rfid pull until secure call returns
31         console.log('rfid_secure_json', t, val);
32         $.getJSON( '///localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
33 }
34
35 function rfid_secure_check(t,val) {
36         if ( barcode_on_screen(t.content) ) {
37                 rfid_secure_json(t, val);
38         }
39 }
40
41
42 var rfid_reset_field = false;
43 var rfid_current_sid = false;
44 var rfid_blank_sid   = false;
45
46 function rfid_scan(data,textStatus) {
47
48         var rfid_count = $.cookie('rfid_count');
49         if ( rfid_count === undefined ) {
50                 rfid_count = rfid_count_timeout;
51         }
52
53         console.debug( 'rfid_scan', data, 'status', textStatus, 'rfid_count', rfid_count);
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: 0; right: 0; background: #fff; border: 0.25em solid #ff0; padding: 0.25em; opacity: 0.9; z-index: 1040; font-size: 200%"><label for="rfid_active"><input type=checkbox id="rfid_active"><!-- local_ip -->&nbsp;<span id="rfid">RFID reader</span><span id="rfid-info"></span></label></div>');
65                 if ( rfid_count ) $('input#rfid_active').attr('checked',true);
66                 $('input#rfid_active').click(activate_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         var info = $('span#rfid-info');
75
76
77         if ( data.tags ) {
78                 if ( data.tags.length === 1 ) {
79                         var t = data.tags[0];
80                         rfid_current_sid = t.sid;
81
82 //                      if ( span.text() != t.content ) {
83                         if ( 1 ) { // force update of security
84
85                                 var script_name = document.location.pathname.split(/\//).pop();
86 //                              var tab_active  = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
87                                 var tab_active  = $("#header_search li[aria-selected=true]").attr('aria-controls');
88                                 console.debug('tab_active', tab_active);
89                                 var action =
90                                         ( script_name == 'circulation.pl' || tab_active == 'circ_search' )   ? 'circulation' :
91                                         ( script_name == 'returns.pl'     || tab_active == 'checkin_search') ? 'checkin' :
92                                         'scan';
93                                 console.debug('script_name', script_name, 'action', action);
94                                 info.text(action);
95
96                                 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
97
98                                         rfid_blank_sid = t.sid;
99                                         span.text( t.sid + ' blank' ).css('color', 'red' );
100
101                                 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
102
103                                         var color = 'blue';
104                                         var icon  = '?';
105                                         if ( t.security.toUpperCase() == 'DA' ) { color = 'red'; icon = '&timesb;' }
106                                         if ( t.security.toUpperCase() == 'D7' ) { color = 'green'; icon = '&rarr;' }
107                                         span.html( t.content + '&nbsp;' + icon ).css('color', color);
108
109
110                                         if ( tab_active == 'catalog_search'
111                                                 && script_name != 'moredetail.pl'
112                                                 && script_name != 'detail.pl'
113                                                 && $('input#rfid_active').attr('checked') ) {
114
115                                                 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
116                                                         $.cookie('rfid_count', rfid_count_timeout);
117                                                         rfid_refresh = 0;
118                                                         $('input[name=q]')
119                                                                 .css('background', '#ff0')
120                                                                 .val( 'bc:' + t.content )
121                                                                 ;//.closest('form').submit();
122                                                 }
123                                         }
124
125                                         if ( ! barcode_on_screen( t.content ) || action == 'returns' || action == 'checkin' ) {
126                                                 rfid_reset_field = 'barcode';
127
128                                                 // return must be first to catch change of tab to check-in
129                                                 var afi_secure =
130                                                         action == 'returns' ? 'DA' :
131                                                         action == 'checkin' ? 'DA' :
132                                                         action == 'FIXME' ? 'D7' :
133                                                         t.security;
134                                                 var form_selector = action == 'returns' ? 'first' : 'last';
135                                                 if ( action == 'returns' || action == 'circulation' || action == 'checkin' ) {
136
137                                                         if ( action == 'circulation' && $('#circ_needsconfirmation').length > 0 ) {
138                                                                 console.log("in circulation, but needs confirmation");
139                                                         } else {
140                                                                         var i = $('input[name=barcode]:focus');
141                                                                         var val = i.val();
142                                                                         if ( i ) {
143                                                                                 console.debug('use focus');
144                                                                         } else {
145                                                                                 console.debug('use', form_selector);
146                                                                                 i = $('input[name=barcode]:'+form_selector);
147                                                                         }
148                                                                         if ( i.val() != t.content )  {
149                                                                                 i.css('background', '#0ff' );
150                                                                                 rfid_secure_json( t, afi_secure, function(data) {
151                                                                                         console.log('secure', afi_secure, data);
152                                                                                         $.cookie('rfid_count', 0); // FIXME once? to see change rfid_count_timeout);
153                                                                                         rfid_refresh = 0;
154                                                                                         i.css('background',
155                                                                                                         afi_secure == 'DA' ? '#f00' :
156                                                                                                         afi_secure == 'D7' ? '#0f0' :
157                                                                                                                                                  '#0ff'
158                                                                                                 )
159                                                                                                 .val( t.content )
160                                                                                                 ; //.closest('form').submit();
161                                                                                 });
162                                                                         }
163                                                         }
164                                                 } else {
165                                                         console.error('not in circulation or returns');
166                                                 }
167                                         }
168
169                                 } else if ( t.content.substr(0,3) == '130' ) {
170
171                                         span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
172
173                                 } else {
174                                         span.html( t.content + '&nbsp;&sstarf;' ).css('color', 'blue' );
175
176                                         if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
177                                                 console.debug('not submitting', t.contains);
178                                         } else {
179                                                 rfid_refresh = 0; // stop rfid scan while submitting form
180                                                 rfid_reset_field = 'findborrower';
181                                                 $('input[name=findborrower]')
182                                                         .css('background', '#ff0')
183                                                         .val( t.content )
184                                                         ;//.parent().submit();
185                                         }
186                                 }
187                         }
188                 } else {
189                         var error = data.tags.length + ' tags near reader: ';
190                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
191                         span.text( error ).css( 'color', 'red' );
192                 }
193
194         } else {
195                 span.text( 'no tags in range' ).css('color','gray');
196                 if ( rfid_reset_field ) {
197                         $('input[name='+rfid_reset_field+']').val( '' );
198                         rfid_reset_field = false;
199                 }
200         }
201
202
203         if (rfid_count > 0) rfid_count--;
204         if (rfid_count == 0) {
205                 //span.text('RFID reader disabled').css('color','black');
206                 $('input#rfid_active').attr('checked', false)
207                 console.log('RFID disabled', rfid_count);
208         }
209         $.cookie('rfid_count', rfid_count);
210
211 /*
212         if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
213                 window.setTimeout( function() {
214                         if ( rfid_refresh ) {
215                                 $('#rfid_popup').css('border','3px solid #ff0');
216                                 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
217                         } else {
218                                 console.error('got setTimeout but rfid_refresh', rfid_refresh, ' is not set');
219                         }
220                 }, rfid_refresh );
221         } else {
222                 console.debug('rfid_refresh disabled',rfid_refresh);
223         }
224
225         $('#rfid_popup').css('border','3px solid #fff');
226 */
227 }
228
229 function scan_tags() {
230         console.info('scan_tags');
231         $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
232 }
233
234 function set_rfid_active(active) {
235         var input_active = $('input#rfid_active').attr('checked');
236         console.info('set_rfid_active', active);
237         if ( active ) {
238                 $.cookie('rfid_count', rfid_count_timeout);
239                 scan_tags();
240                 if ( ! input_active ) $('input#rfid_active').attr('checked', true);
241         } else {
242                 if ( input_active ) $('input#rfid_active').attr('checked', false);
243                 $.cookie('rfid_count', 0);
244         }
245 }
246
247 function activate_scan_tags() {
248         var active = $('input#rfid_active').attr('checked');
249         console.info('activate_scan_tags', active);
250         set_rfid_active(active);
251 }
252
253 $(document).ready( function() {
254         console.log('rfid_active', $('input#rfid_active').attr('checked') );
255
256         scan_tags();    // FIXME should we trigger this on page load even if rfid is not active
257
258         // circulation keyboard shortcuts (FFZG specific!)
259         shortcut.add('Alt+r', function() { set_rfid_active(true); } );
260         shortcut.add('Alt+z', function() { set_rfid_active(true); } );
261         shortcut.add('Alt+k', function() { set_rfid_active(false) } );
262         shortcut.add('Alt+y', function() { set_rfid_active(true); } ); // renew
263
264         // send RFID tag to currently focused field on screen
265         shortcut.add('Alt+s', function() {
266                 var el = $('input:focus');
267                 var tag = $('span#rfid').text().split(/\s+/)[0];
268                 console.log('send', el, tag);
269                 if ( el && tag ) el.css('background', '#ff0').val( tag );
270         } );
271
272         // intranet cataloging
273         shortcut.add('F4', function() {
274                 // extract barcode from window title
275                 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
276                 if ( barcode ) {
277                         if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
278                                 rfid_blank_sid = rfid_current_sid;
279                         }
280
281                         console.debug('program barcode', barcode, 'to', rfid_blank_sid);
282                         $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
283                                 console.info('programmed', rfid_blank_sid, barcode, data);
284                         });
285                 } else {
286                         console.error('no barcode in window title');
287                 }
288         });
289
290 });