update books_count after list
[Biblio-RFID.git] / examples / selfcheck.js
1
2 // configure timeouts
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
6
7 // mock console
8 if(!window.console) {
9         window.console = new function() {
10                 this.info = function(str) {};
11                 this.error = function(str) {};
12                 this.debug = function(str) {};
13         };
14 }
15
16 var state;
17 var scan_timeout;
18 var pending_jsonp = 0;
19
20 function change_page(new_state) {
21         if ( state != new_state ) {
22
23                 if ( new_state == 'checkin' ) {
24                         new_state = 'circulation'; // page has different name
25                         $('.checkout').hide();
26                         $('.checkin').show();
27                         circulation_type = 'checkin';
28                         borrower_cardnumber = 0; // fake
29                 } else if ( new_state == 'checkout' ) {
30                         new_state = 'circulation'; // page has different name
31                         $('.checkout').show();
32                         $('.checkin').hide();
33                         circulation_type = 'checkout';
34                 }
35
36                 state = new_state;
37
38                 $('.page').each( function(i,el) {
39                         if ( el.id != new_state ) {
40                                 $(el).hide();
41                         } else {
42                                 $(el).show();
43                         }
44                 });
45                 console.info('change_page', state);
46
47                 if ( state == 'start' ) {
48                         circulation_type = 'checkout';
49                         book_barcodes = {};
50                         $('ul#books').html(''); // clear book list
51                         $('#books_count').html( 0 );
52                         scan_tags();
53                 }
54
55                 if ( state == 'end' ) {
56                         window.setTimeout(function(){
57                                 change_page('start');
58                         },end_timeout);
59                 }
60
61                 if ( state == 'error' ) {
62                         window.setTimeout(function(){
63                                 change_page('start');
64                         },error_timeout);
65                 }
66         }
67 }
68
69 function got_visible_tags(data,textStatus) {
70         var html = 'No tags in range';
71         if ( data.tags ) {
72                 html = '<ul class="tags">';
73                 $.each(data.tags, function(i,tag) {
74                         console.debug( i, tag );
75                         html += '<li><tt class="' + tag.security + '">' + tag.sid;
76                         var content = tag.content || tag.borrower.cardnumber;
77
78                         if ( content ) {
79                                 var link;
80                                 if ( content.length = 10 && content.substr(0,3) == 130 ) { // book
81                                         link = 'catalogue/search.pl?q=';
82                                 } else if ( content.length == 12 && content.substr(0,2) == 20 ) {
83                                         link = 'members/member.pl?member=';
84                                 } else {
85                                         html += '<b>UNKNOWN TAG</b> '+content;
86                                 }
87
88                                 if ( link ) {
89                                         html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/'
90                                                 + link + content
91                                                 + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
92                                                 + '</tt>';
93                                 }
94
95                                 console.debug( 'calling', state, content );
96                                 window[state]( content, tag.sid ); // call function with barcode
97
98                         }
99                 });
100                 html += '</ul>';
101
102         }
103
104         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
105
106         html = '<div class=status>'
107                 + textStatus
108                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
109                 + '</div>'
110                 + html
111                 ;
112         $('#tags').html( html ); // FIXME leaks memory?
113
114         pending_jsonp--;
115 };
116
117 function scan_tags() {
118         if ( pending_jsonp ) {
119                 console.debug('scan_tags disabled ', pending_jsonp, ' requests waiting');
120         } else {
121                 console.info('scan_tags');
122                 pending_jsonp++;
123                 $.getJSON("/scan?callback=?", got_visible_tags);
124         }
125
126         scan_timeout = window.setTimeout(function(){
127                 scan_tags();
128         },tag_rescan);  // re-scan every 200ms
129 }
130
131 $(document).ready(function() {
132                 $('div#tags').click( function() {
133                         scan_tags();
134                 });
135
136                 change_page('start');
137 });
138
139 function fill_in( where, value ) {
140         $('.'+where).each(function(i, el) {
141                 $(el).html(value);
142         });
143
144 }
145
146 /* Selfcheck state actions */
147
148 var borrower_cardnumber;
149 var circulation_type;
150 var book_barcodes = {};
151
152 function start( cardnumber ) {
153
154         if ( cardnumber.length != 12 || cardnumber.substr(0,2) != "20" ) {
155                 console.error(cardnumber, ' is not borrower card');
156                 return;
157         }
158
159         borrower_cardnumber = cardnumber; // for circulation
160
161         fill_in( 'borrower_number', cardnumber );
162
163         pending_jsonp++;
164         $.getJSON('/sip2/patron_info/'+cardnumber)
165         .done( function( data ) {
166                 console.info('patron', data);
167                 fill_in( 'borrower_name', data['AE'] );
168                 fill_in( 'borrower_email', data['BE'] );
169                 fill_in( 'hold_items',    data['fixed'].substr( 2 + 14 + 3 + 18 + ( 0 * 4 ), 4 ) ) * 1;
170                 fill_in( 'overdue_items', data['fixed'].substr( 2 + 14 + 3 + 18 + ( 1 * 4 ), 4 ) ) * 1;
171                 pending_jsonp--;
172                 change_page('borrower_info');
173         }).fail( function(data) {
174                 pending_jsonp--;
175                 change_page('error');
176         });
177 }
178
179 function borrower_info() {
180         // nop
181 }
182
183 function circulation( barcode, sid ) {
184         if ( barcode
185                         && barcode.length == 10
186                         && barcode.substr(0,3) == 130
187                         && book_barcodes[barcode] != 1
188         ) { // book, not seen yet
189                 book_barcodes[ barcode ] = 1;
190                 pending_jsonp++;
191                 $.getJSON('/sip2/'+circulation_type+'/'+borrower_cardnumber+'/'+barcode+'/'+sid , function( data ) {
192                         console.info( circulation_type, data );
193                         $('ul#books').append('<li>' + ( data['AJ'] || barcode ) + ( data['AF'] ? ' <b>' + data['AF'] + '</b>' : '' ) + '</li>');
194                         $('#books_count').html( $('ul#books > li').length );
195                         console.debug( book_barcodes );
196                         pending_jsonp--;
197                 }).fail( function() {
198                         change_page('error');
199                         pending_jsonp--;
200                 });
201         }
202 }
203
204 function end() {
205         // nop
206 }