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