P.V. SUPA GoodStuff integration
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / addon / pv_supa_goodstuff.js
1 dump('entering addon/pv_supa.js\n');
2 // vim:noet:sw=4:ts=4:
3
4 /*
5     Usage example:
6
7     JSAN.use('addon.pv_supa_goodstuff');
8     var goodstuff = new addon.pv_supa_goodstuff();
9     goodstuff.onData(
10         function(data) {
11             alert('Received: ' + data);
12         },
13         'ACTIVATE'
14     );
15     goodstuff.request_items();
16 */
17
18 if (typeof addon == 'undefined') addon = {};
19 addon.pv_supa_goodstuff = function (params) {
20     var obj = this;
21     try {
22         dump('addon: pv_supa_goodstuff() constructor\n');
23
24         const Cc = Components.classes;
25         const Ci = Components.interfaces;
26         const prefs_Cc = '@mozilla.org/preferences-service;1';
27         this.prefs = Cc[prefs_Cc].getService(Ci['nsIPrefBranch']);
28
29         JSAN.use('OpenILS.data');
30         this.data = new OpenILS.data();
31         this.data.stash_retrieve();
32
33         JSAN.use('util.error');
34         this.error = new util.error();
35
36         this.active = false;
37         if (this.prefs.prefHasUserValue('oils.addon.pvsupa.goodstuff.enabled')){
38             this.active = this.prefs.getBoolPref(
39                 'oils.addon.pvsupa.goodstuff.enabled'
40             );
41         }
42         if (this.active) {
43             dump('addon: pv_supa goodstuff enabled by preference\n');
44         } else {
45             dump('addon: pv_supa goodstuff not enabled by preference\n');
46         }
47
48         if (g) {
49             if (g.checkin && this.active) {
50                 this.common_ui_init();
51                 this.socket_init();
52                 setTimeout(
53                     function() {
54                         obj.checkin_init();
55                     }, 1000
56                 );
57             }
58             if (g.checkout && this.active) {
59                 this.common_ui_init();
60                 this.socket_init();
61                 setTimeout(
62                     function() {
63                         obj.checkout_init();
64                     }, 1000
65                 );
66             }
67             if (String(location.href).match('/patron/barcode_entry.xul') && this.active) {
68                 this.common_ui_init();
69                 this.socket_init();
70                 setTimeout(
71                     function() {
72                         obj.patron_for_checkout_init();
73                     }, 1000
74                 );
75             }
76             if (g.addons_ui) {
77                 this.common_ui_init();
78                 this.prefs_init();
79             }
80         }
81         return this;
82
83     } catch(E) {
84         dump('addon: Error in pv_supa_goodstuff(): ' + E + '\n');
85     }
86 }
87
88 addon.pv_supa_goodstuff.prototype = {
89     'common_ui_init' : function() {
90         dump('addon: pv_supa common_ui_init\n');
91         var obj = this;
92         try {
93             var mc = document.createElement('messagecatalog');
94             mc.setAttribute('id','addon_pvsupa_goodstuff_strings');
95             mc.setAttribute(
96                 'src',
97                 '/xul/server/locale/'+LOCALE+'/addon/pv_supa_goodstuff.properties'
98             );
99             var mc_parent = $('offlineStrings')
100                 ? $('offlineStrings').parentNode
101                 : document.getElementsByTagName('window').item(0)
102                 || document.getElementsByTagName('html').item(0);
103             mc_parent.appendChild(mc);
104
105             // We don't really need CSS here, but as an example:
106             var sss = Cc['@mozilla.org/content/style-sheet-service;1']
107                 .getService(Ci.nsIStyleSheetService);
108             var ios = Cc['@mozilla.org/network/io-service;1']
109                 .getService(Ci.nsIIOService);
110             var uri = ios.newURI(
111                 'oils://remote/xul/server/skin/addon/pv_supa_goodstuff.css',
112                 null,
113                 null
114             );
115             if(!sss.sheetRegistered(uri, sss.USER_SHEET)) {
116                 sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
117             }
118         } catch(E) {
119             dump('addon: pv_supa Error in common_ui_init(): ' + E + '\n');
120         }
121     },
122
123     'socket_init' : function() {
124         dump('addon: pv_supa socket_init on page ' + location.href + '\n');
125         var obj = this;
126         try {
127             if (! this.data.addon) {
128                 this.data.addon = {};
129             }
130             if (! this.data.addon.pv_supa) {
131                 this.data.addon.pv_supa = {};
132             }
133             if (! this.data.addon.pv_supa.goodstuff) {
134                 this.data.addon.pv_supa.goodstuff = {};
135             }
136             if (! this.data.addon.pv_supa.goodstuff.message_log) {
137                 this.data.addon.pv_supa.goodstuff.message_log = [];
138             }
139             if (this.data.addon.pv_supa.goodstuff.socket) {
140                     // don't know if we want to keep and re-use sockets
141                     // Previously I was test .socket.isAlive() and only
142                     // recreating if false
143                     dump('addon: pv_supa goodstuff destroying old socket\n');
144                     this.data.addon.pv_supa.goodstuff.socket.close();
145                     this.data.addon.pv_supa.goodstuff.socket = null;
146             }
147             if (! this.data.addon.pv_supa.goodstuff.socket) {
148                 JSAN.use('util.socket');
149                 this.data.addon.pv_supa.goodstuff.socket = new util.socket(
150
151                     this.prefs.prefHasUserValue('oils.addon.pvsupa.goodstuff.host')
152                     ? this.prefs.getCharPref('oils.addon.pvsupa.goodstuff.host')
153                     : '127.0.0.1',
154
155                     this.prefs.prefHasUserValue('oils.addon.pvsupa.goodstuff.port')
156                     ? this.prefs.getIntPref('oils.addon.pvsupa.goodstuff.port')
157                     : 5000 /* FIXME find out actual default port */
158                 );
159                 this.data.addon.pv_supa.goodstuff.socket.onStopRequest(
160                     function(request,context,result) {
161                         dump('addon: pv_supa goodstuff lost connection on page ' + location.href + '\n');
162                         obj.updateStatusBar('!lost connection\n');
163                         obj.data.addon.pv_supa.goodstuff.last_start_end_msg = null;
164                     }
165                 );
166                 dump('addon: pv_supa goodstuff socket opened\n');
167                 this.updateStatusBar('!new connection\n');
168                 obj.data.addon.pv_supa.goodstuff.last_start_end_msg = null;
169             } else {
170                 dump('addon: pv_supa goodstuff socket already opened\n');
171             }
172             this.socket = this.data.addon.pv_supa.goodstuff.socket;
173             this.token = location.href + ' : ' + new Date();
174             this.data.addon.pv_supa.goodstuff.token = this.token;
175             var obj = this;
176             setTimeout(
177                 function() {
178                     dump(
179                         'addon: pv_supa goodstuff socket\n\t' +
180                         'host: ' + obj.socket.host + '\n\t' +
181                         'port: ' + obj.socket.port + '\n\t' +
182                         'token: '+ obj.token
183                          + '\n'
184                     );
185                 },
186                 0
187             );
188         } catch(E) {
189             dump('addon: pv_supa Error in socket_init(): ' + E + '\n');
190         }
191     },
192     'updateStatusBar' : function(d) {
193         try {
194             var obj = this;
195             if (xulG && xulG.set_statusbar) {
196                 if (obj.data.addon.pv_supa.goodstuff.message_log.length > 16) {
197                     obj.data.addon.pv_supa.goodstuff.message_log.shift();
198                 }
199                 obj.data.addon.pv_supa.goodstuff.message_log.push(d);
200                 xulG.set_statusbar(
201                     5,
202                     'GoodStuff: ' + d,
203                     obj.data.addon.pv_supa.goodstuff.message_log.join(""),
204                     function(ev) {
205                         alert(
206                             obj.data.addon.pv_supa.goodstuff.message_log.join("")
207                         );
208                     }
209                 );
210             }
211         } catch(E) {
212             dump('addon: pv_supa Error in updateStatusBar('+d+'): ' + E + '\n');
213         }
214     },
215     'onData' : function(f,security) {
216         dump('addon: setting pv_supa goodstuff onData callback, on page '
217             + location.href + ' with token ' + this.token + '\n');
218         var obj = this;
219         this.socket.dataCallback(
220             function(d) {
221                 try {
222                     dump('addon: dataCallback func at page '
223                         + location.href + ' with token ' + obj.token + '\n');
224                     obj.updateStatusBar('>' + d);
225                     if (obj.token != obj.data.addon.pv_supa.goodstuff.token) {
226                         var e = 'addon error: pv supa: reading data out of turn\n';
227                         dump(e);
228                         obj.updateStatusBar('!' + e);
229                     }
230                     d= d.replace("\n","","g").replace("\r","","g").replace(" ","","g");
231                     var p = d.split("|");
232                     if (p.length == 1) {
233                         if (f) {
234                             f(p[0]); // hopefully a patron barcode
235                         }
236                     } else if (p[0] == 'START') {
237                         obj.data.addon.pv_supa.goodstuff.last_start_end_msg = p[0];
238                     } else if (p[0] == 'END') {
239                         obj.data.addon.pv_supa.goodstuff.last_start_end_msg = p[0];
240                     } else if (p[1] == 'NOK') {
241                         if (security) {
242                             var msg = $('addon_pvsupa_goodstuff_strings').getFormattedString(
243                                 security == 'ACTIVATE'
244                                 ? 'rfid.set_security_failure.prompt.message.activate_failure'
245                                 : 'rfid.set_security_failure.prompt.message.deactivate_failure',
246                                 [ p[0] ]
247                             );
248                             var choice = obj.error.yns_alert(
249                                 msg,
250                                 $('addon_pvsupa_goodstuff_strings').getString(
251                                     'rfid.set_security_failure.prompt.title'
252                                 ),
253                                 $('addon_pvsupa_goodstuff_strings').getString(
254                                     'rfid.set_security_failure.prompt.button.activate_security'
255                                 ),
256                                 $('addon_pvsupa_goodstuff_strings').getString(
257                                     'rfid.set_security_failure.prompt.button.deactivate_security'
258                                 ),
259                                 $('addon_pvsupa_goodstuff_strings').getString(
260                                     'rfid.set_security_failure.prompt.button.do_nothing_with_security'
261                                 ),
262                                 ''
263                             );
264                             if (choice == 0) {
265                                 obj.write(p[0]+'|ACTIVATE\n');
266                             } else if (choice == 1) {
267                                 obj.write(p[0]+'|DEACTIVATE\n');
268                             } else {
269                                 obj.write(p[0]+'\n');
270                             }
271                         } else {
272                             dump('addon: unknown error\n');
273                         }
274                     } else if (p[1] == 'OK') {
275                         // ignore
276                     } else if (p[1].match('/')) {
277                         var counts = p[1].split('/');
278                         var read = counts[0];
279                         var set = counts[1];
280                         if (read != set) {
281                             var msg = $('addon_pvsupa_goodstuff_strings').getFormattedString(
282                                 'rfid.partial_scan.prompt.message',
283                                 [ read, set, p[0] ]
284                             );
285                             var choice = obj.error.yns_alert(
286                                 msg,
287                                 $('addon_pvsupa_goodstuff_strings').getString(
288                                     'rfid.partial_scan.prompt.title'
289                                 ),
290                                 $('addon_pvsupa_goodstuff_strings').getString(
291                                     'rfid.partial_scan.prompt.button.rescan'
292                                 ),
293                                 $('addon_pvsupa_goodstuff_strings').getString(
294                                     'rfid.partial_scan.prompt.button.skip'
295                                 ),
296                                 $('addon_pvsupa_goodstuff_strings').getString(
297                                     'rfid.partial_scan.prompt.button.proceed'
298                                 ),
299                                 ''
300                             );
301                             if (!choice || choice == 0) {
302                                 obj.write(p[0]+'|REREAD\n');
303                             } else if (choice == 1) {
304                                 obj.write(p[0]+'\n'); // do nothing, skip
305                             } else if (choice == 2) {
306                                 if (f) {
307                                     f(p[0]); // hopefully an item barcode
308                                 }
309                             }
310                         } else {
311                             if (f) {
312                                 f(p[0]); // hopefully an item barcode
313                             }
314                         }
315                     } else {
316                         if (f) {
317                             f(p[0]); // no idea; shouldn't get here
318                         }
319                     }
320                 } catch(E) {
321                     dump('addon: error in onData callback: ' + E + '\n');
322                 }
323             }
324         );
325     },
326     'write' : function(s,ignore_token) {
327         dump('addon: write "' + s + '", on page ' + location.href + ' with token ' + this.token + '\n');
328         if ((this.token != this.data.addon.pv_supa.goodstuff.token) && !ignore_token) {
329             var e = 'addon error: pv supa: sending data out of turn\n';
330             dump(e);
331             this.updateStatusBar('!' + e);
332         }
333         if (!this.socket.socket.isAlive()) {
334             dump('addon error: pv supa: writing to not alive socket\n');
335         }
336         this.updateStatusBar('<' + s);
337         this.socket.write(s);
338     },
339     'request_items' : function() {
340         dump('addon: request_items on page ' + location.href + '\n');
341         if (this.data.addon.pv_supa.goodstuff.last_start_end_msg == 'START') {
342             this.write('END\n');
343         }
344         this.write('START|ITEM\n'); // we expect START|OK
345     },
346     'request_patrons' : function() {
347         dump('addon: request_patrons on page ' + location.href + '\n');
348         if (this.data.addon.pv_supa.goodstuff.last_start_end_msg == 'START') {
349             this.write('END\n');
350         }
351         this.write('START|PATRON\n'); // we expect START|OK
352     },
353     'end_session' : function() {
354         dump('addon: end_session on page ' + location.href + '\n');
355         if (this.token == this.data.addon.pv_supa.goodstuff.token) {
356             if (this.data.addon.pv_supa.goodstuff.last_start_end_msg == 'START') {
357                 this.write('END\n');
358             }
359             this.data.addon.pv_supa.goodstuff.socket.close();
360             this.data.addon.pv_supa.goodstuff.socket = null;
361         }
362     },
363
364     'prefs_init' : function() {
365         dump('addon: pv_supa prefs_init\n');
366         var obj = this;
367
368         try {
369             if (! (g && g.addons_ui)) { return; }
370
371             const Cc = Components.classes;
372             const Ci = Components.interfaces;
373
374             function post_overlay() {
375                 var tab = $('pv_supa_goodstuff_tab');
376                 tab.setAttribute(
377                     'label',
378                     $('addon_pvsupa_goodstuff_strings').getString('prefs.tab.label')
379                 );
380                 tab.setAttribute(
381                     'accesskey',
382                     $('addon_pvsupa_goodstuff_strings').getString('prefs.tab.accesskey')
383                 );
384
385                 var enabled_label = $('pv_supa_goodstuff_enabled_label');
386                 enabled_label.setAttribute(
387                     'value',
388                     $('addon_pvsupa_goodstuff_strings').getString('prefs.checkbox.label')
389                 );
390                 enabled_label.setAttribute(
391                     'accesskey',
392                     $('addon_pvsupa_goodstuff_strings').getString('prefs.checkbox.accesskey')
393                 );
394
395                 var host_label = $('pv_supa_goodstuff_hostname_label');
396                 host_label.setAttribute(
397                     'value',
398                     $('addon_pvsupa_goodstuff_strings').getString('prefs.host.label')
399                 );
400                 host_label.setAttribute(
401                     'accesskey',
402                     $('addon_pvsupa_goodstuff_strings').getString('prefs.host.accesskey')
403                 );
404
405                 var port_label = $('pv_supa_goodstuff_port_label');
406                 port_label.setAttribute(
407                     'value',
408                     $('addon_pvsupa_goodstuff_strings').getString('prefs.port.label')
409                 );
410                 port_label.setAttribute(
411                     'accesskey',
412                     $('addon_pvsupa_goodstuff_strings').getString('prefs.port.accesskey')
413                 );
414
415                 var save_btn = $('pv_supa_goodstuff_save_btn');
416                 save_btn.setAttribute(
417                     'label',
418                     $('addon_pvsupa_goodstuff_strings').getString('prefs.update.label')
419                 );
420                 save_btn.setAttribute(
421                     'accesskey',
422                     $('addon_pvsupa_goodstuff_strings').getString('prefs.update.accesskey')
423                 );
424                 save_btn.addEventListener(
425                     'command',
426                     function() {
427                         obj.save_prefs();
428                     },
429                     false
430                 );
431                 obj.display_prefs();
432             }
433
434             function myObserver() { this.register(); }
435             myObserver.prototype = {
436                 register: function() {
437                     var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
438                     observerService.addObserver(this, "xul-overlay-merged", false);
439                 },
440                 unregister: function() {
441                     var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
442                     observerService.removeObserver(this, "xul-overlay-merged");
443                 },
444                 observe: function(subject,topic,data) {
445                     dump('observe: <'+subject+','+topic+','+data+'>\n');
446                     // setTimeout is needed here for xulrunner 1.8
447                     setTimeout( function() { try { post_overlay(); } catch(E) { alert(E); } }, 0 );
448                 }
449             }
450
451             var observer = new myObserver();
452             var url = '/xul/server/addon/pv_supa_goodstuff_config_overlay.xul';
453             document.loadOverlay(location.protocol + '//' + location.hostname + url,observer)
454
455         } catch(E) {
456             dump('addon: pv_supa Error in prefs_init(): ' + E + '\n');
457         }
458     },
459
460     'display_prefs' : function() {
461         var obj = this;
462         try {
463             $('pv_supa_goodstuff_enabled_cb').checked = obj.active;
464             $('pv_supa_goodstuff_hostname_tb').value =
465                 obj.prefs.prefHasUserValue('oils.addon.pvsupa.goodstuff.host')
466                 ? obj.prefs.getCharPref('oils.addon.pvsupa.goodstuff.host')
467                 : '127.0.0.1';
468
469             $('pv_supa_goodstuff_port_tb').value =
470                 obj.prefs.prefHasUserValue('oils.addon.pvsupa.goodstuff.port')
471                 ? obj.prefs.getIntPref('oils.addon.pvsupa.goodstuff.port')
472                 : 5000; /* FIXME find out actual default port */
473
474         } catch(E) {
475             dump('addon: pv_supa Error in display_prefs(): ' + E + '\n');
476         }
477     },
478
479     'save_prefs' : function() {
480         var obj = this;
481         try {
482             obj.prefs.setBoolPref(
483                 'oils.addon.pvsupa.goodstuff.enabled',
484                 $('pv_supa_goodstuff_enabled_cb').checked
485             );
486             obj.prefs.setCharPref(
487                 'oils.addon.pvsupa.goodstuff.host',
488                 $('pv_supa_goodstuff_hostname_tb').value
489             );
490             obj.prefs.setIntPref(
491                 'oils.addon.pvsupa.goodstuff.port',
492                 $('pv_supa_goodstuff_port_tb').value
493             );
494             location.href = location.href;
495         } catch(E) {
496             dump('addon: pv_supa Error in save_prefs(): ' + E + '\n');
497         }
498     },
499
500     'checkin_init' : function() {
501         dump('addon: pv_supa checkin_init\n');
502         var obj = this;
503
504         try {
505
506             if (! (g && g.checkin)) { return; }
507
508             function setOnData() {
509                 obj.onData(
510                     function(barcode) {
511                         g.checkin.controller.view.checkin_barcode_entry_textbox.value = barcode;
512                         g.checkin.checkin();
513                         // unlike checkout, I don't really care whether the checkin
514                         // succeeds or not; I think we should activate security on
515                         // the item and move on.  Errored items are still listed in
516                         // the interface and can be handled separately.
517                         obj.write(barcode+'|ACTIVATE\n');
518                     },
519                     'ACTIVATE'
520                 );
521             }
522             setOnData();
523
524             var spacer = $('pcii3s');
525             var rfid_cb = document.createElement('checkbox');
526             rfid_cb.setAttribute('id','addon_rfid_cb');
527             rfid_cb.setAttribute(
528                 'label',
529                 $('addon_pvsupa_goodstuff_strings').getString(
530                     'rfid.checkbox.label'
531                 )
532             );
533             rfid_cb.setAttribute(
534                 'accesskey',
535                 $('addon_pvsupa_goodstuff_strings').getString(
536                     'rfid.checkbox.accesskey'
537                 )
538             );
539             spacer.parentNode.insertBefore(rfid_cb,spacer);
540             if (obj.prefs.prefHasUserValue(
541                     'oils.addon.pvsupa.goodstuff.checkin.rfid_checkbox.checked'
542             )){
543                 rfid_cb.checked = obj.prefs.getBoolPref(
544                     'oils.addon.pvsupa.goodstuff.checkin.rfid_checkbox.checked'
545                 );
546             }
547
548             if (rfid_cb.checked) {
549                 obj.request_items();
550             }
551
552             rfid_cb.addEventListener(
553                 'command',
554                 function(ev) {
555                     if (ev.target.checked) {
556                         obj.socket_init();
557                         setTimeout(
558                             function() {
559                                 setOnData();
560                                 obj.request_items();
561                             }, 1000
562                         );
563                     } else {
564                         obj.end_session();
565                     }
566                     obj.prefs.setBoolPref(
567                         'oils.addon.pvsupa.goodstuff.checkin.rfid_checkbox.checked',
568                         ev.target.checked
569                     );
570                 }
571             );
572
573             window.addEventListener(
574                 'unload',
575                 function(ev) {
576                     obj.end_session();
577                 },
578                 false
579             );
580             window.addEventListener(
581                 'tab_focus',
582                 function(ev) {
583                     obj.socket_init();
584                     if (rfid_cb.checked) {
585                         setTimeout(
586                             function() {
587                                 setOnData();
588                                 obj.request_items();
589                             }, 1000
590                         );
591                     }
592                 },
593                 false
594             );
595
596         } catch(E) {
597             dump('addon: pv_supa Error in checkin_init(): ' + E + '\n');
598         }
599     },
600     'checkout_init' : function() {
601         dump('addon: pv_supa checkout_init\n');
602         var obj = this;
603
604         try {
605
606             if (! (g && g.checkout)) { return; }
607
608             function setOnData() {
609                 obj.onData(
610                     function(barcode) {
611                         g.checkout.controller.view.checkout_barcode_entry_textbox.value = barcode;
612                         var pre_list_count = g.checkout.list.row_count.total;
613                         g.checkout.checkout({'barcode':barcode});
614                         var post_list_count = g.checkout.list.row_count.total;
615                         if (pre_list_count != post_list_count) {
616                             obj.write(barcode+'|DEACTIVATE\n'); // checkout success
617                         } else {
618                             obj.write(barcode+'|ACTIVATE\n'); // checkout failure
619                         }
620                     },
621                     'DEACTIVATE'
622                 );
623             }
624             setOnData();
625
626             var spacer = $('pcii3s');
627             var rfid_cb = document.createElement('checkbox');
628             rfid_cb.setAttribute('id','addon_rfid_cb');
629             rfid_cb.setAttribute(
630                 'label',
631                 $('addon_pvsupa_goodstuff_strings').getString(
632                     'rfid.checkbox.label'
633                 )
634             );
635             rfid_cb.setAttribute(
636                 'accesskey',
637                 $('addon_pvsupa_goodstuff_strings').getString(
638                     'rfid.checkbox.accesskey'
639                 )
640             );
641             spacer.parentNode.insertBefore(rfid_cb,spacer);
642             if (obj.prefs.prefHasUserValue(
643                     'oils.addon.pvsupa.goodstuff.checkout.rfid_checkbox.checked'
644             )){
645                 rfid_cb.checked = obj.prefs.getBoolPref(
646                     'oils.addon.pvsupa.goodstuff.checkout.rfid_checkbox.checked'
647                 );
648             }
649
650             if (rfid_cb.checked) {
651                 obj.request_items();
652             }
653
654             rfid_cb.addEventListener(
655                 'command',
656                 function(ev) {
657                     if (ev.target.checked) {
658                         obj.socket_init();
659                         setTimeout(
660                             function() {
661                                 setOnData();
662                                 obj.request_items();
663                             }, 1000
664                         );
665                     } else {
666                         obj.end_session();
667                     }
668                     obj.prefs.setBoolPref(
669                         'oils.addon.pvsupa.goodstuff.checkout.rfid_checkbox.checked',
670                         ev.target.checked
671                     );
672                 }
673             );
674
675             window.addEventListener(
676                 'unload',
677                 function(ev) {
678                     obj.end_session();
679                 },
680                 false
681             );
682             window.addEventListener(
683                 'tab_focus',
684                 function(ev) {
685                     obj.socket_init();
686                     if (rfid_cb.checked) {
687                         setTimeout(
688                             function() {
689                                 setOnData();
690                                 obj.request_items();
691                             }, 1000
692                         );
693                     }
694                 },
695                 false
696             );
697
698         } catch(E) {
699             dump('addon: pv_supa Error in checkout_init(): ' + E + '\n');
700         }
701     },
702     'patron_for_checkout_init' : function() {
703         dump('addon: pv_supa patron_for_checkout_init\n');
704         var obj = this;
705
706         try {
707
708             if (! String(location.href).match('/patron/barcode_entry.xul')) { return; }
709
710             function setOnData() {
711                 obj.onData(
712                     function(barcode) {
713                         obj.write(barcode+'\n');
714                         $('barcode_tb').value = barcode;
715                         window.submit();
716                     },
717                     'DEACTIVATE'
718                 );
719             }
720             setOnData();
721
722             var hbox = $('barcode_tb').parentNode;
723             var rfid_cb = document.createElement('checkbox');
724             rfid_cb.setAttribute('id','addon_rfid_cb');
725             rfid_cb.setAttribute(
726                 'label',
727                 $('addon_pvsupa_goodstuff_strings').getString(
728                     'rfid.checkbox.label'
729                 )
730             );
731             rfid_cb.setAttribute(
732                 'accesskey',
733                 $('addon_pvsupa_goodstuff_strings').getString(
734                     'rfid.checkbox.accesskey'
735                 )
736             );
737             hbox.appendChild(rfid_cb);
738             if (obj.prefs.prefHasUserValue(
739                     'oils.addon.pvsupa.goodstuff.patron_for_checkout.rfid_checkbox.checked'
740             )){
741                 rfid_cb.checked = obj.prefs.getBoolPref(
742                     'oils.addon.pvsupa.goodstuff.patron_for_checkout.rfid_checkbox.checked'
743                 );
744             }
745
746             if (rfid_cb.checked) {
747                 obj.request_patrons();
748             }
749
750             rfid_cb.addEventListener(
751                 'command',
752                 function(ev) {
753                     if (ev.target.checked) {
754                         obj.socket_init();
755                         setTimeout(
756                             function() {
757                                 setOnData();
758                                 obj.request_patrons();
759                             }, 1000
760                         );
761                     } else {
762                         obj.end_session();
763                     }
764                     obj.prefs.setBoolPref(
765                         'oils.addon.pvsupa.goodstuff.patron_for_checkout.rfid_checkbox.checked',
766                         ev.target.checked
767                     );
768                 }
769             );
770
771             window.addEventListener(
772                 'unload',
773                 function(ev) {
774                     obj.end_session();
775                 },
776                 false
777             );
778             window.addEventListener(
779                 'tab_focus',
780                 function(ev) {
781                     obj.socket_init();
782                     if (rfid_cb.checked) {
783                         setTimeout(
784                             function() {
785                                 setOnData();
786                                 obj.request_patrons();
787                             }, 1000
788                         );
789                     }
790                 },
791                 false
792             );
793
794         } catch(E) {
795             dump('addon: pv_supa Error in patron_for_checkout_init(): ' + E + '\n');
796         }
797     }
798
799
800
801 }
802