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