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');
104 // FIXME -- This could be done better.. instead of finding the columns and handling a tree dump,
105 // we could do a dump_with_keys instead
107 switch(params.type) {
108 case 'offline_checkout' :
109 JSAN.use('circ.util');
110 cols = util.functional.map_list(
111 circ.util.offline_checkout_columns( {} ),
113 return '%' + o.id + '%';
118 case 'offline_checkin' :
119 JSAN.use('circ.util');
120 cols = util.functional.map_list(
121 circ.util.offline_checkin_columns( {} ),
123 return '%' + o.id + '%';
128 case 'offline_renew' :
129 JSAN.use('circ.util');
130 cols = util.functional.map_list(
131 circ.util.offline_renew_columns( {} ),
133 return '%' + o.id + '%';
137 case 'offline_inhouse_use' :
138 JSAN.use('circ.util');
139 cols = util.functional.map_list(
140 circ.util.offline_inhouse_use_columns( {} ),
142 return '%' + o.id + '%';
147 JSAN.use('circ.util');
148 cols = util.functional.map_list(
149 circ.util.columns( {} ),
151 return '%' + o.id + '%';
156 JSAN.use('patron.util');
157 cols = util.functional.map_list(
158 patron.util.mbts_columns( {} ),
160 return '%' + o.id + '%';
165 //cols = [ '%bill_id%','%payment%'];
172 JSAN.use('circ.util');
173 cols = util.functional.map_list(
174 circ.util.hold_columns( {} ),
176 return '%' + o.id + '%';
181 JSAN.use('patron.util');
182 cols = util.functional.map_list(
183 patron.util.columns( {} ),
185 return '%' + o.id + '%';
192 var s = this.template_sub( params.header, cols, params );
193 for (var i = 0; i < params.list.length; i++) {
194 params.row = params.list[i];
195 s += this.template_sub( params.line_item, cols, params );
197 s += this.template_sub( params.footer, cols, params );
199 if (params.sample_frame) {
200 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
201 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>');
203 this.simple(s,params);
207 'template_sub' : function( msg, cols, params ) {
208 if (!msg) { dump('template sub called with empty string\n'); return; }
209 JSAN.use('util.date');
212 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
213 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
215 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
216 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
217 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
218 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
219 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
220 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
221 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
222 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
223 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
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 pause');
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('string = <' + s + '> error = ' + js2JSON(E)+'\n',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('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
272 if (typeof params.data != 'undefined') {
273 for (var i in params.data) {
274 var re = new RegExp('%'+i+'%',"g");
275 try{b = s; s=s.replace(re, params.data[i]);}
276 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
279 } catch(E) { dump(E+'\n'); }
285 'NSPrint' : function(w,silent,params) {
289 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
291 if (obj.data.print_strategy) {
293 switch(obj.data.print_strategy) {
295 obj._NSPrint_dos_print(w,silent,params);
300 case 'webBrowserPrint':
301 obj._NSPrint_webBrowserPrint(w,silent,params);
305 obj._NSPrint_webBrowserPrint(w,silent,params);
311 obj._NSPrint_webBrowserPrint(w,silent,params);
315 //alert('Probably not printing: ' + e);
316 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
321 '_NSPrint_dos_print' : function(w,silent,params) {
324 /* This is a kludge/workaround. webBrowserPrint doesn't always work. So we're going to let
325 the html window handle our receipt template rendering, and then force a selection of all
326 the text nodes and dump that to a file, for printing through a dos utility */
328 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
329 w.getSelection().selectAllChildren(w.document.firstChild);
330 var text = w.getSelection().toString();
332 JSAN.use('util.file'); var file = new util.file('receipt.txt');
333 file.write_content('truncate',text); file.close();
335 file = new util.file('receipt.bat');
336 if (! file._file.exists()) {
337 file.write_content('truncate','copy chrome\\open_ils_staff_client\\content\\conf\\receipt.txt lpt1 /b\n');
339 file = new util.file('receipt.bat');
342 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
343 process.init(file._file);
347 process.run(true, args, args.length);
350 //alert('Probably not printing: ' + e);
351 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
355 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
358 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
359 var webBrowserPrint = w
360 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
361 .getInterface(Components.interfaces.nsIWebBrowserPrint);
362 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
363 if (webBrowserPrint) {
364 var gPrintSettings = obj.GetPrintSettings();
365 if (silent) gPrintSettings.printSilent = true;
366 else gPrintSettings.printSilent = false;
368 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
370 webBrowserPrint.print(gPrintSettings, null);
371 this.error.sdump('D_PRINT','Should be printing\n');
373 this.error.sdump('D_ERROR','Should not be printing\n');
376 //alert('Probably not printing: ' + e);
377 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
378 // causing an exception to be thrown which we catch here.
379 // Unfortunately this will also consume helpful failures
380 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
384 'GetPrintSettings' : function() {
386 //alert('entering GetPrintSettings');
387 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
388 var pref = Components.classes["@mozilla.org/preferences-service;1"]
389 .getService(Components.interfaces.nsIPrefBranch);
390 //alert('pref = ' + pref);
392 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
393 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
394 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
397 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
398 .getService(Components.interfaces.nsIPrintSettingsService);
399 if (this.gPrintSettingsAreGlobal) {
400 this.gPrintSettings = printService.globalPrintSettings;
401 //alert('called setPrinterDefaultsForSelectedPrinter');
402 this.setPrinterDefaultsForSelectedPrinter(printService);
404 this.gPrintSettings = printService.newPrintSettings;
405 //alert('used printService.newPrintSettings');
408 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
409 //alert("GetPrintSettings() "+e+"\n");
412 return this.gPrintSettings;
415 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
417 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
418 if (this.gPrintSettings.printerName == "") {
419 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
420 //alert('used .defaultPrinterName');
422 //alert('printerName = ' + this.gPrintSettings.printerName);
424 // First get any defaults from the printer
425 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
427 // now augment them with any values from last time
428 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
430 // now augment from our own saved settings if they exist
431 this.load_settings();
434 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
438 'page_settings' : function() {
440 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
441 this.GetPrintSettings();
442 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
443 PO.ShowPrintSetupDialog(this.gPrintSettings);
445 this.error.standard_unexpected_error_alert("page_settings()",E);
449 'load_settings' : function() {
451 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
452 JSAN.use('util.file'); var file = new util.file('gPrintSettings');
453 if (file._file.exists()) {
454 temp = file.get_object(); file.close();
455 for (var i in temp) {
456 this.gPrintSettings[i] = temp[i];
459 this.gPrintSettings.marginTop = 0;
460 this.gPrintSettings.marginLeft = 0;
461 this.gPrintSettings.marginBottom = 0;
462 this.gPrintSettings.marginRight = 0;
463 this.gPrintSettings.headerStrLeft = '';
464 this.gPrintSettings.headerStrCenter = '';
465 this.gPrintSettings.headerStrRight = '';
466 this.gPrintSettings.footerStrLeft = '';
467 this.gPrintSettings.footerStrCenter = '';
468 this.gPrintSettings.footerStrRight = '';
471 this.error.standard_unexpected_error_alert("load_settings()",E);
475 'save_settings' : function() {
477 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
478 JSAN.use('util.file'); var file = new util.file('gPrintSettings');
479 file.set_object(this.gPrintSettings); file.close();
481 this.error.standard_unexpected_error_alert("save_settings()",E);
486 dump('exiting util/print.js\n');