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');
17 util.print.prototype = {
19 'reprint_last' : function() {
21 var obj = this; obj.data.init({'via':'stash'});
22 if (!obj.data.last_print) {
23 alert('Nothing to re-print');
26 var msg = obj.data.last_print.msg;
27 var params = obj.data.last_print.params; params.no_prompt = false;
28 obj.simple( msg, params );
30 this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
34 'simple' : function(msg,params) {
36 if (!params) params = {};
40 obj.data.last_print = { 'msg' : msg, 'params' : params}; obj.data.stash('last_print');
43 if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
48 if (params && params.content_type) {
49 content_type = params.content_type;
51 content_type = 'text/html';
56 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
57 obj.data.init({'via':'stash'});
59 if (params.print_strategy || obj.data.print_strategy) {
61 switch(params.print_strategy || obj.data.print_strategy) {
63 /* FIXME - this it a kludge.. we're going to sidestep window-based html rendering for printing */
64 /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
65 /* templates aren't guaranteed to be valid xml */
66 if (content_type=='text/html') {
67 w = msg.replace(/<br.*?>/gi,'\r\n').replace(/<table.*?>/gi,'\r\n').replace(/<tr.*?>/gi,'\r\n').replace(/<hr.*?>/gi,'\r\n').replace(/<p.*?>/gi,'\r\n').replace(/<block.*?>/gi,'\r\n').replace(/<li.*?>/gi,'\r\n * ').replace(/<.+?>/gi,'');
71 if (! params.no_form_feed) { w = w + '\r\n\r\n\r\n\r\n\r\n\r\n\f'; }
72 obj.NSPrint(w, silent, params);
78 switch(content_type) {
80 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 */ }');
81 w = obj.win.open('data:text/html,<html id="top"><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head><body onload="try{my_init();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>','receipt_temp','chrome,resizable');
83 w.go_print = function() {
85 obj.NSPrint(w, silent, params);
87 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);
90 w.minimize(); w.close();
94 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
99 obj.NSPrint(w, silent, params);
101 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);
104 w.minimize(); w.close();
111 this.error.standard_unexpected_error_alert('util.print.simple',E);
115 'tree_list' : function (params) {
117 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
124 if (params.header) s += this.template_sub( params.header, cols, params );
126 for (var i = 0; i < params.list.length; i++) {
127 params.row = params.list[i];
129 s += this.template_sub( params.line_item, cols, params );
132 if (params.footer) s += this.template_sub( params.footer, cols, params );
134 if (params.sample_frame) {
135 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
136 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>');
138 this.simple(s,params);
142 'template_sub' : function( msg, cols, params ) {
143 if (!msg) { dump('template sub called with empty string\n'); return; }
144 JSAN.use('util.date');
147 try{b = s; s = s.replace(/%LINE_NO%/,Number(params.row_idx)+1);}
148 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
150 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
151 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
153 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
154 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
155 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
156 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
157 try{b = s; s = s.replace(/%SHORTNAME%/,params.lib.shortname());}
158 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
159 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
160 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
161 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
162 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
163 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
164 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
165 try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
166 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
167 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
168 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
169 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
170 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
171 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() ) ;}
172 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
174 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
175 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
176 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
177 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
178 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
179 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
180 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
181 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
182 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
183 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
184 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
185 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
186 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
187 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
188 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
189 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
190 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
191 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
192 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
193 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
196 if (typeof params.row != 'undefined') {
197 if (params.row.length >= 0) {
198 alert('debug - please tell the developers that deprecated template code tried to execute');
199 for (var i = 0; i < cols.length; i++) {
200 var re = new RegExp(cols[i],"g");
201 try{b = s; s=s.replace(re, params.row[i]);}
202 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
205 /* for dump_with_keys */
206 for (var i in params.row) {
207 var re = new RegExp('%'+i+'%',"g");
208 try{b = s; s=s.replace(re, params.row[i]);}
209 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
214 if (typeof params.data != 'undefined') {
215 for (var i in params.data) {
216 var re = new RegExp('%'+i+'%',"g");
217 try{b = s; s=s.replace(re, params.data[i]);}
218 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
221 } catch(E) { dump(E+'\n'); }
227 'NSPrint' : function(w,silent,params) {
231 if (!params) params = {};
233 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
234 obj.data.init({'via':'stash'});
236 if (params.print_strategy || obj.data.print_strategy) {
238 switch(params.print_strategy || obj.data.print_strategy) {
240 if (typeof w != 'string') {
241 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
242 w.getSelection().selectAllChildren(w.document.firstChild);
243 w = w.getSelection().toString();
245 obj._NSPrint_dos_print(w,silent,params);
250 case 'webBrowserPrint':
251 obj._NSPrint_webBrowserPrint(w,silent,params);
255 obj._NSPrint_webBrowserPrint(w,silent,params);
261 obj._NSPrint_webBrowserPrint(w,silent,params);
265 alert('Probably not printing: ' + e);
266 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
271 '_NSPrint_dos_print' : function(w,silent,params) {
275 /* OLD way: This is a kludge/workaround. webBrowserPrint doesn't always work. So we're going to let
276 the html window handle our receipt template rendering, and then force a selection of all
277 the text nodes and dump that to a file, for printing through a dos utility */
279 /* NEW way: we just pass in the text */
283 var file = new util.file('receipt.txt');
284 file.write_content('truncate',text);
285 var path = file._file.path;
288 file = new util.file('receipt.bat');
289 file.write_content('truncate+exec','#!/bin/sh\ncopy "' + path + '" lpt1 /b\nlpr ' + path + '\n');
291 file = new util.file('receipt.bat');
293 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
294 process.init(file._file);
298 dump('process.run = ' + process.run(true, args, args.length) + '\n');
303 //alert('Probably not printing: ' + e);
304 this.error.sdump('D_ERROR','_NSPrint_dos_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
308 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
311 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
312 var webBrowserPrint = w
313 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
314 .getInterface(Components.interfaces.nsIWebBrowserPrint);
315 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
316 if (webBrowserPrint) {
317 var gPrintSettings = obj.GetPrintSettings();
318 if (silent) gPrintSettings.printSilent = true;
319 else gPrintSettings.printSilent = false;
321 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
323 webBrowserPrint.print(gPrintSettings, null);
324 this.error.sdump('D_PRINT','Should be printing\n');
326 this.error.sdump('D_ERROR','Should not be printing\n');
329 //alert('Probably not printing: ' + e);
330 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
331 // causing an exception to be thrown which we catch here.
332 // Unfortunately this will also consume helpful failures
333 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
337 'GetPrintSettings' : function() {
339 //alert('entering GetPrintSettings');
340 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
341 var pref = Components.classes["@mozilla.org/preferences-service;1"]
342 .getService(Components.interfaces.nsIPrefBranch);
343 //alert('pref = ' + pref);
345 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
346 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
347 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
350 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
351 .getService(Components.interfaces.nsIPrintSettingsService);
352 if (this.gPrintSettingsAreGlobal) {
353 this.gPrintSettings = printService.globalPrintSettings;
354 //alert('called setPrinterDefaultsForSelectedPrinter');
355 this.setPrinterDefaultsForSelectedPrinter(printService);
357 this.gPrintSettings = printService.newPrintSettings;
358 //alert('used printService.newPrintSettings');
361 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
362 //alert("GetPrintSettings() "+e+"\n");
365 return this.gPrintSettings;
368 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
370 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
371 if (this.gPrintSettings.printerName == "") {
372 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
373 //alert('used .defaultPrinterName');
375 //alert('printerName = ' + this.gPrintSettings.printerName);
377 // First get any defaults from the printer
378 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
380 // now augment them with any values from last time
381 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
383 // now augment from our own saved settings if they exist
384 this.load_settings();
387 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
391 'page_settings' : function() {
393 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
394 this.GetPrintSettings();
395 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
396 PO.ShowPrintSetupDialog(this.gPrintSettings);
398 this.error.standard_unexpected_error_alert("page_settings()",E);
402 'load_settings' : function() {
404 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
405 var file = new util.file('gPrintSettings');
406 if (file._file.exists()) {
407 temp = file.get_object(); file.close();
408 for (var i in temp) {
409 this.gPrintSettings[i] = temp[i];
412 this.gPrintSettings.marginTop = 0;
413 this.gPrintSettings.marginLeft = 0;
414 this.gPrintSettings.marginBottom = 0;
415 this.gPrintSettings.marginRight = 0;
416 this.gPrintSettings.headerStrLeft = '';
417 this.gPrintSettings.headerStrCenter = '';
418 this.gPrintSettings.headerStrRight = '';
419 this.gPrintSettings.footerStrLeft = '';
420 this.gPrintSettings.footerStrCenter = '';
421 this.gPrintSettings.footerStrRight = '';
424 this.error.standard_unexpected_error_alert("load_settings()",E);
428 'save_settings' : function() {
431 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
432 var file = new util.file('gPrintSettings');
433 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
434 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
437 this.error.standard_unexpected_error_alert("save_settings()",E);
442 dump('exiting util/print.js\n');