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