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