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