RFID: color fileds which will be filled
[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 = 1000; // ms
30 var rfid_count_timeout = 30; // number of times to scan reader before turning off
31
32 function rfid_secure_json(t,val, success) {
33         if ( t.security.toUpperCase() == val.toUpperCase() ) return;
34         rfid_refresh = 0; // disable rfid pull until secure call returns
35         console.log('rfid_secure_json', t, val);
36         $.getJSON( '///localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
37 }
38
39 function rfid_secure_check(t,val) {
40         if ( barcode_on_screen(t.content) ) {
41                 rfid_secure_json(t, val);
42         }
43 }
44
45
46 var rfid_reset_field = false;
47 var rfid_current_sid = false;
48 var rfid_blank_sid   = false;
49
50 function rfid_scan(data,textStatus) {
51
52         var rfid_count = $.cookie('rfid_count');
53         if ( rfid_count === undefined ) {
54                 rfid_count = rfid_count_timeout;
55         }
56
57         console.debug( 'rfid_scan', data, 'status', textStatus, 'rfid_count', rfid_count);
58         rfid_current_sid = false;
59         rfid_blank_sid = false;
60
61         var span = $('span#rfid');
62
63         if ( span.size() == 0 ) {
64                 // insert last in language bar on bottom
65 //              span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
66
67                 // alternative pop-up version
68                 span = $('#breadcrumbs').append('<div id="rfid_popup" style="position: fixed; bottom: 0; right: 1em; background: #fff; border: 3px solid #ff0; padding: 1em; opacity: 0.7; z-index: 1040;"><label for="rfid_active"><input type=checkbox id="rfid_active"><!-- local_ip -->&nbsp;<span id="rfid">RFID reader</span></label></div>');
69                 if ( rfid_count ) $('input#rfid_active').attr('checked',true);
70                 $('input#rfid_active').click(activate_scan_tags);
71         }
72
73
74         if ( span.size() == 0 ) // or before login on top
75                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
76
77         span = $('span#rfid');
78
79
80         if ( data.tags ) {
81                 if ( data.tags.length === 1 ) {
82                         var t = data.tags[0];
83                         rfid_current_sid = t.sid;
84
85 //                      if ( span.text() != t.content ) {
86                         if ( 1 ) { // force update of security
87
88                                 var script_name = document.location.pathname.split(/\//).pop();
89 //                              var tab_active  = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
90                                 var tab_active  = $("#header_search li[aria-selected=true]").attr('aria-controls');
91                                 console.debug('tab_active', tab_active);
92                                 var circulation = script_name == 'circulation.pl' || tab_active == 'circ_search' ;
93                                 var returns     = script_name == 'returns.pl' || tab_active == 'checkin_search';
94                                 console.debug('script_name', script_name, 'circulation', circulation, 'returns', returns);
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 ) || returns ) {
126                                                 rfid_reset_field = 'barcode';
127
128                                                 // return must be first to catch change of tab to check-in
129                                                 var afi_secure    = returns ? 'DA' : 'D7';
130                                                 var form_selector = returns ? 'first' : 'last';
131                                                 if ( returns || circulation ) {
132
133                                                         if ( circulation && $('#circ_needsconfirmation').length > 0 ) {
134                                                                 console.log("in circulation, but needs confirmation");
135                                                         } else {
136                                                                         var i = $('input[name=barcode]:focus');
137                                                                         var val = i.val();
138                                                                         if ( i ) {
139                                                                                 console.debug('use focus');
140                                                                         } else {
141                                                                                 console.debug('use', form_selector);
142                                                                                 i = $('input[name=barcode]:'+form_selector);
143                                                                         }
144                                                                         if ( i.val() != t.content )  {
145                                                                                 i.css('background', '#0ff' )
146                                                                                                 .val( t.content );
147                                                                                 rfid_secure_json( t, afi_secure, function(data) {
148                                                                                         console.log('secure', afi_secure, data);
149                                                                                         $.cookie('rfid_count', rfid_count_timeout);
150                                                                                         rfid_refresh = 0;
151                                                                                         i.css('background',
152                                                                                                         afi_secure == 'DA' ? '#f00' :
153                                                                                                         afi_secure == 'D7' ? '#0f0' :
154                                                                                                                                                  '#0f0'
155                                                                                                 )
156                                                                                                 .closest('form').submit();
157                                                                                 });
158                                                                         }
159                                                         }
160                                                 } else {
161                                                         console.error('not in circulation or returns');
162                                                 }
163                                         }
164
165                                 } else if ( t.content.substr(0,3) == '130' ) {
166
167                                         span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
168
169                                 } else {
170                                         span.html( t.content + '&nbsp;&sstarf;' ).css('color', 'blue' );
171
172                                         if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
173                                                 console.debug('not submitting', t.contains);
174                                         } else {
175                                                 rfid_refresh = 0; // stop rfid scan while submitting form
176                                                 rfid_reset_field = 'findborrower';
177                                                 $('input[name=findborrower]')
178                                                         .css('background', '#ff0')
179                                                         .val( t.content )
180                                                         .parent().submit();
181                                         }
182                                 }
183                         }
184                 } else {
185                         var error = data.tags.length + ' tags near reader: ';
186                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
187                         span.text( error ).css( 'color', 'red' );
188                 }
189
190         } else {
191                 span.text( 'no tags in range' ).css('color','gray');
192                 if ( rfid_reset_field ) {
193                         $('input[name='+rfid_reset_field+']').val( '' );
194                         rfid_reset_field = false;
195                 }
196         }
197
198
199         if (rfid_count > 0) rfid_count--;
200         if (rfid_count == 0) {
201                 //span.text('RFID reader disabled').css('color','black');
202                 $('input#rfid_active').attr('checked', false)
203                 console.log('RFID disabled', rfid_count);
204         }
205         $.cookie('rfid_count', rfid_count);
206
207
208         if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
209                 window.setTimeout( function() {
210                         if ( rfid_refresh ) {
211                                 $('#rfid_popup').css('border','3px solid #ff0');
212                                 $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
213                         } else {
214                                 console.error('got setTimeout but rfid_refresh', rfid_refresh, ' is not set');
215                         }
216                 }, rfid_refresh );
217         } else {
218                 console.debug('rfid_refresh disabled',rfid_refresh);
219         }
220
221         $('#rfid_popup').css('border','3px solid #fff');
222 }
223
224 function scan_tags() {
225         console.info('scan_tags');
226         $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
227 }
228
229 function set_rfid_active(active) {
230         var input_active = $('input#rfid_active').attr('checked');
231         console.info('set_rfid_active', active);
232         if ( active ) {
233                 $.cookie('rfid_count', rfid_count_timeout);
234                 scan_tags();
235                 if ( ! input_active ) $('input#rfid_active').attr('checked', true);
236         } else {
237                 if ( input_active ) $('input#rfid_active').attr('checked', false);
238                 $.cookie('rfid_count', 0);
239         }
240 }
241
242 function activate_scan_tags() {
243         var active = $('input#rfid_active').attr('checked');
244         console.info('activate_scan_tags', active);
245         set_rfid_active(active);
246 }
247
248 $(document).ready( function() {
249         console.log('rfid_active', $('input#rfid_active').attr('checked') );
250
251         scan_tags();
252
253         // circulation keyboard shortcuts (FFZG specific!)
254         shortcut.add('Alt+r', function() { set_rfid_active(true); } );
255         shortcut.add('Alt+z', function() { set_rfid_active(true); } );
256         shortcut.add('Alt+k', function() { set_rfid_active(false) } );
257         shortcut.add('Alt+y', function() { set_rfid_active(true); } ); // renew
258
259         // intranet cataloging
260         shortcut.add('F4', function() {
261                 // extract barcode from window title
262                 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
263                 if ( barcode ) {
264                         if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
265                                 rfid_blank_sid = rfid_current_sid;
266                         }
267
268                         console.debug('program barcode', barcode, 'to', rfid_blank_sid);
269                         $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
270                                 console.info('programmed', rfid_blank_sid, barcode, data);
271                         });
272                 } else {
273                         console.error('no barcode in window title');
274                 }
275         });
276
277 });