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');
16 util.print.prototype = {
18 'reprint_last' : function() {
20 var obj = this; obj.data.init({'via':'stash'});
21 if (!obj.data.last_print) {
22 alert('Nothing to re-print');
25 var msg = obj.data.last_print.msg;
26 var params = obj.data.last_print.params; params.no_prompt = false;
27 obj.simple( msg, params );
29 this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
33 'simple' : function(msg,params) {
35 if (!params) params = {};
39 obj.data.last_print = { 'msg' : msg, 'params' : params}; obj.data.stash('last_print');
42 if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
47 if (params && params.content_type) {
48 content_type = params.content_type;
50 content_type = 'text/html';
54 switch(content_type) {
56 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); }');
57 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');
59 w.go_print = function() {
64 obj.NSPrint(w, silent, params);
66 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);
69 w.minimize(); w.close();
76 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
81 obj.NSPrint(w, silent, params);
83 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);
86 w.minimize(); w.close();
93 this.error.standard_unexpected_error_alert('util.print.simple',E);
97 'tree_list' : function (params) {
99 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
106 if (params.header) s += this.template_sub( params.header, cols, params );
108 for (var i = 0; i < params.list.length; i++) {
109 params.row = params.list[i];
110 s += this.template_sub( params.line_item, cols, params );
113 if (params.footer) s += this.template_sub( params.footer, cols, params );
115 if (params.sample_frame) {
116 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
117 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>');
119 this.simple(s,params);
123 'template_sub' : function( msg, cols, params ) {
124 if (!msg) { dump('template sub called with empty string\n'); return; }
125 JSAN.use('util.date');
128 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
129 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
131 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
132 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
133 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
134 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
135 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
136 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
137 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
138 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
139 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
140 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
141 try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
142 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
143 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
144 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
145 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
146 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
147 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() ) ;}
148 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
150 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
151 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
152 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
153 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
154 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
155 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
156 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
157 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
158 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
159 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
160 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
161 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
162 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
163 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
164 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
165 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
166 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
167 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
168 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
169 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
172 if (typeof params.row != 'undefined') {
173 if (params.row.length >= 0) {
174 alert('debug - please tell the developers that deprecated template code tried to execute');
175 for (var i = 0; i < cols.length; i++) {
176 var re = new RegExp(cols[i],"g");
177 try{b = s; s=s.replace(re, params.row[i]);}
178 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
181 /* for dump_with_keys */
182 for (var i in params.row) {
183 var re = new RegExp('%'+i+'%',"g");
184 try{b = s; s=s.replace(re, params.row[i]);}
185 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
190 if (typeof params.data != 'undefined') {
191 for (var i in params.data) {
192 var re = new RegExp('%'+i+'%',"g");
193 try{b = s; s=s.replace(re, params.data[i]);}
194 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
197 } catch(E) { dump(E+'\n'); }
203 'NSPrint' : function(w,silent,params) {
207 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
209 if (params.print_strategy || obj.data.print_strategy) {
211 switch(params.print_strategy || obj.data.print_strategy) {
213 obj._NSPrint_dos_print(w,silent,params);
218 case 'webBrowserPrint':
219 obj._NSPrint_webBrowserPrint(w,silent,params);
223 obj._NSPrint_webBrowserPrint(w,silent,params);
229 obj._NSPrint_webBrowserPrint(w,silent,params);
233 //alert('Probably not printing: ' + e);
234 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
239 '_NSPrint_dos_print' : function(w,silent,params) {
242 /* This is a kludge/workaround. webBrowserPrint doesn't always work. So we're going to let
243 the html window handle our receipt template rendering, and then force a selection of all
244 the text nodes and dump that to a file, for printing through a dos utility */
246 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
247 w.getSelection().selectAllChildren(w.document.firstChild);
248 var text = w.getSelection().toString();
250 JSAN.use('util.file'); var file = new util.file('receipt.txt');
251 file.write_content('truncate',text); file.close();
253 file = new util.file('receipt.bat');
254 if (! file._file.exists()) {
255 file.write_content('truncate','copy chrome\\open_ils_staff_client\\content\\conf\\receipt.txt lpt1 /b\n');
257 file = new util.file('receipt.bat');
260 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
261 process.init(file._file);
265 process.run(true, args, args.length);
268 //alert('Probably not printing: ' + e);
269 this.error.sdump('D_ERROR','_NSPrint_dos_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
273 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
276 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
277 var webBrowserPrint = w
278 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
279 .getInterface(Components.interfaces.nsIWebBrowserPrint);
280 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
281 if (webBrowserPrint) {
282 var gPrintSettings = obj.GetPrintSettings();
283 if (silent) gPrintSettings.printSilent = true;
284 else gPrintSettings.printSilent = false;
286 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
288 webBrowserPrint.print(gPrintSettings, null);
289 this.error.sdump('D_PRINT','Should be printing\n');
291 this.error.sdump('D_ERROR','Should not be printing\n');
294 //alert('Probably not printing: ' + e);
295 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
296 // causing an exception to be thrown which we catch here.
297 // Unfortunately this will also consume helpful failures
298 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
302 'GetPrintSettings' : function() {
304 //alert('entering GetPrintSettings');
305 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
306 var pref = Components.classes["@mozilla.org/preferences-service;1"]
307 .getService(Components.interfaces.nsIPrefBranch);
308 //alert('pref = ' + pref);
310 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
311 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
312 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
315 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
316 .getService(Components.interfaces.nsIPrintSettingsService);
317 if (this.gPrintSettingsAreGlobal) {
318 this.gPrintSettings = printService.globalPrintSettings;
319 //alert('called setPrinterDefaultsForSelectedPrinter');
320 this.setPrinterDefaultsForSelectedPrinter(printService);
322 this.gPrintSettings = printService.newPrintSettings;
323 //alert('used printService.newPrintSettings');
326 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
327 //alert("GetPrintSettings() "+e+"\n");
330 return this.gPrintSettings;
333 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
335 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
336 if (this.gPrintSettings.printerName == "") {
337 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
338 //alert('used .defaultPrinterName');
340 //alert('printerName = ' + this.gPrintSettings.printerName);
342 // First get any defaults from the printer
343 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
345 // now augment them with any values from last time
346 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
348 // now augment from our own saved settings if they exist
349 this.load_settings();
352 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
356 'page_settings' : function() {
358 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
359 this.GetPrintSettings();
360 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
361 PO.ShowPrintSetupDialog(this.gPrintSettings);
363 this.error.standard_unexpected_error_alert("page_settings()",E);
367 'load_settings' : function() {
369 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
370 JSAN.use('util.file'); var file = new util.file('gPrintSettings');
371 if (file._file.exists()) {
372 temp = file.get_object(); file.close();
373 for (var i in temp) {
374 this.gPrintSettings[i] = temp[i];
377 this.gPrintSettings.marginTop = 0;
378 this.gPrintSettings.marginLeft = 0;
379 this.gPrintSettings.marginBottom = 0;
380 this.gPrintSettings.marginRight = 0;
381 this.gPrintSettings.headerStrLeft = '';
382 this.gPrintSettings.headerStrCenter = '';
383 this.gPrintSettings.headerStrRight = '';
384 this.gPrintSettings.footerStrLeft = '';
385 this.gPrintSettings.footerStrCenter = '';
386 this.gPrintSettings.footerStrRight = '';
389 this.error.standard_unexpected_error_alert("load_settings()",E);
393 'save_settings' : function() {
395 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
396 JSAN.use('util.file'); var file = new util.file('gPrintSettings');
397 file.set_object(this.gPrintSettings); file.close();
399 this.error.standard_unexpected_error_alert("save_settings()",E);
404 dump('exiting util/print.js\n');