1 dump('entering util/print.js\n');
3 if (typeof util == 'undefined') util = {};
4 util.print = function () {
6 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
8 JSAN.use('util.error'); this.error = new util.error();
9 JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init( { 'via':'stash' } );
10 JSAN.use('util.window'); this.win = new util.window();
11 JSAN.use('util.functional');
12 JSAN.use('util.file');
14 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
15 var key = 'oils.printer.external.cmd';
16 var has_key = prefs.prefHasUserValue(key);
17 this.oils_printer_external_cmd = has_key ? prefs.getCharPref(key) : '';
22 util.print.prototype = {
24 'reprint_last' : function() {
26 var obj = this; obj.data.init({'via':'stash'});
27 if (!obj.data.last_print) {
29 document.getElementById('offlineStrings').getString('printing.nothing_to_reprint')
33 var msg = obj.data.last_print.msg;
34 var params = obj.data.last_print.params; params.no_prompt = false;
35 obj.simple( msg, params );
37 this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
41 'html2txt' : function(html) {
42 JSAN.use('util.text');
43 //dump('html2txt, before:\n' + html + '\n');
44 var lines = html.split(/\n/);
46 for (var i = 0; i < lines.length; i++) {
49 // This undoes the util.text.preserve_string_in_html call that spine_label.js does
50 line = util.text.reverse_preserve_string_in_html(line);
51 // This looks for @hex attributes containing 2-digit hex codes, and converts them into real characters
52 line = line.replace(/(<.+?)hex=['"](.+?)['"](.*?>)/gi, function(str,p1,p2,p3,offset,s) {
54 var hex_chars = p2.match(/[0-9,a-f,A-F][0-9,a-f,A-F]/g);
55 for (var j = 0; j < hex_chars.length; j++) {
56 raw_chars += String.fromCharCode( parseInt(hex_chars[j],16) );
58 return p1 + p3 + raw_chars;
60 line = line.replace(/<head.*?>.*?<\/head>/gi, '');
61 line = line.replace(/<br.*?>/gi,'\r\n');
62 line = line.replace(/<table.*?>/gi,'');
63 line = line.replace(/<tr.*?>/gi,'');
64 line = line.replace(/<hr.*?>/gi,'\r\n');
65 line = line.replace(/<p.*?>/gi,'');
66 line = line.replace(/<block.*?>/gi,'');
67 line = line.replace(/<li.*?>/gi,' * ');
68 line = line.replace(/<.+?>/gi,'');
69 if (line) { new_lines.push(line); }
74 var new_html = new_lines.join('\n');
75 //dump('html2txt, after:\n' + new_html + '\nhtml2txt, done.\n');
79 'simple' : function(msg,params) {
81 if (!params) params = {};
86 obj.data.last_print = { 'msg' : msg, 'params' : params}; obj.data.stash('last_print');
89 if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
94 if (params && params.content_type) {
95 content_type = params.content_type;
97 content_type = 'text/html';
102 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
103 obj.data.init({'via':'stash'});
105 if (params.print_strategy || obj.data.print_strategy) {
107 switch(params.print_strategy || obj.data.print_strategy) {
109 params.dos_print = true;
111 /* FIXME - this it a kludge.. we're going to sidestep window-based html rendering for printing */
112 /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
113 /* templates aren't guaranteed to be valid xml. The unadulterated msg is still preserved in */
115 if (content_type=='text/html') {
116 w = obj.html2txt(msg);
120 if (! params.no_form_feed) { w = w + '\f'; }
121 obj.NSPrint(w, silent, params);
127 switch(content_type) {
129 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '}; function my_init() { if (typeof go_print == "function") { go_print(); } else { setTimeout( function() { if (typeof go_print == "function") { alert("Please tell the developers that the 2-second go_print workaround executed, and let them know whether this job printed successfully. Thanks!"); go_print(); } else { alert("Please tell the developers that the 2-second go_print workaround did not work. We will try to print one more time; there have been reports of wasted receipt paper at this point. Please check the settings in the print dialog and/or prepare to power off your printer. Thanks!"); window.print(); } }, 2000 ); } /* FIXME - mozilla bug#301560 - xpcom kills it too */ }');
130 var print_url = 'data:text/html,'
131 + '<html id="top"><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head>'
132 + '<body onload="try{my_init();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>';
133 w = obj.win.open(print_url,'receipt_temp','chrome,resizable');
135 w.go_print = function() {
137 obj.NSPrint(w, silent, params);
139 obj.error.standard_unexpected_error_alert("Print Error in util.print.simple. After this dialog we'll try a second print attempt. content_type = " + content_type,E);
142 w.minimize(); w.close();
146 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
151 obj.NSPrint(w, silent, params);
153 obj.error.standard_unexpected_error_alert("Print Error in util.print.simple. After this dialog we'll try a second print attempt. content_type = " + content_type,E);
156 w.minimize(); w.close();
163 this.error.standard_unexpected_error_alert('util.print.simple',E);
167 'tree_list' : function (params) {
169 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
170 dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
176 if (params.header) s += this.template_sub( params.header, cols, params );
178 for (var i = 0; i < params.list.length; i++) {
179 params.row = params.list[i];
181 s += this.template_sub( params.line_item, cols, params );
184 if (params.footer) s += this.template_sub( params.footer, cols, params );
186 if (params.sample_frame) {
187 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
188 params.sample_frame.setAttribute('src','data:text/html,<html id="top"><head><script src="' + window.escape(jsrc) + '"></script></head><body>' + window.escape(s) + '</body></html>');
190 this.simple(s,params);
194 'template_sub' : function( msg, cols, params ) {
197 if (!msg) { dump('template sub called with empty string\n'); return; }
198 JSAN.use('util.date');
201 try{b = s; s = s.replace(/%LINE_NO%/,Number(params.row_idx)+1);}
202 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
204 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
205 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
207 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
208 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
209 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
210 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
211 try{b = s; s = s.replace(/%SHORTNAME%/,params.lib.shortname());}
212 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
213 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
214 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
215 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
216 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
217 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
218 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
219 try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
220 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
221 try{b = s; s = s.replace(/%PATRON_ALIAS_OR_FIRSTNAME%/,(params.patron.alias() == '' || params.patron.alias() == null) ? params.patron.first_given_name() : params.patron.alias());}
222 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
223 try{b = s; s = s.replace(/%PATRON_ALIAS%/,(params.patron.alias() == '' || params.patron.alias() == null) ? '' : params.patron.alias());}
224 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
225 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
226 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
227 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
228 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
229 try{b = s; s = s.replace(/%PATRON_BARCODE%/,typeof params.patron.card() == 'object' ? params.patron.card().barcode() : util.functional.find_id_object_in_list( params.patron.cards(), params.patron.card() ).barcode() ) ;}
230 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
232 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
233 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
234 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
235 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
236 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
237 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
238 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
239 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
240 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
241 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
242 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
243 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
244 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
245 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
246 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
247 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
248 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
249 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
250 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
251 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
254 if (typeof params.row != 'undefined') {
255 if (params.row.length >= 0) {
256 alert('debug - please tell the developers that deprecated template code tried to execute');
257 for (var i = 0; i < cols.length; i++) {
258 var re = new RegExp(cols[i],"g");
259 try{b = s; s=s.replace(re, params.row[i]);}
260 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
263 /* for dump_with_keys */
264 for (var i in params.row) {
265 var re = new RegExp('%'+i+'%',"g");
266 try{b = s; s=s.replace(re, params.row[i]);}
267 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
272 if (typeof params.data != 'undefined') {
273 for (var i in params.data) {
274 var re = new RegExp('%'+i+'%',"g");
275 if (typeof params.data[i] == 'string') {
276 try{b = s; s=s.replace(re, params.data[i]);}
277 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
281 } catch(E) { dump(E+'\n'); }
285 alert('Error in print.js, template_sub(): ' + E);
290 'NSPrint' : function(w,silent,params) {
294 if (!params) params = {};
296 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
297 obj.data.init({'via':'stash'});
299 if (params.print_strategy || obj.data.print_strategy) {
301 dump('params.print_strategy = ' + params.print_strategy + ' || obj.data.print_strategy = ' + obj.data.print_strategy + ' => ' + ( params.print_strategy || obj.data.print_strategy ) + '\n');
302 switch(params.print_strategy || obj.data.print_strategy) {
305 if (typeof w != 'string') {
307 var temp_w = params.msg || w.document.firstChild.innerHTML;
308 if (!params.msg) { params.msg = temp_w; }
309 if (typeof temp_w != 'string') { throw(temp_w); }
310 w = obj.html2txt(temp_w);
312 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
313 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
314 w.getSelection().selectAllChildren(w.document.firstChild);
315 w = w.getSelection().toString();
318 obj._NSPrint_custom_print(w,silent,params);
321 if (typeof w == 'object') {
324 if (params.content_type == 'text/plain') {
325 w = window.open('data:text/plain,'+escape(params.msg));
327 w = window.open('data:text/html,'+escape(params.msg));
341 case 'webBrowserPrint':
343 if (typeof w == 'object') {
344 obj._NSPrint_webBrowserPrint(w,silent,params);
346 if (params.content_type == 'text/plain') {
347 w = window.open('data:text/plain,'+escape(params.msg));
349 w = window.open('data:text/html,'+escape(params.msg));
353 obj._NSPrint_webBrowserPrint(w,silent,params);
367 obj._NSPrint_webBrowserPrint(w,silent,params);
371 alert('Probably not printing: ' + e);
372 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
377 '_NSPrint_custom_print' : function(w,silent,params) {
382 var html = params.msg || w;
384 var txt_file = new util.file('receipt.txt');
385 txt_file.write_content('truncate',text);
386 var text_path = '"' + txt_file._file.path + '"';
389 var html_file = new util.file('receipt.html');
390 html_file.write_content('truncate',html);
391 var html_path = '"' + html_file._file.path + '"';
394 var cmd = params.dos_print ?
395 'copy ' + text_path + ' lpt1 /b\n'
396 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
399 file = new util.file('receipt.bat');
400 file.write_content('truncate+exec',cmd);
402 file = new util.file('receipt.bat');
404 dump('print exec: ' + cmd + '\n');
405 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
406 process.init(file._file);
410 dump('process.run = ' + process.run(true, args, args.length) + '\n');
415 //alert('Probably not printing: ' + e);
416 this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
420 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
423 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
424 var webBrowserPrint = w
425 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
426 .getInterface(Components.interfaces.nsIWebBrowserPrint);
427 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
428 if (webBrowserPrint) {
429 var gPrintSettings = obj.GetPrintSettings();
430 if (silent) gPrintSettings.printSilent = true;
431 else gPrintSettings.printSilent = false;
433 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
435 webBrowserPrint.print(gPrintSettings, null);
436 this.error.sdump('D_PRINT','Should be printing\n');
438 this.error.sdump('D_ERROR','Should not be printing\n');
441 //alert('Probably not printing: ' + e);
442 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
443 // causing an exception to be thrown which we catch here.
444 // Unfortunately this will also consume helpful failures
445 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
449 'GetPrintSettings' : function() {
451 //alert('entering GetPrintSettings');
452 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
453 var pref = Components.classes["@mozilla.org/preferences-service;1"]
454 .getService(Components.interfaces.nsIPrefBranch);
455 //alert('pref = ' + pref);
457 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
458 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
459 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
462 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
463 .getService(Components.interfaces.nsIPrintSettingsService);
464 if (this.gPrintSettingsAreGlobal) {
465 this.gPrintSettings = printService.globalPrintSettings;
466 //alert('called setPrinterDefaultsForSelectedPrinter');
467 this.setPrinterDefaultsForSelectedPrinter(printService);
469 this.gPrintSettings = printService.newPrintSettings;
470 //alert('used printService.newPrintSettings');
473 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
474 //alert("GetPrintSettings() "+e+"\n");
477 return this.gPrintSettings;
480 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
482 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
483 if (this.gPrintSettings.printerName == "") {
484 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
485 //alert('used .defaultPrinterName');
487 //alert('printerName = ' + this.gPrintSettings.printerName);
489 // First get any defaults from the printer
490 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
492 // now augment them with any values from last time
493 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
495 // now augment from our own saved settings if they exist
496 this.load_settings();
499 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
503 'page_settings' : function() {
505 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
506 this.GetPrintSettings();
507 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
508 PO.ShowPrintSetupDialog(this.gPrintSettings);
510 this.error.standard_unexpected_error_alert("page_settings()",E);
514 'load_settings' : function() {
517 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
518 var file = new util.file('gPrintSettings');
519 if (file._file.exists()) {
520 temp = file.get_object(); file.close();
521 for (var i in temp) {
522 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
525 this.gPrintSettings.marginTop = 0;
526 this.gPrintSettings.marginLeft = 0;
527 this.gPrintSettings.marginBottom = 0;
528 this.gPrintSettings.marginRight = 0;
529 this.gPrintSettings.headerStrLeft = '';
530 this.gPrintSettings.headerStrCenter = '';
531 this.gPrintSettings.headerStrRight = '';
532 this.gPrintSettings.footerStrLeft = '';
533 this.gPrintSettings.footerStrCenter = '';
534 this.gPrintSettings.footerStrRight = '';
537 this.error.sdump('D_PRINT',error_msg);
538 this.error.yns_alert(
539 document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
540 document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
541 document.getElementById('offlineStrings').getString('common.ok'),
548 this.error.standard_unexpected_error_alert("load_settings()",E);
552 'save_settings' : function() {
555 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
556 var file = new util.file('gPrintSettings');
557 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
558 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
561 this.error.standard_unexpected_error_alert("save_settings()",E);
566 dump('exiting util/print.js\n');