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