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