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 { alert("Please inform the developers that the go_print bug occurred. After this alert, we will try to print again."); window.print(); } /* FIXME - mozilla bug#301560 - xpcom kills it too */ if (' + (typeof params.modal != 'undefined' ? 'true' : 'false') + ') setTimeout(function(){ try { window.print(); window.close(); } catch(E) { alert(E); } },0); }');
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() {
65 obj.NSPrint(w, silent, params);
67 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);
70 w.minimize(); w.close();
77 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
82 obj.NSPrint(w, silent, params);
84 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);
87 w.minimize(); w.close();
94 this.error.standard_unexpected_error_alert('util.print.simple',E);
98 'tree_list' : function (params) {
100 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
107 if (params.header) s += this.template_sub( params.header, cols, params );
109 for (var i = 0; i < params.list.length; i++) {
110 params.row = params.list[i];
111 s += this.template_sub( params.line_item, cols, params );
114 if (params.footer) s += this.template_sub( params.footer, cols, params );
116 if (params.sample_frame) {
117 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
118 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>');
120 this.simple(s,params);
124 'template_sub' : function( msg, cols, params ) {
125 if (!msg) { dump('template sub called with empty string\n'); return; }
126 JSAN.use('util.date');
129 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
130 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
132 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
133 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
134 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
135 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
136 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
137 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
138 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
139 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
140 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
141 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
142 try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
143 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
144 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
145 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
146 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
147 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
148 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() ) ;}
149 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
151 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
152 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
153 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
154 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
155 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
156 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
157 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
158 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
159 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
160 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
161 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
162 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
163 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
164 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
165 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
166 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
167 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
168 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
169 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
170 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
173 if (typeof params.row != 'undefined') {
174 if (params.row.length >= 0) {
175 alert('debug - please tell the developers that deprecated template code tried to execute');
176 for (var i = 0; i < cols.length; i++) {
177 var re = new RegExp(cols[i],"g");
178 try{b = s; s=s.replace(re, params.row[i]);}
179 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
182 /* for dump_with_keys */
183 for (var i in params.row) {
184 var re = new RegExp('%'+i+'%',"g");
185 try{b = s; s=s.replace(re, params.row[i]);}
186 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
191 if (typeof params.data != 'undefined') {
192 for (var i in params.data) {
193 var re = new RegExp('%'+i+'%',"g");
194 try{b = s; s=s.replace(re, params.data[i]);}
195 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
198 } catch(E) { dump(E+'\n'); }
204 'NSPrint' : function(w,silent,params) {
208 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
210 if (params.print_strategy || obj.data.print_strategy) {
212 switch(params.print_strategy || obj.data.print_strategy) {
214 obj._NSPrint_dos_print(w,silent,params);
219 case 'webBrowserPrint':
220 obj._NSPrint_webBrowserPrint(w,silent,params);
224 obj._NSPrint_webBrowserPrint(w,silent,params);
230 obj._NSPrint_webBrowserPrint(w,silent,params);
234 //alert('Probably not printing: ' + e);
235 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
240 '_NSPrint_dos_print' : function(w,silent,params) {
243 /* This is a kludge/workaround. webBrowserPrint doesn't always work. So we're going to let
244 the html window handle our receipt template rendering, and then force a selection of all
245 the text nodes and dump that to a file, for printing through a dos utility */
247 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
248 w.getSelection().selectAllChildren(w.document.firstChild);
249 var text = w.getSelection().toString();
251 var file = new util.file('receipt.txt');
252 file.write_content('truncate',text); file.close();
254 file = new util.file('receipt.bat');
255 if (! file._file.exists()) {
256 file.write_content('truncate','copy chrome\\open_ils_staff_client\\content\\conf\\receipt.txt lpt1 /b\n');
258 file = new util.file('receipt.bat');
261 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
262 process.init(file._file);
266 process.run(true, args, args.length);
269 //alert('Probably not printing: ' + e);
270 this.error.sdump('D_ERROR','_NSPrint_dos_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
274 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
277 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
278 var webBrowserPrint = w
279 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
280 .getInterface(Components.interfaces.nsIWebBrowserPrint);
281 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
282 if (webBrowserPrint) {
283 var gPrintSettings = obj.GetPrintSettings();
284 if (silent) gPrintSettings.printSilent = true;
285 else gPrintSettings.printSilent = false;
287 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
289 webBrowserPrint.print(gPrintSettings, null);
290 this.error.sdump('D_PRINT','Should be printing\n');
292 this.error.sdump('D_ERROR','Should not be printing\n');
295 //alert('Probably not printing: ' + e);
296 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
297 // causing an exception to be thrown which we catch here.
298 // Unfortunately this will also consume helpful failures
299 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
303 'GetPrintSettings' : function() {
305 //alert('entering GetPrintSettings');
306 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
307 var pref = Components.classes["@mozilla.org/preferences-service;1"]
308 .getService(Components.interfaces.nsIPrefBranch);
309 //alert('pref = ' + pref);
311 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
312 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
313 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
316 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
317 .getService(Components.interfaces.nsIPrintSettingsService);
318 if (this.gPrintSettingsAreGlobal) {
319 this.gPrintSettings = printService.globalPrintSettings;
320 //alert('called setPrinterDefaultsForSelectedPrinter');
321 this.setPrinterDefaultsForSelectedPrinter(printService);
323 this.gPrintSettings = printService.newPrintSettings;
324 //alert('used printService.newPrintSettings');
327 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
328 //alert("GetPrintSettings() "+e+"\n");
331 return this.gPrintSettings;
334 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
336 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
337 if (this.gPrintSettings.printerName == "") {
338 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
339 //alert('used .defaultPrinterName');
341 //alert('printerName = ' + this.gPrintSettings.printerName);
343 // First get any defaults from the printer
344 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
346 // now augment them with any values from last time
347 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
349 // now augment from our own saved settings if they exist
350 this.load_settings();
353 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
357 'page_settings' : function() {
359 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
360 this.GetPrintSettings();
361 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
362 PO.ShowPrintSetupDialog(this.gPrintSettings);
364 this.error.standard_unexpected_error_alert("page_settings()",E);
368 'load_settings' : function() {
370 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
371 var file = new util.file('gPrintSettings');
372 if (file._file.exists()) {
373 temp = file.get_object(); file.close();
374 for (var i in temp) {
375 this.gPrintSettings[i] = temp[i];
378 this.gPrintSettings.marginTop = 0;
379 this.gPrintSettings.marginLeft = 0;
380 this.gPrintSettings.marginBottom = 0;
381 this.gPrintSettings.marginRight = 0;
382 this.gPrintSettings.headerStrLeft = '';
383 this.gPrintSettings.headerStrCenter = '';
384 this.gPrintSettings.headerStrRight = '';
385 this.gPrintSettings.footerStrLeft = '';
386 this.gPrintSettings.footerStrCenter = '';
387 this.gPrintSettings.footerStrRight = '';
390 this.error.standard_unexpected_error_alert("load_settings()",E);
394 'save_settings' : function() {
396 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
397 var file = new util.file('gPrintSettings');
398 file.set_object(this.gPrintSettings); file.close();
400 this.error.standard_unexpected_error_alert("save_settings()",E);
405 dump('exiting util/print.js\n');