RFID: disable register popup
[koha_ffzg] / 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 var rainbow_colors = [ '#9400D3', '#4B0082', '#0000FF', '#00FF00', '#FFFF00', '#FF7F00', '#FF0000' ];
15
16 function barcode_on_screen(barcode) {
17         // FIXME: don't work without checkbox, probably broken
18         var found = 0;
19         $('table tr td a:contains(130)').each( function(i,o) {
20                 var possible = $(o).text();
21                 if ( possible == barcode ) found++;
22         })
23         var lastchecked = $('div.lastchecked').text();
24         if ( lastchecked ) {
25                 console.info('found lastchecked div', lastchecked);
26                 var checked_out_barcode = lastchecked.split(/\(/)[1].split(/\)/)[0];
27                 if ( checked_out_barcode == barcode ) found++;
28         }
29         // Not checked out message in returns.pl
30         var alert_dialog = $('div.alert p a:contains(130)').text();
31         if ( alert_dialog ) {
32                 console.info('found alert dialog', alert_dialog);
33                 var alert_barcode = alert_dialog.split(/:/)[0];
34                 if ( alert_barcode == barcode ) found++;
35         }
36         console.debug('barcode_on_screen', barcode, found);
37         return found;
38 }
39
40 var rfid_refresh = 200; // ms
41 var rfid_count_timeout = 50; // number of times to scan reader before turning off
42
43
44 function rfid_secure_json(t,val, success) {
45         if ( t.security.toUpperCase() == val.toUpperCase() ) return success({ verified: val });
46         rfid_refresh = 0; // disable rfid pull until secure call returns
47         console.log('rfid_secure_json', t, val);
48         $.getJSON( '///localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
49 }
50
51
52
53 var rfid_reset_field = false;
54 var rfid_current_sid = false;
55 var rfid_blank_sid   = false;
56 var rfid_action = undefined;
57 var rfid_scan_busy = false;
58
59 function rfid_scan(data,textStatus) {
60
61         rfid_scan_busy = false;
62
63         var rfid_count = $.cookie('rfid_count');
64         if ( rfid_count === undefined ) {
65                 rfid_count = rfid_count_timeout;
66         }
67
68         console.debug( 'rfid_scan', data, 'status', textStatus, 'rfid_count', rfid_count);
69         rfid_current_sid = false;
70         rfid_blank_sid = false;
71
72         var span = $('span#rfid');
73
74         if ( span.size() == 0 ) {
75                 // insert last in language bar on bottom
76 //              span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
77
78                 // alternative pop-up version
79                 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>&nbsp;<span id="rfid-info"></span></label></div>');
80                 if ( rfid_count ) $('input#rfid_active').attr('checked',true);
81                 $('input#rfid_active').click(activate_scan_tags); // FIXME don't activate actions on page load
82         }
83
84
85         if ( span.size() == 0 ) // or before login on top
86                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
87
88         span = $('span#rfid');
89         var info = $('span#rfid-info');
90
91
92         if ( data.tags ) {
93                 if ( data.tags.length === 1 ) {
94                         var t = data.tags[0];
95                         rfid_current_sid = t.sid;
96                         var rfid_last_tag = $.cookie('rfid_last_tag');
97
98 //                      if ( span.text() != t.content ) {
99                         if ( 1 ) { // force update of security
100
101                                 var script_name = document.location.pathname.split(/\//).pop();
102                                 var referrer_name = document.referrer.split(/\//).pop();
103                                 var tab_active  = $("#header_search li[aria-selected=true]").attr('aria-controls');
104                                 var focused_form = $('input:focus').first().name;
105                                 var action =
106                                         rfid_action                                                          ? rfid_action :
107                                         ( script_name == 'returns.pl'     || tab_active == 'checkin_search') ? 'checkin' : // must be before circulation
108                                         ( script_name == 'circulation.pl' || tab_active == 'circ_search' )   ? 'circulation' :
109                     'scan';
110                                 rfid_action = undefined; // one-shot
111                                 console.debug('script_name', script_name, 'referrer_name', referrer_name, 'tab_active', tab_active, 'action', action, 'focused_form', focused_form, 'rfid_last_tag' , rfid_last_tag );
112                                 info.text(action);
113
114                                 // keep refreshing rfid reader
115                                 if ( referrer_name == 'circulation.pl' ) {
116                                         rfid_count = rfid_count_timeout;
117                                 }
118
119
120                                 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
121
122                                         rfid_blank_sid = t.sid;
123                                         span.text( t.sid + ' blank' ).css('color', 'red' );
124
125                                 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
126
127                                         var color = 'blue';
128                                         var icon  = '?';
129                                         if ( t.security.toUpperCase() == 'DA' ) { color = 'red'; icon = '&timesb;' }
130                                         if ( t.security.toUpperCase() == 'D7' ) { color = 'green'; icon = '&rarr;' }
131                                         span.html( t.content + '&nbsp;' + icon ).css('color', color);
132
133
134                                         if ( tab_active == 'catalog_search'
135                                                 && script_name != 'moredetail.pl'
136                                                 && script_name != 'detail.pl'
137                                                 && $('input#rfid_active').attr('checked') ) {
138
139                                                 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
140                                                         $.cookie('rfid_count', rfid_count_timeout);
141                                                         rfid_refresh = 0;
142                                                         $('input[name=q]')
143                                                                 .css('background', '#ff0')
144                                                                 .val( 'bc:' + t.content )
145                                                                 .closest('form').submit();
146                                                 }
147                                         }
148
149                                         if (
150                                                 ( action == 'returns' || action == 'checkin' || action == 'circulation' )
151                                                 && ! barcode_on_screen( t.content )
152                                                 //&& t.content != rfid_last_tag
153                                         ) {
154                                                 rfid_reset_field = 'barcode';
155
156                                                 // return must be first to catch change of tab to check-in
157                                                 var afi_secure =
158                                                         action == 'checkin' ? 'DA' :
159                                                         action == 'circulation' ? 'D7' :
160                                                         t.security;
161
162                                                 var form_selector =
163                                                         script_name == 'returns.pl' ? 'last' : 'first';
164
165                                                 var i = $('input[name=barcode]:focus');
166                                                 if ( i.length == 1 ) {
167                                                         i.css('background', '#ff0');
168                                                         console.log('input barcode focus', i, i.val());
169                                                 } else {
170                                                         i = $('input[name=barcode]:'+form_selector).first();
171                                                         i.css('background', '#ff0');
172                                                         console.log('input barcode', form_selector, i, i.val());
173                                                 }
174
175                                                 if ( action == 'circulation' && $('#circ_needsconfirmation').length > 0 ) {
176                                                         console.log("in circulation, but needs confirmation");
177                                                 } else if (i) {
178
179                                                         console.debug('val', i.val(), 'name', i.name, 'i', i);
180
181                                                         if ( i.val() != t.content ) { // && i.name == 'barcode' )  {
182                                                                 i.css('background', '#0ff' );
183                                                                 rfid_refresh = 0;
184                                                                 rfid_secure_json( t, afi_secure, function(data) {
185                                                                         console.log('secure', afi_secure, data);
186                                                                         $.cookie('rfid_count', 0); // FIXME once? to see change rfid_count_timeout);
187                                                                         i.css('background',
188                                                                                         afi_secure == 'DA' ? '#f00' :
189                                                                                         afi_secure == 'D7' ? '#0f0' :
190                                                                                                                                 '#0ff'
191                                                                                 )
192                                                                                 .val( t.content )
193                                                                                 .closest('form').submit();
194                                                                 });
195                                                         } else {
196                                                                 console.error('not using element', i);
197                                                         }
198                                                 } else {
199                                                         console.error('element not found', i);
200                                                 }
201
202                                         } else {
203                                                 console.debug(action, 'no form submit');
204                                         }
205
206                                 } else if ( t.content.substr(0,3) == '130' ) {
207
208                                         span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
209
210                                 } else {
211                                         span.html( t.content + '&nbsp;&sstarf;' ).css('color', 'blue' );
212
213                                         if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
214                                                 console.debug('not submitting', t.contains);
215                                         } else {
216                                                 rfid_refresh = 0; // stop rfid scan while submitting form
217                                                 rfid_reset_field = 'findborrower';
218                                                 $('input[name=findborrower]')
219                                                         .css('background', '#00f')
220                                                         .val( t.content )
221                                                         .parent().submit();
222                                         }
223                                 }
224                         }
225                 } else {
226                         var error = data.tags.length + ' tags near reader: ';
227                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
228                         span.text( error ).css( 'color', 'red' );
229                 }
230
231         } else {
232                 span.text( 'no tags in range' ).css('color','gray');
233                 if ( rfid_reset_field ) {
234                         $('input[name='+rfid_reset_field+']').val( '' );
235                         rfid_reset_field = false;
236                 }
237         }
238
239         if (rfid_count > 0) rfid_count--;
240         if (rfid_count == 0) {
241                 //span.text('RFID reader disabled').css('color','black');
242                 $('input#rfid_active').attr('checked', false)
243                 console.log('RFID disabled', rfid_count);
244         }
245         $.cookie('rfid_count', rfid_count);
246
247         if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
248                 window.setTimeout( function() {
249                         if ( rfid_refresh ) {
250                                 var color = rainbow_colors[ rfid_count % rainbow_colors.length ];
251                                 console.debug('color', color);
252                                 $('#rfid_popup').css('border','3px solid '+color);
253                                 scan_tags();
254                         } else {
255                                 console.error('got setTimeout but rfid_refresh', rfid_refresh, ' is not set');
256                         }
257                 }, rfid_refresh );
258         } else {
259                 console.debug('rfid_refresh disabled',rfid_refresh);
260                 $('#rfid_popup').css('border','3px solid #fff');
261         }
262
263         $.cookie('rfid_last_tag', t ? t.content : '--none--');
264
265 }
266
267 function scan_tags() {
268         if ( rfid_scan_busy ) {
269                 console.error('rfid_scan_busy');
270                 return;
271         }
272         rfid_scan_busy = true;
273         console.info('scan_tags');
274         $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
275 }
276
277 function set_rfid_active(active,action) {
278         rfid_action = action;
279         var input_active = $('input#rfid_active').attr('checked');
280         if ( active && input_active ) {
281                 $.cookie('rfid_count', rfid_count_timeout);
282                 console.info('ignored set_rfid_active ', active, action);
283                 scan_tags();
284                 return;
285         }
286         console.info('set_rfid_active', active);
287         if ( active ) {
288                 $.cookie('rfid_count', rfid_count_timeout);
289                 scan_tags();
290                 if ( ! input_active ) $('input#rfid_active').attr('checked', true);
291         } else {
292                 if ( input_active ) $('input#rfid_active').attr('checked', false);
293                 $.cookie('rfid_count', 0);
294         }
295 }
296
297 function activate_scan_tags() {
298         var active = $('input#rfid_active').attr('checked');
299         console.info('activate_scan_tags', active);
300         set_rfid_active(active);
301 }
302
303 $(document).ready( function() {
304         console.log('rfid_active', $('input#rfid_active').attr('checked') );
305
306
307         rfid_action = 'scan';
308         scan_tags();    // FIXME should we trigger this on page load even if rfid is not active
309
310         // circulation keyboard shortcuts (FFZG specific!)
311         shortcut.add('Alt+r', function() { set_rfid_active(true,'checkin'    )});
312         shortcut.add('Alt+z', function() { set_rfid_active(true,'circulation')});
313         shortcut.add('Alt+y', function() { set_rfid_active(true,'circulation')});
314 /*
315         shortcut.add('Alt+3', function() { set_rfid_active(true,'search?'    )});
316         shortcut.add('Alt+4', function() { set_rfid_active(true,'renew'      )}); // renew
317 */
318
319         // send RFID tag to currently focused field on screen
320         shortcut.add('Alt+s', function() {
321                 var el = $('input:focus');
322                 var tag = $('span#rfid').text().split(/\s+/)[0];
323                 console.log('send', el[0].name, tag, el);
324                 if ( el && tag ) el.css('background', '#ff0').val( tag )
325                         ;//.closest('form').submit();
326         } );
327
328         shortcut.add('F9', function() {
329                 console.log('F9');
330                 //set_rfid_active(true,'F9');
331                 scan_tags();
332         });
333
334         // intranet cataloging
335         shortcut.add('F4', function() {
336                 // extract barcode from window title
337                 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
338                 if ( barcode ) {
339                         if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
340                                 rfid_blank_sid = rfid_current_sid;
341                         }
342
343                         console.debug('program barcode', barcode, 'to', rfid_blank_sid);
344                         $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
345                                 console.info('programmed', rfid_blank_sid, barcode, data);
346                         });
347                 } else {
348                         console.error('no barcode in window title');
349                 }
350         });
351
352 });