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';
55 switch(content_type) {
57 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 */ }');
58 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');
60 w.go_print = function() {
62 obj.NSPrint(w, silent, params);
64 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);
67 w.minimize(); w.close();
71 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
76 obj.NSPrint(w, silent, params);
78 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);
81 w.minimize(); w.close();
88 this.error.standard_unexpected_error_alert('util.print.simple',E);
92 'tree_list' : function (params) {
94 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
101 if (params.header) s += this.template_sub( params.header, cols, params );
103 for (var i = 0; i < params.list.length; i++) {
104 params.row = params.list[i];
105 s += this.template_sub( params.line_item, cols, params );
108 if (params.footer) s += this.template_sub( params.footer, cols, params );
110 if (params.sample_frame) {
111 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
112 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>');
114 this.simple(s,params);
118 'template_sub' : function( msg, cols, params ) {
119 if (!msg) { dump('template sub called with empty string\n'); return; }
120 JSAN.use('util.date');
123 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
124 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
126 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
127 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
128 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
129 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
130 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
131 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
132 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
133 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
134 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
135 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
136 try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
137 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
138 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
139 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
140 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
141 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
142 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() ) ;}
143 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
145 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
146 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
147 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
148 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
149 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
150 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
151 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
152 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
153 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
154 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
155 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
156 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
157 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
158 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
159 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
160 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
161 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
162 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
163 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
164 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
167 if (typeof params.row != 'undefined') {
168 if (params.row.length >= 0) {
169 alert('debug - please tell the developers that deprecated template code tried to execute');
170 for (var i = 0; i < cols.length; i++) {
171 var re = new RegExp(cols[i],"g");
172 try{b = s; s=s.replace(re, params.row[i]);}
173 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
176 /* for dump_with_keys */
177 for (var i in params.row) {
178 var re = new RegExp('%'+i+'%',"g");
179 try{b = s; s=s.replace(re, params.row[i]);}
180 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
185 if (typeof params.data != 'undefined') {
186 for (var i in params.data) {
187 var re = new RegExp('%'+i+'%',"g");
188 try{b = s; s=s.replace(re, params.data[i]);}
189 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
192 } catch(E) { dump(E+'\n'); }
198 'NSPrint' : function(w,silent,params) {
202 if (!params) params = {};
204 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
205 obj.data.init({'via':'stash'});
207 if (params.print_strategy || obj.data.print_strategy) {
209 switch(params.print_strategy || obj.data.print_strategy) {
211 obj._NSPrint_dos_print(w,silent,params);
216 case 'webBrowserPrint':
217 obj._NSPrint_webBrowserPrint(w,silent,params);
221 obj._NSPrint_webBrowserPrint(w,silent,params);
227 obj._NSPrint_webBrowserPrint(w,silent,params);
231 alert('Probably not printing: ' + e);
232 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
237 '_NSPrint_dos_print' : function(w,silent,params) {
241 /* This is a kludge/workaround. webBrowserPrint doesn't always work. So we're going to let
242 the html window handle our receipt template rendering, and then force a selection of all
243 the text nodes and dump that to a file, for printing through a dos utility */
245 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
246 w.getSelection().selectAllChildren(w.document.firstChild);
247 var text = w.getSelection().toString();
249 var file = new util.file('receipt.txt');
250 file.write_content('truncate',text); file.close();
252 file = new util.file('receipt.bat');
253 if (! file._file.exists()) {
254 file.write_content('truncate','copy chrome\\open_ils_staff_client\\content\\conf\\receipt.txt lpt1 /b\n');
256 file = new util.file('receipt.bat');
259 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
260 process.init(file._file);
264 process.run(true, args, args.length);
267 //alert('Probably not printing: ' + e);
268 this.error.sdump('D_ERROR','_NSPrint_dos_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
272 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
275 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
276 var webBrowserPrint = w
277 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
278 .getInterface(Components.interfaces.nsIWebBrowserPrint);
279 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
280 if (webBrowserPrint) {
281 var gPrintSettings = obj.GetPrintSettings();
282 if (silent) gPrintSettings.printSilent = true;
283 else gPrintSettings.printSilent = false;
285 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
287 webBrowserPrint.print(gPrintSettings, null);
288 this.error.sdump('D_PRINT','Should be printing\n');
290 this.error.sdump('D_ERROR','Should not be printing\n');
293 //alert('Probably not printing: ' + e);
294 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
295 // causing an exception to be thrown which we catch here.
296 // Unfortunately this will also consume helpful failures
297 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
301 'GetPrintSettings' : function() {
303 //alert('entering GetPrintSettings');
304 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
305 var pref = Components.classes["@mozilla.org/preferences-service;1"]
306 .getService(Components.interfaces.nsIPrefBranch);
307 //alert('pref = ' + pref);
309 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
310 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
311 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
314 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
315 .getService(Components.interfaces.nsIPrintSettingsService);
316 if (this.gPrintSettingsAreGlobal) {
317 this.gPrintSettings = printService.globalPrintSettings;
318 //alert('called setPrinterDefaultsForSelectedPrinter');
319 this.setPrinterDefaultsForSelectedPrinter(printService);
321 this.gPrintSettings = printService.newPrintSettings;
322 //alert('used printService.newPrintSettings');
325 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
326 //alert("GetPrintSettings() "+e+"\n");
329 return this.gPrintSettings;
332 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
334 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
335 if (this.gPrintSettings.printerName == "") {
336 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
337 //alert('used .defaultPrinterName');
339 //alert('printerName = ' + this.gPrintSettings.printerName);
341 // First get any defaults from the printer
342 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
344 // now augment them with any values from last time
345 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
347 // now augment from our own saved settings if they exist
348 this.load_settings();
351 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
355 'page_settings' : function() {
357 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
358 this.GetPrintSettings();
359 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
360 PO.ShowPrintSetupDialog(this.gPrintSettings);
362 this.error.standard_unexpected_error_alert("page_settings()",E);
366 'load_settings' : function() {
368 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
369 var file = new util.file('gPrintSettings');
370 if (file._file.exists()) {
371 temp = file.get_object(); file.close();
372 for (var i in temp) {
373 this.gPrintSettings[i] = temp[i];
376 this.gPrintSettings.marginTop = 0;
377 this.gPrintSettings.marginLeft = 0;
378 this.gPrintSettings.marginBottom = 0;
379 this.gPrintSettings.marginRight = 0;
380 this.gPrintSettings.headerStrLeft = '';
381 this.gPrintSettings.headerStrCenter = '';
382 this.gPrintSettings.headerStrRight = '';
383 this.gPrintSettings.footerStrLeft = '';
384 this.gPrintSettings.footerStrCenter = '';
385 this.gPrintSettings.footerStrRight = '';
388 this.error.standard_unexpected_error_alert("load_settings()",E);
392 'save_settings' : function() {
395 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
396 var file = new util.file('gPrintSettings');
397 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
398 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
401 this.error.standard_unexpected_error_alert("save_settings()",E);
406 dump('exiting util/print.js\n');