]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/print.js
possible workaround for go_print bug
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / print.js
1 dump('entering util/print.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.print = function () {
5
6         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
7
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');
13
14         return this;
15 };
16
17 util.print.prototype = {
18
19         'reprint_last' : function() {
20                 try {
21                         var obj = this; obj.data.init({'via':'stash'});
22                         if (!obj.data.last_print) {
23                                 alert('Nothing to re-print');
24                                 return;
25                         }
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 );
29                 } catch(E) {
30                         this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
31                 }
32         },
33
34         'simple' : function(msg,params) {
35                 try {
36                         if (!params) params = {};
37
38                         var obj = this;
39
40                         obj.data.last_print = { 'msg' : msg, 'params' : params}; obj.data.stash('last_print');
41
42                         var silent = false;
43                         if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
44                                 silent = true;
45                         }
46
47                         var content_type;
48                         if (params && params.content_type) {
49                                 content_type = params.content_type;
50                         } else {
51                                 content_type = 'text/html';
52                         }
53
54                         var w;
55                         switch(content_type) {
56                                 case 'text/html' :
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');
59                                         w.minimize();
60                                         w.go_print = function() { 
61                                                 try {
62                                                         obj.NSPrint(w, silent, params);
63                                                 } catch(E) {
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);
65                                                         w.print();
66                                                 }
67                                                 w.minimize(); w.close();
68                                         }
69                                 break;
70                                 default:
71                                         w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
72                                         w.minimize();
73                                         setTimeout(
74                                                 function() {
75                                                         try {
76                                                                 obj.NSPrint(w, silent, params);
77                                                         } catch(E) {
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);
79                                                                 w.print();
80                                                         }
81                                                         w.minimize(); w.close();
82                                                 }, 1000
83                                         );
84                                 break;
85                         }
86
87                 } catch(E) {
88                         this.error.standard_unexpected_error_alert('util.print.simple',E);
89                 }
90         },
91         
92         'tree_list' : function (params) { 
93                 try {
94                         dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
95                 } catch(E) {
96                         dump(E+'\n');
97                 }
98                 var cols = [];
99
100                 var s = '';
101                 if (params.header) s += this.template_sub( params.header, cols, params );
102                 if (params.list) {
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 );
106                         }
107                 }
108                 if (params.footer) s += this.template_sub( params.footer, cols, params );
109
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>');
113                 } else {
114                         this.simple(s,params);
115                 }
116         },
117
118         'template_sub' : function( msg, cols, params ) {
119                 if (!msg) { dump('template sub called with empty string\n'); return; }
120                 JSAN.use('util.date');
121                 var s = msg; var b;
122
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');}
125
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');}
144
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');}
165
166                 try {
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);}
174                                         }
175                                 } else { 
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);}
181                                         }
182                                 }
183                         }
184
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);}
190                                 }
191                         }
192                 } catch(E) { dump(E+'\n'); }
193
194                 return s;
195         },
196
197
198         'NSPrint' : function(w,silent,params) {
199                 if (!w) w = window;
200                 var obj = this;
201                 try {
202                         if (!params) params = {};
203
204                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
205                         obj.data.init({'via':'stash'});
206
207                         if (params.print_strategy || obj.data.print_strategy) {
208
209                                 switch(params.print_strategy || obj.data.print_strategy) {
210                                         case 'dos.print':
211                                                 obj._NSPrint_dos_print(w,silent,params);
212                                         break;  
213                                         case 'window.print':
214                                                 w.print();
215                                         break;  
216                                         case 'webBrowserPrint':
217                                                 obj._NSPrint_webBrowserPrint(w,silent,params);
218                                         break;  
219                                         default:
220                                                 //w.print();
221                                                 obj._NSPrint_webBrowserPrint(w,silent,params);
222                                         break;  
223                                 }
224
225                         } else {
226                                 //w.print();
227                                 obj._NSPrint_webBrowserPrint(w,silent,params);
228                         }
229
230                 } catch (e) {
231                         alert('Probably not printing: ' + e);
232                         this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
233                 }
234
235         },
236
237         '_NSPrint_dos_print' : function(w,silent,params) {
238                 var obj = this;
239                 try {
240
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 */
244
245                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
246                         w.getSelection().selectAllChildren(w.document.firstChild);
247                         var text = w.getSelection().toString();
248
249                         var file = new util.file('receipt.txt');
250                         file.write_content('truncate',text); file.close();
251                         
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');
255                                 file.close();
256                                 file = new util.file('receipt.bat');
257                         }
258
259                         var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
260                         process.init(file._file);
261
262                         var args = [];
263
264                         process.run(true, args, args.length);
265
266                 } catch (e) {
267                         //alert('Probably not printing: ' + e);
268                         this.error.sdump('D_ERROR','_NSPrint_dos_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
269                 }
270         },
271
272         '_NSPrint_webBrowserPrint' : function(w,silent,params) {
273                 var obj = this;
274                 try {
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;
284                                 if (params) {
285                                         if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
286                                 }
287                                 webBrowserPrint.print(gPrintSettings, null);
288                                 this.error.sdump('D_PRINT','Should be printing\n');
289                         } else {
290                                 this.error.sdump('D_ERROR','Should not be printing\n');
291                         }
292                 } catch (e) {
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');
298                 }
299         },
300
301         'GetPrintSettings' : function() {
302                 try {
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);
308                         if (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);
312                         }
313  
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);
320                         } else {
321                                 this.gPrintSettings = printService.newPrintSettings;
322                                 //alert('used printService.newPrintSettings');
323                         }
324                 } catch (e) {
325                         this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
326                         //alert("GetPrintSettings() "+e+"\n");
327                 }
328  
329                 return this.gPrintSettings;
330         },
331
332         'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
333                 try {
334                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
335                         if (this.gPrintSettings.printerName == "") {
336                                 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
337                                 //alert('used .defaultPrinterName');
338                         }
339                         //alert('printerName = ' + this.gPrintSettings.printerName);
340          
341                         // First get any defaults from the printer 
342                         aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
343          
344                         // now augment them with any values from last time
345                         aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
346
347                         // now augment from our own saved settings if they exist
348                         this.load_settings();
349
350                 } catch(E) {
351                         this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
352                 }
353         },
354
355         'page_settings' : function() {
356                 try {
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);
361                 } catch(E) {
362                         this.error.standard_unexpected_error_alert("page_settings()",E);
363                 }
364         },
365
366         'load_settings' : function() {
367                 try {
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];
374                                 }
375                         } else {
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 = '';
386                         }
387                 } catch(E) {
388                         this.error.standard_unexpected_error_alert("load_settings()",E);
389                 }
390         },
391
392         'save_settings' : function() {
393                 try {
394                         var obj = this;
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); 
399                         file.close();
400                 } catch(E) {
401                         this.error.standard_unexpected_error_alert("save_settings()",E);
402                 }
403         },
404 }
405
406 dump('exiting util/print.js\n');