3 var end_timeout = 3000; // ms from end page to start page
4 var error_timeout = 5000; // ms from error page to start page
5 var tag_rescan = 200; // ms rescan tags every 0.2s
9 window.console = new function() {
10 this.info = function(str) {};
11 this.error = function(str) {};
12 this.debug = function(str) {};
18 var pending_jsonp = 0;
21 // timeout warning dialog
22 var tick_timeout = 25; // s
23 var tick_warning = 10; // s
26 function beep( message ) {
28 $.getJSON("/beep/" + message)
29 .done( function(data) {
32 .fail( function(data) {
37 function start_timeout() {
39 tick = Math.round( tick_timeout * ( 1000 / tag_rescan ) );
42 function change_page(new_state) {
43 if ( state != new_state ) {
45 if ( new_state == 'checkin' ) {
46 new_state = 'circulation'; // page has different name
47 $('.checkout').hide();
49 circulation_type = 'checkin';
50 borrower_cardnumber = 0; // fake
51 only_reader = '/only/3M';
52 } else if ( new_state == 'checkout' ) {
53 new_state = 'circulation'; // page has different name
54 $('.checkout').show();
56 circulation_type = 'checkout';
57 only_reader = '/only/3M';
62 $('.page').each( function(i,el) {
63 if ( el.id != new_state ) {
69 console.info('change_page', state);
71 if ( state == 'start' ) {
72 circulation_type = 'checkout';
74 $('ul#books').html(''); // clear book list
75 $('#books_count').html( 0 );
76 only_reader = '/only/librfid';
80 if ( state == 'end' ) {
81 window.setTimeout(function(){
82 //change_page('start');
83 location.reload(); // force js VM to GC?
87 if ( state == 'error' || state == 'error-borrower' ) {
89 window.setTimeout(function(){
90 //change_page('start');
95 if ( state == 'circulation' || state == 'borrower_info' ) {
98 tick = 0; // timeout disabled
103 function got_visible_tags(data,textStatus) {
104 var html = 'No tags in range';
106 html = '<ul class="tags">';
107 $.each(data.tags, function(i,tag) {
108 console.debug( i, tag );
110 if ( tag.hasOwnProperty('error') ) {
111 html += 'ERROR ' + tag.sid + ' ' + tag.error;
112 change_page('error-borrower');
115 html += '<li><tt class="' + tag.security + '">' + tag.sid;
116 var content = tag.content || tag.borrower.cardnumber;
120 if ( content.length = 10 && content.substr(0,3) == 130 ) { // book
121 link = 'catalogue/search.pl?q=';
122 } else if ( content.length == 12 && content.substr(0,2) == 20 ) {
123 link = 'members/member.pl?member=';
124 } else if ( tag.tag_type == 'SmartX' ) {
125 link = 'members/member.pl?member=';
127 html += '<b>UNKNOWN TAG</b> '+content;
131 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/'
133 + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
137 console.debug( 'calling', state, content );
138 window[state]( content, tag ); // call function with barcode
149 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
151 html = '<div class=status>'
153 + ' &#' + arrows[ data.time % arrows.length ] + ';'
157 $('#tags').html( html ); // FIXME leaks memory?
162 var wait_counter = 0;
164 function scan_tags() {
165 if ( pending_jsonp ) {
167 console.debug('scan_tags disabled ', pending_jsonp, ' requests waiting counter', wait_counter);
168 if ( wait_counter > 3 ) $('#working').show();
170 console.info('scan_tags', only_reader);
172 $.getJSON("/scan"+only_reader+"?callback=?", got_visible_tags).fail( function(data) {
173 console.error('scan error pending jsonp', pending_jsonp);
177 $('#working').hide();
181 if ( tick < tick_warning * ( 1000 / tag_rescan ) ) {
182 $('#tick').html( Math.round( tick * tag_rescan / 1000 ) );
183 $('#timeout').show();
187 $('#timeout').hide();
192 scan_timeout = window.setTimeout(function(){
194 },tag_rescan); // re-scan every 200ms
197 $(document).ready(function() {
198 $('div#tags').click( function() {
202 change_page('start');
205 function fill_in( where, value ) {
206 $('.'+where).each(function(i, el) {
212 /* Selfcheck state actions */
214 var borrower_cardnumber;
215 var circulation_type;
216 var book_barcodes = {};
218 function start( cardnumber, tag ) {
220 if ( tag.tag_type != 'SmartX' && ( cardnumber.length != 12 || cardnumber.substr(0,2) != "20" ) ) {
221 console.error(cardnumber, 'is not borrower card', tag);
225 borrower_cardnumber = cardnumber; // for circulation
227 fill_in( 'borrower_number', cardnumber );
230 $.getJSON('/sip2/patron_info/'+cardnumber)
231 .done( function( data ) {
232 console.info('patron', data);
233 fill_in( 'borrower_name', data['AE'] );
234 fill_in( 'borrower_email', data['BE'] );
235 fill_in( 'hold_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 0 * 4 ), 4 ) * 1 );
236 //fill_in( 'overdue_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) * 1 );
237 var overdue = data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) * 1;
239 overdue = '<span style="color:red">'+overdue+'</span>';
240 beep( 'overdue: ' + overdue );
242 fill_in( 'overdue_items', overdue );
243 fill_in( 'charged_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 2 * 4 ), 4 ) * 1 );
244 fill_in( 'fine_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 3 * 4 ), 4 ) * 1 );
248 change_page('borrower_info');
249 }).fail( function(data) {
251 change_page('error');
255 function borrower_info() {
259 function circulation( barcode, tag ) {
261 && barcode.length == 10
262 && barcode.substr(0,3) == 130
263 && book_barcodes[barcode] != 1
264 && tag.reader == '3M810'
265 ) { // book, not seen yet
266 book_barcodes[ barcode ] = 1;
268 $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode+'/'+tag.sid , function( data ) {
269 console.info( circulation_type, data );
272 var message = 'Transakcija neuspješna. Odnesite knjige na pult!';
273 if ( data['fixed'].substr(2,1) == 1 ) {
275 message = circulation_type == 'checkout' ? 'Posuđeno' : 'Vraćeno';
277 beep( circulation_type + ': ' + data['AF'] );
281 message = data['AF'] + ' ' + message;
284 $('ul#books').append('<li>' + ( data['AJ'] || barcode ) + ' <b style="color:'+color+'">' + message + '</b></li>');
285 $('#books_count').html( $('ul#books > li').length );
286 console.debug( book_barcodes );
288 start_timeout(); // reset timeout
289 }).fail( function() {
290 change_page('error');