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